From 01cb061ec32203b03e4171314f33af55f837b5a1 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 | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/lib/rules/comma-style.js b/lib/rules/comma-style.js index bc22f05dd389..6f0169ccea88 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)) { @@ -185,7 +192,7 @@ module.exports = { */ function validateComma(node, property) { const items = node[property], - arrayLiteral = (node.type === "ArrayExpression" || node.type === "ArrayPattern"); + arrayLiteral = ["ArrayExpression", "ArrayPattern"].includes(node.type); if (items.length > 1 || arrayLiteral) { @@ -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);