版本

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 中引入的。

資源

變更語言