Skip to content

Biome v2.0 测试版

The brand of the project. It says "Biome, toolchain of the web" The brand of the project. It says "Biome, toolchain of the web"

经过我们团队的辛勤工作,Biome 期待已久的 2.0 版本即将完成。它将包含许多重要功能,因此我们希望你能参与公开测试版的测试!

¥After hard work from our team, Biome’s long-awaited 2.0 release is nearing completion. It will be packed with many large features, so we would like your help testing it with a public beta!

如果你想试用,可以使用以下命令更新 Biome 并迁移你的配置:

¥If you would like to try it out, you can update Biome and migrate your configuration using the following commands:

Terminal window
npm install --save-dev --save-exact @biomejs/biome@beta
npx @biomejs/biome@beta migrate --write

此外,请确保使用我们 IDE 扩展的预发布版本。我们扩展程序的稳定版本尚未适配 Biome 2.0!

¥Also, make sure you use the prereleases of our IDE extensions. The stable versions of our extensions are not yet prepared for Biome 2.0!

即将发布的版本的文档可在 https://next.biomejs.dev/ 找到。

¥Documentation for the upcoming release can be found at https://next.biomejs.dev/.

¥New features

虽然最终的 2.0 版本在功能集上可能还会有一些细微的改动,但以下是你在 beta 版中可以期待的内容:

¥While the final 2.0 release may still have small changes in its final feature set, here’s what you can expect in the beta:

  • 插件:你可以使用 GritQL 编写自定义 lint 规则。

    ¥Plugins: You can write custom lint rules using GritQL.

  • 域名:域名有助于按技术、框架或域名对 lint 规则进行分组。得益于域名,你的默认推荐 lint 规则集将仅包含与你的项目相关的规则。

    ¥Domains: Domains help to group lint rules by technology, framework, or well, domain. Thanks to domains, your default set of recommended lint rules will only include those that are relevant to your project.

  • 多文件分析:Lint 规则现在可以基于其他文件的信息进行分析,从而支持 noImportCycles 等规则。

    ¥Multi-file analysis: Lint rules can now apply analysis based on information from other files, enabling rules such as noImportCycles.

  • noFloatingPromises:虽然仍处于概念验证阶段,但我们的第一个类型感知 lint 规则即将面世。

    ¥noFloatingPromises: Still a proof-of-concept, but our first type-aware lint rule is making an appearance.

  • 我们的导入管理器已进行了重大改进。

    ¥Our Import Organizer has seen a major revamp.

  • 辅助功能:Biome Assist 可以提供无需诊断的操作,例如对对象键进行排序。

    ¥Assists: Biome Assist can provide actions without diagnostics, such as sorting object keys.

  • 改进了抑制:使用 // biome-ignore-all 抑制整个文件中的规则,或使用 // biome-ignore-start// biome-ignore-end 抑制某个范围。

    ¥Improved suppressions: Suppress a rule in an entire file using // biome-ignore-all, or suppress a range using // biome-ignore-start and // biome-ignore-end.

  • HTML 格式化程序:HTML 格式化程序仍处于预览阶段,这是我们首次发布 HTML 格式化程序。 不过,它的能力足以摘取一些唾手可得的果实。

    ¥HTML formatter: Still in preview, this is the first time we ship an HTML formatter.

  • 修复了大量问题,新增了代码检查规则和其他改进。

    ¥Many, many, fixes, new lint rules, and other improvements.

¥Plugins

Biome 2.0 包含我们第一个版本的 代码检查插件

¥Biome 2.0 comes with our first iteration of Linter Plugins.

这些插件的功能仍然有限:它们允许匹配代码片段并报告其诊断信息。

¥These plugins are still limited in scope: They allow for matching code snippets and reporting diagnostics on them.

以下是一个插件示例,用于报告所有 Object.assign() 的使用情况:

¥Here is an example of a plugin that reports on all usages of Object.assign():

`$fn($args)` where {
$fn <: `Object.assign`,
register_diagnostic(
span = $fn,
message = "Prefer object spread instead of `Object.assign()`"
)
}

这只是第一步,我们有很多想法来增强它的功能,我们非常期待听到用户的意见,了解他们希望优先考虑哪些功能。

¥It’s a first step, but we have plenty of ideas for making them more powerful, and we’ll eagerly hear from our users on what they would like to see prioritised.

¥Domains

