Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix: preserve whitespace in multiline-comment-style (fixes #12312)
  • Loading branch information
kaicataldo committed Sep 26, 2019
1 parent f5537b2 commit 428cabc
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 7 deletions.
17 changes: 11 additions & 6 deletions lib/rules/multiline-comment-style.js
Expand Up @@ -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];
Expand Down Expand Up @@ -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);
}
Expand Down
136 changes: 135 additions & 1 deletion tests/lib/rules/multiline-comment-style.js
Expand Up @@ -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: `
Expand All @@ -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: `
/*
Expand Down Expand Up @@ -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 }
]
}
]
});

0 comments on commit 428cabc

Please sign in to comment.