Skip to content

Commit

Permalink
Fix: add comma-style handling of lone commas when destructuring arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
justincorrigible committed Jan 11, 2020
1 parent 9dfc850 commit 832f906
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 30 deletions.
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.

} 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"
}]
},
{
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

0 comments on commit 832f906

Please sign in to comment.