Biome
Biome 是一个快速的一体化代码格式化、lint 工具,旨在替代 ESLint 和 Prettier 的组合。它使用 Rust 编写,具有极快的性能。
安装
npm install --save-dev @biomejs/biome
配置文件
在项目根目录创建 biome.json 文件:
{
"$schema": "https://biomejs.dev/schemas/1.5.3/schema.json",
"organizeImports": {
"enabled": true
},
"linter": {
"enabled": true,
"rules": {
"recommended": true,
"style": {
"noNonNullAssertion": "error",
"useTemplate": "error"
},
"suspicious": {
"noExplicitAny": "error",
"noConsoleLog": "warn"
},
"correctness": {
"noUnusedVariables": "error",
"useExhaustiveDependencies": "error"
}
}
},
"formatter": {
"enabled": true,
"formatWithErrors": false,
"indentStyle": "space",
"indentWidth": 2,
"lineWidth": 80,
"lineEnding": "lf",
"ignore": [
"node_modules/**",
"dist/**",
"build/**"
]
},
"javascript": {
"formatter": {
"quoteStyle": "single",
"trailingComma": "all",
"semicolons": "always"
}
}
}
主要特点
- 统一的工具链,无需配置多个工具
- 极快的执行速度(比 ESLint + Prettier 快 20-50 倍)
- 内置格式化和 lint 功能
- 支持自 动修复
- 支持导入语句排序
- 默认配置经过精心设计
- 支持增量检查,提高大型项目的性能
常用命令
# 格式化代码
npx biome format .
# 运行 lint
npx biome lint .
# 检查和修复问题
npx biome check --apply .
# 仅检查暂存的文件
npx biome check $(git diff --staged --name-only)
# 使用 --verbose 查看详细信息
npx biome check --verbose .
# 检查特定文件类型
npx biome check "src/**/*.{js,ts,jsx,tsx}"
常见规则配置
1. 导入语句排序
{
"organizeImports": {
"enabled": true,
"groupMode": "maintain", // 保持现有的导入组
"groups": [
"react", // React 相关导入
"type", // 类型导入
"external", // 外部依赖
"internal", // 内部模块
"style" // 样式文件
]
}
}