diff --git a/lib/rules/multiline-comment-style.js b/lib/rules/multiline-comment-style.js index 5bea92ded7b..17c297fad7e 100644 --- a/lib/rules/multiline-comment-style.js +++ b/lib/rules/multiline-comment-style.js @@ -286,8 +286,7 @@ module.exports = { fix(fixer) { const lineStartIndex = sourceCode.getIndexFromLoc({ line: lineNumber, column: 0 }); const [linePrefix, whitespaceBefore, whitespaceAfter] = lineText.match(/^(\s*)\*?(\s*)/u); - const commentStartIndex = lineStartIndex + linePrefix.length; - const leadingWhitespace = whitespaceAfter || ` ${ + const leadingWhitespace = whitespaceAfter.length && whitespaceAfter || ` ${ whitespaceBefore.startsWith(expectedLeadingWhitespace) ? whitespaceBefore.replace(expectedLeadingWhitespace, "") : "" @@ -295,6 +294,7 @@ module.exports = { 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); } diff --git a/tests/lib/rules/multiline-comment-style.js b/tests/lib/rules/multiline-comment-style.js index b44ea0a05d5..047e4e4f0c7 100644 --- a/tests/lib/rules/multiline-comment-style.js +++ b/tests/lib/rules/multiline-comment-style.js @@ -1160,6 +1160,210 @@ ruleTester.run("multiline-comment-style", rule, { `, options: ["separate-lines"], errors: [{ messageId: "expectedLines", line: 2 }] + }, + { + code: ` + /* + * foo + * + * bar + */ + `, + output: ` + /* foo +${" "} + bar */ + `, + options: ["bare-block"], + errors: [{ messageId: "expectedBareBlock", line: 2 }] + }, + { + code: ` + /* + * foo + *${" "} + * bar + */ + `, + output: ` + /* foo +${" "} + bar */ + `, + options: ["bare-block"], + errors: [{ messageId: "expectedBareBlock", line: 2 }] + }, + { + code: ` + // foo + // + // bar + `, + output: ` + /* + * foo + *${" "} + * bar + */ + `, + options: ["starred-block"], + errors: [{ messageId: "expectedBlock", line: 2 }] + }, + { + code: ` + // foo + //${" "} + // bar + `, + output: ` + /* + * foo + *${" "} + * bar + */ + `, + options: ["starred-block"], + errors: [{ messageId: "expectedBlock", line: 2 }] + }, + { + code: ` + // foo + // + // bar + `, + output: ` + /* foo +${" "} + bar */ + `, + options: ["bare-block"], + errors: [{ messageId: "expectedBlock", line: 2 }] + }, + { + code: ` + // foo + //${" "} + // bar + `, + output: ` + /* foo +${" "} + bar */ + `, + options: ["bare-block"], + errors: [{ messageId: "expectedBlock", line: 2 }] + }, + { + code: ` + /* foo + + bar */ + `, + output: ` + // foo + //${" "} + // bar${" "} + `, + options: ["separate-lines"], + errors: [{ messageid: "expectedlines", line: 2 }] + }, + { + code: ` + /* foo +${" "} + bar */ + `, + output: ` + // foo + //${" "} + // bar${" "} + `, + options: ["separate-lines"], + errors: [{ messageid: "expectedlines", line: 2 }] + }, + { + code: ` + /* foo + + bar */ + `, + output: ` + /* + * foo + * + * bar + */ + `, + options: ["starred-block"], + errors: [ + { messageId: "startNewline", line: 2 }, + { messageId: "missingStar", line: 3 }, + { messageId: "missingStar", line: 4 }, + { messageId: "endNewline", line: 4 } + ] + }, + { + code: ` + /* foo +${" "} + bar */ + `, + output: ` + /* + * foo + * + * bar + */ + `, + options: ["starred-block"], + errors: [ + { messageId: "startNewline", line: 2 }, + { messageId: "missingStar", line: 3 }, + { messageId: "missingStar", line: 4 }, + { messageId: "endNewline", line: 4 } + ] + }, + { + code: ` + /*foo + + bar */ + `, + output: ` + /* + *foo + * + *bar${" "} + */ + `, + options: ["starred-block"], + errors: [ + { messageId: "startNewline", line: 2 }, + { messageId: "missingStar", line: 3 }, + { messageId: "missingStar", line: 4 }, + { messageId: "endNewline", line: 4 } + ] + }, + { + code: ` + /*foo +${" "} + bar */ + `, + output: ` + /* + *foo + * + *bar${" "} + */ + `, + options: ["starred-block"], + errors: [ + { messageId: "startNewline", line: 2 }, + { messageId: "missingStar", line: 3 }, + { messageId: "missingStar", line: 4 }, + { messageId: "endNewline", line: 4 } + ] } ] });