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 10, 2020
1 parent 9dfc850 commit 48a941e
Showing 1 changed file with 21 additions and 16 deletions.
37 changes: 21 additions & 16 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} 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) &&
Expand All @@ -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)) {

Expand Down Expand Up @@ -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);
Expand All @@ -213,7 +219,7 @@ module.exports = {
*/
if (astUtils.isCommaToken(commaToken)) {
validateCommaItemSpacing(previousItemToken, commaToken,
currentItemToken, reportItem);
currentItemToken, reportItem, arrayPattern);
}

if (item) {
Expand All @@ -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);

Expand Down

0 comments on commit 48a941e

Please sign in to comment.