設定規則 (已棄用)
規則是 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
鍵以及錯誤層級和您想要使用的任何選項。例如
{
"rules": {
"eqeqeq": "off",
"curly": "error",
"quotes": ["error", "double"]
}
}
在 YAML 中
---
rules:
eqeqeq: off
curly: error
quotes:
- error
- double
外掛程式的規則
若要設定外掛程式中定義的規則,請在規則 ID 前面加上外掛程式名稱和 /
。
例如在設定檔中
{
"plugins": [
"plugin1"
],
"rules": {
"eqeqeq": "off",
"curly": "error",
"quotes": ["error", "double"],
"plugin1/rule1": "error"
}
}
在 YAML 中
---
plugins:
- plugin1
rules:
eqeqeq: 0
curly: error
quotes:
- error
- "double"
plugin1/rule1: error
在這些設定檔中,規則 plugin1/rule1
來自名為 plugin1
的外掛程式,該外掛程式包含在名為 eslint-plugin-plugin1
的 npm 套件中。
您也可以將此格式用於設定註解,例如
/* eslint "plugin1/rule1": "error" */
注意:指定外掛程式的規則時,請務必省略 eslint-plugin-
。ESLint 僅在內部使用未加首碼的名稱來尋找規則。
停用規則
使用設定註解
- 謹慎使用。應限制內嵌停用 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-enable */
會導致重新啟用所有已停用的規則。
若要停用整個檔案中的規則警告,請將 /* 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-example
的 rule-name
規則,請將外掛程式的名稱 (example
) 和規則的名稱 (rule-name
) 組合成 example/rule-name
foo(); // eslint-disable-line example/rule-name
foo(); /* eslint-disable-line example/rule-name */
注意:停用檔案一部分警告的註解會告訴 ESLint 不要回報已停用程式碼的規則違規。但是,ESLint 仍然會解析整個檔案,因此已停用的程式碼仍然需要是語法有效的 JavaScript。
註解描述
設定註解可以包含描述,以說明為何需要停用或重新啟用規則。描述必須在設定之後,並以兩個或更多個連續的 -
字元與設定分隔。例如
// 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');
使用設定檔
若要針對一組檔案停用設定檔內的規則,請使用 overrides
鍵以及 files
鍵。例如
{
"rules": {...},
"overrides": [
{
"files": ["*-test.js","*.spec.js"],
"rules": {
"no-unused-expressions": "off"
}
}
]
}
停用內嵌註解
若要停用所有內嵌設定註解,請在設定檔中使用 noInlineConfig
設定。例如
{
"rules": {...},
"noInlineConfig": true
}
除了其他內嵌設定之外,您也可以使用 --no-inline-config
CLI 選項來停用規則註解。
回報未使用的 eslint-disable
註解
若要回報未使用的 eslint-disable
註解,請使用 reportUnusedDisableDirectives
設定。例如
{
"rules": {...},
"reportUnusedDisableDirectives": true
}
此設定與 --report-unused-disable-directives
CLI 選項類似,但不會導致程式碼檢查失敗 (以 "warn"
嚴重性回報)。