Skip to content

Linter

Biome 的 linter 会静态分析你的代码以查找和修复常见错误,并帮助你编写更好的现代代码。它 支持多种语言 并提供总共 274 规则

¥Biome’s linter statically analyzes your code to find and fix common errors and to help you write better, modern code. It supports multiple languages and provides a total of 274 rules.

¥Rules

linter 被组织成规则。当规则遇到不符合其要求的代码时,它会发出诊断。例如,noDebugger 规则报告 JavaScript 代码中 debugger 指令的使用情况。

¥The linter is organized into rules. A rule emits a diagnostic when it encounters a code that doesn’t meet its requirements. For example, the noDebugger rule reports the use of the debugger instruction in JavaScript code.

规则发出严重性为 infowarnerror 的诊断。严重性为 error 的诊断会导致命令以非零代码退出,而严重性为 infowarn 的诊断不会导致命令失败。

¥A rule emits diagnostics with a info, warn or error severity. Diagnostics with an error severity cause the command to exit with a non-zero code, While diagnostics with a info or a warn severity don’t cause the command to fail.

你可以使用 --error-on-warnings 选项导致发出 warn 诊断的命令失败:

¥You can cause a command that emits warn diagnostics to fail by using the --error-on-warnings option:

Terminal window
biome lint --error-on-warnings ./src

默认情况下,Biome linter 仅运行 推荐规则。要禁用所有规则,你可以在 Biome 配置文件中禁用推荐的规则。当你只想启用一些规则时,这可能很有用。推荐的规则发出具有 error 严重性的诊断。

¥By default, the Biome linter only runs the recommended rules. To disable all rules, you can disable the recommended rules in your Biome configuration file. This may be useful in cases when you only want to enable a few rules. The recommended rules emit diagnostics with the error severity.

规则分为几组。例如,noDebugger 规则是 suspicious 的一部分。此组中的规则检测可能不正确或无用的代码。每个组的描述可以在 规则页面 上找到。

¥The rules are divided into groups. For example, the noDebugger rule is part of the suspicious group. Rules from this group detect code that is likely to be incorrect or useless. The description of each group can be found on the rules page.

与其他 linters 不同,我们不提供任何检查代码格式的规则。这种检查由我们的 代码格式化程序 涵盖。

¥Unlike other linters, we don’t provide any rules that check for code formatting. This kind of checking is covered by our code formatter.

许多规则提供了可以自动应用的代码修复。Biome 区分安全和不安全的代码修复。

¥Many rules provide a code fix that can be automatically applied. Biome distinguishes between safe and unsafe code fixes.

¥Safe fixes

安全修复保证不会更改代码的语义。它们可以在没有明确审查的情况下应用。

¥Safe fixes are guaranteed to not change the semantic of your code. They can be applied without explicit review.

要应用安全修复,请使用 --write

¥To apply safe fixes, use --write:

Terminal window
npx @biomejs/biome lint --write ./src

¥Unsafe fixes

不安全的修复可能会改变程序的语义。因此,建议手动检查更改。

¥Unsafe fixes may change the semantic of your program. Therefore, it’s advised to manually review the changes.

要应用安全修复和不安全修复,请使用 --write --unsafe

¥To apply both safe fixes and unsafe fixes, use --write --unsafe:

Terminal window
npx @biomejs/biome lint --write --unsafe ./src

¥Rule pillars

我们认为规则应该是信息丰富的,并向用户解释触发规则的原因,并告诉用户他们应该做什么来修复错误。规则应遵循以下支柱:

¥We believe that rules should be informative and explain to the user why a rule is triggered and tell the user what they should to do fix the error. A rule should follow these pillars:

  1. 向用户解释错误。通常,这是诊断的消息。

    ¥Explain to the user the error. Generally, this is the message of the diagnostic.

  2. 向用户解释触发错误的原因。通常,这是使用附加节点实现的。

    ¥Explain to the user why the error is triggered. Generally, this is implemented with an additional node.

  3. 告诉用户他们应该做什么。通常,这是使用代码操作实现的。如果代码操作不适用,则应在注释中告诉用户他们应该做什么来修复错误。

    ¥Tell the user what they should do. Generally, this is implemented using a code action. If a code action is not applicable a note should tell the user what they should do to fix the error.

如果你认为规则不遵循这些支柱,请使用 打开问题

¥If you think a rule doesn’t follow these pillars, please open an issue.

以下命令对 src 目录中的所有文件运行 linter:

¥The following command runs the linter on all files in the src directory:

Terminal window
npx @biomejs/biome lint ./src

该命令接受文件和目录列表。

¥The command accepts a list of files and directories.

有关所有可用选项的更多信息,请查看 CLI 参考

¥For more information about all the available options, check the CLI reference.

¥Skip a rule or a group

自版本 v1.8.0 以来,命令 biome lint 接受选项 --skip,允许禁用属于组的一个或多个规则。

¥Since version v1.8.0, the command biome lint accepts an option --skip that allows to disable a rule or rules that belong to a group.

例如,以下命令跳过属于 style 组和 suspicious/noExplicitAny 规则的所有规则:

¥For example, the following command skips all the rules that belong to the style group and the suspicious/noExplicitAny rule:

Terminal window
biome lint --skip=style --skip=suspicious/noExplicitAny

¥Run a rule or a group

自版本 v1.8.0 以来,命令 biome lint 接受选项 --only,允许你运行单个规则或属于组的规则。

