版本

multiline-comment-style

强制多行注释使用特定风格

🔧 可修复

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

重要

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

了解更多

许多风格指南要求跨越多行的注释使用特定风格。例如,一些风格指南偏好对多行注释使用单块注释,而其他风格指南则偏好连续的行注释。

规则详情

此规则旨在强制多行注释使用特定风格。

选项

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

  • "starred-block" (默认): 禁止连续的行注释,而倾向于块注释。此外,要求块注释在每行之前都有对齐的 * 字符。
  • "bare-block": 禁止连续的行注释,而倾向于块注释,并禁止块注释在每行之前有 "*" 字符。此选项忽略 JSDoc 注释。
  • "separate-lines": 禁止块注释,而倾向于连续的行注释。默认情况下,此选项忽略 JSDoc 注释。要将此规则也应用于 JSDoc 注释,请将 checkJSDoc 选项设置为 true

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

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

在线演示中打开

/* 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" 选项时,此规则的 正确 代码示例

在线演示中打开
/* eslint multiline-comment-style: ["error", "starred-block"] */

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

// single-line comment

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

在线演示中打开
/* eslint multiline-comment-style: ["error", "bare-block"] */

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

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

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

在线演示中打开
/* eslint multiline-comment-style: ["error", "bare-block"] */

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

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

在线演示中打开

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

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

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

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

在线演示中打开
/* eslint multiline-comment-style: ["error", "separate-lines"] */

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

使用 "separate-lines" 选项且 checkJSDoc 设置为 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 时,此规则的 正确 代码示例

在线演示中打开
/* eslint multiline-comment-style: ["error", "separate-lines", { "checkJSDoc": true }] */

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

何时不使用

如果你不想强制多行注释使用特定风格,你可以禁用此规则。

版本

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

资源

更改语言