Biome代码检查技能Skill biome-linting

这个技能用于通过Biome工具对JavaScript和TypeScript项目进行自动化代码检查,提升代码质量、性能和安全性。关键词:Biome, 代码检查, linting, JavaScript, TypeScript, 代码质量, 自动化检查, 规则配置, 安全优化。

前端开发 0 次安装 0 次浏览 更新于 3/25/2026

名称: biome-linting 用户可调用: false 描述: 用于将Biome的代码检查能力、规则类别和代码质量强制执行应用到JavaScript/TypeScript项目。 允许的工具: [Read, Write, Edit, Bash, Glob, Grep]

Biome 代码检查

Biome代码检查能力的专业知识,包括规则类别和代码质量强制执行,适用于JavaScript和TypeScript项目。

概述

Biome的检查器提供快速、全面的代码质量检查,专注于正确性、性能、安全和最佳实践。它旨在捕获常见错误并强制执行一致的代码模式。

核心命令

基本检查

# 检查文件但不修复
biome check .

# 检查并自动修复
biome check --write .

# 检查特定文件
biome check src/**/*.ts

# CI模式(严格,警告即失败)
biome ci .

命令选项

# 详细输出
biome check --verbose .

# JSON输出
biome check --json .

# 仅检查(跳过格式化)
biome lint .

# 仅应用安全修复
biome check --write --unsafe=false .

规则类别

可访问性(a11y)

用于Web可访问性和WCAG合规性的规则:

{
  "linter": {
    "rules": {
      "a11y": {
        "recommended": true,
        "noAccessKey": "error",
        "noAriaHiddenOnFocusable": "error",
        "noAutofocus": "warn",
        "noBlankTarget": "error",
        "noPositiveTabindex": "error",
        "useAltText": "error",
        "useAnchorContent": "error",
        "useButtonType": "error",
        "useValidAriaProps": "error"
      }
    }
  }
}

常见违规:

  • 图像缺少alt文本
  • 表单元素上的自动聚焦
  • 正的tabindex值
  • 无内容的链接
  • 无效的ARIA属性

复杂度

减少代码复杂度的规则:

{
  "linter": {
    "rules": {
      "complexity": {
        "recommended": true,
        "noBannedTypes": "error",
        "noExcessiveCognitiveComplexity": "warn",
        "noExtraBooleanCast": "error",
        "noMultipleSpacesInRegularExpressionLiterals": "error",
        "noUselessCatch": "error",
        "noUselessConstructor": "error",
        "noUselessEmptyExport": "error",
        "noUselessFragments": "error",
        "noUselessLabel": "error",
        "noUselessRename": "error",
        "noUselessSwitchCase": "error",
        "noWith": "error"
      }
    }
  }
}

正确性

用于代码正确性和错误预防的规则:

{
  "linter": {
    "rules": {
      "correctness": {
        "recommended": true,
        "noChildrenProp": "error",
        "noConstAssign": "error",
        "noConstantCondition": "error",
        "noConstructorReturn": "error",
        "noEmptyPattern": "error",
        "noGlobalObjectCalls": "error",
        "noInnerDeclarations": "error",
        "noInvalidConstructorSuper": "error",
        "noNewSymbol": "error",
        "noNonoctalDecimalEscape": "error",
        "noPrecisionLoss": "error",
        "noSelfAssign": "error",
        "noSetterReturn": "error",
        "noSwitchDeclarations": "error",
        "noUndeclaredVariables": "error",
        "noUnreachable": "error",
        "noUnreachableSuper": "error",
        "noUnsafeFinally": "error",
        "noUnsafeOptionalChaining": "error",
        "noUnusedLabels": "error",
        "noUnusedVariables": "error",
        "useIsNan": "error",
        "useValidForDirection": "error",
        "useYield": "error"
      }
    }
  }
}

关键规则:

  • noUndeclaredVariables: 捕获未声明的变量
  • noUnusedVariables: 移除未使用的代码
  • noConstAssign: 防止const重新赋值
  • noUnreachable: 检测不可达代码
  • useIsNan: 使用isNaN()进行NaN检查

性能

性能优化的规则:

{
  "linter": {
    "rules": {
      "performance": {
        "recommended": true,
        "noAccumulatingSpread": "warn",
        "noDelete": "error"
      }
    }
  }
}

安全

安全最佳实践的规则:

