格式化程序
Biome是一款具有停止所有关于样式的持续辩论的目标的主观格式化程序。它遵循类似于Prettier的哲学,只支持少量选项,以避免关于样式的辩论转变为关于Biome选项的辩论。它故意抵制添加新选项的冲动,以防止团队中的琐事讨论,以便他们可以专注于真正重要的事情。
Biome支持的与语言无关的选项有:
- 缩进样式(默认为
tab):使用空格或制表符进行缩进 - 制表符宽度(默认为
2):每个缩进级别的空格数 - 行宽(默认为
80):Biome在此列宽处换行代码 
还有其他针对特定语言的格式化选项。有关详细信息,请参阅配置选项。
使用CLI格式化程序
Section titled “使用CLI格式化程序”默认情况下,格式化程序检查代码并在格式化发生更改时发出诊断:
npx @biomejs/biome format ./srcpnpm exec biome format ./srcbunx --bun biome format ./srcdeno run -A npm:@biomejs/biome format ./srcyarn exec biome format ./src如果要应用新的格式化,请使用--write选项:
npx @biomejs/biome format --write ./srcpnpm exec biome format --write ./srcbunx --bun biome format --write ./srcdeno run -A npm:@biomejs/biome format --write ./srcyarn exec biome format --write ./src使用--help标志来了解可用选项:
npx @biomejs/biome format --helppnpm exec biome format --helpbunx --bun biome format --helpdeno run -A npm:@biomejs/biome format --helpyarn exec biome format --help或者查看CLI参考部分。
您可以使用biome.json来配置Biome。应用以下默认值:
{  "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",      "trailingCommas": "all"    }  },  "json": {    "formatter": {      "trailingCommas": "none"    }  }}有时,格式化后的代码并不理想。
对于这些情况,您可以使用格式化抑制注释:
// biome-ignore format: <explanation>示例:
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,  ];Copyright (c) 2023-present Biome Developers and Contributors.