Skip to content

Commit

Permalink
Fix: fix the fixer of lone comma with comments (fixes #10632) (#11154)
Browse files Browse the repository at this point in the history
  • Loading branch information
g-plane authored and not-an-aardvark committed Dec 8, 2018
1 parent f850726 commit 3bf0332
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/rules/comma-style.js
Expand Up @@ -85,7 +85,7 @@ module.exports = {
function getReplacedText(styleType, text) {
switch (styleType) {
case "between":
return `,${text.replace("\n", "")}`;
return `,${text.replace(astUtils.LINEBREAK_MATCHER, "")}`;

case "first":
return `${text},`;
Expand Down Expand Up @@ -138,6 +138,11 @@ module.exports = {
} else if (!astUtils.isTokenOnSameLine(commaToken, currentItemToken) &&
!astUtils.isTokenOnSameLine(previousItemToken, commaToken)) {

const comment = sourceCode.getCommentsAfter(commaToken)[0];
const styleType = comment && comment.type === "Block" && astUtils.isTokenOnSameLine(commaToken, comment)
? style
: "between";

// lone comma
context.report({
node: reportItem,
Expand All @@ -146,7 +151,7 @@ module.exports = {
column: commaToken.loc.start.column
},
messageId: "unexpectedLineBeforeAndAfterComma",
fix: getFixerFunction("between", previousItemToken, commaToken, currentItemToken)
fix: getFixerFunction(styleType, previousItemToken, commaToken, currentItemToken)
});

} else if (style === "first" && !astUtils.isTokenOnSameLine(commaToken, currentItemToken)) {
Expand Down
7 changes: 7 additions & 0 deletions tests/lib/rules/comma-style.js
Expand Up @@ -612,6 +612,13 @@ ruleTester.run("comma-style", rule, {
code: "[\n[foo(3)],\n,\nbar\n];",
output: "[\n[foo(3)],,\nbar\n];",
errors: [{ messageId: "unexpectedLineBeforeAndAfterComma" }]
},
{

// https://github.com/eslint/eslint/issues/10632
code: "[foo//\n,/*block\ncomment*/];",
output: "[foo,//\n/*block\ncomment*/];",
errors: [{ messageId: "unexpectedLineBeforeAndAfterComma" }]
}
]
});

0 comments on commit 3bf0332

Please sign in to comment.