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

add handling of lone commas when destructuring arrays #12772

Closed
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
30 changes: 18 additions & 12 deletions lib/rules/comma-style.js
Expand Up @@ -124,10 +124,11 @@ module.exports = {
* @param {Token} commaToken The token representing the comma.
* @param {Token} currentItemToken The first token of the current item.
* @param {Token} reportItem The item to use when reporting an error.
* @param {boolean} arrayLiteral whether the comma is in a destructuring assignment.
* @returns {void}
* @private
*/
function validateCommaItemSpacing(previousItemToken, commaToken, currentItemToken, reportItem) {
function validateCommaItemSpacing(previousItemToken, commaToken, currentItemToken, reportItem, arrayLiteral) {

// if single line
if (astUtils.isTokenOnSameLine(commaToken, currentItemToken) &&
Expand All @@ -144,15 +145,21 @@ module.exports = {
: "between";

// lone comma
context.report({
node: reportItem,
loc: {
line: commaToken.loc.end.line,
column: commaToken.loc.start.column
},
messageId: "unexpectedLineBeforeAndAfterComma",
fix: getFixerFunction(styleType, previousItemToken, commaToken, currentItemToken)
});
if (arrayLiteral) {

// ignored element, move on.
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Place holder in case we may want to report other things later, based on new options.


} else {
context.report({
node: reportItem,
loc: {
line: commaToken.loc.end.line,
column: commaToken.loc.start.column
},
messageId: "unexpectedLineBeforeAndAfterComma",
fix: getFixerFunction(styleType, previousItemToken, commaToken, currentItemToken)
});
}

} else if (style === "first" && !astUtils.isTokenOnSameLine(commaToken, currentItemToken)) {

Expand Down Expand Up @@ -213,7 +220,7 @@ module.exports = {
*/
if (astUtils.isCommaToken(commaToken)) {
validateCommaItemSpacing(previousItemToken, commaToken,
currentItemToken, reportItem);
currentItemToken, reportItem, arrayLiteral);
}

if (item) {
Expand All @@ -232,7 +239,6 @@ module.exports = {
* dangling comma.
*/
if (arrayLiteral) {

const lastToken = sourceCode.getLastToken(node),
nextToLastToken = sourceCode.getTokenBefore(lastToken);

Expand Down
18 changes: 0 additions & 18 deletions tests/lib/rules/comma-style.js
Expand Up @@ -585,19 +585,6 @@ ruleTester.run("comma-style", rule, {
}],
errors: [{ messageId: "expectedCommaFirst" }]
},
{
code: "var foo = [\n(bar\n)\n,\nbaz\n];",
output: "var foo = [\n(bar\n),\nbaz\n];",
errors: [{
messageId: "unexpectedLineBeforeAndAfterComma",
type: "Identifier"
}]
},
Comment on lines -588 to -595
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This particular test doesn't have empty elements. If it's failing, then the change might be incorrect since it modifies behavior for arrays/patterns that don't have empty elements.

A simplified example, this time with an array pattern that doesn't have empty elements:

/*eslint comma-style: ["error", "last", { 
    "exceptions": { "ArrayPattern": false }
}]*/

var [
  foo
  ,
  bar] = [];

This is invalid code by this rule in the actual version - Online Demo

After this change as it's implemented at the moment, this would be a valid code (I think, it would be nice to add a test case). Is this the desired new behavior, i.e., should we ignore any commas on separate lines?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the desired new behavior, i.e., should we ignore any commas on separate lines?

If "comma-style" is enabled, that comma in a new line belongs either with foo (as default 'last'), or bar (if set to 'first'). Meaning your example should indeed fail.
My attempted fix is therefor incomplete.
Will revise and amend it... thanks for pointing it out. 👍

{
code: "[(foo),\n,\nbar]",
output: "[(foo),,\nbar]",
errors: [{ messageId: "unexpectedLineBeforeAndAfterComma" }]
},
{
code: "new Foo(a\n,b);",
output: "new Foo(a,\nb);",
Expand All @@ -608,11 +595,6 @@ ruleTester.run("comma-style", rule, {
}],
errors: [{ messageId: "expectedCommaLast" }]
},
{
code: "[\n[foo(3)],\n,\nbar\n];",
output: "[\n[foo(3)],,\nbar\n];",
errors: [{ messageId: "unexpectedLineBeforeAndAfterComma" }]
},
{

// https://github.com/eslint/eslint/issues/10632
Expand Down