我们引入了一项新的代码检查功能:域名

¥We’ve introduced a new linter feature: Domains.

域名是一种按技术、框架或域名组织 lint 规则的新方法。目前,我们已确定了四个字段:

¥Domains are a new way to organise lint rules by technology, framework, or well, domain. Right now, we have identified four domains:

  • next:与 Next.js 相关的规则。

    ¥next: Rules related to Next.js.

  • react:与 React 相关的规则。

    ¥react: Rules related to React.

  • solid:与 Solid.js 相关的规则。

    ¥solid: Rules related to Solid.js.

  • test:与测试相关的规则,无论使用何种框架或库。

    ¥test: Rules related to testing, regardless of framework or library.

你可以同时启用和禁用属于同一域的规则:

¥You can enable and disable rules that belong to a domain together:

biome.jsonc
{
"linter": {
"domains": {
"test": "all", // all rules that belong to this domain are enabled
"react": "recommended", // only the recommended rules from this domain are enabled
"solid": "none" // rules related to Solid are disabled
}
}
}

但情况会好转:Biome 将自动检查你的 package.json,并确定哪些域应默认启用。例如,如果你将 react 定义为依赖之一,则 react 域的默认设置将自动变为 recommended

¥But it gets better: Biome will automatically inspect your package.json and determine which domains should be enabled by default. For instance, if you have react defined as one of your dependencies, the default setting for the react domain automatically becomes recommended.

这样,Biome 推荐的规则集应该最符合你的特定项目需求。

¥This way, Biome’s total set of recommended rules should be most relevant to your specific project needs.

最后,域可以将全局变量添加到 javascript.globals 设置中。这将使 Biome 的设置更加简便。

¥And finally, domains can add global variables to the javascript.globals setting. This should make Biome even easier to setup.

¥Multi-file analysis

在 2.0 版本之前,Biome lint 规则一次只能处理一个文件。这让我们取得了很大进展,但许多更有趣的规则还需要来自其他文件的信息。

¥Before version 2.0, Biome lint rules could only operate on one file at a time. This brought us far, but many of the more interesting rules require information from other files too.

为此,我们在 Biome 中添加了一个文件扫描器,它会扫描项目中的所有文件并对其进行索引,类似于 IDE 中的 LSP 服务。我们开门见山地说:扫描项目导致 Biome 在许多项目中运行速度变慢。我们相信多文件分析功能非常值得。如果没有扫描器,多文件分析的速度会更慢,因为规则需要单独执行临时文件系统访问。

¥To accomplish this, we have added a file scanner to Biome that scans all the files in your project and indexes them, similar to what an LSP service might do in your IDE. We’re not going to beat around the bush: Scanning projects means that Biome has become slower for many projects. But we do believe the ability to do multi-file analysis is worth it. And without a scanner, multi-file analysis would become even slower, as rules would need to perform ad-hoc file system access individually.

尽管如此,这仍然是 beta 版,我们的扫描器及其性能还有很大的提升空间。如果你发现某个代码库的性能变得无法接受,请联系我们并 提交问题

¥That said, this is a beta, and there are certainly more opportunities to improve our scanner and its performance. If you have a repository where you feel our performance became unacceptably slow, please reach out and file an issue.

目前,我们有一些有趣的规则可以利用我们的多文件分析功能:

¥For now, we have a few interesting rules that can make use of our multi-file analysis:

  • noImportCycles 能够检查导入语句并检测它们之间的循环引用。

    ¥noImportCycles is able to look at import statements and detect cycles between them.

  • noPrivateImports 是基于 Biome 1.x 中的 useImportRestrictions 子文件夹规则,并受 ESLint 的 plugin-import-access 启发而新增的规则。禁止从其他模块导入带有 @private JSDoc 标签的符号,并且禁止在导入文件不在同一文件夹或其子文件夹中时导入带有 @package 标签的符号。

    ¥noPrivateImports is a new rule based on the useImportRestrictions nursery rule from Biome 1.x, and inspired by ESLint’s plugin-import-access. It forbids importing symbols with an @private JSDoc tag from other modules, and forbids importing symbols with an @package tag if the importing file is not in the same folder or one of its subfolders.

  • useImportExtensions 已得到改进,因为它现在可以确定导入时需要使用的实际扩展名,而不是基于启发式进行猜测。

    ¥useImportExtensions has been improved because it can now determine the actual extension that needs to be used for an import, instead of guessing based on heuristics.

