Skip to content

Commit 30d0daf

Browse files
authored
feat: group properties with values in parentheses in key-spacing (#16677)
1 parent 10a5c78 commit 30d0daf

2 files changed

Lines changed: 90 additions & 23 deletions

File tree

lib/rules/key-spacing.js

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,40 @@ module.exports = {
347347
);
348348
}
349349

350+
/**
351+
* Starting from the given node (a property.key node here) looks forward
352+
* until it finds the colon punctuator and returns it.
353+
* @param {ASTNode} node The node to start looking from.
354+
* @returns {ASTNode} The colon punctuator.
355+
*/
356+
function getNextColon(node) {
357+
return sourceCode.getTokenAfter(node, astUtils.isColonToken);
358+
}
359+
360+
/**
361+
* Starting from the given node (a property.key node here) looks forward
362+
* until it finds the last token before a colon punctuator and returns it.
363+
* @param {ASTNode} node The node to start looking from.
364+
* @returns {ASTNode} The last token before a colon punctuator.
365+
*/
366+
function getLastTokenBeforeColon(node) {
367+
const colonToken = getNextColon(node);
368+
369+
return sourceCode.getTokenBefore(colonToken);
370+
}
371+
372+
/**
373+
* Starting from the given node (a property.key node here) looks forward
374+
* until it finds the first token after a colon punctuator and returns it.
375+
* @param {ASTNode} node The node to start looking from.
376+
* @returns {ASTNode} The first token after a colon punctuator.
377+
*/
378+
function getFirstTokenAfterColon(node) {
379+
const colonToken = getNextColon(node);
380+
381+
return sourceCode.getTokenAfter(colonToken);
382+
}
383+
350384
/**
351385
* Checks whether a property is a member of the property group it follows.
352386
* @param {ASTNode} lastMember The last Property known to be in the group.
@@ -355,7 +389,7 @@ module.exports = {
355389
*/
356390
function continuesPropertyGroup(lastMember, candidate) {
357391
const groupEndLine = lastMember.loc.start.line,
358-
candidateValueStartLine = (isKeyValueProperty(candidate) ? candidate.value : candidate).loc.start.line;
392+
candidateValueStartLine = (isKeyValueProperty(candidate) ? getFirstTokenAfterColon(candidate.key) : candidate).loc.start.line;
359393

360394
if (candidateValueStartLine - groupEndLine <= 1) {
361395
return true;
@@ -384,28 +418,6 @@ module.exports = {
384418
return false;
385419
}
386420

387-
/**
388-
* Starting from the given a node (a property.key node here) looks forward
389-
* until it finds the last token before a colon punctuator and returns it.
390-
* @param {ASTNode} node The node to start looking from.
391-
* @returns {ASTNode} The last token before a colon punctuator.
392-
*/
393-
function getLastTokenBeforeColon(node) {
394-
const colonToken = sourceCode.getTokenAfter(node, astUtils.isColonToken);
395-
396-
return sourceCode.getTokenBefore(colonToken);
397-
}
398-
399-
/**
400-
* Starting from the given a node (a property.key node here) looks forward
401-
* until it finds the colon punctuator and returns it.
402-
* @param {ASTNode} node The node to start looking from.
403-
* @returns {ASTNode} The colon punctuator.
404-
*/
405-
function getNextColon(node) {
406-
return sourceCode.getTokenAfter(node, astUtils.isColonToken);
407-
}
408-
409421
/**
410422
* Gets an object literal property's key as the identifier name or string value.
411423
* @param {ASTNode} property Property node whose key to retrieve.

tests/lib/rules/key-spacing.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,6 +1055,35 @@ ruleTester.run("key-spacing", rule, {
10551055
align: "value"
10561056
}],
10571057
parserOptions: { ecmaVersion: 6 }
1058+
},
1059+
1060+
// https://github.com/eslint/eslint/issues/16674
1061+
{
1062+
code: `
1063+
a = {
1064+
item : 123,
1065+
longerItem : (
1066+
1 + 1
1067+
),
1068+
};
1069+
`,
1070+
options: [{
1071+
align: {
1072+
beforeColon: true,
1073+
afterColon: true,
1074+
on: "colon"
1075+
}
1076+
}]
1077+
},
1078+
{
1079+
code: `
1080+
a = {
1081+
item: 123,
1082+
longerItem: // a comment - not a token
1083+
(1 + 1),
1084+
};
1085+
`,
1086+
options: [{ align: "value" }]
10581087
}],
10591088
invalid: [{
10601089
code: "var a ={'key' : value };",
@@ -2580,5 +2609,31 @@ ruleTester.run("key-spacing", rule, {
25802609
{ messageId: "extraKey", data: { computed: "", key: "singleLine" }, line: 2, column: 15, type: "Identifier" },
25812610
{ messageId: "extraKey", data: { computed: "", key: "newGroup" }, line: 3, column: 13, type: "Identifier" }
25822611
]
2612+
},
2613+
2614+
// https://github.com/eslint/eslint/issues/16674
2615+
{
2616+
code:
2617+
`
2618+
c = {
2619+
item: 123,
2620+
longerItem: (
2621+
1 + 1
2622+
),
2623+
};
2624+
`,
2625+
output:
2626+
`
2627+
c = {
2628+
item : 123,
2629+
longerItem: (
2630+
1 + 1
2631+
),
2632+
};
2633+
`,
2634+
options: [{ align: "colon" }],
2635+
errors: [
2636+
{ messageId: "missingKey", data: { computed: "", key: "item" }, line: 3, column: 13, type: "Identifier" }
2637+
]
25832638
}]
25842639
});

0 commit comments

Comments
 (0)