diff --git a/lib/rules/key-spacing.js b/lib/rules/key-spacing.js index 5aced89a68e..994c35627f7 100644 --- a/lib/rules/key-spacing.js +++ b/lib/rules/key-spacing.js @@ -42,30 +42,6 @@ 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 @@ -654,7 +630,7 @@ module.exports = { return { ObjectExpression(node) { - if (isSingleLine(node) || isPropertiesOnSameLine(node)) { + if (isSingleLine(node)) { verifyListSpacing(node.properties.filter(isKeyValueProperty)); } else { verifyAlignment(node); @@ -667,9 +643,7 @@ module.exports = { // Obey beforeColon and afterColon in each property as configured return { Property(node) { - const option = (isSingleLine(node.parent) || isPropertiesOnSameLine(node.parent)) ? singleLineOptions : multiLineOptions; - - verifySpacing(node, option); + verifySpacing(node, isSingleLine(node.parent) ? singleLineOptions : multiLineOptions); } }; diff --git a/tests/lib/rules/key-spacing.js b/tests/lib/rules/key-spacing.js index 9225f8b8cc4..a9ed533741c 100644 --- a/tests/lib/rules/key-spacing.js +++ b/tests/lib/rules/key-spacing.js @@ -384,214 +384,6 @@ 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 { @@ -1977,218 +1769,5 @@ 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" } - ] }] });