Skip to content

Commit

Permalink
add 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 b05010c
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions lib/rules/comma-style.js
Expand Up @@ -127,7 +127,7 @@ module.exports = {
* @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,7 +144,7 @@ module.exports = {
: "between";

// lone comma
context.report({
arrayPattern || context.report({
node: reportItem,
loc: {
line: commaToken.loc.end.line,
Expand Down Expand Up @@ -185,9 +185,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 +214,7 @@ module.exports = {
*/
if (astUtils.isCommaToken(commaToken)) {
validateCommaItemSpacing(previousItemToken, commaToken,
currentItemToken, reportItem);
currentItemToken, reportItem, arrayPattern);
}

if (item) {
Expand All @@ -226,13 +227,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 b05010c

Please sign in to comment.