忽略檔案
您可以透過在以下方式中指定一個或多個 glob 模式,來設定 ESLint 在程式碼檢查時忽略特定的檔案和目錄
- 在您的
eslint.config.js
檔案中 - 在命令列中使用
--ignore-pattern
忽略檔案
在您的 eslint.config.js
檔案中,如果 ignores
鍵在設定物件中沒有任何其他鍵的情況下使用,則這些模式會作為全域忽略。以下是一個範例
// eslint.config.js
export default [
{
ignores: [".config/*"]
}
];
此設定指定應忽略 .config
目錄中的所有檔案。此模式是在預設模式 ["**/node_modules/", ".git/"]
之後加入的。
您也可以在命令列中使用 --ignore-pattern
來忽略檔案,例如
npx eslint . --ignore-pattern ".config/*"
忽略目錄
忽略目錄的方式與忽略檔案相同,只需將模式放入沒有其他鍵的設定物件的 ignores
鍵中即可。例如,以下範例忽略整個 .config
目錄(表示檔案搜尋完全不會進入該目錄)
// eslint.config.js
export default [
{
ignores: [".config/"]
}
];
與 .gitignore
不同,像 .config
這樣的忽略模式只會忽略與設定檔位於同一目錄下的 .config
目錄。如果您想要遞迴地忽略所有名為 .config
的目錄,則需要使用 **/.config/
,如以下範例所示
// eslint.config.js
export default [
{
ignores: ["**/.config/"]
}
];
取消忽略檔案和目錄
您也可以取消忽略先前模式(包括預設模式)所忽略的檔案和目錄。例如,此設定會取消忽略 node_modules/mylibrary
export default [
{
ignores: [
"!node_modules/", // unignore `node_modules/` directory
"node_modules/*", // ignore its content
"!node_modules/mylibrary/" // unignore `node_modules/mylibrary` directory
]
}
];
如果您想忽略目錄,但保留特定的檔案或子目錄,則必須使用忽略模式 directory/**/*
,而不是 directory/**
。模式 directory/**
會忽略整個目錄及其內容,因此搜尋會完全跳過該目錄,您無法取消忽略其中的任何內容。
例如,build/**
會忽略目錄 build
及其內容,而 build/**/*
僅忽略其內容。如果您想忽略 build
目錄中的所有內容,但保留 build/test.js
,則需要建立如下所示的設定
export default [
{
ignores: [
"build/**/*", // ignore all contents in and under `build/` directory but not the `build/` directory itself
"!build/test.js" // unignore `!build/test.js`
]
}
];
如果您想忽略目錄,但保留該目錄下任何層級的特定檔案,則還應確保子目錄未被忽略。請注意,雖然以 /
結尾的模式僅匹配目錄,但未以 /
結尾的模式會匹配檔案和目錄,因此無法撰寫單一模式來僅忽略檔案,但您可以使用兩個模式來實現此目的:一個用於忽略所有內容,另一個用於取消忽略子目錄。
例如,此設定會忽略 build
目錄中及其下的所有檔案,但保留任何層級中名為 test.js
的檔案
export default [
{
ignores: [
"build/**/*", // ignore all contents in and under `build/` directory but not the `build/` directory itself
"!build/**/*/", // unignore all subdirectories
"!build/**/test.js" // unignore `test.js` files
]
}
];
您也可以在命令列中使用 --ignore-pattern
來取消忽略檔案,例如
npx eslint . --ignore-pattern "!node_modules/"
Glob 模式解析
glob 模式的評估方式取決於它們的位置和使用方式
- 在
eslint.config.js
檔案中使用ignores
時,glob 模式會相對於eslint.config.js
檔案進行評估。 - 當使用
--config
命令列選項指定替代設定檔時,glob 模式會相對於目前的工作目錄進行評估。 - 當使用
--ignore-pattern
時,glob 模式會相對於目前的工作目錄進行評估。
已忽略檔案警告
當您將目錄傳遞給 ESLint CLI 時,檔案和目錄會被靜默忽略。如果您將特定檔案傳遞給 ESLint,則 ESLint 會產生一個警告,指出該檔案已被跳過。例如,假設您有一個如下所示的 eslint.config.js
檔案
// eslint.config.js
export default [
{
ignores: ["foo.js"]
}
]
然後您執行
npx eslint foo.js
您會看到此警告
foo.js
0:0 warning File ignored because of a matching ignore pattern. Use "--no-ignore" to disable file ignore settings or use "--no-warn-ignored" to suppress this warning.
✖ 1 problem (0 errors, 1 warning)
發生此訊息是因為 ESLint 不確定您是否真的想檢查該檔案。如訊息所示,您可以使用 --no-ignore
來略過使用忽略規則。
包含 .gitignore
檔案
如果您想包含來自 .gitignore
檔案或任何其他具有 gitignore 樣式模式的檔案的模式,您可以使用來自 @eslint/compat
套件的 includeIgnoreFile
工具。
// eslint.config.js
import { includeIgnoreFile } from "@eslint/compat";
import path from "node:path";
import { fileURLToPath } from "node:url";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const gitignorePath = path.resolve(__dirname, ".gitignore");
export default [
includeIgnoreFile(gitignorePath),
{
// your overrides
}
];
這會自動載入指定的檔案,並將 gitignore 樣式模式轉換為 ignores
glob 模式。