版本

設定規則

規則是 ESLint 的核心組成部分。規則會驗證您的程式碼是否符合特定期望,以及在不符合期望時該怎麼辦。規則還可以包含特定於該規則的其他設定選項。

ESLint 內建了大量的規則,您也可以透過外掛新增更多規則。您可以使用設定註解或設定檔來修改您的專案使用的規則。

規則嚴重性

若要變更規則的嚴重性,請將規則 ID 設定為下列其中一個值

  • "off"0 - 關閉規則
  • "warn"1 - 將規則開啟為警告 (不影響結束代碼)
  • "error"2 - 將規則開啟為錯誤 (觸發時結束代碼為 1)

規則通常設定為 "error",以便在持續整合測試、預提交檢查和 Pull Request 合併期間強制執行規則,因為這樣做會導致 ESLint 以非零結束代碼結束。

如果您不想強制執行規則,但仍希望 ESLint 回報規則的違規行為,請將嚴重性設定為 "warn"。這通常用於引入最終將設定為 "error" 的新規則、規則標示潛在的建置時或執行時錯誤以外的內容 (例如未使用的變數),或規則無法確定是否已發現問題 (規則可能有誤報並需要人工審查) 時。

使用設定註解

若要在檔案內使用設定註解來設定規則,請使用以下格式的註解

/* eslint eqeqeq: "off", curly: "error" */

在此範例中,eqeqeq 已關閉,而 curly 已開啟為錯誤。您也可以使用規則嚴重性的數字等效值

/* eslint eqeqeq: 0, curly: 2 */

此範例與上一個範例相同,只是它使用數字代碼而不是字串值。eqeqeq 規則已關閉,而 curly 規則設定為錯誤。

如果規則有其他選項,您可以使用陣列實值語法來指定它們,例如

/* eslint quotes: ["error", "double"], curly: 2 */

此註解為 quotes 規則指定 "double" 選項。陣列中的第一個項目始終是規則嚴重性 (數字或字串)。

設定註解說明

設定註解可以包含說明,以解釋為什麼需要註解。說明必須出現在設定之後,並以兩個或多個連續的 - 字元與設定分隔。例如

/* eslint eqeqeq: "off", curly: "error" -- Here's a description about why this configuration is necessary. */
/* eslint eqeqeq: "off", curly: "error"
    --------
    Here's a description about why this configuration is necessary. */
/* eslint eqeqeq: "off", curly: "error"
 * --------
 * This will not work due to the line above starting with a '*' character.
 */

使用設定檔

若要在設定檔內設定規則,請使用 rules 鍵以及錯誤層級和您要使用的任何選項。例如

export default [
    {
        rules: {
            eqeqeq: "off",
            "no-unused-vars": "error",
            "prefer-const": ["error", { "ignoreReadBeforeAssign": true }]
        }
    }
];

當多個設定物件指定相同的規則時,規則設定會合併,後面的物件會優先於任何先前的物件。例如

export default [
    {
        rules: {
            semi: ["error", "never"]
        }
    },
    {
        rules: {
            semi: ["warn", "always"]
        }
    }
];

使用此設定,semi 的最終規則設定為 ["warn", "always"],因為它最後出現在陣列中。此陣列表示該設定用於嚴重性和任何選項。您可以僅透過定義字串或數字來變更嚴重性,如此範例所示

export default [
    {
        rules: {
            semi: ["error", "never"]
        }
    },
    {
        rules: {
            semi: "warn"
        }
    }
];

在此,第二個設定物件只會覆寫嚴重性,因此 semi 的最終設定為 ["warn", "never"]

來自外掛的規則

若要設定在外掛內定義的規則,請在規則 ID 前面加上外掛命名空間和 /

例如,在設定檔

// eslint.config.js
import example from "eslint-plugin-example";

export default [
    {
        plugins: {
            example
        },
        rules: {
            "example/rule1": "warn"
        }
    }
];

在此設定檔中,規則 example/rule1 來自名為 eslint-plugin-example 的外掛。

您也可以將此格式與設定註解搭配使用,例如

/* eslint "example/rule1": "error" */

停用規則

