diff --git a/lib/rules/key-spacing.js b/lib/rules/key-spacing.js index 083a7e62898..b0491b95fe3 100644 --- a/lib/rules/key-spacing.js +++ b/lib/rules/key-spacing.js @@ -347,6 +347,40 @@ module.exports = { ); } + /** + * Starting from the given node (a property.key node here) looks forward + * until it finds the colon punctuator and returns it. + * @param {ASTNode} node The node to start looking from. + * @returns {ASTNode} The colon punctuator. + */ + function getNextColon(node) { + return sourceCode.getTokenAfter(node, astUtils.isColonToken); + } + + /** + * Starting from the given node (a property.key node here) looks forward + * until it finds the last token before a colon punctuator and returns it. + * @param {ASTNode} node The node to start looking from. + * @returns {ASTNode} The last token before a colon punctuator. + */ + function getLastTokenBeforeColon(node) { + const colonToken = getNextColon(node); + + return sourceCode.getTokenBefore(colonToken); + } + + /** + * Starting from the given node (a property.key node here) looks forward + * until it finds the first token after a colon punctuator and returns it. + * @param {ASTNode} node The node to start looking from. + * @returns {ASTNode} The first token after a colon punctuator. + */ + function getFirstTokenAfterColon(node) { + const colonToken = getNextColon(node); + + return sourceCode.getTokenAfter(colonToken); + } + /** * Checks whether a property is a member of the property group it follows. * @param {ASTNode} lastMember The last Property known to be in the group. @@ -355,7 +389,7 @@ module.exports = { */ function continuesPropertyGroup(lastMember, candidate) { const groupEndLine = lastMember.loc.start.line, - candidateValueStartLine = (isKeyValueProperty(candidate) ? candidate.value : candidate).loc.start.line; + candidateValueStartLine = (isKeyValueProperty(candidate) ? getFirstTokenAfterColon(candidate.key) : candidate).loc.start.line; if (candidateValueStartLine - groupEndLine <= 1) { return true; @@ -384,28 +418,6 @@ module.exports = { return false; } - /** - * Starting from the given a node (a property.key node here) looks forward - * until it finds the last token before a colon punctuator and returns it. - * @param {ASTNode} node The node to start looking from. - * @returns {ASTNode} The last token before a colon punctuator. - */ - function getLastTokenBeforeColon(node) { - const colonToken = sourceCode.getTokenAfter(node, astUtils.isColonToken); - - return sourceCode.getTokenBefore(colonToken); - } - - /** - * Starting from the given a node (a property.key node here) looks forward - * until it finds the colon punctuator and returns it. - * @param {ASTNode} node The node to start looking from. - * @returns {ASTNode} The colon punctuator. - */ - function getNextColon(node) { - return sourceCode.getTokenAfter(node, astUtils.isColonToken); - } - /** * Gets an object literal property's key as the identifier name or string value. * @param {ASTNode} property Property node whose key to retrieve. diff --git a/tests/lib/rules/key-spacing.js b/tests/lib/rules/key-spacing.js index bd2d5111851..9ac345d39c1 100644 --- a/tests/lib/rules/key-spacing.js +++ b/tests/lib/rules/key-spacing.js @@ -1055,6 +1055,35 @@ ruleTester.run("key-spacing", rule, { align: "value" }], parserOptions: { ecmaVersion: 6 } + }, + + // https://github.com/eslint/eslint/issues/16674 + { + code: ` + a = { + item : 123, + longerItem : ( + 1 + 1 + ), + }; + `, + options: [{ + align: { + beforeColon: true, + afterColon: true, + on: "colon" + } + }] + }, + { + code: ` + a = { + item: 123, + longerItem: // a comment - not a token + (1 + 1), + }; + `, + options: [{ align: "value" }] }], invalid: [{ code: "var a ={'key' : value };", @@ -2580,5 +2609,31 @@ ruleTester.run("key-spacing", rule, { { messageId: "extraKey", data: { computed: "", key: "singleLine" }, line: 2, column: 15, type: "Identifier" }, { messageId: "extraKey", data: { computed: "", key: "newGroup" }, line: 3, column: 13, type: "Identifier" } ] + }, + + // https://github.com/eslint/eslint/issues/16674 + { + code: + ` + c = { + item: 123, + longerItem: ( + 1 + 1 + ), + }; + `, + output: + ` + c = { + item : 123, + longerItem: ( + 1 + 1 + ), + }; + `, + options: [{ align: "colon" }], + errors: [ + { messageId: "missingKey", data: { computed: "", key: "item" }, line: 3, column: 13, type: "Identifier" } + ] }] });