diff --git a/lib/rules/comma-style.js b/lib/rules/comma-style.js index bc22f05dd389..80f20702630b 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} arrayPattern whether the comma is in a destructuring assignment. * @returns {void} * @private */ - function validateCommaItemSpacing(previousItemToken, commaToken, currentItemToken, reportItem) { + function validateCommaItemSpacing(previousItemToken, commaToken, currentItemToken, reportItem, arrayPattern) { // if single line if (astUtils.isTokenOnSameLine(commaToken, currentItemToken) && @@ -144,15 +145,19 @@ 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 (arrayPattern) { + // 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,9 +190,10 @@ module.exports = { */ function validateComma(node, property) { const items = node[property], - arrayLiteral = (node.type === "ArrayExpression" || node.type === "ArrayPattern"); + arrayLiteral = node.type === "ArrayExpression", + arrayPattern = node.type === "ArrayPattern"; - if (items.length > 1 || arrayLiteral) { + if (items.length > 1 || arrayLiteral || arrayPattern) { // seed as opening [ let previousItemToken = sourceCode.getFirstToken(node); @@ -213,7 +219,7 @@ module.exports = { */ if (astUtils.isCommaToken(commaToken)) { validateCommaItemSpacing(previousItemToken, commaToken, - currentItemToken, reportItem); + currentItemToken, reportItem, arrayPattern); } if (item) { @@ -226,13 +232,12 @@ module.exports = { }); /* - * Special case for array literals that have empty last items, such + * Special case for arrays that have empty last items, such * as [ 1, 2, ]. These arrays only have two items show up in the * AST, so we need to look at the token to verify that there's no * dangling comma. */ - if (arrayLiteral) { - + if (arrayLiteral || arrayPattern) { const lastToken = sourceCode.getLastToken(node), nextToLastToken = sourceCode.getTokenBefore(lastToken);