diff --git a/lib/rules/multiline-comment-style.js b/lib/rules/multiline-comment-style.js index 17c297fad7e8..742d814a8a1b 100644 --- a/lib/rules/multiline-comment-style.js +++ b/lib/rules/multiline-comment-style.js @@ -42,6 +42,15 @@ module.exports = { // Helpers //---------------------------------------------------------------------- + /** + * Checks if a comment line is starred. + * @param {string} line A string representing a comment line. + * @returns {boolean} Whether or not the comment line is starred. + */ + function isStarredCommentLine(line) { + return /^\s*\*/u.test(line); + } + /** * Checks if a comment group is in starred-block form. * @param {Token[]} commentGroup A group of comments, containing either multiple line comments or a single block comment. @@ -273,30 +282,42 @@ module.exports = { for (let lineNumber = firstComment.loc.start.line + 1; lineNumber <= firstComment.loc.end.line; lineNumber++) { const lineText = sourceCode.lines[lineNumber - 1]; + const errorType = isStarredCommentLine(lineText) + ? "alignment" + : "missingStar"; if (!lineText.startsWith(expectedLinePrefix)) { context.report({ loc: { start: { line: lineNumber, column: 0 }, - end: { line: lineNumber, column: sourceCode.lines[lineNumber - 1].length } + end: { line: lineNumber, column: lineText.length } }, - messageId: /^\s*\*/u.test(lineText) - ? "alignment" - : "missingStar", + messageId: errorType, fix(fixer) { const lineStartIndex = sourceCode.getIndexFromLoc({ line: lineNumber, column: 0 }); - const [linePrefix, whitespaceBefore, whitespaceAfter] = lineText.match(/^(\s*)\*?(\s*)/u); - const leadingWhitespace = whitespaceAfter.length && whitespaceAfter || ` ${ - whitespaceBefore.startsWith(expectedLeadingWhitespace) - ? whitespaceBefore.replace(expectedLeadingWhitespace, "") - : "" - }`; - const replacementText = lineNumber === firstComment.loc.end.line || lineText.length === linePrefix.length - ? expectedLinePrefix - : `${expectedLinePrefix}${leadingWhitespace}`; - const commentStartIndex = lineStartIndex + linePrefix.length; - - return fixer.replaceTextRange([lineStartIndex, commentStartIndex], replacementText); + + if (errorType === "alignment") { + const [, commentTextPrefix = ""] = lineText.match(/^(\s*\*)/u) || []; + const commentTextStartIndex = lineStartIndex + commentTextPrefix.length; + + return fixer.replaceTextRange([lineStartIndex, commentTextStartIndex], expectedLinePrefix); + } + + const [, commentTextPrefix = ""] = lineText.match(/^(\s*)/u) || []; + const commentTextStartIndex = lineStartIndex + commentTextPrefix.length; + let offset; + + for (const [idx, line] of commentLines.entries()) { + if (line.trim().length > 0) { + const lineTextToAlignWith = sourceCode.lines[firstComment.loc.start.line - 1 + idx]; + const [, prefix = "", initialOffset = ""] = lineTextToAlignWith.match(/^(\s*(?:\/?\*)?(\s*))/u) || []; + + offset = `${commentTextPrefix.slice(prefix.length)}${initialOffset}`; + break; + } + } + + return fixer.replaceTextRange([lineStartIndex, commentTextStartIndex], `${expectedLinePrefix}${offset}`); } }); } diff --git a/tests/lib/rules/multiline-comment-style.js b/tests/lib/rules/multiline-comment-style.js index 047e4e4f0c73..c69265ba9e21 100644 --- a/tests/lib/rules/multiline-comment-style.js +++ b/tests/lib/rules/multiline-comment-style.js @@ -471,7 +471,7 @@ ruleTester.run("multiline-comment-style", rule, { code: ` /* * the following line - is missing a '*' at the start + is missing a '*' at the start */ `, output: ` @@ -492,7 +492,22 @@ ruleTester.run("multiline-comment-style", rule, { output: ` /* * the following line - * is missing a '*' at the start + * is missing a '*' at the start + */ + `, + errors: [{ messageId: "missingStar", line: 4 }] + }, + { + code: ` + /* + * the following line + is missing a '*' at the start + */ + `, + output: ` + /* + * the following line + * is missing a '*' at the start */ `, errors: [{ messageId: "missingStar", line: 4 }] @@ -784,10 +799,10 @@ ruleTester.run("multiline-comment-style", rule, { `, output: ` /* - * { - * "foo": 1, - * "bar": 2 - * } + *{ + * "foo": 1, + * "bar": 2 + *} */ `, errors: [ @@ -809,10 +824,10 @@ ruleTester.run("multiline-comment-style", rule, { `, output: ` /* - * { - * \t"foo": 1, - * \t"bar": 2 - * } + *{ + *\t"foo": 1, + *\t"bar": 2 + *} */ `, errors: [ @@ -834,10 +849,10 @@ ruleTester.run("multiline-comment-style", rule, { `, output: ` /* - * { - * \t "foo": 1, - * \t "bar": 2 - * } + *{ + *\t "foo": 1, + *\t "bar": 2 + *} */ `, errors: [ @@ -859,10 +874,10 @@ ruleTester.run("multiline-comment-style", rule, { `, output: ` /* - * { - * "foo": 1, - * "bar": 2 - * } + *{ + *"foo": 1, + *"bar": 2 + *} */ `, errors: [ @@ -884,10 +899,10 @@ ruleTester.run("multiline-comment-style", rule, { `, output: ` \t /* - \t * { - \t * "foo": 1, - \t * "bar": 2 - \t * } + \t *{ + \t *"foo": 1, + \t *"bar": 2 + \t *} \t */ `, errors: [ @@ -1290,8 +1305,8 @@ ${" "} output: ` /* * foo - * - * bar + *${" "} + * bar${" "} */ `, options: ["starred-block"], @@ -1311,8 +1326,8 @@ ${" "} output: ` /* * foo - * - * bar + *${" "} + * bar${" "} */ `, options: ["starred-block"], @@ -1353,7 +1368,7 @@ ${" "} output: ` /* *foo - * + *${" "} *bar${" "} */ `,