multiline-comment-style

为多行注释强制执行特定样式

🔧 Fixable

此规则报告的一些问题可通过 --fix 命令行选项自动修复

该规则在 ESLint v9.3.0 中已弃用。请在 @stylistic/eslint-plugin-js 中使用 相应的规则

¥This rule was deprecated in ESLint v9.3.0. Please use the corresponding rule in @stylistic/eslint-plugin-js.

许多风格指南要求跨多行注释的特定样式。例如,一些风格指南更喜欢对多行注释使用单个块注释,而其他风格指南更喜欢连续行注释。

¥Many style guides require a particular style for comments that span multiple lines. For example, some style guides prefer the use of a single block comment for multiline comments, whereas other style guides prefer consecutive line comments.

规则详情

¥Rule Details

此规则旨在为多行注释强制执行特定样式。

¥This rule aims to enforce a particular style for multiline comments.

选项

¥Options

此规则有一个字符串选项,它可以具有以下值之一:

¥This rule has a string option, which can have one of the following values:

  • "starred-block"(默认):不允许连续的行注释以支持块注释。此外,要求块注释在每行之前有一个对齐的 * 字符。

    ¥"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":不允许连续的行注释支持块注释,并且不允许块注释在每行之前有一个 "*" 字符。此选项忽略 JSDoc 注释。

    ¥"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":不允许块注释支持连续行注释。默认情况下,此选项会忽略 JSDoc 注释。要将此规则也应用于 JSDoc 注释,请将 checkJSDoc 选项设置为 true

    ¥"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.

该规则始终忽略指令注释,例如 /* eslint-disable */

¥The rule always ignores directive comments such as /* eslint-disable */.

使用默认 "starred-block" 选项的此规则的错误代码示例:

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

在线运行

/* eslint multiline-comment-style: ["error", "starred-block"] */

// this line
// calls foo()
foo();

/* this line
calls foo() */
foo();

/* this comment

 * is missing a newline after /*
 */

/*

 * this comment

 * is missing a newline at the end */

/*

* the star in this line should have a space before it
 */

/*

 * the star on the following line should have a space before it
*/

使用默认 "starred-block" 选项的此规则的正确代码示例:

¥Examples of correct code for this rule with the default "starred-block" option:

在线运行
/* eslint multiline-comment-style: ["error", "starred-block"] */

/*

 * this line

 * calls foo()
 */
foo();

// single-line comment

使用 "bare-block" 选项的此规则的错误代码示例:

¥Examples of incorrect code for this rule with the "bare-block" option:

在线运行
/* eslint multiline-comment-style: ["error", "bare-block"] */

// this line
// calls foo()
foo();

/*

 * this line

 * calls foo()
 */
foo();

使用 "bare-block" 选项的此规则的正确代码示例:

¥Examples of correct code for this rule with the "bare-block" option:

在线运行
/* eslint multiline-comment-style: ["error", "bare-block"] */

/* this line
   calls foo() */
foo();

使用 "separate-lines" 选项的此规则的错误代码示例:

¥Examples of incorrect code for this rule with the "separate-lines" option:

在线运行

/* eslint multiline-comment-style: ["error", "separate-lines"] */

/* This line
calls foo() */
foo();

/*

 * This line

 * calls foo()
 */
foo();

使用 "separate-lines" 选项的此规则的正确代码示例:

¥Examples of correct code for this rule with the "separate-lines" option:

在线运行
/* eslint multiline-comment-style: ["error", "separate-lines"] */

// This line
// calls foo()
foo();

此规则的错误代码示例("separate-lines" 选项和 checkJSDoc 设置为 true):

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

在线运行

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

/**

 * I am a JSDoc comment

 * and I'm not allowed
 */
foo();

此规则的正确代码示例(将 "separate-lines" 选项和 checkJSDoc 设置为 true):

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

在线运行
/* 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.

版本

此规则是在 ESLint v4.10.0 中引入。

资源

ESLint 中文网
粤ICP备13048890号