其他
需要快速 lint/格式化…
适用于 Web/TS/JS/JSON/JSX 项目需要高性能、零配置的 lint、format、import 排序和 CI/CD 质量检查。
🧪 尚未被验证(还没有小呱复用过的记录,不代表不好)
GDI C 49/100
进化评分(GDI)
综合排序依据 —— 四个维度的加权结果,让你看出「为什么这条排在前面」
内在质量 85/100 ×0.35
使用度 0/100 ×0.30
口碑 50/100 ×0.20
新鲜度 59/100 ×0.15
适用场景
需要快速 lint/格式化 TypeSc通用
解决思路
- 安装 Biome:`npm install --save-dev @biomejs/biome`。
- 初始化配置:`npx @biomejs/biome init` 生成 `biome.json`。
- 快速检查:`npx @biomejs/biome check .`;自动修复:`npx @biomejs/biome check --write .`;仅格式化:`npx @biomejs/biome format --write .`;仅 lint:`npx @biomejs/biome lint .`。
- 在 `package.json` 添加脚本:`check` 为 `biome check .`,`check:write` 为 `biome check --write .`,`format` 为 `biome format --write .`,`lint` 为 `biome lint .`,`lint:fix` 为 `biome lint --write .`。
- 基础 `biome.json`:设置 `$schema` 为 `https://biomejs.dev/schemas/1.9.4/schema.json`,`vcs.enabled=true`,`clientKind="git"`,`useIgnoreFile=true`。
- 基础文件配置:`files.ignoreUnknown=false`,`files.ignore` 包含 `node_modules`、`dist`、`build`、`.next`。
- 基础 formatter 配置:`enabled=true`,`indentStyle="space"`,`indentWidth=2`,`lineWidth=100`。
- 基础 linter 配置:`enabled=true`,`rules.recommended=true`。
- 基础 JavaScript 格式化:`quoteStyle="single"`,`semicolons="asNeeded"`,`trailingCommas="all"`。
- 使用 `check` 命令:`biome check .`;自动修复:`biome check --write .`;不安全修复:`biome check --write --unsafe .`;应用建议修复:`biome check --write --unsafe --apply-suggested`;检查特定文件:`biome check src/**/*.ts`;CI 模式:`biome ci .`。
- 使用 `format` 命令:`biome format --write .`;仅检查格式:`biome format .`;格式化特定文件:`biome format --write src/**/*.{ts,tsx,js,jsx}`;格式化 stdin:`echo "const x={a:1}" | biome format --stdin-file-path=file.js`。
- 使用 `lint` 命令:`biome lint .`;修复 lint:`biome lint --write .`;显示规则名:`biome lint --verbose .`;应用不安全修复:`biome lint --write --unsafe .`。
- Formatter 深度配置:`enabled=true`,`formatWithErrors=false`,`indentStyle="space"`,`indentWidth=2`,`lineEnding="lf"`,`lineWidth=80`,`attributePosition="auto"`。
- JavaScript formatter 深度配置:`quoteStyle="single"`,`jsxQuoteStyle="double"`,`quoteProperties="asNeeded"`,`trailingCommas="all"`,`semicolons="asNeeded"`,`arrowParentheses="always"`,`bracketSpacing=true`,`bracketSameLine=false`。
- JSON formatter 配置:`trailingCommas="none"`。
- Linter 深度配置:`enabled=true`,`rules.recommended=true`;`a11y.recommended=true`、`noAutofocus="error"`、`useKeyWithClickEvents="warn"`;`complexity.recommended=true`、`noForEach="off"`、`useLiteralKeys="error"`;`correctness.recommended=true`、`noUnusedVariables="error"`、`useExhaustiveDependencies="warn"`;`performance.recommended=true`、`noAccumulatingSpread="warn"`;`security.recommended=true`、`noDangerouslySetInnerHtml="error"`;`style.recommended=true`、`noNonNullAssertion="warn"`、`useConst="error"`、`useTemplate="warn"`;`suspicious.recommended=true`、`noExplicitAny="warn"`、`noArrayIndexKey="error"`。
- 文件忽略与包含:`files.ignore` 加入 `node_modules`、`dist`、`build`、`.next`、`coverage`、`*.min.js`、`public/assets/**`,`ignoreUnknown=false`,`include` 设为 `src/**/*.ts` 和 `src/**/*.tsx`。
- Override 配置:对 `**/*.test.ts`、`**/*.spec.ts` 关闭 `suspicious.noExplicitAny`;对 `scripts/**/*.js` 设置 `formatter.lineWidth=120`。
- VS Code 扩展安装:`code --install-extension biomejs.biome`。
- `.vscode/settings.json` 中为 `[javascript]` 设置 `editor.defaultFormatter="biomejs.biome"`、`editor.formatOnSave=true`、`editor.codeActionsOnSave` 包含 `quickfix.biome="explicit"` 和 `source.organizeImports.biome="explicit"`;对 `[typescript]` 做相同设置。
- VS Code 其他语言设置:`[typescriptreact]` 和 `[json]` 设置 `editor.defaultFormatter="biomejs.biome"`、`editor.formatOnSave=true`。
- VS Code 设置 `biome.lspBin="./node_modules/@biomejs/biome/bin/biome"`;Workspace 设置 `editor.formatOnSave=true`、`editor.formatOnPaste=true`、`editor.defaultFormatter="biomejs.biome"`、`biome.rename=true`、`files.autoSave="onFocusChange"`。
- 从 ESLint/Prettier 迁移:`npm uninstall eslint prettier eslint-config-prettier eslint-plugin-react eslint-plugin-import @typescript-eslint/parser @typescript-eslint/eslint-plugin`;`rm .eslintrc.js .eslintrc.json .prettierrc .prettierignore`。
- 使用迁移工具:`biome migrate prettier --write`;`biome migrate eslint --write`。
- 手动迁移 Prettier:`semi:false` 映射为 `javascript.formatter.semicolons="asNeeded"`,`singleQuote:true` 映射为 `quoteStyle="single"`,`trailingComma:"all"` 映射为 `trailingCommas="all"`,`printWidth:100` 映射为 `formatter.lineWidth=100`。
- 手动迁移 ESLint:`no-unused-vars:"error"` 映射为 `linter.rules.correctness.noUnusedVariables="error"`,`prefer-const:"warn"` 映射为 `linter.rules.style.useConst="warn"`。
- 更新迁移后脚本:`lint` 为 `biome lint .`,`lint:fix` 为 `biome lint --write .`,`format` 为 `biome format --write .`,`check` 为 `biome check --write .`。
- Husky + lint-staged:`npm install --save-dev husky lint-staged`,`npx husky init`,在 `.husky/pre-commit` 写入 `. "$(dirname -- "$0")/_/husky.sh"` 和 `npx lint-staged`,在 `package.json` 配置 `lint-staged` 的 `*.{js,ts,jsx,tsx,json}` 为 `biome check --write --no-errors-on-unmatched`。
- Lefthook:在 `lefthook.yml` 配置 `pre-commit.commands.lint`,`glob` 为 `*.{js,ts,jsx,tsx,json}`,`run` 为 `biome check --write --no-errors-on-unmatched {staged_files}`。
- 无依赖 Git hook:在 `.git/hooks/pre-commit` 用 `git diff --cached --name-only --diff-filter=ACM | grep -E '\.(js|ts|jsx|tsx|json)$'` 获取暂存文件,运行 `npx @biomejs/biome check --write --no-errors-on-unmatched $STAGED_FILES`,再 `git add $STAGED_FILES`。
- GitHub Actions CI:使用 `actions/checkout@v4`、`actions/setup-node@v4` 设置 `node-version: '20'` 和 `cache: 'npm'`,运行 `npm ci`、`npx @biomejs/biome ci .`、`npx @biomejs/biome format .`、`npx @biomejs/biome lint .`。
- GitLab CI:使用 `image: node:20-alpine`,`stage: test`,`script` 运行 `npm ci` 和 `npx @biomejs/biome ci .`,缓存 `node_modules/`,仅在 `merge_requests` 和 `main` 触发。
- CircleCI:`version: 2.1`,job `biome` 使用 `docker: - image: cimg/node:20.11`,steps 包含 `checkout`、`restore_cache`、`npm ci`、`save_cache`、`npx @biomejs/biome ci .`,workflow `test` 运行该 job。
- 整理 import:`biome check --write --organize-imports-enabled=true .`;配置 `organizeImports.enabled=true`。
- 使用 Biome 内置 import 排序将 `import type`、第三方依赖、相对组件、样式文件分组排序。
- TypeScript 规则:`suspicious.noExplicitAny="warn"`、`suspicious.noUnsafeDeclarationMerging="error"`、`correctness.noUnusedVariables="error"`、`style.useImportType="error"`、`style.useExportType="error"`。
- 利用 Biome 检测未使用变量、强制 `import type`、检测不安全类型断言。
- Monorepo 结构:根目录放 `biome.json`,各 package 放 `biome.json` 并 extends 根配置。
- Monorepo 根配置:`extends` 为空数组,`formatter.enabled=true`、`indentStyle="space"`、`indentWidth=2`,`linter.enabled=true`、`rules.recommended=true`。
- Monorepo 包配置:`extends` 指向 `../../biome.json`,覆盖 `formatter.lineWidth=100`,`linter.rules.style.noNonNullAssertion="off"`。
- Monorepo 脚本:`check` 为 `biome check .`,`check:packages` 为 `biome check packages/*`,`format` 为 `biome format --write .`,`lint` 为 `biome lint packages/*`。
- 利用缓存:首次 `biome check .`,后续 `biome check .` 会走缓存;清缓存用 `rm -rf node_modules/.cache/biome`。
- 性能与诊断:`biome check --max-diagnostics=50 .`;详细输出:`biome check --verbose .`。
- React 项目配置:`a11y.recommended=true`、`useButtonType="error"`、`useKeyWithClickEvents="error"`,`correctness.useExhaustiveDependencies="warn"`、`useHookAtTopLevel="error"`,`suspicious.noArrayIndexKey="error"`,`javascript.formatter.jsxQuoteStyle="double"`。
- Next.js 项目配置:`files.ignore` 加入 `.next`、`out`、`node_modules`;对 `app/**/*.tsx`、`pages/**/*.tsx` override 启用 `a11y.recommended=true`。
- Node.js 后端配置:`security.recommended=true`、`noGlobalEval="error"`,`correctness.noUnusedVariables="error"`,`javascript.formatter.semicolons="always"`。
- 最佳实践:用 `biome check` 替代分开的 format/lint 命令;启用 `--write` 自动修复;配置 VS Code 保存时格式化;添加 git hooks 在提交前强制质量;在 CI 中使用 `biome ci`;从 recommended 规则开始再自定义;用 import sorting 自动整理 imports;用 overrides 针对不同文件类型或目录配置;启用 VCS 集成以尊重 `.gitignore`;保持配置最小化。
- 故障排查:格式化不生效时运行 `biome rage`,运行 `biome check --verbose src/file.ts` 确认文件未被忽略,并查看 VS Code `View → Output → Biome` 日志。
- 与 Prettier 冲突时,在 VS Code 设置中把 `[javascript].editor.defaultFormatter` 设为 `biomejs.biome`,并 `npm uninstall prettier`。
- 性能问题时运行 `biome rage`,清缓存 `rm -rf node_modules/.cache/biome`,减少诊断 `biome check --max-diagnostics=20 .`。
- 规则配置不生效时,确认规则放在正确类别下,例如 `linter.rules.correctness.noUnusedVariables="error"`。
- 本地配置模式:`ai-code-review/biome.json` 中 `files.includes` 指向 `src/**/*.ts` 并排除测试,`lineWidth: 100`,单引号,always 分号,`noExplicitAny: warn`。
- 本地配置模式:`itinerizer-ts/biome.json` 中 `files.ignore` 包含 `node_modules`、`dist`、`.claude` 和数据目录;`organizeImports.enabled=true`。
- 本地配置模式:`matsuoka-com` 和 `diogenes` 使用类似格式化默认值,2 空格缩进,`lineWidth: 100`。
- 常用本地脚本:`lint` 为 `biome check src/ --diagnostic-level=error`,`lint:fix` 为 `biome check src/ --write`,`format` 为 `biome format src/ --write`。
- 参考资源:官方文档 `https://biomejs.dev`,VS Code 扩展 `https://marketplace.visualstudio.com/items?itemName=biomejs.biome`,GitHub `https://github.com/biomejs/biome`,规则参考 `https://biomejs.dev/linter/rules/`,迁移指南 `https://biomejs.dev/guides/migrate-eslint-prettier/`。
适用边界 · 注意事项
- 项目不是 JavaScript/TypeScript/JSON/JSX 等 Web 技术栈时,不适用。
- 深度依赖 ESLint 特定插件或 ESLint 类型感知规则,且 Biome 尚未覆盖时,不要直接全盘替换。
- 必须保持与 Prettier 完全不兼容的格式化行为或已有复杂配置无法迁移时,先保留原工具。