{
  "linter": {
    "rules": {
      "security": {
        "recommended": true,
        "noDangerouslySetInnerHtml": "error",
        "noDangerouslySetInnerHtmlWithChildren": "error",
        "noGlobalEval": "error"
      }
    }
  }
}

关键安全规则:

  • 防止通过innerHTML的XSS
  • 阻止eval()使用
  • 检测安全漏洞

风格

代码风格和一致性的规则:

{
  "linter": {
    "rules": {
      "style": {
        "recommended": true,
        "noArguments": "error",
        "noCommaOperator": "error",
        "noImplicitBoolean": "warn",
        "noNegationElse": "warn",
        "noNonNullAssertion": "warn",
        "noParameterAssign": "error",
        "noRestrictedGlobals": "error",
        "noShoutyConstants": "warn",
        "noUnusedTemplateLiteral": "error",
        "noVar": "error",
        "useBlockStatements": "warn",
        "useCollapsedElseIf": "warn",
        "useConst": "error",
        "useDefaultParameterLast": "error",
        "useEnumInitializers": "warn",
        "useExponentiationOperator": "error",
        "useFragmentSyntax": "error",
        "useNumericLiterals": "error",
        "useSelfClosingElements": "error",
        "useShorthandArrayType": "error",
        "useSingleVarDeclarator": "error",
        "useTemplate": "warn",
        "useWhile": "error"
      }
    }
  }
}

关键风格规则:

  • noVar: 使用let/const代替var
  • useConst: 对不可变绑定偏好const
  • noNonNullAssertion: 避免TypeScript中的!断言
  • useTemplate: 偏好模板字符串

可疑

可疑代码模式的规则:

{
  "linter": {
    "rules": {
      "suspicious": {
        "recommended": true,
        "noArrayIndexKey": "warn",
        "noAssignInExpressions": "error",
        "noAsyncPromiseExecutor": "error",
        "noCatchAssign": "error",
        "noClassAssign": "error",
        "noCommentText": "error",
        "noCompareNegZero": "error",
        "noConsoleLog": "warn",
        "noControlCharactersInRegex": "error",
        "noDebugger": "error",
        "noDoubleEquals": "error",
        "noDuplicateCase": "error",
        "noDuplicateClassMembers": "error",
        "noDuplicateObjectKeys": "error",
        "noDuplicateParameters": "error",
        "noEmptyBlockStatements": "warn",
        "noExplicitAny": "warn",
        "noExtraNonNullAssertion": "error",
        "noFallthroughSwitchClause": "error",
        "noFunctionAssign": "error",
        "noGlobalAssign": "error",
        "noImportAssign": "error",
        "noLabelVar": "error",
        "noMisleadingCharacterClass": "error",
        "noPrototypeBuiltins": "error",
        "noRedeclare": "error",
        "noSelfCompare": "error",
        "noShadowRestrictedNames": "error",
        "noUnsafeNegation": "error",
        "useDefaultSwitchClauseLast": "error",
        "useGetterReturn": "error",
        "useValidTypeof": "error"
      }
    }
  }
}

关键可疑模式:

  • noExplicitAny: 避免TypeScript中的any
  • noConsoleLog: 生产环境中移除console.log
  • noDebugger: 移除调试器语句
  • noDoubleEquals: 使用===代替==
  • noDuplicateObjectKeys: 捕获重复键

规则配置模式

严格配置

高质量代码库的最大严格度:

{
  "linter": {
    "enabled": true,
    "rules": {
      "recommended": true,
      "a11y": { "recommended": true },
      "complexity": { "recommended": true },
      "correctness": { "recommended": true },
      "performance": { "recommended": true },
      "security": { "recommended": true },
      "style": { "recommended": true },
      "suspicious": {
        "recommended": true,
        "noExplicitAny": "error",
        "noConsoleLog": "error"
      }
    }
  }
}

渐进采用

从宽松开始并逐步收紧:

{
  "linter": {
    "enabled": true,
    "rules": {
      "recommended": true,
      "suspicious": {
        "noExplicitAny": "warn",
        "noConsoleLog": "warn"
      },
      "style": {
        "noVar": "error",
        "useConst": "warn"
      }
    }
  }
}

框架特定

React配置示例:

{
  "linter": {
    "rules": {
      "recommended": true,
      "a11y": { "recommended": true },
      "correctness": {
        "recommended": true,
        "noChildrenProp": "error"
      },
      "security": {
        "noDangerouslySetInnerHtml": "error"
      },
      "suspicious": {
        "noArrayIndexKey": "error"
      }
    }
  }
}

