From 0e7cb291dce196a7a0603de9b824b0d5bc0a5109 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 | 106 +++++++++++++++++++++ 2 files changed, 112 insertions(+), 4 deletions(-) diff --git a/lib/rules/multiline-comment-style.js b/lib/rules/multiline-comment-style.js index bfb4b7fb448..5bea92ded7b 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 9e9e71b10a4..b44ea0a05d5 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: [ @@ -1054,6 +1128,38 @@ ruleTester.run("multiline-comment-style", rule, { 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: ["separate-lines"], + errors: [{ messageId: "expectedLines", line: 2 }] } ] });