From 2f6afb774b12444e8afa3881f51989493fd16873 Mon Sep 17 00:00:00 2001 From: karthik Date: Thu, 23 Jan 2020 02:39:34 +0530 Subject: [PATCH 1/5] Fix: multiline-comment-style rule missed an extra space after * (fixes #12785) --- lib/rules/multiline-comment-style.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/rules/multiline-comment-style.js b/lib/rules/multiline-comment-style.js index fb50e1522ea..a1e47a00154 100644 --- a/lib/rules/multiline-comment-style.js +++ b/lib/rules/multiline-comment-style.js @@ -315,7 +315,7 @@ module.exports = { const lineTextToAlignWith = sourceCode.lines[firstComment.loc.start.line - 1 + idx]; const [, prefix = "", initialOffset = ""] = lineTextToAlignWith.match(/^(\s*(?:\/?\*)?(\s*))/u) || []; - offset = `${commentTextPrefix.slice(prefix.length)}${initialOffset}`; + offset = `${commentTextPrefix.slice(prefix.length)}${initialOffset} `; break; } From fc2ba597ee593831709034be13c6098eb3a4d57a Mon Sep 17 00:00:00 2001 From: karthik Date: Thu, 23 Jan 2020 20:15:07 +0530 Subject: [PATCH 2/5] logic changed to handled only the edge case --- lib/rules/multiline-comment-style.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/rules/multiline-comment-style.js b/lib/rules/multiline-comment-style.js index a1e47a00154..a64d06d3bd1 100644 --- a/lib/rules/multiline-comment-style.js +++ b/lib/rules/multiline-comment-style.js @@ -315,7 +315,11 @@ module.exports = { const lineTextToAlignWith = sourceCode.lines[firstComment.loc.start.line - 1 + idx]; const [, prefix = "", initialOffset = ""] = lineTextToAlignWith.match(/^(\s*(?:\/?\*)?(\s*))/u) || []; - offset = `${commentTextPrefix.slice(prefix.length)}${initialOffset} `; + if (/^\s*\//u.test(lineText)) { + offset = `${commentTextPrefix.slice(prefix.length)}${initialOffset} `; + } else { + offset = `${commentTextPrefix.slice(prefix.length)}${initialOffset}`; + } break; } From 1ef1354ea5d0827f42d2c6ba494f5b4a55d3c8ff Mon Sep 17 00:00:00 2001 From: karthik Date: Thu, 23 Jan 2020 22:32:40 +0530 Subject: [PATCH 3/5] test case added for the edge case --- tests/lib/rules/multiline-comment-style.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/tests/lib/rules/multiline-comment-style.js b/tests/lib/rules/multiline-comment-style.js index c69265ba9e2..008e326528b 100644 --- a/tests/lib/rules/multiline-comment-style.js +++ b/tests/lib/rules/multiline-comment-style.js @@ -1379,6 +1379,25 @@ ${" "} { 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 } + ] } ] }); From add0e213bce119e3473b4af0ca08bc57a6d56372 Mon Sep 17 00:00:00 2001 From: karthik Date: Sun, 26 Jan 2020 18:20:15 +0530 Subject: [PATCH 4/5] checks full offset and apply space after * only if there already isn't white space after it. --- lib/rules/multiline-comment-style.js | 6 +-- tests/lib/rules/multiline-comment-style.js | 49 ++++++++++++++++++++++ 2 files changed, 52 insertions(+), 3 deletions(-) diff --git a/lib/rules/multiline-comment-style.js b/lib/rules/multiline-comment-style.js index a64d06d3bd1..642d6bafef4 100644 --- a/lib/rules/multiline-comment-style.js +++ b/lib/rules/multiline-comment-style.js @@ -315,10 +315,10 @@ module.exports = { const lineTextToAlignWith = sourceCode.lines[firstComment.loc.start.line - 1 + idx]; const [, prefix = "", initialOffset = ""] = lineTextToAlignWith.match(/^(\s*(?:\/?\*)?(\s*))/u) || []; - if (/^\s*\//u.test(lineText)) { + offset = `${commentTextPrefix.slice(prefix.length)}${initialOffset}`; + + if (/^\s*\//u.test(lineText) && offset.length === 0) { offset = `${commentTextPrefix.slice(prefix.length)}${initialOffset} `; - } else { - offset = `${commentTextPrefix.slice(prefix.length)}${initialOffset}`; } break; } diff --git a/tests/lib/rules/multiline-comment-style.js b/tests/lib/rules/multiline-comment-style.js index 008e326528b..81ebed0f4ce 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"] } ], @@ -1398,6 +1407,46 @@ ${" "} { 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 } + ] } ] }); From 73c99ec0d5473a7e6ec79802ca1c75d69d107d46 Mon Sep 17 00:00:00 2001 From: karthik Date: Mon, 27 Jan 2020 17:23:58 +0530 Subject: [PATCH 5/5] addeed new test case --- lib/rules/multiline-comment-style.js | 2 +- tests/lib/rules/multiline-comment-style.js | 26 ++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/lib/rules/multiline-comment-style.js b/lib/rules/multiline-comment-style.js index 642d6bafef4..9524818b8bd 100644 --- a/lib/rules/multiline-comment-style.js +++ b/lib/rules/multiline-comment-style.js @@ -318,7 +318,7 @@ module.exports = { offset = `${commentTextPrefix.slice(prefix.length)}${initialOffset}`; if (/^\s*\//u.test(lineText) && offset.length === 0) { - offset = `${commentTextPrefix.slice(prefix.length)}${initialOffset} `; + offset += " "; } break; } diff --git a/tests/lib/rules/multiline-comment-style.js b/tests/lib/rules/multiline-comment-style.js index 81ebed0f4ce..456494b7bd5 100644 --- a/tests/lib/rules/multiline-comment-style.js +++ b/tests/lib/rules/multiline-comment-style.js @@ -1447,6 +1447,32 @@ ${" "} 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 } + ] } ] });