忽略违规

内联注释

抑制特定违规:

// biome-ignore lint/suspicious/noExplicitAny: 遗留代码
function legacyFunction(data: any) {
  return data;
}

// biome-ignore lint/suspicious/noConsoleLog: 调试日志
console.log('调试信息');

// 多个规则
// biome-ignore lint/suspicious/noExplicitAny lint/style/useConst: 迁移
var config: any = {};

文件级忽略

忽略整个文件:

/* biome-ignore-file */

// 遗留文件,跳过所有检查

配置忽略

在biome.json中忽略模式:

{
  "files": {
    "ignore": [
      "**/generated/**",
      "**/*.config.js",
      "**/vendor/**"
    ]
  }
}

最佳实践

  1. 启用推荐规则 - 从 "recommended": true 开始
  2. 渐进增强 - 逐步添加更严格的规则
  3. 文档化例外 - 解释为什么规则被禁用
  4. 少用内联忽略 - 偏好修复而非忽略
  5. 安全优先 - 永不关闭安全规则
  6. CI强制执行 - 在管道中使用 biome ci
  7. 预提交钩子 - 在提交前捕获问题
  8. 团队共识 - 与团队讨论规则变更
  9. 定期审查 - 定期审查被禁用的规则
  10. 修复警告 - 不让警告累积

常见陷阱

  1. 忽略错误 - 使用biome-ignore代替修复
  2. 禁用安全 - 关闭安全规则
  3. 没有CI检查 - 未在持续集成中强制执行
  4. 过于宽松 - 将所有内容设置为“warn”
  5. 无文档 - 未解释被禁用的规则
  6. 配置不一致 - 不同包使用不同规则
  7. 忽略警告 - 将警告视为可选
  8. 错误的规则名称 - 规则配置中的拼写错误
  9. 过于严格 - 阻碍团队生产力
  10. 没有迁移计划 - 一次性启用所有规则

高级主题

自定义规则集

为组织创建共享规则集:

{
  "extends": ["@company/biome-config"],
  "linter": {
    "rules": {
      "suspicious": {
        "noConsoleLog": "error"
      }
    }
  }
}

每目录规则

对不同代码区域使用覆盖:

{
  "overrides": [
    {
      "include": ["src/**/*.ts"],
      "linter": {
        "rules": {
          "suspicious": {
            "noExplicitAny": "error"
          }
        }
      }
    },
    {
      "include": ["scripts/**/*.js"],
      "linter": {
        "rules": {
          "suspicious": {
            "noConsoleLog": "off"
          }
        }
      }
    }
  ]
}

迁移策略

从ESLint迁移:

  1. 安装Biomenpm install -D @biomejs/biome
  2. 初始化npx biome init
  3. 运行检查npx biome check . 查看违规
  4. 自动修复npx biome check --write .
  5. 处理剩余:修复无法自动修复的问题
  6. 调整规则:根据团队需求调整规则
  7. 更新CI:用Biome替换ESLint
  8. 移除ESLint:验证后移除

集成模式

Package.json脚本

{
  "scripts": {
    "lint": "biome check .",
    "lint:fix": "biome check --write .",
    "lint:ci": "biome ci ."
  }
}

预提交钩子

使用husky:

{
  "husky": {
    "hooks": {
      "pre-commit": "biome check --write --changed"
    }
  }
}

GitHub Actions

- name: 运行Biome
  run: npx biome ci .

故障排除

假阳性

如果规则错误触发:

  1. 验证规则是否适用于您的代码
  2. 检查是否为Biome中的错误(向上游报告)
  3. 使用biome-ignore并附解释
  4. 如果广泛存在,考虑禁用规则

性能问题

如果检查缓慢:

  1. 更新到最新Biome版本
  2. 使用VCS集成
  3. 忽略大型生成目录
  4. 检查文件模式问题

规则未应用

验证:

  1. 检查器在配置中启用
  2. 规则类别已启用
  3. 规则名称拼写正确
  4. 无覆盖禁用它
  5. 文件未被忽略

何时使用此技能

  • 为新项目设置代码检查
  • 从ESLint迁移到Biome
  • 为团队配置规则集
  • 故障排除检查错误
  • 优化代码质量检查
  • 建立代码标准
  • 培训团队关于Biome检查
  • 将检查集成到CI/CD中