設定規則
規則是 ESLint 的核心建構區塊。規則會驗證您的程式碼是否符合特定期望,以及在不符合期望時該怎麼做。規則也可以包含特定於該規則的其他設定選項。
ESLint 隨附大量內建規則,您可以透過外掛程式新增更多規則。您可以使用設定註解或設定檔修改專案使用的規則。
規則嚴重性
若要變更規則的嚴重性,請將規則 ID 設定為下列其中一個值
"off"
或0
- 關閉規則。"warn"
或1
- 將規則開啟為警告(不影響結束代碼)。"error"
或2
- 將規則開啟為錯誤(觸發時結束代碼為 1)。
規則通常設定為 "error"
,以便在持續整合測試、pre-commit 檢查和 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.
*/
回報未使用的 eslint
行內設定註解
若要回報未使用的 eslint
行內設定註解(那些不會從已設定的內容變更任何內容的註解),請使用 reportUnusedInlineConfigs
設定。例如
// eslint.config.js
import { defineConfig } from "eslint/config";
export default defineConfig([
{
linterOptions: {
reportUnusedInlineConfigs: "error"
}
}
]);
此設定預設為 "off"
。
此設定類似於 --report-unused-inline-configs
CLI 選項。
使用設定檔
若要在設定檔內設定規則,請使用 rules
鍵以及錯誤層級和您想要使用的任何選項。例如
// eslint.config.js
import { defineConfig } from "eslint/config";
export default defineConfig([
{
rules: {
eqeqeq: "off",
"no-unused-vars": "error",
"prefer-const": ["error", { "ignoreReadBeforeAssign": true }]
}
}
]);
當多個設定物件指定相同的規則時,規則設定會合併,且後面的物件優先於任何先前的物件。例如
import { defineConfig } from "eslint/config";
export default defineConfig([
{
rules: {
semi: ["error", "never"]
}
},
{
rules: {
semi: ["warn", "always"]
}
}
]);
使用此設定,semi
的最終規則設定為 ["warn", "always"]
,因為它在陣列中最後出現。陣列表示設定適用於嚴重性和任何選項。您可以僅透過定義字串或數字來變更嚴重性,如此範例所示
import { defineConfig } from "eslint/config";
export default defineConfig([
{
rules: {
semi: ["error", "never"]
}
},
{
rules: {
semi: "warn"
}
}
]);
在此,第二個設定物件僅覆寫嚴重性,因此 semi
的最終設定為 ["warn", "never"]
。
來自外掛程式的規則
若要設定在外掛程式中定義的規則,請在外掛程式命名空間和 /
前面加上規則 ID。
在設定檔中,例如
// eslint.config.js
import example from "eslint-plugin-example";
import { defineConfig } from "eslint/config";
export default defineConfig([
{
plugins: {
example
},
rules: {
"example/rule1": "warn"
}
}
]);
在此設定檔中,規則 example/rule1
來自名為 eslint-plugin-example
的外掛程式。
您也可以將此格式與設定註解搭配使用,例如
/* eslint "example/rule1": "error" */
停用規則
使用設定註解
- 謹慎使用。 應限制行內停用 ESLint 規則,且僅在有明確且有效的原因時才使用。行內停用規則不應是解決 linting 錯誤的預設解決方案。
- 記錄原因。 在註解的
--
區段之後提供註解,說明停用特定規則的原因。此文件應闡明為何停用規則,以及為何在該特定情況下有其必要性。 - 臨時解決方案。 如果新增停用註解作為解決迫切問題的臨時措施,請建立後續任務以充分解決根本問題。這可確保稍後重新檢視並解決停用註解。
- 程式碼審查和雙人程式設計。 鼓勵團隊成員定期審查彼此的程式碼。程式碼審查可以協助識別停用註解背後的原因,並確保它們得到適當使用。
- 設定。 在可能的情況下,盡可能優先使用 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-example
的 rule-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
import { defineConfig } from "eslint/config";
export default defineConfig([
{
rules: {
"no-unused-expressions": "error"
}
},
{
files: ["*-test.js","*.spec.js"],
rules: {
"no-unused-expressions": "off"
}
}
]);
停用行內註解
若要停用所有行內設定註解,請在您的設定檔中使用 noInlineConfig
設定。例如
// eslint.config.js
import { defineConfig } from "eslint/config";
export default defineConfig([
{
linterOptions: {
noInlineConfig: true
},
rules: {
"no-unused-expressions": "error"
}
}
]);
您也可以使用 --no-inline-config
CLI 選項來停用規則註解,以及其他行內設定。
回報未使用的 eslint-disable
註解
若要回報未使用的 eslint-disable
註解(那些停用不會在已停用行上回報的規則的註解),請使用 reportUnusedDisableDirectives
設定。例如
// eslint.config.js
import { defineConfig } from "eslint/config";
export default defineConfig([
{
linterOptions: {
reportUnusedDisableDirectives: "error"
}
}
]);
此設定預設為 "warn"
。
此設定類似於 --report-unused-disable-directives
和 --report-unused-disable-directives-severity
CLI 選項。