禁用检查
Biome 分析器是 linter 和 assist 的基础;事实上,这两个工具中拥有大量相似之处。
它们之中分享者同样的禁用检查的引擎,这意味着你可以像禁用一条 lint 规则一样禁用 assist 操作。
通过禁用检查,您可以关闭特定代码行、范围或整个文件的 lint 规则(或操作)。
禁用检查可以通过 禁用检查注释 实现。
禁用检查语法
Section titled “禁用检查语法”禁用检查注释拥有以下格式:
// biome-ignore lint: <解释>// biome-ignore assist: <解释>// biome-ignore syntax: <解释>// biome-ignore lint/suspicious: <解释>// biome-ignore lint/suspicious/noDebugger: <解释>// biome-ignore lint/suspicious/noDebugger(foo): <解释>// biome-ignore-all lint: <解释>// biome-ignore-start lint: <解释>// biome-ignore-end lint: <解释>
让我们分析一下:
biome-ignore
、biome-ignore-all
、biome-ignore-start
和biome-ignore-end
是禁用检查注释的开始。- 空格后跟随着禁用检查的 类别。类别可以是
lint
、assist
或syntax
。 - 类别后跟一个可选的组或名称,用斜杠分隔。例如,
/suspicious
或/suspicious/noDebugger
。您指定的详细,您的禁用检查就越具有针对性。 - 有些规则甚至允许您在禁用检查期间指定值。这些可以在括号之间指定,例如
(foo)
。请参阅规则文档以了解有关这些内容的更多信息来了解它们是否被使用。 <解释>
解释了为什么会禁用检查。
如果您不确定规则或操作的确切类别,可以参考他们的文档页面并使用他们的 诊断类别。
行内禁用检查
Section titled “行内禁用检查”它会禁用代码 下一行 的某个规则检查。
在以下示例中,禁用检查注释 biome-ignore lint/suspicious/noDebugger: reason
将禁用检查第 2 行的 debugger;
语句,但第 3 行的 debugger;
仍会触发检查:
// biome-ignore lint/suspicious/noDebugger: reasondebugger;debugger;
顶层禁用检查
Section titled “顶层禁用检查”它会禁用检查整个文件的某个规则。它必须被放置在 文件的最顶端,并且它必须起始于 biome-ignore-all
。
这些禁用检查注释在你想要 对于特定文件 禁用某些规则检查来说非常有用,并且您不必再重复某个单一简单的配置了。
在以下示例中,禁用检查注释 biome-ignore-all lint/suspicious/noDebugger: reason
将会禁用 generated.js
的 lint/suspicious/noDebugger
lint 规则:
// biome-ignore-all lint/suspicious/noDebugger: reason
debuggerdebugger
当一个顶层禁用检查注释不在一个文件的最顶端,它会标记为 未使用 并且 Biome 将会标记一个 suppression/unused
诊断。
范围禁用检查
Section titled “范围禁用检查”它将会文件的在特定范围内禁用一条检查规则,从一行开始禁用检查注释开始,并在一行结束禁用检查注释结束。
要标记范围禁用检查的开始,禁用检查注释必须以 // biome-ignore-start
开头。
要标记它的结尾,禁用检查注释必须以 // biome-ignore-end
开头。
在以下示例中,将会从禁用从第 2 行到第 3 行的 lint/suspicious/noDoubleEquals
检查规则,但是第五行还是会发出一个诊断
// biome-ignore-start lint/suspicious/noDoubleEquals: reasona == b;c == d;// biome-ignore-end lint/suspicious/noDoubleEquals: reasonf == g;
范围引用检查可以重叠,参考以下的例子:
debugger;// biome-ignore-start lint/suspicious/noDebugger: reasondebugger// biome-ignore-start lint/suspicious/noDoubleEquals: reasona == b;c == d;// biome-ignore-end lint/suspicious/noDoubleEquals: reasondebuggerf == g;// biome-ignore-end lint/suspicious/noDebugger: reason
在以上的代码中:
- 第 1 行的
debugger
语句将会发出诊断因为没有禁用检查注释禁用它的检查。 - 第 2 行的禁用检查注释
// biome-ignore-start lint/suspicious/noDebugger: reason
将会从第 3 行开始禁用noDebugger
规则的检查。 - 第 4 行的禁用检查注释
// biome-ignore-start lint/suspicious/noDoubleEquals: reason
将会从第 5 行开始禁用noDebugger
规则的检查。 - 第 7 行的禁用检查注释
// biome-ignore-end lint/suspicious/noDoubleEquals: reason
结束了从第 4 行开始的禁用noDoubleEquals
规则的检查 - 第 8 行的
debugger
语句 不会 发出诊断因为第二行的禁用检查注释阻止了它。 - 第 9 行的
f == g
表达式将会发出诊断因为noDoubleEquals
规则没有被禁用检查。 - 第 10 行的
// biome-ignore-end lint/suspicious/noDebugger: reason
结束了从第 2 行开始的禁用noDebugger
。