名称: 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代替varuseConst: 对不可变绑定偏好constnoNonNullAssertion: 避免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中的anynoConsoleLog: 生产环境中移除console.lognoDebugger: 移除调试器语句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/**"
]
}
}
最佳实践
- 启用推荐规则 - 从
"recommended": true开始 - 渐进增强 - 逐步添加更严格的规则
- 文档化例外 - 解释为什么规则被禁用
- 少用内联忽略 - 偏好修复而非忽略
- 安全优先 - 永不关闭安全规则
- CI强制执行 - 在管道中使用
biome ci - 预提交钩子 - 在提交前捕获问题
- 团队共识 - 与团队讨论规则变更
- 定期审查 - 定期审查被禁用的规则
- 修复警告 - 不让警告累积
常见陷阱
- 忽略错误 - 使用biome-ignore代替修复
- 禁用安全 - 关闭安全规则
- 没有CI检查 - 未在持续集成中强制执行
- 过于宽松 - 将所有内容设置为“warn”
- 无文档 - 未解释被禁用的规则
- 配置不一致 - 不同包使用不同规则
- 忽略警告 - 将警告视为可选
- 错误的规则名称 - 规则配置中的拼写错误
- 过于严格 - 阻碍团队生产力
- 没有迁移计划 - 一次性启用所有规则
高级主题
自定义规则集
为组织创建共享规则集:
{
"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迁移:
- 安装Biome:
npm install -D @biomejs/biome - 初始化:
npx biome init - 运行检查:
npx biome check .查看违规 - 自动修复:
npx biome check --write . - 处理剩余:修复无法自动修复的问题
- 调整规则:根据团队需求调整规则
- 更新CI:用Biome替换ESLint
- 移除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 .
故障排除
假阳性
如果规则错误触发:
- 验证规则是否适用于您的代码
- 检查是否为Biome中的错误(向上游报告)
- 使用biome-ignore并附解释
- 如果广泛存在,考虑禁用规则
性能问题
如果检查缓慢:
- 更新到最新Biome版本
- 使用VCS集成
- 忽略大型生成目录
- 检查文件模式问题
规则未应用
验证:
- 检查器在配置中启用
- 规则类别已启用
- 规则名称拼写正确
- 无覆盖禁用它
- 文件未被忽略
何时使用此技能
- 为新项目设置代码检查
- 从ESLint迁移到Biome
- 为团队配置规则集
- 故障排除检查错误
- 优化代码质量检查
- 建立代码标准
- 培训团队关于Biome检查
- 将检查集成到CI/CD中