Skip to content

辅助功能

Biome Assist 提供一系列旨在提升代码质量和开发者体验的操作。

¥Biome Assist offers a series of actions meant to improve code quality and developer experience.

与代码检查器规则不同,辅助操作始终提供代码修复。它们可以对属性或字段进行排序、简化二进制表达式、执行重构等等。辅助操作并非旨在捕获错误或强制执行特定的编码风格。目前有 4 辅助操作可用。

¥Contrary to linter rules, assist actions always offer a code fix. They might sort properties or fields, simplify binary expressions, perform refactorings, and more. Assist actions are not meant to catch bugs or impose a particular coding style. There are currently 4 assist actions available.

Assist 代码修复通常可以安全应用。如果辅助修复导致你的代码出现问题,我们会将其视为错误,并欢迎你提交错误报告。

¥Assist code fixes are generally safe to apply. If an assist fix breaks your code, we would consider this a bug and appreciate bug reports.

Assist 在编辑器和 IDE 中效果最佳。即使使用 CLI,也可以强制使用辅助操作。辅助操作在语义上与 LSP 代码操作 非常接近,它们被划分到 groups 中。

¥Assist works best in editors and IDEs. However, it’s possible to enforce the use of assist actions even with the CLI. Assist actions are very close to LSP code actions in semantics, and they are divided in groups.

Biome 助手默认启用,并且一些规则包含在推荐规则集中。以下示例演示如何启用 useSortedKeys 操作:

¥Biome assist is enabled by default, and some rules are in the recommended rule set. The following example shows how to enable the useSortedKeys action:

biome.json
{
"assist": {
"enabled": true,
"actions": {
"source": {
"useSortedKeys": "on"
}
}
}
}

¥Use assist actions in your IDE

如果你使用的是兼容 LSP 的 IDE,则可以配置 Biome 在保存时执行特定操作。每个辅助操作都有一个名为 “代码操作” 的特定代码。虽然大多数名称遵循相同的模式,但可能存在一些例外(例如 organizeImports),因此请参阅每个操作的文档页面以了解相关的代码。

¥If you have an LSP-compatible IDE, then you can configure Biome to execute particular actions on save. Each assist action has a particular code called “code action”. While the majority of names follow the same pattern, there might be few exceptions (e.g. organizeImports), so refer to the documentation page of each action to learn to associated code.

首先,你需要设置编辑器,以便在保存时应用所有修复。配置会根据你的编辑器而变化。代码操作名称为 source.fixAll.biome

¥First, you need to setup your editor for apply all fixes on save. The configuration changes based on your editor. The code action name is source.fixAll.biome:

.vscode/settings.json
{
"editor.codeActionsOnSave": {
"source.fixAll.biome": "explicit",
}
}

之后,你可以添加每个操作的代码。例如,动作 useSortedKeys 有一个名为 source.action.useSortedKeys.biome 的代码动作。如果你使用 VSCode,可以复制此代码并将其粘贴到 editor.codeActionsOnSave 部分,Biome 将在你保存文档时应用此设置:

¥Then, you can add the code of each action. For example, the action useSortedKeys has a code action called source.action.useSortedKeys.biome. If you use VSCode, you can copy this code and place it in the editor.codeActionsOnSave section, and Biome will apply it when you save a document:

.vscode/settings.json
{
"editor.codeActionsOnSave": {
"source.action.useSortedKeys.biome": "explicit",
"source.fixAll.biome": "explicit"
}
}

¥Enforce assist actions via CLI

Assist 可以通过 CLI 使用 check 命令强制执行操作:

¥Assist actions can be enforced via CLI via check command:

npx @biomejs/biome check

check 旨在同时运行多个工具,因此如果你只想检查辅助操作,则应运行:

¥However, the check is meant for running multiple tools at once, so if you want to check only the assist actions, you should run:

npx @biomejs/biome check --formatter-enabled=false --linter-enabled=false

¥Don’t enforce assist

默认情况下,Biome 在运行 check 命令时会强制启用辅助功能。如果你不想强制使用辅助功能,可以使用 --enforce-assist CLI 标志来执行 false。这样,如果某些操作尚未执行,Biome 将不会发出诊断错误:

¥By default, Biome enforces assists when running the check command. If you wish to not enforce assist, you can use the --enforce-assist CLI flag to false. This way, Biome won’t emit a diagnostic error if some actions haven’t been applied:

Terminal window
biome check --enforce-assist=false

¥Suppression assist actions

你可以参考 抑制页面

¥You can refer to the suppression page.

¥Groups

¥Source

此组表示在保存文档时可以安全应用的操作。这些操作通常都是安全的,它们通常不会改变程序的功能。

¥This group represents those actions that can be safely applied to a document upon saving. These actions are all generally safe, they typically don’t change the functionality of the program.