Skip to content

格式化程序

Biome 是一个有态度的 支持多种语言 格式化程序。它遵循类似的 Prettier 的理念,仅支持一些选项,以避免对样式的争论,从而变成对 Biome 选项的争论。它故意 抵制添加新选项的冲动 以防止团队中的 bike-shed 讨论,这样他们就可以专注于真正重要的事情。

¥Biome is an opinionated formatter that supports multiple languages. It follows a similar philosophy to Prettier, only supporting a few options to avoid debates over styles, turning into debates over Biome options. It deliberately resists the urge to add new options to prevent bike-shed discussions in teams so they can focus on what really matters instead.

以下命令检查 src 目录中文件的格式。如果它发现未格式化的代码,它会发出文本差异。

¥The following command checks the formatting of the files in the src directory. It emits text differences if it finds code that is not formatted.

Terminal window
npx @biomejs/biome format ./src

如果你想应用新格式,请传递 --write 选项:

¥If you want to apply the new formatting, pass the --write option:

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

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

¥The command accepts a list of files and directories.

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

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

¥Options

Biome 提供了一些选项来调整其格式化程序的行为。与其他工具不同,Biome 将语言无关选项与语言特定选项分开。

¥Biome provides some options to tune the behavior of its formatter. Differently from other tools, Biome separates language-agnostic options from language-specific options.

可以在 CLI 上或通过 Biome 配置文件 设置格式化程序选项。Biome 不支持 .editorconfig yet

¥The formatter options can be set on the CLI or via a Biome configuration file. Biome doesn’t support .editorconfig yet.

建议使用 Biome 配置文件 以确保 Biome CLI 和 Biome LSP 都应用相同的选项。应用以下默认值:

¥It’s recommended to use a Biome configuration file to ensure that both the Biome CLI and the Biome LSP apply the same options. The following defaults are applied:

biome.json
{
"formatter": {
"enabled": true,
"formatWithErrors": false,
"ignore": [],
"attributePosition": "auto",
"indentStyle": "tab",
"indentWidth": 2,
"lineWidth": 80,
"lineEnding": "lf"
},
"javascript": {
"formatter": {
"arrowParentheses":"always",
"bracketSameLine": false,
"bracketSpacing": true,
"jsxQuoteStyle": "double",
"quoteProperties": "asNeeded",
"semicolons": "always",
"trailingCommas": "all"
}
},
"json": {
"formatter": {
"trailingCommas": "none"
}
}
}

Biome 格式化程序支持的主要语言无关选项是:

¥The main language-agnostic options supported by the Biome formatter are:

  • 缩进样式(默认值:tab):使用空格或制表符进行缩进;

    ¥indent style (default: tab): Use spaces or tabs for indention;

  • 缩进宽度(默认值:2):每个缩进级别的空格数。

    ¥indent width (default: 2): The number of spaces per indention level.

  • 线宽(默认值:80):Biome 封装代码的列宽;

    ¥line width (default: 80): The column width at which Biome wraps code;

有关更多详细信息,请参阅 配置参考

¥See the configuration reference for more details.

¥Ignore Code

有时格式化的代码并不理想。

¥There are times when the formatted code isn’t ideal.

对于这些情况,你可以使用格式抑制注释:

¥For these cases, you can use a format suppression comment:

example.js
// biome-ignore format: <explanation>

示例:

¥Example:

example.js
const expr =
// biome-ignore format: the array should not be formatted
[
(2 * n) / (r - l),
0,
(r + l) / (r - l),
0,
0,
(2 * n) / (t - b),
(t + b) / (t - b),
0,
0,
0,
-(f + n) / (f - n),
-(2 * f * n) / (f - n),
0,
0,
-1,
0,
];

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

¥Biome doesn’t provide ignore comments that ignore an entire file. However, you can ignore a file using the Biome configuration file.