From 533c1140dc98bebdc3ae8334ab2e6c27c7df0c21 Mon Sep 17 00:00:00 2001 From: Karthik Priyadarshan Date: Tue, 28 Jan 2020 05:14:18 +0530 Subject: [PATCH] Fix: multiline-comment-style rule add extra space after * (fixes #12785) (#12823) * Fix: multiline-comment-style rule missed an extra space after * (fixes #12785) * logic changed to handled only the edge case * test case added for the edge case * checks full offset and apply space after * only if there already isn't white space after it. * addeed new test case --- lib/rules/multiline-comment-style.js | 4 + tests/lib/rules/multiline-comment-style.js | 94 ++++++++++++++++++++++ 2 files changed, 98 insertions(+) diff --git a/lib/rules/multiline-comment-style.js b/lib/rules/multiline-comment-style.js index fb50e1522ea..9524818b8bd 100644 --- a/lib/rules/multiline-comment-style.js +++ b/lib/rules/multiline-comment-style.js @@ -316,6 +316,10 @@ module.exports = { const [, prefix = "", initialOffset = ""] = lineTextToAlignWith.match(/^(\s*(?:\/?\*)?(\s*))/u) || []; offset = `${commentTextPrefix.slice(prefix.length)}${initialOffset}`; + + if (/^\s*\//u.test(lineText) && offset.length === 0) { + offset += " "; + } break; } diff --git a/tests/lib/rules/multiline-comment-style.js b/tests/lib/rules/multiline-comment-style.js index c69265ba9e2..456494b7bd5 100644 --- a/tests/lib/rules/multiline-comment-style.js +++ b/tests/lib/rules/multiline-comment-style.js @@ -328,6 +328,15 @@ ruleTester.run("multiline-comment-style", rule, { */ `, options: ["separate-lines"] + }, + { + code: ` + /* + * // a line comment + *some.code(); + */ + `, + options: ["starred-block"] } ], @@ -1379,6 +1388,91 @@ ${" "} { messageId: "missingStar", line: 4 }, { messageId: "endNewline", line: 4 } ] + }, + { + code: ` + /* + // a line comment + some.code(); + */ + `, + output: ` + /* + * // a line comment + *some.code(); + */ + `, + options: ["starred-block"], + errors: [ + { messageId: "missingStar", line: 3 }, + { messageId: "missingStar", line: 4 } + ] + }, + { + code: ` + /* + // a line comment + * some.code(); + */ + `, + output: ` + /* + * // a line comment + * some.code(); + */ + `, + options: ["starred-block"], + errors: [ + { messageId: "missingStar", line: 3 } + ] + }, + { + code: ` + ////This comment is in + //\`separate-lines\` format. + `, + output: null, + options: ["starred-block"], + errors: [ + { messageId: "expectedBlock", line: 2 } + ] + }, + { + code: ` + // // This comment is in + // \`separate-lines\` format. + `, + output: null, + options: ["starred-block"], + errors: [ + { messageId: "expectedBlock", line: 2 } + ] + }, + { + code: ` + /* + { + \t"foo": 1, + \t//"bar": 2 + } + */ + `, + output: ` + /* + *{ + *\t"foo": 1, + *\t//"bar": 2 + *} + */ + `, + options: ["starred-block"], + errors: [ + { messageId: "missingStar", line: 3 }, + { messageId: "missingStar", line: 4 }, + { messageId: "missingStar", line: 5 }, + { messageId: "missingStar", line: 6 }, + { messageId: "alignment", line: 7 } + ] } ] });