From ed0ecdad18d715eed9b6314dbb0de8d510f557c8 Mon Sep 17 00:00:00 2001 From: Ika Date: Wed, 18 Jul 2018 05:08:59 +0800 Subject: [PATCH] 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 --- src/language-js/printer-estree.js | 5 +- tests/json/__snapshots__/jsfmt.spec.js.snap | 87 +++++++++++++++++-- tests/json/key-value.json | 5 ++ .../__snapshots__/support-info.js.snap | 36 +++----- 4 files changed, 100 insertions(+), 33 deletions(-) create mode 100644 tests/json/key-value.json diff --git a/src/language-js/printer-estree.js b/src/language-js/printer-estree.js index 2009401e..719178ae 100644 --- a/src/language-js/printer-estree.js +++ b/src/language-js/printer-estree.js @@ -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])); diff --git a/tests/json/__snapshots__/jsfmt.spec.js.snap b/tests/json/__snapshots__/jsfmt.spec.js.snap index d8803b9c..ac2b14cd 100644 --- a/tests/json/__snapshots__/jsfmt.spec.js.snap +++ b/tests/json/__snapshots__/jsfmt.spec.js.snap @@ -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": "" \\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, @@ -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: "" \\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, @@ -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: "" \\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, diff --git a/tests/json/key-value.json b/tests/json/key-value.json new file mode 100644 index 00000000..ea1ace0c --- /dev/null +++ b/tests/json/key-value.json @@ -0,0 +1,5 @@ +{ + "string": "stringstringstringstringstringstringstringstringstringstringstringstringstringstringstring", + "stringstringstringstringstringstringstringstring": "stringstringstringstringstringstringstringstring", + "stringstringstringstringstringstringstringstringstringstringstringstringstringstringstring": "string" +} diff --git a/tests_integration/__tests__/__snapshots__/support-info.js.snap b/tests_integration/__tests__/__snapshots__/support-info.js.snap index 34680d0c..6c666eed 100644 --- a/tests_integration/__tests__/__snapshots__/support-info.js.snap +++ b/tests_integration/__tests__/__snapshots__/support-info.js.snap @@ -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\\" } ],