功能标志

ESLint 在功能标志后面提供实验性和未来的重大更改,以让用户选择他们想要的行为。标志用于以下情况:

¥ESLint ships experimental and future breaking changes behind feature flags to let users opt-in to behavior they want. Flags are used in these situations:

  1. 当某个功能是实验性的并且尚未准备好为所有人启用时。

    ¥When a feature is experimental and not ready to be enabled for everyone.

  2. 当某个功能是重大更改时,将在下一个主要版本中正式合并,但用户可以在下一个主要版本之前选择加入该行为。

    ¥When a feature is a breaking change that will be formally merged in the next major release, but users may opt-in to that behavior prior to the next major release.

标志前缀

¥Flag Prefixes

标志的前缀表示其状态:

¥The prefix of a flag indicates its status:

  • unstable_ 表示该功能是实验性的,并且在功能稳定之前实现可能会发生变化。这是 “使用风险自负” 功能。

    ¥unstable_ indicates that the feature is experimental and the implementation may change before the feature is stabilized. This is a “use at your own risk” feature.

  • v##_ 表示该功能已稳定并将在下一个主要版本中可用。例如,v10_some_feature 表示这是一个重大更改,将在 ESLint v10.0.0 中正式发布。这些标志在每个主要版本中都会被删除。

    ¥v##_ indicates that the feature is stabilized and will be available in the next major release. For example, v10_some_feature indicates that this is a breaking change that will be formally released in ESLint v10.0.0. These flags are removed each major release.

如果功能是非重大更改,则可能会从不稳定状态变为稳定状态,而无需进行重大发布。

¥A feature may move from unstable to stable without a major release if it is a non-breaking change.

活跃标志

¥Active Flags

以下标志目前可在 ESLint 中使用。

¥The following flags are currently available for use in ESLint.

标志 描述
test_onlyUsed only for testing.
unstable_config_lookup_from_fileLook up eslint.config.js from the file being linted.
unstable_ts_configEnable TypeScript configuration files.

非活跃标志

¥Inactive Flags

以下标志曾经使用过,但不再有效。

¥The following flags were once used but are no longer active.

标志 描述
test_only_oldUsed only for testing.

如何使用功能标志

¥How to Use Feature Flags

由于功能标志是严格可选的,因此你需要手动启用所需的标志。

¥Because feature flags are strictly opt-in, you need to manually enable the flags that you want.

使用 CLI 启用功能标志

¥Enable Feature Flags with the CLI

在命令行上,你可以使用 --flag 选项指定功能标志。你可以根据需要指定任意数量的标志:

¥On the command line, you can specify feature flags using the --flag option. You can specify as many flags as you’d like:

npx eslint --flag flag_one --flag flag_two file.js

使用 API 启用功能标志

¥Enable Feature Flags with the API

使用 API 时,你可以将 flags 数组传递给 ESLintLinter 类:

¥When using the API, you can pass a flags array to both the ESLint and Linter classes:

const { ESLint, Linter } = require("eslint");

const eslint = new ESLint({
    flags: ["flag_one", "flag_two"]
});

const linter = new Linter({
    flags: ["flag_one", "flag_two"]
});

在 VS Code 中启用功能标志

¥Enable Feature Flags in VS Code

要在 VS Code ESLint 扩展中为编辑器启用标志,请在 settings.json 文件中的 eslint.options 设置中指定你想要的标志:

¥To enable flags in the VS Code ESLint Extension for the editor, specify the flags you’d like in the eslint.options setting in your settings.json file:

{
  "eslint.options": { "flags": ["flag_one", "flag_two"] }
}

要在 VS Code ESLint 扩展中为 lint 任务启用标志,请指定 eslint.lintTask.options 设置:

¥To enable flags in the VS Code ESLint Extension for a lint task, specify the eslint.lintTask.options settings:

{
  "eslint.lintTask.options": "--flag flag_one --flag flag_two ."
}