diff --git a/lib/rules/comma-style.js b/lib/rules/comma-style.js index 2586cf66e92..78438a858db 100644 --- a/lib/rules/comma-style.js +++ b/lib/rules/comma-style.js @@ -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},`; @@ -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, @@ -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)) { diff --git a/tests/lib/rules/comma-style.js b/tests/lib/rules/comma-style.js index 83a1ca2c957..a63d80ae876 100644 --- a/tests/lib/rules/comma-style.js +++ b/tests/lib/rules/comma-style.js @@ -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" }] } ] });