功能标志
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:
- 当某个功能是实验性的并且尚未准备好为所有人启用时。
- 当某个功能是重大更改时,将在下一个主要版本中正式合并,但用户可以在下一个主要版本之前选择加入该行为。
标志前缀
🌐 Flag Prefixes
标志的前缀表示其状态:
🌐 The prefix of a flag indicates its status:
unstable_表示该功能是实验性的,具体实现可能在功能稳定之前发生变化。这是一个“自行承担风险”的功能。v##_表示该功能已稳定,并将在下一个主要版本中提供。例如,v10_some_feature表示这是一个破坏性更改,将在 ESLint v10.0.0 正式发布。这些标志会在每个主要版本中移除,进一步使用它们会导致错误。
如果某项功能是非重大更改,则它可能会从不稳定状态变为默认启用状态,而无需发布主要版本。
🌐 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.
- 当该功能稳定时
- 如果默认启用该功能会导致破坏性更改,将添加一个新的
v##_标志为激活状态,而unstable_标志变为非激活状态。进一步使用unstable_标志会自动启用v##_标志,但会发出警告。 - 否则,该功能默认启用,并且
unstable_标志变为不活动状态。进一步使用unstable_标志会发出警告。
- 如果默认启用该功能会导致破坏性更改,将添加一个新的
- 如果该功能被弃用,
unstable_标志将变为非激活状态。再次使用它会抛出错误。 - 每个主要版本都会移除所有不活跃的
unstable_标志,进一步使用它们会抛出错误。
活跃标志
🌐 Active Flags
以下标志目前可在 ESLint 中使用。
🌐 The following flags are currently available for use in ESLint.
| 标志 | 描述 |
|---|---|
unstable_native_nodejs_ts_config | Use native Node.js to load TypeScript configuration. |
非活跃标志
🌐 Inactive Flags
当前没有非活跃标志。
🌐 There are currently no inactive flags.
如何使用功能标志
🌐 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
使用环境变量启用功能标志
🌐 Enable Feature Flags with Environment Variables
你还可以使用 ESLINT_FLAGS 环境变量设置功能标志。可以将多个标志指定为逗号分隔的列表,并与通过 CLI 或 API 传递的任何标志合并。例如,以下是如何将功能标志添加到你的 .bashrc 或 .bash_profile 文件的示例:
🌐 You can also set feature flags using the ESLINT_FLAGS environment variable. Multiple flags can be specified as a comma-separated list and are merged with any flags passed on the CLI or in the API. For example, here’s how you can add feature flags to your .bashrc or .bash_profile files:
export ESLINT_FLAGS="flag_one,flag_two"
此方法在 CI/CD 流水线中或你想要在多个 ESLint 命令中启用相同标志时特别有用。
🌐 This approach is especially useful in CI/CD pipelines or when you want to enable the same flags across multiple ESLint commands.
使用 API 启用功能标志
🌐 Enable Feature Flags with the API
在使用 API 时,你可以将 flags 数组传递给 ESLint 和 Linter 类:
🌐 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 ."
}