SuperAPI AI 工具集

biome-linting

编程与开发

查看项目

更新于 10-16 04:26

技能介绍

用途

Biome Linting 是面向 JavaScript/TypeScript 项目的统一代码检查(linting)与格式化(formatting)方案,主打速度与配置简化。据资料,其速度约为 ESLint + Prettier 的 10-25 倍,处理 1 万行代码约需 200ms,而 ESLint + Prettier 约需 3-5 秒。

主要能力

  • 统一工具:仅需 1 个配置文件(biome.json)和 1 个二进制包,而 ESLint + Prettier 需要 4 个以上配置文件和 127 个以上 npm 包。
  • 规则数量:内置 421 条 lint 规则。
  • 类型推断:Biome 2.0+ 支持类型推断,会读取 node_modules 中的 .d.ts 文件以启用类型感知规则,例如 noFloatingPromises 可捕获未处理的 Promise。
  • 多文件分析:支持跨模块分析,以提供更好的诊断信息。
  • ESLint 迁移:提供自动迁移命令,并支持常见规则映射,如 no-unused-vars 对应 correctness/noUnusedVariables,no-console 对应 suspicious/noConsole,以及多数 @typescript-eslint/*、eslint-plugin-react、eslint-plugin-jsx-a11y 规则。
  • CI 集成:可通过 GitHub Actions 使用 biomejs/setup-biome@v2 并运行 biome ci .。
  • 渐进式采用:通过 overrides 针对测试文件或 legacy 目录调整规则或关闭 linter。

使用方式

安装:

npm install --save-dev --save-exact @biomejs/biome

初始化:

npx @biomejs/biome init

检查(lint + format):

npx @biomejs/biome check .

自动修复:

npx @biomejs/biome check --write .

CI 模式(遇错失败):

npx @biomejs/biome ci .

ESLint 配置自动迁移:

npx @biomejs/biome migrate eslint --write

基础配置示例:

{
  "$schema": "https://biomejs.dev/schemas/2.0.0/schema.json",
  "formatter": {
    "enabled": true,
    "indentStyle": "space",
    "indentWidth": 2,
    "lineWidth": 100
  },
  "linter": {
    "enabled": true,
    "rules": {
      "recommended": true,
      "correctness": {
        "noUnusedVariables": "error",
        "noUnusedImports": "error"
      },
      "suspicious": {
        "noExplicitAny": "warn"
      }
    }
  },
  "javascript": {
    "formatter": {
      "quoteStyle": "single",
      "trailingCommas": "all"
    }
  }
}

类型推断规则配置示例:

{
  "linter": {
    "rules": {
      "nursery": {
        "noFloatingPromises": "error"
      }
    }
  }
}

CI 工作流示例:

- uses: biomejs/setup-biome@v2
- run: biome ci .

渐进式采用配置示例:

{
  "overrides": [
    {
      "include": ["*.test.ts", "*.spec.ts"],
      "linter": {
        "rules": {
          "suspicious": { "noExplicitAny": "off" }
        }
      }
    },
    {
      "include": ["legacy/**"],
      "linter": { "enabled": false }
    }
  ]
}

适用场景

  • 新项目:建议优先采用 Biome。
  • 已有项目:建议逐步从 ESLint 迁移。
  • 配置严格度:建议从 recommended 开始,逐步收紧。
  • CI 策略:本地使用 biome check,CI 使用 biome ci 严格模式。
  • TypeScript 项目:建议启用类型推断(v2.0+)。

限制

资料未提供 Biome 的收费、价格、平台支持范围、具体性能基准测试环境等细节,暂未提供。