功能标志

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, and further use of them throws an error.

如果某项功能是非重大更改,则它可能会从不稳定状态变为默认启用状态,而无需发布主要版本。

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

以下政策适用于 unstable_ 标志。

¥The following policies apply to unstable_ flags.

  • 当该功能稳定时

    ¥When the feature is stabilized

    • 如果默认启用该功能会造成重大更改,则会添加新的 v##_ 标志作为活动标志,并且 unstable_ 标志将变为非活动标志。继续使用 unstable_ 标志会自动启用 v##_ 标志,但会发出警告。

      ¥If enabling the feature by default would be a breaking change, a new v##_ flag is added as active, and the unstable_ flag becomes inactive. Further use of the unstable_ flag automatically enables the v##_ flag but emits a warning.

    • 否则,该功能将默认启用,并且 unstable_ 标志将变为非活动标志。继续使用 unstable_ 标志会发出警告。

      ¥Otherwise, the feature is enabled by default, and the unstable_ flag becomes inactive. Further use of the unstable_ flag emits a warning.

  • 如果放弃该功能,unstable_ 标志将变为非活动标志。继续使用它会引发错误。

    ¥If the feature is abandoned, the unstable_ flag becomes inactive. Further use of it throws an error.

  • 每次发布主要版本时,所有非活动 unstable_ 标志都会被删除,继续使用它们会引发错误。

    ¥All inactive unstable_ flags are removed each major release, and further use of them throws an error.

活跃标志

¥Active Flags

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

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

标志 描述
unstable_config_lookup_from_fileLook up `eslint.config.js` from the file being linted.

非活跃标志

¥Inactive Flags

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

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

标志 描述 不活动原因
unstable_ts_configEnable TypeScript configuration files. This feature is now enabled by default.

如何使用功能标志

¥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:

npm

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

yarn

yarn dlx eslint --flag flag_one --flag flag_two file.js 

pnpm

pnpm dlx eslint --flag flag_one --flag flag_two file.js 

bun

bunx 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 ."
}