Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add checkJSDoc option to multiline-comment-style #16807

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 36 additions & 3 deletions docs/src/rules/multiline-comment-style.md
Expand Up @@ -16,10 +16,10 @@ This rule aims to enforce a particular style for multiline comments.
This rule has a string option, which can have one of the following values:

* `"starred-block"` (default): Disallows consecutive line comments in favor of block comments. Additionally, requires block comments to have an aligned `*` character before each line.
* `"bare-block"`: Disallows consecutive line comments in favor of block comments, and disallows block comments from having a `"*"` character before each line.
* `"separate-lines"`: Disallows block comments in favor of consecutive line comments
* `"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.
* `"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`.

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

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

Expand Down Expand Up @@ -146,6 +146,39 @@ foo();

:::

Examples of **incorrect** code for this rule with the `"separate-lines"` option and `checkJSDoc` set to `true`:

::: incorrect

```js

/* eslint multiline-comment-style: ["error", "separate-lines", { "checkJSDoc": true }] */

/**
* I am a JSDoc comment
* and I'm not allowed
*/
foo();

```

:::

Examples of **correct** code for this rule with the `"separate-lines"` option and `checkJSDoc` set to `true`:

::: correct

```js
/* eslint multiline-comment-style: ["error", "separate-lines", { "checkJSDoc": true }] */

// I am a JSDoc comment
// and I'm not allowed
foo();

```

:::

## When Not To Use It

If you don't want to enforce a particular style for multiline comments, you can disable the rule.
45 changes: 42 additions & 3 deletions lib/rules/multiline-comment-style.js
Expand Up @@ -22,7 +22,37 @@ module.exports = {
},

fixable: "whitespace",
schema: [{ enum: ["starred-block", "separate-lines", "bare-block"] }],
schema: {
anyOf: [
{
type: "array",
items: [
{
enum: ["starred-block", "bare-block"]
}
],
additionalItems: false
},
{
type: "array",
items: [
{
enum: ["separate-lines"]
},
{
type: "object",
properties: {
checkJSDoc: {
type: "boolean"
}
},
additionalProperties: false
}
],
additionalItems: false
}
]
},
messages: {
expectedBlock: "Expected a block comment instead of consecutive line comments.",
expectedBareBlock: "Expected a block comment without padding stars.",
Expand All @@ -37,6 +67,8 @@ module.exports = {
create(context) {
const sourceCode = context.getSourceCode();
const option = context.options[0] || "starred-block";
const params = context.options[1] || {};
const checkJSDoc = !!params.checkJSDoc;

//----------------------------------------------------------------------
// Helpers
Expand Down Expand Up @@ -333,11 +365,18 @@ module.exports = {
"separate-lines"(commentGroup) {
const [firstComment] = commentGroup;

if (firstComment.type !== "Block" || isJSDocComment(commentGroup)) {
const isJSDoc = isJSDocComment(commentGroup);

if (firstComment.type !== "Block" || (!checkJSDoc && isJSDoc)) {
return;
}

const commentLines = getCommentLines(commentGroup);
let commentLines = getCommentLines(commentGroup);

if (isJSDoc) {
commentLines = commentLines.slice(1, commentLines.length - 1);
}

const tokenAfter = sourceCode.getTokenAfter(firstComment, { includeComments: true });

if (tokenAfter && firstComment.loc.end.line === tokenAfter.loc.start.line) {
Expand Down
14 changes: 14 additions & 0 deletions tests/lib/rules/multiline-comment-style.js
Expand Up @@ -628,6 +628,20 @@ ruleTester.run("multiline-comment-style", rule, {
options: ["separate-lines"],
errors: [{ messageId: "expectedLines", line: 2 }]
},
{
code: `
/**
* JSDoc
* Comment
*/
`,
output: `
// JSDoc
// Comment
`,
options: ["separate-lines", { checkJSDoc: true }],
errors: [{ messageId: "expectedLines", line: 2 }]
},
{
code: `
/* foo
Expand Down