From 6503cb8d99e549fece53b80b110e890a7978b9fd Mon Sep 17 00:00:00 2001 From: YeonJuan Date: Fri, 15 Nov 2019 09:59:38 +0900 Subject: [PATCH] Update: Fix uglified object align in key-spacing (fixes #11414) (#12472) --- lib/rules/key-spacing.js | 30 ++- tests/lib/rules/key-spacing.js | 421 +++++++++++++++++++++++++++++++++ 2 files changed, 449 insertions(+), 2 deletions(-) diff --git a/lib/rules/key-spacing.js b/lib/rules/key-spacing.js index 994c35627f7..5aced89a68e 100644 --- a/lib/rules/key-spacing.js +++ b/lib/rules/key-spacing.js @@ -42,6 +42,30 @@ function isSingleLine(node) { return (node.loc.end.line === node.loc.start.line); } +/** + * Checks whether both nodes are on the same line. + * @param {ASTNode} nodeA AST Node being evaluated. + * @param {ASTNode} nodeB AST Node being evaluated. + * @returns {boolean} True if both nodes are on the same line. + */ +function isOnSameLine(nodeA, nodeB) { + return (nodeA.loc.end.line === nodeB.loc.end.line); +} + +/** + * Checks whether the properties of a node on the same line. + * @param {ASTNode} node node + * @returns {boolean} True if the properties of a node are on the same line. + */ +function isPropertiesOnSameLine(node) { + if (node.properties.length <= 1) { + return true; + } + const [firstProperty] = node.properties; + + return node.properties.every(property => isOnSameLine(firstProperty, property)); +} + /** * Initializes a single option property from the configuration with defaults for undefined values * @param {Object} toOptions Object to be initialized @@ -630,7 +654,7 @@ module.exports = { return { ObjectExpression(node) { - if (isSingleLine(node)) { + if (isSingleLine(node) || isPropertiesOnSameLine(node)) { verifyListSpacing(node.properties.filter(isKeyValueProperty)); } else { verifyAlignment(node); @@ -643,7 +667,9 @@ module.exports = { // Obey beforeColon and afterColon in each property as configured return { Property(node) { - verifySpacing(node, isSingleLine(node.parent) ? singleLineOptions : multiLineOptions); + const option = (isSingleLine(node.parent) || isPropertiesOnSameLine(node.parent)) ? singleLineOptions : multiLineOptions; + + verifySpacing(node, option); } }; diff --git a/tests/lib/rules/key-spacing.js b/tests/lib/rules/key-spacing.js index a9ed533741c..9225f8b8cc4 100644 --- a/tests/lib/rules/key-spacing.js +++ b/tests/lib/rules/key-spacing.js @@ -384,6 +384,214 @@ ruleTester.run("key-spacing", rule, { }], parserOptions: { ecmaVersion: 2018 } }, + { + code: [ + "({", + " foo : 1, bar : 2, baz : 3", + "});" + ].join("\n"), + options: [{ + beforeColon: true + }] + }, + { + code: [ + "({", + " foo: 1, bar: 2, baz: 3", + "});" + ].join("\n"), + options: [{ + beforeColon: false + }] + }, + { + code: [ + "({", + " foo: 1, bar: 2, baz: 3", + "});" + ].join("\n"), + options: [{ + afterColon: true + }] + }, + { + code: [ + "({", + " foo:1, bar:2, baz:3", + "});" + ].join("\n"), + options: [{ + afterColon: false + }] + }, + { + code: [ + "({", + " foo :1, bar :2, baz :3", + "});" + ].join("\n"), + options: [{ + beforeColon: true, + afterColon: false + }] + }, + { + code: [ + "({", + " foo: 1, bar: 2, baz: 3", + "});" + ].join("\n"), + options: [{ + beforeColon: false, + afterColon: true + }] + }, + { + code: [ + "({", + " foo : 1, bar : 2, baz : 3", + "});" + ].join("\n"), + options: [{ + beforeColon: true, + afterColon: true + }] + }, + { + code: [ + "({", + " foo:1, bar:2, baz:3", + "});" + ].join("\n"), + options: [{ + beforeColon: false, + afterColon: false + }] + }, + { + code: [ + "({", + " foo: 1, bar: 2, baz: 3", + "});" + ].join("\n"), + options: [{ + mode: "strict" + }] + }, + { + code: [ + "({", + " foo: 1, bar: 2, baz: 3", + "});" + ].join("\n"), + options: [{ + mode: "minimum" + }] + }, + { + code: [ + "({", + " foo: 1, bar: 2, baz: 3", + "});" + ].join("\n"), + options: [{ + singleLine: { + afterColon: true, + beforeColon: false + } + }] + }, + { + code: [ + "({", + " foo :1, bar :2, baz :3", + "});" + ].join("\n"), + options: [{ + singleLine: { + afterColon: false, + beforeColon: true + } + }] + }, + { + code: [ + "({", + " foo: 'bar',", + "});" + ].join("\n"), + options: [{ + multiLine: { + align: "value" + } + }] + }, + { + code: [ + "({", + " foo: 1, bar: 2, baz: 3", + "});" + ].join("\n"), + options: [{ + multiLine: { + align: "value" + } + }] + }, + { + code: [ + "({", + " foo: 1, bar: { qux: 4", + " }, baz: 3,", + "})" + ].join("\n"), + options: [ + { + singleLine: { + afterColon: true, + beforeColon: false + }, + multiLine: { + align: "value" + } + } + ] + }, + { + code: [ + "({", + " foo: 1, bar: { qux1: 4, qux2: 5", + " }, baz: 3,", + "})" + ].join("\n"), + options: [ + { + singleLine: { + afterColon: true, + beforeColon: false + }, + multiLine: { + align: "value" + } + } + ] + }, + { + code: [ + "({", + " foo: 1, bar: {", + " qux1: 4, qux2: 5", + " }, baz: 3,", + "})" + ].join("\n"), + options: [ + { + multiLine: { + align: "value" + } + } + ] + }, // https://github.com/eslint/eslint/issues/4792 { @@ -1769,5 +1977,218 @@ ruleTester.run("key-spacing", rule, { { messageId: "missingKey", data: { computed: "", key: "foo" }, line: 1, column: 7, type: "Identifier" }, { messageId: "missingValue", data: { computed: "", key: "foo" }, line: 1, column: 19, type: "Identifier" } ] + }, + { + code: [ + "({", + " foo : 1, bar : 2, baz : 3", + "});" + ].join("\n"), + output: [ + "({", + " foo: 1, bar: 2, baz: 3", + "});" + ].join("\n"), + options: [{ + beforeColon: false + }], + errors: [ + { messageId: "extraKey", data: { computed: "", key: "foo" }, line: 2, column: 5, type: "Identifier" }, + { messageId: "extraKey", data: { computed: "", key: "bar" }, line: 2, column: 14, type: "Identifier" }, + { messageId: "extraKey", data: { computed: "", key: "baz" }, line: 2, column: 23, type: "Identifier" } + ] + }, + { + code: [ + "({", + " foo: 1, bar: 2, baz: 3", + "});" + ].join("\n"), + output: [ + "({", + " foo : 1, bar : 2, baz : 3", + "});" + ].join("\n"), + options: [{ + beforeColon: true + }], + errors: [ + { messageId: "missingKey", data: { computed: "", key: "foo" }, line: 2, column: 5, type: "Identifier" }, + { messageId: "missingKey", data: { computed: "", key: "bar" }, line: 2, column: 13, type: "Identifier" }, + { messageId: "missingKey", data: { computed: "", key: "baz" }, line: 2, column: 21, type: "Identifier" } + ] + }, + { + code: [ + "({", + " foo: 1, bar: 2, baz: 3", + "});" + ].join("\n"), + output: [ + "({", + " foo:1, bar:2, baz:3", + "});" + ].join("\n"), + options: [{ + afterColon: false + }], + errors: [ + { messageId: "extraValue", data: { computed: "", key: "foo" }, line: 2, column: 10, type: "Literal" }, + { messageId: "extraValue", data: { computed: "", key: "bar" }, line: 2, column: 18, type: "Literal" }, + { messageId: "extraValue", data: { computed: "", key: "baz" }, line: 2, column: 26, type: "Literal" } + ] + }, + { + code: [ + "({", + " foo:1, bar:2, baz:3", + "});" + ].join("\n"), + output: [ + "({", + " foo: 1, bar: 2, baz: 3", + "});" + ].join("\n"), + options: [{ + afterColon: true + }], + errors: [ + { messageId: "missingValue", data: { computed: "", key: "foo" }, line: 2, column: 9, type: "Literal" }, + { messageId: "missingValue", data: { computed: "", key: "bar" }, line: 2, column: 16, type: "Literal" }, + { messageId: "missingValue", data: { computed: "", key: "baz" }, line: 2, column: 23, type: "Literal" } + ] + }, + { + code: [ + "({", + " foo: 1, bar: 2, baz: 3", + "});" + ].join("\n"), + output: [ + "({", + " foo :1, bar :2, baz :3", + "});" + ].join("\n"), + options: [{ + beforeColon: true, + afterColon: false + }], + errors: [ + { messageId: "missingKey", data: { computed: "", key: "foo" }, line: 2, column: 5, type: "Identifier" }, + { messageId: "extraValue", data: { computed: "", key: "foo" }, line: 2, column: 10, type: "Literal" }, + { messageId: "missingKey", data: { computed: "", key: "bar" }, line: 2, column: 13, type: "Identifier" }, + { messageId: "extraValue", data: { computed: "", key: "bar" }, line: 2, column: 18, type: "Literal" }, + { messageId: "missingKey", data: { computed: "", key: "baz" }, line: 2, column: 21, type: "Identifier" }, + { messageId: "extraValue", data: { computed: "", key: "baz" }, line: 2, column: 26, type: "Literal" } + ] + }, + { + code: [ + "({", + " foo :1, bar :2, baz :3", + "});" + ].join("\n"), + output: [ + "({", + " foo: 1, bar: 2, baz: 3", + "});" + ].join("\n"), + options: [{ + mode: "strict" + }], + errors: [ + { messageId: "extraKey", data: { computed: "", key: "foo" }, line: 2, column: 5, type: "Identifier" }, + { messageId: "missingValue", data: { computed: "", key: "foo" }, line: 2, column: 10, type: "Literal" }, + { messageId: "extraKey", data: { computed: "", key: "bar" }, line: 2, column: 13, type: "Identifier" }, + { messageId: "missingValue", data: { computed: "", key: "bar" }, line: 2, column: 18, type: "Literal" }, + { messageId: "extraKey", data: { computed: "", key: "baz" }, line: 2, column: 21, type: "Identifier" }, + { messageId: "missingValue", data: { computed: "", key: "baz" }, line: 2, column: 26, type: "Literal" } + ] + }, + { + code: [ + "({", + " foo :1, bar :2, baz :3", + "});" + ].join("\n"), + output: [ + "({", + " foo: 1, bar: 2, baz: 3", + "});" + ].join("\n"), + options: [{ + mode: "minimum" + }], + errors: [ + { messageId: "extraKey", data: { computed: "", key: "foo" }, line: 2, column: 5, type: "Identifier" }, + { messageId: "missingValue", data: { computed: "", key: "foo" }, line: 2, column: 10, type: "Literal" }, + { messageId: "extraKey", data: { computed: "", key: "bar" }, line: 2, column: 13, type: "Identifier" }, + { messageId: "missingValue", data: { computed: "", key: "bar" }, line: 2, column: 18, type: "Literal" }, + { messageId: "extraKey", data: { computed: "", key: "baz" }, line: 2, column: 21, type: "Identifier" }, + { messageId: "missingValue", data: { computed: "", key: "baz" }, line: 2, column: 26, type: "Literal" } + ] + }, + { + code: [ + "({", + " foo: 1, bar: 2,", + "});" + ].join("\n"), + output: [ + "({", + " foo :1, bar :2,", + "});" + ].join("\n"), + options: [{ + singleLine: { + beforeColon: true, + afterColon: false + } + }], + errors: [ + { messageId: "missingKey", data: { computed: "", key: "foo" }, line: 2, column: 5, type: "Identifier" }, + { messageId: "extraValue", data: { computed: "", key: "foo" }, line: 2, column: 10, type: "Literal" }, + { messageId: "missingKey", data: { computed: "", key: "bar" }, line: 2, column: 13, type: "Identifier" }, + { messageId: "extraValue", data: { computed: "", key: "bar" }, line: 2, column: 18, type: "Literal" } + ] + }, + { + code: [ + "({", + " foo: 1, bar: { qux1: 4, qux2: 5", + " }, baz: 3,", + "})" + ].join("\n"), + output: [ + "({", + " foo :1, bar :{ qux1 :4, qux2 :5", + " }, baz :3,", + "})" + ].join("\n"), + options: [ + { + singleLine: { + afterColon: false, + beforeColon: true + }, + multiLine: { + align: "value", + afterColon: false, + beforeColon: true + } + } + ], + errors: [ + { messageId: "missingKey", data: { computed: "", key: "foo" }, line: 2, column: 3, type: "Identifier" }, + { messageId: "extraValue", data: { computed: "", key: "foo" }, line: 2, column: 8, type: "Literal" }, + { messageId: "missingKey", data: { computed: "", key: "bar" }, line: 2, column: 11, type: "Identifier" }, + { messageId: "extraValue", data: { computed: "", key: "bar" }, line: 2, column: 16, type: "ObjectExpression" }, + { messageId: "missingKey", data: { computed: "", key: "qux1" }, line: 2, column: 18, type: "Identifier" }, + { messageId: "extraValue", data: { computed: "", key: "qux1" }, line: 2, column: 24, type: "Literal" }, + { messageId: "missingKey", data: { computed: "", key: "qux2" }, line: 2, column: 27, type: "Identifier" }, + { messageId: "extraValue", data: { computed: "", key: "qux2" }, line: 2, column: 33, type: "Literal" }, + { messageId: "missingKey", data: { computed: "", key: "baz" }, line: 3, column: 6, type: "Identifier" }, + { messageId: "extraValue", data: { computed: "", key: "baz" }, line: 3, column: 11, type: "Literal" } + ] }] });