Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: group properties with values in parentheses in key-spacing #16677

Merged
merged 2 commits into from Dec 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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" }
]
}]
});