Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: fix the fixer of lone comma with comments (fixes #10632) #11154

Merged
merged 3 commits into from Dec 8, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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(/\r?\n/, "")}`;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be good to look in ast-utils; I think we have a regular expression stored in there which handles all of the JS line break types, including \u2028 and \u2029.

Copy link
Member Author

@g-plane g-plane Dec 4, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! And I have updated. You can check the latest changes.


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" && comment.loc.start.line === commaToken.loc.start.line
? 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" }]
}
]
});