抑制项
Biome 分析器是 linter 和 assist 的基础;事实上,这两个工具有很多相似之处。
¥The Biome analyzer is the foundation of the linter and assist; in fact, both tools share a lot of similarities.
它们共享同一个抑制引擎,这意味着你可以像抑制辅助操作一样抑制 lint 规则。
¥Among them, they share the same suppression engine, which means that you can suppress a lint rule the same way you would suppress an assist action.
通过抑制功能,你可以针对特定代码行、代码范围或整个文件关闭代码检查规则(或操作)。
¥With suppression, it’s possible to turn off a lint rule (or action) for a specific line of code, a range, or the entire file.
可以通过抑制注释来实现抑制。
¥Suppression is achievable with suppression comments.
¥Suppression syntax
抑制注释具有以下格式:
¥Suppression comments have the following format:
// biome-ignore lint: <explanation>// biome-ignore assist: <explanation>// biome-ignore syntax: <explanation>// biome-ignore lint/suspicious: <explanation>// biome-ignore lint/suspicious/noDebugger: <explanation>// biome-ignore lint/suspicious/noDebugger(foo): <explanation>// biome-ignore-all lint: <explanation>// biome-ignore-start lint: <explanation>// biome-ignore-end lint: <explanation>让我们来分析一下:
¥Let’s break it down:
-
biome-ignore、biome-ignore-all、biome-ignore-start和biome-ignore-end是抑制注释的开始。¥
biome-ignore,biome-ignore-all,biome-ignore-startandbiome-ignore-endare the start of a suppression comment. -
空格后是抑制注释的类别。它可以是
lint、assist或syntax。¥After the space follows the category of the suppression comment. It can be
lint,assist, orsyntax. -
类别后面可以跟一个可选的分组,或者分组和名称,两者之间用斜杠分隔。例如,
/suspicious或/suspicious/noDebugger。你指定的参数越多,抑制就越具体(即目标性越强)。¥The category is followed by an optional group, or group and name, separated by slashes. For example,
/suspiciousor/suspicious/noDebugger. The more you specify, the more specific (i.e. targeted) your suppression is. -
某些规则甚至允许你在抑制期间指定值。这些模式可以用括号指定,例如:
(foo)。请参阅规则文档,了解规则是否支持使用值进行抑制。¥Some rules even allow you to specify values during suppression. These can be specified between parentheses, for example:
(foo). Refer to the rule documentation to see whether a rule supports suppression with values. -
<explanation>规则禁用原因说明。¥
<explanation>explanation why the rule is disabled.
如果你不确定某个规则/操作的具体类别,可以参考其文档页面并使用其诊断类别。
¥If you’re unsure of the exact category of a rule/action, you can refer to their documentation page and use their diagnostic category.
¥Inline suppressions
它们会禁用下一行代码的 lint 规则。
¥They disable a lint rule for the next line of code.
在以下示例中,抑制注释 biome-ignore lint/suspicious/noDebugger: reason 将禁用第 2 行的 debugger; 语句,但第 3 行的 debugger 仍会引发诊断信息:
¥In the following example, the suppression comment biome-ignore lint/suspicious/noDebugger: reason will disable the debugger; statement at line 2, but the debugger at line 3 will still raise a diagnostic:
// biome-ignore lint/suspicious/noDebugger: reasondebugger;debugger;¥Top-level suppressions
它们会禁用整个文件的 lint 规则。它们必须放在文件顶部,并且必须以 biome-ignore-all 开头。
¥They disable a lint rule for an entire file. They must be placed at the top of the file, and they must start with biome-ignore-all.
当你想要针对特定文件抑制某些 lint 规则,并且不想依赖单个配置覆盖来实现此目的时,这些抑制注释非常有用。
¥These suppression comments are very useful when you want to suppress some lint rule for a particular file, and you don’t want to rely on a single configuration override to achieve that.
在以下示例中,抑制注释 biome-ignore-all lint/suspicious/noDebugger: reason 将禁用 generated.js 中所有行的 lint 规则:
¥In the following example, the suppression comment biome-ignore-all lint/suspicious/noDebugger: reason will disable the lint rule for all lines in generated.js:
// biome-ignore-all lint/suspicious/noDebugger: reasondebugger;debugger;当顶层抑制注释不在文件顶部时,它将被视为未使用,Biome 将发出类别为 suppression/unused 的诊断信息。
¥When a top-level suppression comment isn’t at the top of the file, it is considered unused and Biome will emit a diagnostic with category suppression/unused.
¥Range suppressions
它们会禁用文件中特定范围内的 lint 规则,从包含起始注释的行开始,到包含结束注释的行结束。
¥They disable a lint rule from a particular range in the file, starting from the line with the start comment, until the line with the end comment.
要标记范围抑制的开始,抑制注释必须以 // biome-ignore-start 开头。要标记范围抑制的结束,抑制注释必须以 // biome-ignore-end 开头。
¥To mark the beginning of a range suppression, the suppression comment must start with // biome-ignore-start. To mark the end of it, the suppression comment must start with // biome-ignore-end.
以下示例将禁用第 2 行和第 3 行的 lint/suspicious/noDoubleEquals 规则,但第 5 行会引发诊断信息:
¥The following example will disable the rule lint/suspicious/noDoubleEquals for line 2 and 3, but line 5 will raise a diagnostic:
// biome-ignore-start lint/suspicious/noDoubleEquals: reasona == b;c == d;// biome-ignore-end lint/suspicious/noDoubleEquals: reasonf == g;范围抑制也可以重叠。请看以下示例:
¥Range suppressions can also overlap. Consider the following example:
debugger;// biome-ignore-start lint/suspicious/noDebugger: reasondebugger;// biome-ignore-start lint/suspicious/noDoubleEquals: reasona == b;c == d;// biome-ignore-end lint/suspicious/noDoubleEquals: reasondebugger;f == g;// biome-ignore-end lint/suspicious/noDebugger: reason在上面的代码中:
¥In the above code:
-
第 1 行的
debugger语句会引发诊断信息,因为没有抑制注释将其禁用。¥The
debuggerstatement at line 1 will raise a diagnostic because there’s no suppression comment that disables it. -
第 2 行的注释
// biome-ignore-start lint/suspicious/noDebugger: reason从第 3 行开始禁用noDebugger。¥The comment
// biome-ignore-start lint/suspicious/noDebugger: reasonat line 2 starts disablingnoDebuggerfrom line 3 onwards. -
第 4 行的注释
// biome-ignore-start lint/suspicious/noDoubleEquals: reason从第 5 行开始禁用noDoubleEquals。¥The comment
// biome-ignore-start lint/suspicious/noDoubleEquals: reasonat line 4 starts disablingnoDoubleEqualsfrom line 5 onwards. -
第 7 行的注释
// biome-ignore-end lint/suspicious/noDoubleEquals: reason终止了由第 4 行的抑制注释开始的对noDoubleEquals的抑制。¥The comment
// biome-ignore-end lint/suspicious/noDoubleEquals: reasonat line 7 terminates the suppression ofnoDoubleEqualsthat was started by the suppression comment at line 4. -
第 8 行的
debugger语句不会引发诊断信息,因为第 2 行有抑制注释。¥The
debuggerstatement at line 8 doesn’t raise diagnostics due to the suppression comment at line 2. -
f == g语句会引发诊断信息,因为规则noDoubleEquals不再被抑制。¥The
f == gstatement raises a diagnostic because the rulenoDoubleEqualsisn’t suppressed anymore. -
第 10 行的注释
// biome-ignore-end lint/suspicious/noDebugger: reason终止了由第 2 行的抑制注释开始的对noDebugger的抑制。¥The comment
// biome-ignore-end lint/suspicious/noDebugger: reasonat line 10 terminates the suppression ofnoDebuggerthat was started by the suppression comment at line 2.
Biome v2.1 中文网 - 粤ICP备13048890号