From 71d6c76eaaa2a56574aa3d62756afb619d63fff9 Mon Sep 17 00:00:00 2001 From: Kai Cataldo Date: Thu, 24 Oct 2019 20:57:03 -0400 Subject: [PATCH] Handle blank lines in starred-block comments correctly --- lib/rules/multiline-comment-style.js | 10 +-- tests/lib/rules/multiline-comment-style.js | 74 ++++++++++++++++++++++ 2 files changed, 80 insertions(+), 4 deletions(-) diff --git a/lib/rules/multiline-comment-style.js b/lib/rules/multiline-comment-style.js index bfb4b7fb4488..5bea92ded7bf 100644 --- a/lib/rules/multiline-comment-style.js +++ b/lib/rules/multiline-comment-style.js @@ -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, "")); } /** @@ -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)); } }); }, @@ -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)); } }); } diff --git a/tests/lib/rules/multiline-comment-style.js b/tests/lib/rules/multiline-comment-style.js index 9e9e71b10a4c..f3b253aac248 100644 --- a/tests/lib/rules/multiline-comment-style.js +++ b/tests/lib/rules/multiline-comment-style.js @@ -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: ` /* @@ -1027,10 +1099,12 @@ ruleTester.run("multiline-comment-style", rule, { */ `, output: ` + //${" "} // { // "foo": 1, // "bar": 2 // } + //${" "} `, options: ["separate-lines"], errors: [