忽略文件

你可以通过以下方式指定一个或多个 通配符模式来将 ESLint 配置为在 linting 时忽略某些文件和目录:

¥You can configure ESLint to ignore certain files and directories while linting by specifying one or more glob patterns in the following ways:

  • 在你的 eslint.config.js 文件里面

    ¥Inside of your eslint.config.js file

  • 在命令行上使用 --ignore-pattern

    ¥On the command line using --ignore-pattern

忽略文件

¥Ignoring Files

eslint.config.js 文件中,如果在配置对象中使用 ignores 键而没有任何其他键,则模式将充当全局忽略。这是一个例子:

¥In your eslint.config.js file, if an ignores key is used without any other keys in the configuration object, then the patterns act as global ignores. Here’s an example:

// eslint.config.js
export default [
    {
        ignores: [".config/*"]
    }
];

此配置指定应忽略 .config 目录中的所有文件。此模式添加在默认模式之后,即 ["**/node_modules/", ".git/"]

¥This configuration specifies that all of the files in the .config directory should be ignored. This pattern is added after the default patterns, which are ["**/node_modules/", ".git/"].

你还可以在命令行上使用 --ignore-pattern 忽略文件,例如:

¥You can also ignore files on the command line using --ignore-pattern, such as:

npx eslint . --ignore-pattern ".config/*"

忽略目录

¥Ignoring Directories

忽略目录的工作方式与忽略文件相同,即在配置对象的 ignores 键中放置一个模式,而没有其他键。例如,以下内容将忽略整个 .config 目录(意味着文件搜索根本不会遍历到该目录):

¥Ignoring directories works the same way as ignoring files, by placing a pattern in the ignores key of a configuration object with no other keys. For example, the following ignores the .config directory as a whole (meaning file search will not traverse into it at all):

// eslint.config.js
export default [
    {
        ignores: [".config/"]
    }
];

.gitignore 不同,像 .config 这样的忽略模式只会忽略与配置文件位于同一目录中的 .config 目录。如果要递归地忽略名为 .config 的所有目录,则需要使用 **/.config/,如下例所示:

¥Unlike .gitignore, an ignore pattern like .config will only ignore the .config directory in the same directory as the configuration file. If you want to recursively ignore all directories named .config, you need to use **/.config/, as in this example:

// eslint.config.js
export default [
    {
        ignores: ["**/.config/"]
    }
];

不忽略文件和目录

¥Unignoring Files and Directories

你还可以取消忽略先前模式(包括默认模式)所忽略的文件和目录。例如,此配置忽略 node_modules/mylibrary

¥You can also unignore files and directories that are ignored by previous patterns, including the default patterns. For example, this config unignores 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/** 会忽略整个目录及其内容,因此遍历将完全跳过该目录,并且你无法忽略其中的任何内容。

¥If you’d like to ignore a directory except for specific files or subdirectories, then the ignore pattern directory/**/* must be used instead of directory/**. The pattern directory/** ignores the entire directory and its contents, so traversal will skip over the directory completely and you cannot unignore anything inside.

例如,build/** 忽略目录 build 及其内容,而 build/**/* 仅忽略其内容。如果你想忽略 build 目录中除 build/test.js 之外的所有内容,则需要创建如下配置:

¥For example, build/** ignores directory build and its contents, whereas build/**/* ignores only its contents. If you’d like to ignore everything in the build directory except for build/test.js, you’d need to create a config like this:

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`
        ]
    }
];

如果你想忽略某个目录,但该目录下任何级别的特定文件除外,你还应该确保不忽略子目录。请注意,虽然以 / 结尾的模式仅匹配目录,但不以 / 结尾的模式同时匹配文件和目录,因此不可能编写仅忽略文件的单个模式,但你可以使用两种模式来实现此目的:一个忽略所有内容,另一个忽略子目录。

¥If you’d like to ignore a directory except for specific files at any level under the directory, you should also ensure that subdirectories are not ignored. Note that while patterns that end with / only match directories, patterns that don’t end with / match both files and directories so it isn’t possible to write a single pattern that only ignores files, but you can achieve this with two patterns: one to ignore all contents and another to unignore subdirectories.

例如,此配置忽略 build 目录中及其下的所有文件,但任何级别的名为 test.js 的文件除外:

¥For example, this config ignores all files in and under build directory except for files named test.js at any level:

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 在命令行上取消忽略文件,例如:

¥You can also unignore files on the command line using --ignore-pattern, such as:

npx eslint . --ignore-pattern "!node_modules/"

通配符模式解析

¥Glob Pattern Resolution

通配符模式的评估方式取决于它们所在的位置以及它们的使用方式:

¥How glob patterns are evaluated depends on where they are located and how they are used:

  1. eslint.config.js 文件中使用 ignores 时,将相对于 eslint.config.js 文件评估 通配符模式。

    ¥When using ignores in an eslint.config.js file, glob patterns are evaluated relative to the eslint.config.js file.

  2. 当在使用 --config 命令行选项指定的备用配置文件中使用 ignores 时,将相对于当前工作目录评估 通配符模式。

    ¥When using ignores in an alternate configuration file specified using the --config command line option, glob patterns are evaluated relative to the current working directory.

  3. 使用 --ignore-pattern 时,通配符模式是相对于当前工作目录进行评估的。

    ¥When using --ignore-pattern, glob patterns are evaluated relative to the current working directory.

忽略的文件警告

¥Ignored File Warnings

当你将目录传递给 ESLint CLI 时,文件和目录将被静默忽略。如果你将特定文件传递给 ESLint,那么 ESLint 会创建一个警告,指出该文件已被跳过。例如,假设你有一个如下所示的 eslint.config.js 文件:

¥When you pass directories to the ESLint CLI, files and directories are silently ignored. If you pass a specific file to ESLint, then ESLint creates a warning that the file was skipped. For example, suppose you have an eslint.config.js file that looks like this:

// eslint.config.js
export default [
    {
        ignores: ["foo.js"]
    }
]

然后你运行:

¥And then you run:

npx eslint foo.js

你会看到这个警告:

¥You’ll see this warning:

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 不确定你是否要实际 lint 文件。如消息所示,你可以使用 --no-ignore 忽略使用忽略规则。

¥This message occurs because ESLint is unsure if you wanted to actually lint the file or not. As the message indicates, you can use --no-ignore to omit using the ignore rules.

包括 .gitignore 文件

¥Including .gitignore Files

如果你想在 .gitignore 文件或任何其他具有 gitignore 样式模式的文件中包括模式,你可以使用 @eslint/compat 包中的 includeIgnoreFile 实用程序。

¥If you want to include patterns from a .gitignore file or any other file with gitignore-style patterns, you can use includeIgnoreFile utility from the @eslint/compat package.

// 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 通配符模式。

¥This automatically loads the specified file and translates gitignore-style patterns into ignores glob patterns.

ESLint 中文网
粤ICP备13048890号