Skip to content

Commit

Permalink
Handle blank lines in starred-block comments correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
kaicataldo committed Oct 25, 2019
1 parent a77a6d4 commit 71d6c76
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 4 deletions.
10 changes: 6 additions & 4 deletions lib/rules/multiline-comment-style.js
Expand Up @@ -95,13 +95,15 @@ module.exports = {
* @returns {string[]} An array of the processed lines.
*/
function processStarredBlockComment(comment) {
const lines = comment.value.split(astUtils.LINEBREAK_MATCHER).map(line => line.replace(/^\s*$/u, ""));
const lines = comment.value.split(astUtils.LINEBREAK_MATCHER)
.filter((line, i, linesArr) => !(i === 0 || i === linesArr.length - 1))
.map(line => line.replace(/^\s*$/u, ""));
const allLinesHaveLeadingSpace = lines
.map(line => line.replace(/\s*\*/u, ""))
.filter(line => line.trim().length)
.every(line => line.startsWith(" "));

return lines.map(line => line.replace(allLinesHaveLeadingSpace ? /\s*\* /u : /\s*\*/u, ""));
return lines.map(line => line.replace(allLinesHaveLeadingSpace ? /\s*\* ?/u : /\s*\*/u, ""));
}

/**
Expand Down Expand Up @@ -322,7 +324,7 @@ module.exports = {
},
messageId: "expectedLines",
fix(fixer) {
return fixer.replaceText(firstComment, convertToSeparateLines(firstComment, commentLines.filter(line => line.length)));
return fixer.replaceText(firstComment, convertToSeparateLines(firstComment, commentLines));
}
});
},
Expand Down Expand Up @@ -361,7 +363,7 @@ module.exports = {
},
messageId: "expectedBareBlock",
fix(fixer) {
return fixer.replaceText(firstComment, convertToBlock(firstComment, commentLines.filter(line => line.length)));
return fixer.replaceText(firstComment, convertToBlock(firstComment, commentLines));
}
});
}
Expand Down
74 changes: 74 additions & 0 deletions tests/lib/rules/multiline-comment-style.js
Expand Up @@ -937,6 +937,78 @@ ruleTester.run("multiline-comment-style", rule, {
{ messageId: "expectedLines", line: 2 }
]
},
{
code: `
/*
*
* {
* "foo": 1,
* "bar": 2
* }
*
*/
`,
output: `
//${" "}
// {
// "foo": 1,
// "bar": 2
// }
//${" "}
`,
options: ["separate-lines"],
errors: [
{ messageId: "expectedLines", line: 2 }
]
},
{
code: `
/*
*
* {
* "foo": 1,
* "bar": 2
* }
*
*/
`,
output: `
/*${" "}
{
"foo": 1,
"bar": 2
}
*/
`,
options: ["bare-block"],
errors: [
{ messageId: "expectedBareBlock", line: 2 }
]
},
{
code: `
/*
*
*{
* "foo": 1,
* "bar": 2
*}
*
*/
`,
output: `
/*${" "}
{
"foo": 1,
"bar": 2
}
*/
`,
options: ["bare-block"],
errors: [
{ messageId: "expectedBareBlock", line: 2 }
]
},
{
code: `
/*
Expand Down Expand Up @@ -1027,10 +1099,12 @@ ruleTester.run("multiline-comment-style", rule, {
*/
`,
output: `
//${" "}
// {
// "foo": 1,
// "bar": 2
// }
//${" "}
`,
options: ["separate-lines"],
errors: [
Expand Down

0 comments on commit 71d6c76

Please sign in to comment.