有没有办法将原因注释添加到内联规则禁用注释中?

DJL*_*DJL 5 eslint

有时我们有充分的理由制定 linting 规则,但需要针对特定​​行禁用这些规则。

我希望能够向禁用评论添加评论

例如

// eslint-disable-next-line rulename because very good reasons
code.that.violates==rulename
Run Code Online (Sandbox Code Playgroud)

尝试这样做会给 eslint 带来问题,因为它认为您要禁用的规则被调用rulename because very good reasons

显然我可以使用单独的注释,但该注释必须位于禁用注释之前,否则该规则仅针对注释行禁用 - 即这不起作用

// eslint-disable-next-line rulename
// because very good reasons
code.that.violates==rulename
Run Code Online (Sandbox Code Playgroud)

将评论放在其他地方会让读者感到非常困惑

有没有一种方法可以在一条评论中实现这一切?也许有某种未记录的终结者?(我尝试了一些明显的,例如;// #

IE

// eslint-disable-next-line rulename ; because very good reasons
Run Code Online (Sandbox Code Playgroud)

更好的是,(假设这样的事情是可能的),我们可以强制使用这样的注释吗?

jon*_*rpe 13

根据文档,自ESLint 7起:

配置注释可以包括说明,以解释为什么需要该注释。描述必须位于配置之后,并且需要与配置用两个或多个连续-字符分隔。例如:

// eslint-disable-next-line no-console -- Here's a description about why this configuration is necessary.
console.log('hello');

/* eslint-disable-next-line no-console --
 * Here's a very long description about why this configuration is necessary
 * along with some additional information
**/
console.log('hello');
Run Code Online (Sandbox Code Playgroud)

在你的情况下:

// eslint-disable-next-line rulename -- because very good reasons
code.that.violates==rulename
Run Code Online (Sandbox Code Playgroud)

如果您想强制执行此操作,可以使用eslint-comments/require-description.