Navigation Menu

Skip to content

Commit

Permalink
feat: group properties with values in parentheses in key-spacing (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
fasttime committed Dec 21, 2022
1 parent 10a5c78 commit 30d0daf
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 23 deletions.
58 changes: 35 additions & 23 deletions lib/rules/key-spacing.js
Expand Up @@ -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.
Expand All @@ -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;
Expand Down Expand Up @@ -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.
Expand Down
55 changes: 55 additions & 0 deletions tests/lib/rules/key-spacing.js
Expand Up @@ -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 };",
Expand Down Expand Up @@ -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" }
]
}]
});

0 comments on commit 30d0daf

Please sign in to comment.