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 11, 2020
1 parent 9dfc850 commit deb43bd
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 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} 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) &&
Expand All @@ -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)) {

Expand Down Expand Up @@ -213,7 +220,7 @@ module.exports = {
*/
if (astUtils.isCommaToken(commaToken)) {
validateCommaItemSpacing(previousItemToken, commaToken,
currentItemToken, reportItem);
currentItemToken, reportItem, arrayLiteral);
}

if (item) {
Expand All @@ -232,7 +239,6 @@ module.exports = {
* dangling comma.
*/
if (arrayLiteral) {

const lastToken = sourceCode.getLastToken(node),
nextToLastToken = sourceCode.getTokenBefore(lastToken);

Expand Down

0 comments on commit deb43bd

Please sign in to comment.