From 428cabc4bf2d5e75b1f7dcd530fec719c33049fc Mon Sep 17 00:00:00 2001 From: Kai Cataldo Date: Thu, 26 Sep 2019 03:52:06 -0400 Subject: [PATCH] Fix: preserve whitespace in multiline-comment-style (fixes #12312) --- lib/rules/multiline-comment-style.js | 17 ++- tests/lib/rules/multiline-comment-style.js | 136 ++++++++++++++++++++- 2 files changed, 146 insertions(+), 7 deletions(-) diff --git a/lib/rules/multiline-comment-style.js b/lib/rules/multiline-comment-style.js index 6578a120126b..dd5826976217 100644 --- a/lib/rules/multiline-comment-style.js +++ b/lib/rules/multiline-comment-style.js @@ -142,7 +142,8 @@ module.exports = { } else { const block = commentGroup[0]; const lines = block.value.split(astUtils.LINEBREAK_MATCHER); - const expectedLinePrefix = `${sourceCode.text.slice(block.range[0] - block.loc.start.column, block.range[0])} *`; + const expectedLeadingWhitespace = sourceCode.text.slice(block.range[0] - block.loc.start.column, block.range[0]); + const expectedLinePrefix = `${expectedLeadingWhitespace} *`; if (!/^\*?\s*$/u.test(lines[0])) { const start = block.value.startsWith("*") ? block.range[0] + 1 : block.range[0]; @@ -182,12 +183,16 @@ module.exports = { : "missingStar", fix(fixer) { const lineStartIndex = sourceCode.getIndexFromLoc({ line: lineNumber, column: 0 }); - const linePrefixLength = lineText.match(/^\s*\*? ?/u)[0].length; - const commentStartIndex = lineStartIndex + linePrefixLength; - - const replacementText = lineNumber === block.loc.end.line || lineText.length === linePrefixLength + const [linePrefix, whitespaceBefore, whitespaceAfter] = lineText.match(/^(\s*)\*?(\s*)/u); + const commentStartIndex = lineStartIndex + linePrefix.length; + const leadingWhitespace = whitespaceAfter || ` ${ + whitespaceBefore.startsWith(expectedLeadingWhitespace) + ? whitespaceBefore.replace(expectedLeadingWhitespace, "") + : "" + }`; + const replacementText = lineNumber === block.loc.end.line || lineText.length === linePrefix.length ? expectedLinePrefix - : `${expectedLinePrefix} `; + : `${expectedLinePrefix}${leadingWhitespace}`; 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 58c13581d4dc..6d4216a75f81 100644 --- a/tests/lib/rules/multiline-comment-style.js +++ b/tests/lib/rules/multiline-comment-style.js @@ -322,7 +322,7 @@ ruleTester.run("multiline-comment-style", rule, { code: ` /* * the following line - is missing a '*' at the start + is missing a '*' at the start */ `, output: ` @@ -333,6 +333,21 @@ ruleTester.run("multiline-comment-style", rule, { `, 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 }] + }, { code: ` /* @@ -482,6 +497,125 @@ ruleTester.run("multiline-comment-style", rule, { `, options: ["bare-block"], errors: [{ messageId: "expectedBareBlock", line: 2 }] + }, + { + code: ` + /* + { + "foo": 1, + "bar": 2 + } + */ + `, + output: ` + /* + * { + * "foo": 1, + * "bar": 2 + * } + */ + `, + errors: [ + { messageId: "missingStar", line: 3 }, + { messageId: "missingStar", line: 4 }, + { messageId: "missingStar", line: 5 }, + { messageId: "missingStar", line: 6 }, + { messageId: "alignment", line: 7 } + ] + }, + { + code: ` + /* + { + \t"foo": 1, + \t"bar": 2 + } + */ + `, + output: ` + /* + * { + * \t"foo": 1, + * \t"bar": 2 + * } + */ + `, + errors: [ + { messageId: "missingStar", line: 3 }, + { messageId: "missingStar", line: 4 }, + { messageId: "missingStar", line: 5 }, + { messageId: "missingStar", line: 6 }, + { messageId: "alignment", line: 7 } + ] + }, + { + code: ` + /* + { + \t "foo": 1, + \t "bar": 2 + } + */ + `, + output: ` + /* + * { + * \t "foo": 1, + * \t "bar": 2 + * } + */ + `, + errors: [ + { messageId: "missingStar", line: 3 }, + { messageId: "missingStar", line: 4 }, + { messageId: "missingStar", line: 5 }, + { messageId: "missingStar", line: 6 }, + { messageId: "alignment", line: 7 } + ] + }, + { + code: ` + /* + { + \t"foo": 1, + \t"bar": 2 + } + */ + `, + output: ` + /* + * { + * "foo": 1, + * "bar": 2 + * } + */ + `, + errors: [ + { messageId: "missingStar", line: 3 }, + { messageId: "missingStar", line: 4 }, + { messageId: "missingStar", line: 5 }, + { messageId: "missingStar", line: 6 }, + { messageId: "alignment", line: 7 } + ] + }, + { + code: ` + //{ + // "foo": 1, + // "bar": 2 + //} + `, + output: ` + /* + *{ + * "foo": 1, + * "bar": 2 + *} + */ + `, + errors: [ + { messageId: "expectedBlock", line: 2 } + ] } ] });