From 832f906910b6737052f006f4960f30e3a4d665d8 Mon Sep 17 00:00:00 2001 From: Anders Richardsson Date: Fri, 10 Jan 2020 11:57:07 -0500 Subject: [PATCH] Fix: add comma-style handling of lone commas when destructuring arrays This handles ignored elements as per https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#Ignoring_some_returned_values Closes #12756 --- lib/rules/comma-style.js | 30 ++++++++++++++++++------------ tests/lib/rules/comma-style.js | 18 ------------------ 2 files changed, 18 insertions(+), 30 deletions(-) diff --git a/lib/rules/comma-style.js b/lib/rules/comma-style.js index bc22f05dd38..8190c533de5 100644 --- a/lib/rules/comma-style.js +++ b/lib/rules/comma-style.js @@ -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) && @@ -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)) { @@ -213,7 +220,7 @@ module.exports = { */ if (astUtils.isCommaToken(commaToken)) { validateCommaItemSpacing(previousItemToken, commaToken, - currentItemToken, reportItem); + currentItemToken, reportItem, arrayLiteral); } if (item) { @@ -232,7 +239,6 @@ module.exports = { * dangling comma. */ if (arrayLiteral) { - const lastToken = sourceCode.getLastToken(node), nextToLastToken = sourceCode.getTokenBefore(lastToken); diff --git a/tests/lib/rules/comma-style.js b/tests/lib/rules/comma-style.js index 5aa401f914a..396f85a8281 100644 --- a/tests/lib/rules/comma-style.js +++ b/tests/lib/rules/comma-style.js @@ -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);", @@ -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