批量抑制
在代码库存在许多违规且该规则不可自动修复的情况下,将新的 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 it exits with a non-zero exit code and an error 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
在计算退出代码时忽略未使用的抑制,并且不报告未使用抑制的错误,可以使用 --pass-on-unpruned-suppressions 标志。
🌐 To ignore unused suppressions when calculating the exit code and not report an error about unused suppressions, you can use the --pass-on-unpruned-suppressions flag.
eslint --pass-on-unpruned-suppressions
有关可用 CLI 选项的更多信息,请参阅 命令行接口。
🌐 For more information on the available CLI options, refer to Command Line Interface.
与 Node.js API 的使用
🌐 Usage with the Node.js API
在通过 Node.js API 以编程方式使用 ESLint 时,也可以应用抑制。要启用抑制,请在 ESLint 构造函数中将 applySuppressions 选项设置为 true:
🌐 Suppressions can also be applied when using ESLint programmatically through the Node.js API. To enable suppressions, set the applySuppressions option to true in the ESLint constructor:
const eslint = new ESLint({
applySuppressions: true,
});
默认情况下,ESLint 会在当前工作目录中查找 eslint-suppressions.json。你可以使用 suppressionsLocation 选项指定自定义抑制文件的位置:
🌐 By default, ESLint looks for eslint-suppressions.json in the current working directory. You can specify a custom suppressions file location using the suppressionsLocation option:
const eslint = new ESLint({
applySuppressions: true,
suppressionsLocation: "./config/my-suppressions.json",
});
使用 lintText() 时,必须提供 filePath 选项才能使抑制生效,因为抑制是通过文件路径匹配的。
🌐 When using lintText(), you must provide the filePath option for suppressions to take effect, since suppressions are matched by file path.
注意: Node.js API 仅支持应用现有的抑制。创建新的抑制 (--suppress-all、--suppress-rule) 和清理未使用的抑制 (--prune-suppressions) 仅通过 CLI 提供。