批量抑制
当代码库存在许多违规且规则无法自动修复时,将新的 lint 规则启用为 "error"
可能会很困难。除非在项目的早期阶段启用该规则,否则随着代码库的增长,启用它会变得越来越困难。启用规则之前必须解决现有违规行为,但在此过程中可能会发生其他违规行为。
¥Enabling a new lint rule as "error"
can be challenging when the codebase has many violations and the rule isn’t auto-fixable. Unless the rule is enabled during the early stages of the project, it becomes harder and harder to enable it as the codebase grows. Existing violations must be resolved before enabling the rule, but while doing that other violations may occur.
为了解决这个问题,ESLint 提供了一种方法来抑制一个或多个规则的现有违规行为。虽然该规则将针对新代码强制执行,但不会报告现有的违规行为。这样,你可以按照自己的节奏解决现有的违规行为。
¥To address this, ESLint provides a way to suppress existing violations for one or more rules. While the rule will be enforced for new code, the existing violations will not be reported. This way, you can address the existing violations at your own pace.
在配置文件中将规则启用为 "error"
后,你可以使用 --suppress-all
标志一次性抑制所有现有违规。建议使用 --fix
标志执行命令,这样你就不会抑制可以自动修复的违规行为。
¥After you enable a rule as "error"
in your configuration file, you can suppress all the existing violations at once by using the --suppress-all
flag. It is recommended to execute the command with the --fix
flag so that you don’t suppress violations that can be auto-fixed.
eslint --fix --suppress-all
此命令将抑制所有启用为 "error"
的规则的所有现有违规行为。再次运行 eslint
命令不会报告这些违规行为。
¥This command will suppress all the existing violations of all the rules that are enabled as "error"
. Running the eslint
command again will not report these violations.
如果你想要抑制特定规则的违规行为,可以使用 --suppress-rule
标志。
¥If you would like to suppress violations of a specific rule, you can use the --suppress-rule
flag.
eslint --fix --suppress-rule no-unused-expressions
你还可以通过提供多个规则名称来抑制多个规则的违规行为。
¥You can also suppress violations of multiple rules by providing multiple rule names.
eslint --fix --suppress-rule no-unused-expressions --suppress-rule no-unsafe-assignment
抑制文件
¥Suppressions File
当你抑制违规行为时,ESLint 会在项目的根目录中创建一个 eslint-suppressions.json
文件。此文件包含已被抑制的规则列表。你应该将此文件提交到存储库,以便与所有开发者共享抑制内容。
¥When you suppress violations, ESLint creates a eslint-suppressions.json
file in the root of the project. This file contains the list of rules that have been suppressed. You should commit this file to the repository so that the suppressions are shared with all the developers.
如有必要,你可以使用 --suppressions-location
参数更改抑制文件的位置。请注意,不仅在抑制违规时必须提供参数,而且在运行 ESLint 时也必须提供参数。这是必要的,以便 ESLint 能够选择正确的抑制文件。
¥If necessary, you can change the location of the suppressions file by using the --suppressions-location
argument. Note that the argument must be provided not only when suppressing violations but also when running ESLint. This is necessary so that ESLint picks up the correct suppressions file.
eslint --suppressions-location .github/.eslint-suppressions
解决抑制
¥Resolving Suppressions
你可以像往常一样通过对代码进行必要的更改来解决任何报告的违规行为。如果再次运行 ESLint,你会注意到报告了有关未使用抑制的警告。这是因为违规行为已解决,但抑制仍然存在。
¥You can address any of the reported violations by making the necessary changes to the code as usual. If you run ESLint again you will notice that a warning is reported about unused suppressions. This is because the violations have been resolved but the suppressions are still in place.
> eslint
There are suppressions left that do not occur anymore. Consider re-running the command with `--prune-suppressions`.
要删除不再需要的抑制,你可以使用 --prune-suppressions
标志。
¥To remove the suppressions that are no longer needed, you can use the --prune-suppressions
flag.
eslint --prune-suppressions
有关可用 CLI 选项的更多信息,请参阅 命令行接口。
¥For more information on the available CLI options, refer to Command Line Interface.