忽略文件(已弃用)
你可以通过指定一个或多个通配符模式来配置 ESLint 在进行 linting 时忽略某些文件和目录。你可以通过以下方式忽略文件:
¥You can configure ESLint to ignore certain files and directories while linting by specifying one or more glob patterns. You can ignore files in the following ways:
-
将
ignorePatterns
添加到配置文件。¥Add
ignorePatterns
to a configuration file. -
创建一个包含忽略模式(默认为
.eslintignore
)的专用文件。¥Create a dedicated file that contains the ignore patterns (
.eslintignore
by default).
配置文件中的 ignorePatterns
¥ignorePatterns
in Config Files
你可以在配置文件中使用 ignorePatterns
告诉 ESLint 忽略特定的文件和目录。ignorePatterns
模式遵循与 .eslintignore
相同的规则。请参阅 .eslintignore
文件文档 以了解更多信息。
¥You can tell ESLint to ignore specific files and directories using ignorePatterns
in your config files. ignorePatterns
patterns follow the same rules as .eslintignore
. Please see the .eslintignore
file documentation to learn more.
{
"ignorePatterns": ["temp.js", "**/vendor/*.js"],
"rules": {
//...
}
}
-
ignorePatterns
中的通配符模式与配置文件所在的目录相关。¥Glob patterns in
ignorePatterns
are relative to the directory that the config file is placed in. -
你不能在
overrides
属性下写入ignorePatterns
属性。¥You cannot write
ignorePatterns
property underoverrides
property. -
.eslintignore
中定义的模式优先于配置文件的ignorePatterns
属性。¥Patterns defined in
.eslintignore
take precedence over theignorePatterns
property of config files.
如果通配符模式以 /
开头,则该模式与配置文件的基本目录相关。例如,lib/.eslintrc.json
中的 /foo.js
与 lib/foo.js
匹配,但不与 lib/subdir/foo.js
匹配。
¥If a glob pattern starts with /
, the pattern is relative to the base directory of the config file. For example, /foo.js
in lib/.eslintrc.json
matches to lib/foo.js
but not lib/subdir/foo.js
.
如果通过 --config
CLI 选项提供配置,则配置中以 /
开头的忽略模式与当前工作目录相关,而不是与给定配置的基本目录相关。例如,如果存在 --config configs/.eslintrc.json
,则配置中的忽略模式与 .
而不是 ./configs
相关。
¥If a config is provided via the --config
CLI option, the ignore patterns that start with /
in the config are relative to the current working directory rather than the base directory of the given config. For example, if --config configs/.eslintrc.json
is present, the ignore patterns in the config are relative to .
rather than ./configs
.
.eslintignore
文件
¥The .eslintignore
File
你可以通过在项目的根目录中创建 .eslintignore
文件来告诉 ESLint 忽略特定的文件和目录。.eslintignore
文件是一个纯文本文件,其中每行都是一个通配符模式,指示应从 linting 中省略哪些路径。例如,以下内容省略了所有 JavaScript 文件:
¥You can tell ESLint to ignore specific files and directories by creating an .eslintignore
file in your project’s root directory. The .eslintignore
file is a plain text file where each line is a glob pattern indicating which paths should be omitted from linting. For example, the following omits all JavaScript files:
**/*.js
运行 ESLint 时,它会在当前工作目录中查找 .eslintignore
文件,然后再确定要 lint 哪些文件。如果找到此文件,则在遍历目录时将应用这些首选项。一次只能使用一个 .eslintignore
文件,因此不使用当前工作目录中的 .eslintignore
文件以外的其他 .eslintignore
文件。
¥When ESLint is run, it looks in the current working directory to find an .eslintignore
file before determining which files to lint. If this file is found, then those preferences are applied when traversing directories. Only one .eslintignore
file can be used at a time, so .eslintignore
files other than the one in the current working directory are not used.
Globs 使用 node-ignore 进行匹配,因此有许多功能可用:
¥Globs are matched using node-ignore, so a number of features are available:
-
以
#
开头的行被视为注释,不会影响忽略模式。¥Lines beginning with
#
are treated as comments and do not affect the ignore patterns. -
路径相对于当前工作目录。对于通过
--ignore-pattern
command 传入的路径也是如此。¥Paths are relative to the current working directory. This is also true of paths passed in via the
--ignore-pattern
command. -
以
!
开头的行是否定模式,重新包含被早期模式忽略的模式。¥Lines preceded by
!
are negated patterns that re-include a pattern that was ignored by an earlier pattern. -
忽略模式按照
.gitignore
规范 运行。¥Ignore patterns behave according to the
.gitignore
specification.
特别值得注意的是,与 .gitignore
文件一样,用作 .eslintignore
和 --ignore-pattern
模式的所有路径都必须使用正斜杠作为路径分隔符。
¥Of particular note is that like .gitignore
files, all paths used as patterns for both .eslintignore
and --ignore-pattern
must use forward slashes as their path separators.
# Valid
/root/src/*.js
# Invalid
\root\src\*.js
请参阅 .gitignore
的规范以获取有效语法的更多示例。
¥Please see .gitignore
’s specification for further examples of valid syntax.
除了 .eslintignore
文件中的任何模式之外,即使传递了 --no-ignore
标志,ESLint 也始终遵循几个隐式忽略规则。隐式规则如下:
¥In addition to any patterns in the .eslintignore
file, ESLint always follows a couple of implicit ignore rules even if the --no-ignore
flag is passed. The implicit rules are as follows:
-
node_modules/
被忽略。¥
node_modules/
is ignored. -
点文件(
.eslintrc.*
除外)以及点文件夹及其内容将被忽略。¥dot-files (except for
.eslintrc.*
) as well as dot-folders and their contents are ignored.
这些规则也有一些例外:
¥There are also some exceptions to these rules:
-
如果 lint 的路径是通配符模式或目录路径并包含点文件夹,则所有点文件和点文件夹都将被 lint。这包括目录结构中较深的点文件和点文件夹。
¥If the path to lint is a glob pattern or directory path and contains a dot-folder, all dot-files and dot-folders are linted. This includes dot-files and dot-folders that are buried deeper in the directory structure.
例如,
eslint .config/
会对.config
目录中的所有点文件夹和点文件进行 lint,包括直接子级以及目录结构中更深的子级。¥For example,
eslint .config/
would lint all dot-folders and dot-files in the.config
directory, including immediate children as well as children that are deeper in the directory structure. -
如果 lint 的路径是特定文件路径且已传递
--no-ignore
标志,则 ESLint 会不管隐式忽略规则如何都对文件进行 lint。¥If the path to lint is a specific file path and the
--no-ignore
flag has been passed, ESLint would lint the file regardless of the implicit ignore rules.例如,
eslint .config/my-config-file.js --no-ignore
会导致my-config-file.js
被 lint。应该注意的是,没有--no-ignore
行的相同命令不会对my-config-file.js
文件进行 linting。¥For example,
eslint .config/my-config-file.js --no-ignore
would causemy-config-file.js
to be linted. It should be noted that the same command without the--no-ignore
line would not lint themy-config-file.js
file. -
通过
--ignore-pattern
或.eslintignore
指定的允许列表和拒绝列表规则优先于隐式忽略规则。¥Allowlist and denylist rules specified via
--ignore-pattern
or.eslintignore
are prioritized above implicit ignore rules.例如,在这种情况下,
.build/test.js
是要列入白名单的所需文件。由于默认情况下会忽略所有点文件夹及其子文件夹,因此必须首先将.build
列入允许名单,以便 eslint 知道其子文件夹。然后,必须明确将.build/test.js
列入允许名单,而将其余内容列入拒绝名单。这通过以下.eslintignore
文件完成:¥For example, in this scenario,
.build/test.js
is the desired file to allowlist. Because all dot-folders and their children are ignored by default,.build
must first be allowlisted so that eslint becomes aware of its children. Then,.build/test.js
must be explicitly allowlisted, while the rest of the content is denylisted. This is done with the following.eslintignore
file:# Allowlist 'test.js' in the '.build' folder # But do not allow anything else in the '.build' folder to be linted !.build .build/* !.build/test.js
以下
--ignore-pattern
也是等效的:¥The following
--ignore-pattern
is also equivalent:eslint --ignore-pattern '!.build' --ignore-pattern '.build/*' --ignore-pattern '!.build/test.js' parent-folder/
使用备用文件
¥Using an Alternate File
如果你希望在当前工作目录中使用与 .eslintignore
不同的文件,你可以在命令行上使用 --ignore-path
选项指定它。例如,你可以使用 .jshintignore
文件,因为它具有相同的格式:
¥If you’d prefer to use a different file than the .eslintignore
in the current working directory, you can specify it on the command line using the --ignore-path
option. For example, you can use .jshintignore
file because it has the same format:
eslint --ignore-path .jshintignore file.js
你还可以使用你的 .gitignore
文件:
¥You can also use your .gitignore
file:
eslint --ignore-path .gitignore file.js
可以使用遵循标准忽略文件格式的任何文件。请记住,指定 --ignore-path
意味着不使用现有的 .eslintignore
文件。请注意,.eslintignore
中的通配符规则遵循 .gitignore
的规则。
¥Any file that follows the standard ignore file format can be used. Keep in mind that specifying --ignore-path
means that the existing .eslintignore
file is not used. Note that globbing rules in .eslintignore
follow those of .gitignore
.
在 package.json 中使用 eslintIgnore
¥Using eslintIgnore in package.json
如果未找到 .eslintignore
文件且未指定备用文件,则 ESLint 会在 package.json
中查找 eslintIgnore
键以检查要忽略的文件。
¥If an .eslintignore
file is not found and an alternate file is not specified, ESLint looks in package.json
for the eslintIgnore
key to check for files to ignore.
{
"name": "mypackage",
"version": "0.0.1",
"eslintConfig": {
"env": {
"browser": true,
"node": true
}
},
"eslintIgnore": ["hello.js", "world.js"]
}
忽略的文件警告
¥Ignored File Warnings
当你将目录传递给 ESLint 时,文件和目录将被默默忽略。如果你将特定文件传递给 ESLint,那么 ESLint 会创建一个警告,指出该文件已被跳过。例如,假设你有一个如下所示的 .eslintignore
文件:
¥When you pass directories to ESLint, 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 .eslintignore
file that looks like this:
foo.js
然后你运行:
¥And then you run:
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.
考虑另一种情况,你想在特定的点文件或点文件夹上运行 ESLint,但忘记在 .eslintignore
文件中明确允许这些文件。你将运行类似这样的程序:
¥Consider another scenario where you want to run ESLint on a specific dot-file or dot-folder, but have forgotten to specifically allow those files in your .eslintignore
file. You would run something like this:
eslint .config/foo.js
你将看到此警告:
¥You would see this warning:
.config/foo.js
0:0 warning File ignored by default. Use a negated ignore pattern (like "--ignore-pattern '!<relative/path/to/filename>'") to override
✖ 1 problem (0 errors, 1 warning)
出现此消息的原因是,通常情况下,该文件将被 ESLint 的隐式忽略规则忽略(如上所述)。.eslintignore
文件中的否定忽略规则将覆盖隐式规则并重新包含此文件以进行 linting。此外,在这种情况下,--no-ignore
也可用于 lint 文件。
¥This message occurs because, normally, this file would be ignored by ESLint’s implicit ignore rules (as mentioned above). A negated ignore rule in your .eslintignore
file would override the implicit rule and reinclude this file for linting. Additionally, in this case, --no-ignore
could be used to lint the file as well.