最后,我们在设计多文件分析时也考虑到了单体仓库(monorepo)。虽然完整的 monorepo 支持可能无法在 2.0 版本发布时实现,但我们预计很快就能在这方面做出更多改进。

¥Finally, we’ve also designed the multi-file analysis with monorepos in mind. While full monorepo support may not make it in time for the 2.0 release, we expect to be able to deliver more on this front soon.

Biome 的代码检查工具一直致力于提供开箱即用的代码检查方案。这意味着我们的目标不仅是取代 ESLint,还要取代它的插件。typescript-eslint 是最难替换的插件之一。

¥With Biome’s linter we have always strived to provide a battery-included approach to linting. This means we’re not just aiming to replace ESLint, but also its plugins. One of the hardest plugins to replace is typescript-eslint.

Biome 已经采用了一些 typescript-eslint 的规则,但我们无法完全替换所有规则,因为它们的分析依赖于类型信息。为了获取类型信息,typescript-eslint 依赖于 tsc 本身,这相当慢,而且也使设置变得复杂。

¥Biome has featured some rules from typescript-eslint for a while now, but we could never replace all rules, because they relied on type information for their analysis. And in order to get type information, typescript-eslint relies on tsc itself, which is rather slow and also complicates setup.

这种情况即将改变。Biome 2.0 首次引入了 noFloatingPromises 规则,这是用户呼声最高的规则之一,它依赖于类型信息。公平地说,我们现在不应将其视为概念验证,因为它的功能存在一些明显的局限性:

¥This is about to change. With Biome 2.0, we’re introducing a first version of the noFloatingPromises rule, one of the most-requested rules that relies on type information. In fairness, we should not consider it more than a proof-of-concept right now, because there are some notable limitations to its capabilities:

  • 目前尚不支持复杂类型。

    ¥It doesn’t understand complex types yet.

  • 目前尚不支持类型推断。

    ¥It cannot do type inference yet.

  • 目前它只能分析出现在同一文件中的类型。

    ¥It can currently only analyse types that occur in the same file.

尽管如此,它的功能足以解决一些容易解决的问题。请看以下代码片段:

¥Still, its capabilities are sufficient to catch some of the low-hanging fruit. Consider this small snippet:

example.js
async function returnsPromise() { /* ... */ }
returnsPromise().then(() => {});

它将触发以下诊断信息:

¥It will trigger the following diagnostic:

example.js:3:1 lint/nursery/noFloatingPromises ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ A “floating” Promise was found, meaning it is not properly handled and could lead to ignored errors or unexpected behavior.
1 │ async function returnsPromise() { /* ... */ }
2 │
> 3 │ returnsPromise().then(() => {});
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5 │
ℹ This happens when a Promise is not awaited, lacks a .catch or .then rejection handler, or is not explicitly ignored using the void operator.

正如你所料,我们计划随着时间的推移扩展此规则的功能。有了新的多文件分析功能,我们预计能够在这方面取得显著进展。敬请关注更多相关公告!

¥As you can guess, we intend to expand this rule’s capabilities over time. And with our new multi-file analysis in place, we expect to be able to make serious strides with this. Stay tuned for more announcements on this front!

¥Import Organizer Revamp

在 Biome 1.x 中,我们的导入管理器存在一些限制:

¥In Biome 1.x, our Import Organizer had several limitations:

  • 导入或导出的分组将被视为独立的数据块,这意味着它们将独立排序。这意味着以下操作未按预期运行:

    ¥Groups of imports or exports would be considered separate chunks, meaning they would be sorted independently. This meant the following didn’t work as expected:

    example.js
    import { lib2 } from "library2";
    import { util } from "./utils.js";
    import { lib1 } from "library1";

    它可以正确地将 "library1" 排序到 "./utils.js" 的上方,但无法将其移到换行符的顶部。我们得到了……这段代码:

    ¥It would correctly sort "library1" to be placed above "./utils.js", but it wouldn’t be able to carry it over the newline to the top. What we got was this:

    organizer_v1.js
    import { lib2 } from "library2";
    import { lib1 } from "library1";
    import { util } from "./utils.js";

    但我们真正想要的是:

    ¥But instead, what we really wanted was this:

    organizer_v2.js
    import { lib1 } from "library1";
    import { lib2 } from "library2";
    import { util } from "./utils.js";
  • 来自同一模块的不同导入不会被合并。请看以下示例:

    ¥Separate imports from the same module wouldn’t be merged. Consider the following example:

    example.js
    import { util1 } from "./utils.js";
    import { util2 } from "./utils.js";

    这些导入语句不会被合并,而我们想要的是:

    ¥Nothing would be done to merge these import statements, whereas what we would have wanted was this:

    organizer_v2.js
    import { util1, util2 } from "./utils.js";
  • 无法配置自定义排序。也许你并不喜欢默认的按源文件中的 “distance” 排序的方式。也许你想这样组织代码:

    ¥No custom ordering could be configured. Maybe you didn’t really like the default approach of ordering by “distance” from the source file that you’re importing from. Maybe you wanted to organise like this:

    organizer_v2.js
    import { open } from "node:fs";
    import { internalLib1 } from "@company/library1";
    import { internalLib2 } from "@company/library2";
    import { lib1 } from "library1";

