跳转到内容

格式化程序

Biome是一款具有停止所有关于样式的持续辩论的目标的主观格式化程序。它遵循类似于Prettier的哲学,只支持少量选项,以避免关于样式的辩论转变为关于Biome选项的辩论。它故意抵制添加新选项的冲动,以防止团队中的琐事讨论,以便他们可以专注于真正重要的事情。

Biome支持的与语言无关的选项有:

  • 缩进样式(默认为tab):使用空格或制表符进行缩进
  • 制表符宽度(默认为2):每个缩进级别的空格数
  • 行宽(默认为80):Biome在此列宽处换行代码

还有其他针对特定语言的格式化选项。有关详细信息,请参阅配置选项。

默认情况下,格式化程序检查代码并在格式化发生更改时发出诊断:

Terminal window
npx @biomejs/biome format ./src

如果要应用新的格式化,请使用--write选项:

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

使用--help标志来了解可用选项:

Terminal window
npx @biomejs/biome format --help

或者查看CLI参考部分

您可以使用biome.json配置Biome。应用以下默认值:

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

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

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

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

示例:

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,
];