Skip to content

行内忽略注释

🌐 Inline ignore comments

忽略注释为特殊情况提供了一个退出通道,即规则在一般情况下是正确的,但需要在代码的小范围内被抑制。内联注释会覆盖配置文件。

🌐 Ignore comments provide an escape hatch for exceptional cases where a rule is correct in general but needs to be suppressed in a small, well scoped section of code. Inline comments override configuration files.

Oxlint 支持行注释(//)和块注释(/* */)。注释必须以以下关键字之一开头。

🌐 Oxlint supports line comments (//) and block comments (/* */). Comments must start with one of the keywords below.

oxlint-disable

禁用一条或多条规则,直到文件末尾,或者直到它们被重新启用。

🌐 Disable one or more rules until the end of the file, or until they are re-enabled.

js
// Disable all Oxlint rules for the rest of the file
/* oxlint-disable */

// Disable a single rule in this file
/* oxlint-disable no-console */

// Disable multiple rules in this file
/* oxlint-disable no-console, typescript/no-floating-promises */

oxlint-enable

启用一个或多个规则,直到文件末尾,或直到它们再次被禁用。

🌐 Enable one or more rules until the end of the file, or until they are disabled again.

js
/* oxlint-enable no-console */

/* oxlint-enable no-console, no-alert */

oxlint-disable-line

在当前行禁用一个或多个规则。

🌐 Disable one or more rules on the current line.

js
console.log("Hello, world!"); // oxlint-disable-line no-console

console.log(x++); // oxlint-disable-line no-console, no-plusplus

oxlint-disable-next-line

在注释后的行禁用一个或多个规则,然后重新启用它们以用于后续行。

🌐 Disable one or more rules on the line following the comment, then re-enables them for subsequent lines.

js
// oxlint-disable-next-line no-console
console.log("Hello, world!"); // allowed because of the previous comment
console.log(x++); // not allowed because the previous comment only applied to the previous line

// oxlint-disable-next-line no-console, no-plusplus
console.log("Hello, world!"); // allowed

ESLint 兼容性

🌐 ESLint compatibility

为了与现有的 ESLint 代码库兼容,支持使用相同的关键字,只需将 oxlint 替换为 eslint,例如 /* eslint-disable */// eslint-disable-next-line

🌐 For compatibility with existing ESLint codebases, the same keywords are supported with oxlint replaced by eslint, such as /* eslint-disable */ and // eslint-disable-next-line.

推荐使用 oxlint-* 表单。在迁移期间,对于 Oxlint 还不支持的规则,eslint-* 表单很有用。

🌐 The oxlint-* form is recommended. The eslint-* form is useful during migration for rules that Oxlint does not support yet.

规则选项无法在内联中更改

🌐 Rule options cannot be changed inline

忽略注释可以启用或禁用规则,但无法更改规则选项。规则选项应放在配置文件中。

🌐 Ignore comments can enable or disable rules, but they cannot change rule options. Rule options belong in configuration files.

报告未使用的忽略注释

🌐 Report unused ignore comments

默认情况下,报告未使用的忽略注释是禁用的。启用后,当某行没有任何诊断需要报告时,Oxlint 会报告类似 // oxlint-disable-line 的注释。

🌐 Reporting unused ignore comments is disabled by default. When enabled, Oxlint reports comments such as // oxlint-disable-line when no diagnostics would have been reported on that line.

启用报告:

🌐 Enable reporting:

bash
oxlint --report-unused-disable-directives

指定严重程度:

🌐 Specify the severity:

bash
oxlint --report-unused-disable-directives-severity error

一次只能使用其中一个选项。

🌐 Only one of these options can be used at a time.