在 Biome 2.0 中,所有这些限制都已解除。实际上,如果你查看上面的示例,所有标记为 organizer_v2.js 的代码片段都可以由我们新的导入管理器轻松生成。

¥In Biome 2.0, all these limitations are lifted. In fact, if you look at the examples above, all snippets labeled organizer_v2.js can be produced just like that by our new import organizer.

其他改进包括支持组织 export 语句、支持使用 “detached” 注释在必要时显式分隔导入块,以及导入属性排序。

¥Other improvements include support for organizing export statements, support for “detached” comments for explicitly separating import chunks if necessary, and import attribute sorting.

你可以在 https://next.biomejs.dev/assist/actions/organize-imports/ 中找到有关新导入管理器的文档。

¥You can find the documentation on the new import organizer at https://next.biomejs.dev/assist/actions/organize-imports/.

¥Assists

在 Biome 中,导入管理器一直是一个比较特殊的案例。它既不是代码检查器的一部分,也不是格式化程序的一部分。这是因为我们不希望它像代码检查器那样显示诊断信息,而它的组织功能超出了我们对格式化程序的预期。

¥The Import Organizer was always a bit of a special case in Biome. It was neither part of the linter, nor of the formatter. This was because we didn’t want it to show diagnostics the way the linter does, while its organizing features went beyond what we expect from the formatter.

在 Biome 2.0 中,我们以 Biome Assist 的形式对这些用例进行了通用化处理。辅助功能旨在提供类似于代码检查规则中修复的操作,但不包含诊断信息。

¥In Biome 2.0, we have generalised such use cases in the form of Biome Assist. The assist is meant to provide actions, which are similar to the fixes in lint rules, but without the diagnostics.

导入管理器已升级为辅助功能,但我们也开始对其他辅助功能采用这种方法:useSortedKeys 可以对对象字面量中的键进行排序,而 useSortedAttributes 可以对 JSX 中的属性进行排序。

¥The Import Organizer has become an assist, but we’ve started using this approach for new assists too: useSortedKeys can sort keys in object literals, while useSortedAttributes can sort attributes in JSX.

有关辅助功能的更多信息,请参阅:https://next.biomejs.dev/assist/

¥For more information about assists, see: https://next.biomejs.dev/assist/

¥Improved suppressions

除了我们之前支持的 // biome-ignore 注释之外,现在我们还支持 // biome-ignore-all,用于禁止对整个文件执行 lint 规则或格式化程序。

¥In addition to the // biome-ignore comments we already supported, we now support // biome-ignore-all for suppressing a lint rule or the formatter in an entire file.

我们还添加了对使用 // biome-ignore-start// biome-ignore-end 设置抑制范围的支持。请注意,如果你希望范围运行到文件末尾,则 // biome-ignore-end 是可选的。

¥We also added support for suppression ranges using // biome-ignore-start and // biome-ignore-end. Note that // biome-ignore-end is optional in case you want to let a range run until the end of the file.

有关抑制规则的更多信息,请参阅:https://next.biomejs.dev/linter/#suppress-lint-rules

¥For more information about suppressions, see: https://next.biomejs.dev/linter/#suppress-lint-rules

¥HTML formatter

经过几个月的努力,我们很高兴地宣布 HTML 格式化程序现已准备就绪,供用户试用并开始报告错误!这是 Biome 向全面支持 Vue 和 Svelte 等框架中使用的类 HTML 模板语言迈出的重要一步。

