fix(json): do not put values on a separate line from the key (#4852)

* test: add tests

* fix(json): do not put values on a separate line from the key
master
Ika 2018-07-18 05:08:59 +08:00 committed by Lucas Duailibe
parent cdcb7b11c9
commit ed0ecdad18
4 changed files with 100 additions and 33 deletions

View File

@ -5271,7 +5271,10 @@ function printAssignmentRight(leftNode, rightNode, printedRight, options) {
((leftNode.type === "Identifier" ||
isStringLiteral(leftNode) ||
leftNode.type === "MemberExpression") &&
(isStringLiteral(rightNode) || isMemberExpressionChain(rightNode)));
(isStringLiteral(rightNode) || isMemberExpressionChain(rightNode)) &&
// do not put values on a separate line from the key in json
options.parser !== "json" &&
options.parser !== "json5");
if (canBreak) {
return indent(concat([line, printedRight]));

View File

@ -35,6 +35,81 @@ true
`;
exports[`key-value.json - json-stringify-verify 1`] = `
{
"string": "stringstringstringstringstringstringstringstringstringstringstringstringstringstringstring",
"stringstringstringstringstringstringstringstring": "stringstringstringstringstringstringstringstring",
"stringstringstringstringstringstringstringstringstringstringstringstringstringstringstring": "string"
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
{
"string": "stringstringstringstringstringstringstringstringstringstringstringstringstringstringstring",
"stringstringstringstringstringstringstringstring": "stringstringstringstringstringstringstringstring",
"stringstringstringstringstringstringstringstringstringstringstringstringstringstringstring": "string"
}
`;
exports[`key-value.json - json-verify 1`] = `
{
"string": "stringstringstringstringstringstringstringstringstringstringstringstringstringstringstring",
"stringstringstringstringstringstringstringstring": "stringstringstringstringstringstringstringstring",
"stringstringstringstringstringstringstringstringstringstringstringstringstringstringstring": "string"
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
{
"string": "stringstringstringstringstringstringstringstringstringstringstringstringstringstringstring",
"stringstringstringstringstringstringstringstring": "stringstringstringstringstringstringstringstring",
"stringstringstringstringstringstringstringstringstringstringstringstringstringstringstring": "string"
}
`;
exports[`key-value.json - json-verify 2`] = `
{
"string": "stringstringstringstringstringstringstringstringstringstringstringstringstringstringstring",
"stringstringstringstringstringstringstringstring": "stringstringstringstringstringstringstringstring",
"stringstringstringstringstringstringstringstringstringstringstringstringstringstringstring": "string"
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
{
"string": "stringstringstringstringstringstringstringstringstringstringstringstringstringstringstring",
"stringstringstringstringstringstringstringstring": "stringstringstringstringstringstringstringstring",
"stringstringstringstringstringstringstringstringstringstringstringstringstringstringstring": "string"
}
`;
exports[`key-value.json - json5-verify 1`] = `
{
"string": "stringstringstringstringstringstringstringstringstringstringstringstringstringstringstring",
"stringstringstringstringstringstringstringstring": "stringstringstringstringstringstringstringstring",
"stringstringstringstringstringstringstringstringstringstringstringstringstringstringstring": "string"
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
{
string: "stringstringstringstringstringstringstringstringstringstringstringstringstringstringstring",
stringstringstringstringstringstringstringstring: "stringstringstringstringstringstringstringstring",
stringstringstringstringstringstringstringstringstringstringstringstringstringstringstring: "string"
}
`;
exports[`key-value.json - json5-verify 2`] = `
{
"string": "stringstringstringstringstringstringstringstringstringstringstringstringstringstringstring",
"stringstringstringstringstringstringstringstring": "stringstringstringstringstringstringstringstring",
"stringstringstringstringstringstringstringstringstringstringstringstringstringstringstring": "string"
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
{
string: "stringstringstringstringstringstringstringstringstringstringstringstringstringstringstring",
stringstringstringstringstringstringstringstring: "stringstringstringstringstringstringstringstring",
stringstringstringstringstringstringstringstringstringstringstringstringstringstringstring: "string",
}
`;
exports[`multi-line.json - json-stringify-verify 1`] = `
{"key1":[true,false,null],"key2":{"key3":[1,2,"3",
1e10,1e-3]}}
@ -419,8 +494,7 @@ exports[`pass1.json - json-verify 1`] = `
"compact": [1, 2, 3, 4, 5, 6, 7],
"jsontext": "{\\"object with 1 member\\":[\\"array with 1 element\\"]}",
"quotes": "" \\u0022 %22 0x22 034 "",
"/\\\\\\"\\uCAFE\\uBABE\\uAB98\\uFCDE\\ubcda\\uef4A\\b\\f\\n\\r\\t\`1~!@#$%^&*()_+-=[]{}|;:',./<>?":
"A key can be any string"
"/\\\\\\"\\uCAFE\\uBABE\\uAB98\\uFCDE\\ubcda\\uef4A\\b\\f\\n\\r\\t\`1~!@#$%^&*()_+-=[]{}|;:',./<>?": "A key can be any string"
},
0.5,
98.6,
@ -538,8 +612,7 @@ exports[`pass1.json - json-verify 2`] = `
"compact": [1, 2, 3, 4, 5, 6, 7],
"jsontext": "{\\"object with 1 member\\":[\\"array with 1 element\\"]}",
"quotes": "&#34; \\u0022 %22 0x22 034 &#x22;",
"/\\\\\\"\\uCAFE\\uBABE\\uAB98\\uFCDE\\ubcda\\uef4A\\b\\f\\n\\r\\t\`1~!@#$%^&*()_+-=[]{}|;:',./<>?":
"A key can be any string"
"/\\\\\\"\\uCAFE\\uBABE\\uAB98\\uFCDE\\ubcda\\uef4A\\b\\f\\n\\r\\t\`1~!@#$%^&*()_+-=[]{}|;:',./<>?": "A key can be any string"
},
0.5,
98.6,
@ -657,8 +730,7 @@ exports[`pass1.json - json5-verify 1`] = `
compact: [1, 2, 3, 4, 5, 6, 7],
jsontext: '{"object with 1 member":["array with 1 element"]}',
quotes: "&#34; \\u0022 %22 0x22 034 &#x22;",
"/\\\\\\"\\uCAFE\\uBABE\\uAB98\\uFCDE\\ubcda\\uef4A\\b\\f\\n\\r\\t\`1~!@#$%^&*()_+-=[]{}|;:',./<>?":
"A key can be any string"
"/\\\\\\"\\uCAFE\\uBABE\\uAB98\\uFCDE\\ubcda\\uef4A\\b\\f\\n\\r\\t\`1~!@#$%^&*()_+-=[]{}|;:',./<>?": "A key can be any string"
},
0.5,
98.6,
@ -776,8 +848,7 @@ exports[`pass1.json - json5-verify 2`] = `
compact: [1, 2, 3, 4, 5, 6, 7],
jsontext: '{"object with 1 member":["array with 1 element"]}',
quotes: "&#34; \\u0022 %22 0x22 034 &#x22;",
"/\\\\\\"\\uCAFE\\uBABE\\uAB98\\uFCDE\\ubcda\\uef4A\\b\\f\\n\\r\\t\`1~!@#$%^&*()_+-=[]{}|;:',./<>?":
"A key can be any string",
"/\\\\\\"\\uCAFE\\uBABE\\uAB98\\uFCDE\\ubcda\\uef4A\\b\\f\\n\\r\\t\`1~!@#$%^&*()_+-=[]{}|;:',./<>?": "A key can be any string",
},
0.5,
98.6,

View File

@ -0,0 +1,5 @@
{
"string": "stringstringstringstringstringstringstringstringstringstringstringstringstringstringstring",
"stringstringstringstringstringstringstringstring": "stringstringstringstringstringstringstringstring",
"stringstringstringstringstringstringstringstringstringstringstringstringstringstringstring": "string"
}

View File

@ -802,8 +802,7 @@ exports[`CLI --support-info (stdout) 1`] = `
}
],
\\"default\\": \\"avoid\\",
\\"description\\":
\\"Include parentheses around a sole arrow function parameter.\\",
\\"description\\": \\"Include parentheses around a sole arrow function parameter.\\",
\\"name\\": \\"arrowParens\\",
\\"pluginDefaults\\": {},
\\"since\\": \\"1.9.0\\",
@ -822,8 +821,7 @@ exports[`CLI --support-info (stdout) 1`] = `
{
\\"category\\": \\"Special\\",
\\"default\\": -1,
\\"description\\":
\\"Print (to stderr) where a cursor at the given position would move to after formatting.\\\\nThis option cannot be used with --range-start and --range-end.\\",
\\"description\\": \\"Print (to stderr) where a cursor at the given position would move to after formatting.\\\\nThis option cannot be used with --range-start and --range-end.\\",
\\"name\\": \\"cursorOffset\\",
\\"pluginDefaults\\": {},
\\"range\\": { \\"end\\": null, \\"start\\": -1, \\"step\\": 1 },
@ -832,8 +830,7 @@ exports[`CLI --support-info (stdout) 1`] = `
},
{
\\"category\\": \\"Special\\",
\\"description\\":
\\"Specify the input filepath. This will be used to do parser inference.\\",
\\"description\\": \\"Specify the input filepath. This will be used to do parser inference.\\",
\\"name\\": \\"filepath\\",
\\"pluginDefaults\\": {},
\\"since\\": \\"1.4.0\\",
@ -842,8 +839,7 @@ exports[`CLI --support-info (stdout) 1`] = `
{
\\"category\\": \\"Special\\",
\\"default\\": false,
\\"description\\":
\\"Insert @format pragma into file's first docblock comment.\\",
\\"description\\": \\"Insert @format pragma into file's first docblock comment.\\",
\\"name\\": \\"insertPragma\\",
\\"pluginDefaults\\": {},
\\"since\\": \\"1.8.0\\",
@ -892,8 +888,7 @@ exports[`CLI --support-info (stdout) 1`] = `
\\"array\\": true,
\\"category\\": \\"Global\\",
\\"default\\": [],
\\"description\\":
\\"Custom directory that contains prettier plugins in node_modules subdirectory.\\\\nOverrides default behavior when plugins are searched relatively to the location of Prettier.\\\\nMultiple values are accepted.\\",
\\"description\\": \\"Custom directory that contains prettier plugins in node_modules subdirectory.\\\\nOverrides default behavior when plugins are searched relatively to the location of Prettier.\\\\nMultiple values are accepted.\\",
\\"name\\": \\"pluginSearchDirs\\",
\\"pluginDefaults\\": {},
\\"since\\": \\"1.13.0\\",
@ -903,8 +898,7 @@ exports[`CLI --support-info (stdout) 1`] = `
\\"array\\": true,
\\"category\\": \\"Global\\",
\\"default\\": [],
\\"description\\":
\\"Add a plugin. Multiple plugins can be passed as separate \`--plugin\`s.\\",
\\"description\\": \\"Add a plugin. Multiple plugins can be passed as separate \`--plugin\`s.\\",
\\"name\\": \\"plugins\\",
\\"pluginDefaults\\": {},
\\"since\\": \\"1.10.0\\",
@ -949,8 +943,7 @@ exports[`CLI --support-info (stdout) 1`] = `
{
\\"category\\": \\"Special\\",
\\"default\\": null,
\\"description\\":
\\"Format code ending at a given character offset (exclusive).\\\\nThe range will extend forwards to the end of the selected statement.\\\\nThis option cannot be used with --cursor-offset.\\",
\\"description\\": \\"Format code ending at a given character offset (exclusive).\\\\nThe range will extend forwards to the end of the selected statement.\\\\nThis option cannot be used with --cursor-offset.\\",
\\"name\\": \\"rangeEnd\\",
\\"pluginDefaults\\": {},
\\"range\\": { \\"end\\": null, \\"start\\": 0, \\"step\\": 1 },
@ -960,8 +953,7 @@ exports[`CLI --support-info (stdout) 1`] = `
{
\\"category\\": \\"Special\\",
\\"default\\": 0,
\\"description\\":
\\"Format code starting at a given character offset.\\\\nThe range will extend backwards to the start of the first line containing the selected statement.\\\\nThis option cannot be used with --cursor-offset.\\",
\\"description\\": \\"Format code starting at a given character offset.\\\\nThe range will extend backwards to the start of the first line containing the selected statement.\\\\nThis option cannot be used with --cursor-offset.\\",
\\"name\\": \\"rangeStart\\",
\\"pluginDefaults\\": {},
\\"range\\": { \\"end\\": null, \\"start\\": 0, \\"step\\": 1 },
@ -971,8 +963,7 @@ exports[`CLI --support-info (stdout) 1`] = `
{
\\"category\\": \\"Special\\",
\\"default\\": false,
\\"description\\":
\\"Require either '@prettier' or '@format' to be present in the file's first docblock comment\\\\nin order for it to be formatted.\\",
\\"description\\": \\"Require either '@prettier' or '@format' to be present in the file's first docblock comment\\\\nin order for it to be formatted.\\",
\\"name\\": \\"requirePragma\\",
\\"pluginDefaults\\": {},
\\"since\\": \\"1.7.0\\",
@ -983,8 +974,7 @@ exports[`CLI --support-info (stdout) 1`] = `
\\"default\\": true,
\\"description\\": \\"Print semicolons.\\",
\\"name\\": \\"semi\\",
\\"oppositeDescription\\":
\\"Do not print semicolons, except at the beginning of lines which may need them.\\",
\\"oppositeDescription\\": \\"Do not print semicolons, except at the beginning of lines which may need them.\\",
\\"pluginDefaults\\": {},
\\"since\\": \\"1.0.0\\",
\\"type\\": \\"boolean\\"
@ -1012,13 +1002,11 @@ exports[`CLI --support-info (stdout) 1`] = `
\\"choices\\": [
{ \\"description\\": \\"No trailing commas.\\", \\"value\\": \\"none\\" },
{
\\"description\\":
\\"Trailing commas where valid in ES5 (objects, arrays, etc.)\\",
\\"description\\": \\"Trailing commas where valid in ES5 (objects, arrays, etc.)\\",
\\"value\\": \\"es5\\"
},
{
\\"description\\":
\\"Trailing commas wherever possible (including function arguments).\\",
\\"description\\": \\"Trailing commas wherever possible (including function arguments).\\",
\\"value\\": \\"all\\"
}
],