¥Since version v1.8.0, the command biome lint accepts an option --only that allows you to run a single rule or the rules that belong to a group.

例如,以下命令仅运行规则 style/useNamingConvention、规则 style/noInferrableTypes 和属于 a11y 的规则。如果在配置中禁用规则,则其严重性级别设置为推荐规则的 error,否则设置为 warn

¥For example, the following command runs only the rulestyle/useNamingConvention, the rule style/noInferrableTypes and the rules that belong to a11y. If the rule is disabled in the configuration, then its severity level is set to error for a recommended rule or warn otherwise.

Terminal window
biome lint --only=style/useNamingConvention --only=style/noInferrableTypes --only=a11y

¥Configuration

可以根据你的需要配置规则。

¥A rule can be configured based on your needs.

¥Disable a rule

无论规则的严重性是 errorwarn 还是 info,都会启用规则。你可以使用 off 关闭规则。

¥A rule is enabled whether its severity is error, warn or info. You can turn off a rule with off.

以下配置禁用推荐的 noDebugger 规则并启用 noShoutyConstantsuseNamingConvention 规则。

¥The following configuration disables the recommended noDebugger rule and enables the noShoutyConstants and useNamingConvention rules.

warn 严重性在进行重构并且需要使 CI 通过的情况下很有用。诊断消息为黄色。当触发使用 warn 配置的规则时,你可以使用 --error-on-warnings 退出并显示错误代码。

¥The warn severity is useful in cases where there’s a refactor going on and there’s a need to make the CI pass. The diagnostic message is yellow. You can use --error-on-warnings to exit with an error code when a rule configured with warn is triggered.

即使传递了 --error-on-warningsinfo 严重性也不会影响 CLI 的退出状态代码。诊断消息颜色为蓝色。

¥The info severity won’t affect the exit status code of the CLI, even when --error-on-warnings is passed. The diagnostic message color is blue.

biome.json
{
"linter": {
"rules": {
"suspicious": {
"noDebugger": "off",
"noConsoleLog": "info"
},
"style": {
"noShoutyConstants": "warn",
"useNamingConvention": "error"
}
}
}
}

¥Configure the rule fix

自版本 v1.8.0 以来,可以使用选项 fix 配置修复的实体。有三个选项:

¥Since version v1.8.0, it’s possible to configure the entity of a fix, using the option fix. There are three options:

  • none:该规则不会发出代码修复;

    ¥none: the rule won’t emit a code fix;

  • safe:该规则将发出 安全修复

    ¥safe: the rule will emit a safe fix;

  • unsafe:该规则将发出 不安全修复

    ¥unsafe: the rule will emit an unsafe fix;

biome.json
{
"linter": {
"rules": {
"correctness": {
"noUnusedVariables": {
"level": "error",
"fix": "none"
}
},
"style": {
"useConst": {
"level": "warn",
"fix": "unsafe"
},
"useTemplate": {
"level": "warn",
"fix": "safe"
}
}
}
}
}

¥Rule options

一些规则有选项。你可以通过不同地塑造规则的值来设置它们。

¥A few rules have options. You can set them by shaping the value of the rule differently.

  • level 将指示诊断的严重性;

    ¥level will indicate the severity of the diagnostic;

  • options 将根据规则进行更改。

    ¥options will change based on the rule.

biome.json
{
"linter": {
"rules": {
"style": {
"useNamingConvention": {
"level": "error",
"options": {
"strictCase": false
}
}
}
}
}
}

¥Ignore code

有时开发者想要忽略特定代码行的 lint 规则。你可以通过在发出 lint 诊断的行上方添加抑制注释来实现这一点。

¥There are times when a developer wants to ignore a lint rule for a specific line of the code. You can achieve this by adding a suppression comment above the line that emits the lint diagnostic.

抑制注释具有以下格式:

¥Suppression comments have the following format:

// biome-ignore lint: <explanation>
// biome-ignore lint/suspicious/noDebugger: <explanation>

其中

¥Where

  • biome-ignore 是抑制注释的开始;

    ¥biome-ignore is the start of a suppression comment;

  • lint 抑制 linter;

    ¥lint suppresses the linter;

  • /suspicious/noDebugger:可选,要抑制的规则的组和名称;

    ¥/suspicious/noDebugger: optional, group and name of the rule you want to suppress;

  • <explanation> 解释禁用该规则的原因

    ¥<explanation> explanation why the rule is disabled

这是一个例子:

¥Here’s an example:

// biome-ignore lint: reason
debugger;
// biome-ignore lint/suspicious/noDebugger: reason
debugger;

Biome 不提供忽略整个文件的忽略注释。但是,你可以 使用 Biome 配置文件忽略文件。请注意,你也可以使用 忽略 VCS 忽略的文件

¥Biome doesn’t provide ignore comments that ignore an entire file. However, you can ignore a file using the Biome configuration file. Note that you can also ignore the files ignored by your VCS.

¥Migrate from other linters

许多 Biome lint 规则都受到其他 linter 的启发。如果你想从其他 linters(例如 ESLint 或 typescript-eslint)迁移,请检查 规则来源页面 如果你要从 ESLint 迁移,我们有专用的 迁移指南

¥Many of Biome lint rules are inspired from other linters. If you want to migrate from other linters such as ESLint or typescript-eslint, check the rules sources page If you are migrating from ESLint, we have a dedicated migration guide.