¥After a few months of hard work, we are happy to announce that the HTML formatter is now ready for users to try out and start reporting bugs! This is a huge step towards Biome fully supporting HTML-ish templating languages used in frameworks like Vue and Svelte.

HTML 格式化程序目前仅处理实际的 .html 文件,因此暂不支持 .vue.svelte 文件中的 HTML 格式化。目前它还不支持格式化嵌入式语言,例如 JavaScript 或 CSS。HTML 的选项,例如 attributePositionbracketSameLinewhitespaceSensitivity,已经实现。

¥The HTML formatter only touches actual .html files for now, so no formatting of html in .vue or .svelte files yet. It also won’t format embedded languages like JavaScript or CSS yet. HTML’s options like attributePosition, bracketSameLine, and whitespaceSensitivity have been implemented.

HTML 格式化程序仍处于实验阶段,因此在正式版 2.0 中默认禁用。截至撰写本文时,Biome 能够解析 Prettier 的绝大多数 HTML 测试,并正确格式化其中 124 个测试中的 46 个。尽管目前还不能完全匹配 Prettier,但我们相当有信心它能够输出格式正确的文档,且不会破坏任何内容。如果你发现任何不适用的情况,请 请告知我们

¥The HTML formatter is still pretty experimental, so it will remain disabled by default for the full 2.0 release. At the time of writing, Biome is able to parse the grand majority of Prettier’s HTML tests, and format 46/124 of them correctly. Despite not matching Prettier yet, we’re pretty confident that it should output documents that are formatted adequately without destroying anything. If you find a case where it doesn’t, please let us know!

你可以通过在配置文件中添加以下内容来启用 HTML 格式化程序:

¥You can enable the HTML formatter by adding the following to your config file:

{
"html": {
"formatter": {
"enabled": true
}
}
}

¥New rules

自 v1.9 版本以来,新增了以下几条规则:

¥Several new rules have added since v1.9:

¥Miscellaneous

  • BREAKING:配置字段 includeignore 已被替换为单个字段 includes

    ¥BREAKING: The configuration fields include and ignore have been replaced with a single includes field.

  • BREAKING:修改了一些推荐规则,使其不那么繁琐和阻塞。如果你的项目依赖这些规则在违规时阻止持续集成 (CI),则此变更将导致重大问题。如果你使用了 migrate 命令,则行为应与之前相同。

    ¥BREAKING: Reworked some recommended rules recommended to be less pedantic and blocking. This is a breaking change if your project relied on those rules to block the CI in case of violations. If you used the migrate command, the behaviour should remain as before.

  • BREAKING:不再推荐使用 style 规则。如果你使用了 migrate 命令,则行为应与之前相同。

    ¥BREAKING: The style rules aren’t recommended anymore. If you used the migrate command, the behaviour should remain as before.

  • BREAKING:移除已弃用的规则:

    ¥BREAKING: Removed deprecated rules:

    • noConsoleLog

    • noInvalidNewBuiltin

    • noNewSymbol

    • useShorthandArrayType

    • useSingleCaseStatement

  • BREAKING:许多已弃用的选项(包括一些仍然引用旧 Rome 名称的选项)已被移除。

    ¥BREAKING: Many deprecated options, including some that still referenced the old Rome name, have been removed.

  • 新增选项 javascript.parser.jsxEverywhere,用于控制 Biome 是否应在 .js/.mjs/.cjs 文件中使用 JSX 语法:

    ¥Added a new option javascript.parser.jsxEverywhere to control whether Biome should expect JSX syntax in .js/.mjs/.cjs files.

  • 改进了 monorepo 支持:规则 noUndeclaredDependencies 现在在单体仓库中也能正常工作,它会使用最近的 package.json 文件,而不仅仅是根文件。

    ¥Improved monorepo support: The rule noUndeclaredDependencies now works correctly in monorepos by using the nearest package.json file, instead of only the root one.

  • 我们已默认启用对 .editorconfig 文件的支持。

    ¥We have enabled support for .editorconfig files by default.

  • 更改了 package.json 的默认格式,使其与包管理器的格式更加一致。

    ¥Changed default formatting of package.json to align better with formatting by package managers.

¥And more!

完整的变更列表请参阅我们的 changelog 文件。

¥For the full list of changes, please refer to our changelog.