Skip to content

Commit c89a485

Browse files
feat: Add checkJSDoc option to multiline-comment-style (#16807)
* feat: Add support for checkJSDoc param for "multiline-comment-style" * Update lib/rules/multiline-comment-style.js Co-authored-by: Milos Djermanovic <[email protected]> * Apply suggestions from code review Co-authored-by: Milos Djermanovic <[email protected]> * Update docs/src/rules/multiline-comment-style.md Co-authored-by: Milos Djermanovic <[email protected]> --------- Co-authored-by: Milos Djermanovic <[email protected]>
1 parent f5f5e11 commit c89a485

3 files changed

Lines changed: 92 additions & 6 deletions

File tree

docs/src/rules/multiline-comment-style.md

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ This rule aims to enforce a particular style for multiline comments.
1616
This rule has a string option, which can have one of the following values:
1717

1818
* `"starred-block"` (default): Disallows consecutive line comments in favor of block comments. Additionally, requires block comments to have an aligned `*` character before each line.
19-
* `"bare-block"`: Disallows consecutive line comments in favor of block comments, and disallows block comments from having a `"*"` character before each line.
20-
* `"separate-lines"`: Disallows block comments in favor of consecutive line comments
19+
* `"bare-block"`: Disallows consecutive line comments in favor of block comments, and disallows block comments from having a `"*"` character before each line. This option ignores JSDoc comments.
20+
* `"separate-lines"`: Disallows block comments in favor of consecutive line comments. By default, this option ignores JSDoc comments. To also apply this rule to JSDoc comments, set the `checkJSDoc` option to `true`.
2121

22-
The rule always ignores directive comments such as `/* eslint-disable */`. Additionally, unless the mode is `"starred-block"`, the rule ignores JSDoc comments.
22+
The rule always ignores directive comments such as `/* eslint-disable */`.
2323

2424
Examples of **incorrect** code for this rule with the default `"starred-block"` option:
2525

@@ -146,6 +146,39 @@ foo();
146146

147147
:::
148148

149+
Examples of **incorrect** code for this rule with the `"separate-lines"` option and `checkJSDoc` set to `true`:
150+
151+
::: incorrect
152+
153+
```js
154+
155+
/* eslint multiline-comment-style: ["error", "separate-lines", { "checkJSDoc": true }] */
156+
157+
/**
158+
* I am a JSDoc comment
159+
* and I'm not allowed
160+
*/
161+
foo();
162+
163+
```
164+
165+
:::
166+
167+
Examples of **correct** code for this rule with the `"separate-lines"` option and `checkJSDoc` set to `true`:
168+
169+
::: correct
170+
171+
```js
172+
/* eslint multiline-comment-style: ["error", "separate-lines", { "checkJSDoc": true }] */
173+
174+
// I am a JSDoc comment
175+
// and I'm not allowed
176+
foo();
177+
178+
```
179+
180+
:::
181+
149182
## When Not To Use It
150183

151184
If you don't want to enforce a particular style for multiline comments, you can disable the rule.

lib/rules/multiline-comment-style.js

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,37 @@ module.exports = {
2222
},
2323

2424
fixable: "whitespace",
25-
schema: [{ enum: ["starred-block", "separate-lines", "bare-block"] }],
25+
schema: {
26+
anyOf: [
27+
{
28+
type: "array",
29+
items: [
30+
{
31+
enum: ["starred-block", "bare-block"]
32+
}
33+
],
34+
additionalItems: false
35+
},
36+
{
37+
type: "array",
38+
items: [
39+
{
40+
enum: ["separate-lines"]
41+
},
42+
{
43+
type: "object",
44+
properties: {
45+
checkJSDoc: {
46+
type: "boolean"
47+
}
48+
},
49+
additionalProperties: false
50+
}
51+
],
52+
additionalItems: false
53+
}
54+
]
55+
},
2656
messages: {
2757
expectedBlock: "Expected a block comment instead of consecutive line comments.",
2858
expectedBareBlock: "Expected a block comment without padding stars.",
@@ -37,6 +67,8 @@ module.exports = {
3767
create(context) {
3868
const sourceCode = context.getSourceCode();
3969
const option = context.options[0] || "starred-block";
70+
const params = context.options[1] || {};
71+
const checkJSDoc = !!params.checkJSDoc;
4072

4173
//----------------------------------------------------------------------
4274
// Helpers
@@ -333,11 +365,18 @@ module.exports = {
333365
"separate-lines"(commentGroup) {
334366
const [firstComment] = commentGroup;
335367

336-
if (firstComment.type !== "Block" || isJSDocComment(commentGroup)) {
368+
const isJSDoc = isJSDocComment(commentGroup);
369+
370+
if (firstComment.type !== "Block" || (!checkJSDoc && isJSDoc)) {
337371
return;
338372
}
339373

340-
const commentLines = getCommentLines(commentGroup);
374+
let commentLines = getCommentLines(commentGroup);
375+
376+
if (isJSDoc) {
377+
commentLines = commentLines.slice(1, commentLines.length - 1);
378+
}
379+
341380
const tokenAfter = sourceCode.getTokenAfter(firstComment, { includeComments: true });
342381

343382
if (tokenAfter && firstComment.loc.end.line === tokenAfter.loc.start.line) {

tests/lib/rules/multiline-comment-style.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,20 @@ ruleTester.run("multiline-comment-style", rule, {
628628
options: ["separate-lines"],
629629
errors: [{ messageId: "expectedLines", line: 2 }]
630630
},
631+
{
632+
code: `
633+
/**
634+
* JSDoc
635+
* Comment
636+
*/
637+
`,
638+
output: `
639+
// JSDoc
640+
// Comment
641+
`,
642+
options: ["separate-lines", { checkJSDoc: true }],
643+
errors: [{ messageId: "expectedLines", line: 2 }]
644+
},
631645
{
632646
code: `
633647
/* foo

0 commit comments

Comments
 (0)