Linter
Biome 的 linter 静态分析你的代码以捕获常见错误并帮助编写符合规范的代码。
318通过 CLI 使用 linter
Section titled 通过 CLI 使用 linter你可以通过运行 CLI 来开始,使用以下命令来检查可能的错误:
biome lint ./src
有关所有可用选项的更多信息,请查看 CLI 页面
我们认为规则应该是有信息量的,并向用户解释为什么一个规则被触发,并告诉用户他们应该做什么来修复错误。规则应遵循这些支柱:
- 向用户解释错误。通常,这是诊断消息。
- 向用户解释为什么错误被触发。通常,这是通过附加节点实现的。
- 告诉用户他们应该做什么。通常,这是通过代码操作实现的。如果代码操作不适用,注释应告诉用户他们应该做什么来修复错误。
如果你认为规则没有遵循这些支柱,请提交一个问题。
lint规则可能提供自动代码修复。Biome 区分两种类型的修复。
安全修复保证不会改变你的代码的语义。他们可以在没有明确审查的情况下应用。
要应用 安全修复,使用 --write
:
biome check --write ./src
不安全的修复
Section titled 不安全的修复不安全的修复可能改变你的程序的语义。因此,建议手动审查更改。
要应用 不安全的修复,使用 --write --unsafe
:
biome check --write --unsafe ./src
推荐的规则
Section titled 推荐的规则当 linter 启用时,它会推荐一些规则。推荐的规则将发出错误诊断。以下是推荐规则的列表:
- noAccessKey (Severity: Error)
- noAriaHiddenOnFocusable (Severity: Error)
- noAriaUnsupportedElements (Severity: Error)
- noAutofocus (Severity: Error)
- noBlankTarget (Severity: Error)
- noDistractingElements (Severity: Error)
- noHeaderScope (Severity: Error)
- noInteractiveElementToNoninteractiveRole (Severity: Error)
- noLabelWithoutControl (Severity: Error)
- noNoninteractiveElementToInteractiveRole (Severity: Error)
- noNoninteractiveTabindex (Severity: Error)
- noPositiveTabindex (Severity: Error)
- noRedundantAlt (Severity: Error)
- noRedundantRoles (Severity: Error)
- noSvgWithoutTitle (Severity: Error)
- useAltText (Severity: Error)
- useAnchorContent (Severity: Error)
- useAriaActivedescendantWithTabindex (Severity: Error)
- useAriaPropsForRole (Severity: Error)
- useButtonType (Severity: Error)
- useFocusableInteractive (Severity: Error)
- useGenericFontNames (Severity: Error)
- useHeadingContent (Severity: Error)
- useHtmlLang (Severity: Error)
- useIframeTitle (Severity: Error)
- useKeyWithClickEvents (Severity: Error)
- useKeyWithMouseEvents (Severity: Error)
- useMediaCaption (Severity: Error)
- useSemanticElements (Severity: Error)
- useValidAnchor (Severity: Error)
- useValidAriaProps (Severity: Error)
- useValidAriaRole (Severity: Error)
- useValidAriaValues (Severity: Error)
- useValidLang (Severity: Error)
- noBannedTypes (Severity: Error)
- noEmptyTypeParameters (Severity: Error)
- noExtraBooleanCast (Severity: Error)
- noForEach (Severity: Error)
- noMultipleSpacesInRegularExpressionLiterals (Severity: Error)
- noStaticOnlyClass (Severity: Error)
- noThisInStatic (Severity: Error)
- noUselessCatch (Severity: Error)
- noUselessConstructor (Severity: Error)
- noUselessEmptyExport (Severity: Error)
- noUselessFragments (Severity: Error)
- noUselessLabel (Severity: Error)
- noUselessLoneBlockStatements (Severity: Error)
- noUselessRename (Severity: Error)
- noUselessSwitchCase (Severity: Error)
- noUselessTernary (Severity: Error)
- noUselessThisAlias (Severity: Error)
- noUselessTypeConstraint (Severity: Error)
- noWith (Severity: Error)
- useArrowFunction (Severity: Error)
- useFlatMap (Severity: Error)
- useLiteralKeys (Severity: Error)
- useOptionalChain (Severity: Error)
- useRegexLiterals (Severity: Error)
- useSimpleNumberKeys (Severity: Error)
- noChildrenProp (Severity: Error)
- noConstAssign (Severity: Error)
- noConstantCondition (Severity: Error)
- noConstructorReturn (Severity: Error)
- noEmptyCharacterClassInRegex (Severity: Error)
- noEmptyPattern (Severity: Error)
- noFlatMapIdentity (Severity: Error)
- noGlobalObjectCalls (Severity: Error)
- noInnerDeclarations (Severity: Error)
- noInvalidBuiltinInstantiation (Severity: Error)
- noInvalidConstructorSuper (Severity: Error)
- noInvalidDirectionInLinearGradient (Severity: Error)
- noInvalidGridAreas (Severity: Error)
- noInvalidPositionAtImportRule (Severity: Error)
- noInvalidUseBeforeDeclaration (Severity: Error)
- noNonoctalDecimalEscape (Severity: Error)
- noPrecisionLoss (Severity: Error)
- noRenderReturnValue (Severity: Error)
- noSelfAssign (Severity: Error)
- noSetterReturn (Severity: Error)
- noStringCaseMismatch (Severity: Error)
- noSwitchDeclarations (Severity: Error)
- noUnknownFunction (Severity: Error)
- noUnknownMediaFeatureName (Severity: Error)
- noUnknownProperty (Severity: Error)
- noUnknownUnit (Severity: Error)
- noUnmatchableAnbSelector (Severity: Error)
- noUnnecessaryContinue (Severity: Error)
- noUnreachable (Severity: Error)
- noUnreachableSuper (Severity: Error)
- noUnsafeFinally (Severity: Error)
- noUnsafeOptionalChaining (Severity: Error)
- noUnusedLabels (Severity: Error)
- noVoidElementsWithChildren (Severity: Error)
- noVoidTypeReturn (Severity: Error)
- useIsNan (Severity: Error)
- useValidForDirection (Severity: Error)
- useYield (Severity: Error)
- noAccumulatingSpread (Severity: Error)
- noDelete (Severity: Error)
- noDangerouslySetInnerHtml (Severity: Error)
- noDangerouslySetInnerHtmlWithChildren (Severity: Error)
- noGlobalEval (Severity: Error)
- noApproximativeNumericConstant (Severity: Error)
- noArrayIndexKey (Severity: Error)
- noAssignInExpressions (Severity: Error)
- noAsyncPromiseExecutor (Severity: Error)
- noCatchAssign (Severity: Error)
- noClassAssign (Severity: Error)
- noCommentText (Severity: Error)
- noCompareNegZero (Severity: Error)
- noConfusingLabels (Severity: Error)
- noConfusingVoidType (Severity: Error)
- noConstEnum (Severity: Error)
- noControlCharactersInRegex (Severity: Error)
- noDebugger (Severity: Error)
- noDoubleEquals (Severity: Error)
- noDuplicateAtImportRules (Severity: Error)
- noDuplicateCase (Severity: Error)
- noDuplicateClassMembers (Severity: Error)
- noDuplicateFontNames (Severity: Error)
- noDuplicateJsxProps (Severity: Error)
- noDuplicateObjectKeys (Severity: Error)
- noDuplicateObjectKeys (Severity: Error)
- noDuplicateParameters (Severity: Error)
- noDuplicateSelectorsKeyframeBlock (Severity: Error)
- noEmptyBlock (Severity: Error)
- noEmptyInterface (Severity: Error)
- noExplicitAny (Severity: Error)
- noExtraNonNullAssertion (Severity: Error)
- noFallthroughSwitchClause (Severity: Error)
- noFunctionAssign (Severity: Error)
- noGlobalAssign (Severity: Error)
- noGlobalIsFinite (Severity: Error)
- noGlobalIsNan (Severity: Error)
- noImplicitAnyLet (Severity: Error)
- noImportAssign (Severity: Error)
- noImportantInKeyframe (Severity: Error)
- noLabelVar (Severity: Error)
- noMisleadingCharacterClass (Severity: Error)
- noMisleadingInstantiator (Severity: Error)
- noMisrefactoredShorthandAssign (Severity: Error)
- noPrototypeBuiltins (Severity: Error)
- noRedeclare (Severity: Error)
- noRedundantUseStrict (Severity: Error)
- noSelfCompare (Severity: Error)
- noShadowRestrictedNames (Severity: Error)
- noShorthandPropertyOverrides (Severity: Error)
- noSparseArray (Severity: Error)
- noSuspiciousSemicolonInJsx (Severity: Error)
- noThenProperty (Severity: Error)
- noUnsafeDeclarationMerging (Severity: Error)
- noUnsafeNegation (Severity: Error)
- useDefaultSwitchClauseLast (Severity: Error)
- useGetterReturn (Severity: Error)
- useIsArray (Severity: Error)
- useNamespaceKeyword (Severity: Error)
- useValidTypeof (Severity: Error)
有时,开发者希望忽略代码中的某一行的 lint 规则。 你可以通过在发出 lint 诊断的行上方添加一个抑制注释来实现这一点。
抑制注释的格式如下:
// biome-ignore lint: <explanation>// biome-ignore lint/suspicious/noDebugger: <explanation>
其中
biome-ignore
是抑制注释的开始;lint
抑制 linter;/suspicious/noDebugger
: 可选, 你想要抑制的规则的组和名称;<explanation>
解释为什么禁用规则
这里有一个例子:
// biome-ignore lint: reasondebugger;// biome-ignore lint/suspicious/noDebugger: reasondebugger;
启用 lint 规则
Section titled 启用 lint 规则推荐的规则默认启用,并发出错误严重性的诊断。 默认情况下禁用不推荐的规则,但可以通过配置启用它们。 这些规则发出的诊断在文档中以警告严重性显示。
要启用规则,你需要根据你的需要改变它们的诊断严重性:
{ "linter": { "enabled": true, "rules": { "style": { "useBlockStatements": "error", "useShorthandArrayType": "error", "noShoutyConstants": "warn" } } }}
禁用 lint 规则
Section titled 禁用 lint 规则只需在其配置中添加 "off"
作为值。例如:
{ "linter": { "enabled": true, "rules": { "suspicious": { "noCommentText": "off" }, "style": { "noUnusedTemplateLiteral": "off" } } }}
改变诊断严重性
Section titled 改变诊断严重性Biome 的大多数规则会发出一个错误,但你可以自由改变它们的严重性。
只需添加 "warn"
作为规则的值。例如:
{ "linter": { "enabled": true, "rules": { "suspicious": { "noCommentText": "warn" } } }}
这在需要进行重构并需要使 CI 通过的情况下很有用。
一些规则有选项。 当他们确实接受一些选项时,你可以通过改变规则的值方式传递它们。
{ "linter": { "enabled": true, "rules": { "correctness": { "noCommentText": { "level": "warn", "options": {} } } } }}
level
将指示诊断的严重性,有效值为:“off”、“warn” 和 “error”;options
将根据规则改变。