Is there a way to add a reason comment to an inline rule-disabling comment?

Viewed 256

Sometimes we have linting rules with good reason, but which need to be disabled for a specific line.

I would like to be able to add a comment to the disabling comment

e.g.

// eslint-disable-next-line rulename because very good reasons
code.that.violates==rulename

Trying to do this causes problems for eslint because it thinks the rule you want to disable is called rulename because very good reasons

Obviously I could use a separate comment, but this comment must go before the disabling comment, otherwise the rule is only disabled for the comment line - i.e. this doesn't work

// eslint-disable-next-line rulename
// because very good reasons
code.that.violates==rulename

Putting the comment anywhere else is very confusing for the reader

Is there a way to achieve this all in one comment? Maybe with some kind of undocumented terminator? (I tried some obvious ones like ;, // # )

i.e.

// eslint-disable-next-line rulename ; because very good reasons

Better still, (assuming something like this is possible), can we force such a comment to be used?

1 Answers

Per the documentation, since ESLint 7:

Configuration comments can include descriptions to explain why the comment is necessary. The description must come after the configuration and needs to be separated from the configuration by two or more consecutive - characters. For example:

// 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');

In your case:

// eslint-disable-next-line rulename -- because very good reasons
code.that.violates==rulename

If you want to enforce this, you can use eslint-comments/require-description.

Related