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 } + ] } ] });