使用設定註解

  • 謹慎使用。應限制內嵌停用 ESLint 規則,且僅在有明確且有效的原因時才使用。內嵌停用規則不應是解決程式碼檢查錯誤的預設解決方案。
  • 記錄原因。請在註解的 -- 區段之後提供註解,說明停用特定規則的原因。此文件應闡明為何停用規則,以及在該特定情況下為何有其必要。
  • 暫時解決方案。如果加入停用註解作為解決緊急問題的暫時措施,請建立後續工作來充分解決根本問題。這可確保稍後會重新檢視和解決停用註解。
  • 程式碼審查和配對程式設計。鼓勵團隊成員定期審查彼此的程式碼。程式碼審查可以協助找出停用註解背後的原因,並確保它們被適當使用。
  • 設定。盡可能優先使用 ESLint 設定檔,而非停用註解。設定檔允許一致且專案範圍的規則處理。

若要在檔案的一部分中停用規則警告,請使用以下格式的區塊註解

/* eslint-disable */

alert('foo');

/* eslint-enable */

您也可以停用或啟用特定規則的警告

/* eslint-disable no-alert, no-console */

alert('foo');
console.log('bar');

/* eslint-enable no-alert, no-console */

若要在整個檔案中停用規則警告,請在檔案頂端放置 /* eslint-disable */ 區塊註解

/* eslint-disable */

alert('foo');

您也可以在整個檔案中停用或啟用特定規則

/* eslint-disable no-alert */

alert('foo');

若要確保永遠不會套用規則 (無論任何未來的啟用/停用行)

/* eslint no-alert: "off" */

alert('foo');

若要在特定行停用所有規則,請使用以下其中一種格式的行或區塊註解

alert('foo'); // eslint-disable-line

// eslint-disable-next-line
alert('foo');

/* eslint-disable-next-line */
alert('foo');

alert('foo'); /* eslint-disable-line */

若要在特定行停用特定規則

alert('foo'); // eslint-disable-line no-alert

// eslint-disable-next-line no-alert
alert('foo');

alert('foo'); /* eslint-disable-line no-alert */

/* eslint-disable-next-line no-alert */
alert('foo');

若要在特定行停用多個規則

alert('foo'); // eslint-disable-line no-alert, quotes, semi

// eslint-disable-next-line no-alert, quotes, semi
alert('foo');

alert('foo'); /* eslint-disable-line no-alert, quotes, semi */

/* eslint-disable-next-line no-alert, quotes, semi */
alert('foo');

/* eslint-disable-next-line
  no-alert,
  quotes,
  semi
*/
alert('foo');

上述所有方法也適用於外掛規則。例如,若要停用 eslint-plugin-examplerule-name 規則,請將外掛的名稱 (example) 和規則的名稱 (rule-name) 合併為 example/rule-name

foo(); // eslint-disable-line example/rule-name
foo(); /* eslint-disable-line example/rule-name */

註解說明

設定註解可以包含說明,以解釋為什麼需要停用或重新啟用規則。說明必須出現在設定之後,並且需要以兩個或多個連續的 - 字元與設定分隔。例如

// eslint-disable-next-line no-console -- Here's a description about why this configuration is necessary.
console.log('hello');

/* eslint-disable-next-line no-console --
 * Here's a very long description about why this configuration is necessary
 * along with some additional information
**/
console.log('hello');

使用設定檔

若要為一組檔案停用設定檔內的規則,請使用具有 files 鍵的後續設定物件。例如

// eslint.config.js
export default [
    {
        rules: {
            "no-unused-expressions": "error"
        }
    },
    {
        files: ["*-test.js","*.spec.js"],
        rules: {
            "no-unused-expressions": "off"
        }
    }
];

停用行內註解

若要停用所有內嵌設定註解,請在您的設定檔中使用 noInlineConfig 設定。例如

// eslint.config.js
export default [
    {
        linterOptions: {
            noInlineConfig: true
        },
        rules: {
            "no-unused-expressions": "error"
        }
    }
];

除了其他內嵌設定外,您也可以使用 --no-inline-config CLI 選項來停用規則註解。

回報未使用的 eslint-disable 註解

若要回報未使用的 eslint-disable 註解,請使用 reportUnusedDisableDirectives 設定。例如

// eslint.config.js
export default [
    {
        linterOptions: {
            reportUnusedDisableDirectives: "error"
        }
    }
];

此設定的預設值為 "warn"

此設定類似於 --report-unused-disable-directives--report-unused-disable-directives-severity CLI 選項。

變更語言