Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add exception to a eslint rule?

I am trying to use linting rule -- Disallow multiple spaces (no-multi-spaces) in my eslint configuration file. I am using current latest version of eslint.

I know that this rule does not allows multiple spaces in statments e.g

if(foo  === "bar") {}

It will fail here, which is perfectly fine. But It will fail in this case also

var React          = require('react');
var ReactRouter    = require('react-router');

I don't want the rule to be applied here.

Is there any way I can stop lint rule from being applied in this case of variable declaration?

like image 869
WitVault Avatar asked Jun 05 '16 21:06

WitVault


People also ask

How do you exclude something from ESLint?

To turn off ESLint in the whole file, you can add /* eslint-disable */ in the first line of that file. Alternatively, you can create a file . eslintignore in the root catalog.

How do I disable a specific ESLint rule?

If you want to disable an ESLint rule in a file or on a specific line, you can add a comment. On a single line: const message = 'foo'; console. log(message); // eslint-disable-line no-console // eslint-disable-next-line no-console console.


1 Answers

This rule (no-multi-spaces) has an option exceptions that you can configure to ignore all variable declarations. Configuration should look something like this:

no-multi-spaces: ["error", { exceptions: { "VariableDeclarator": true } }]

This will skip all of the variable declarations from the check.

In general, you can also use comments to disable any of the ESLint rules. ESLint supports block style comments: /* eslint-disable */ /* eslint-enable */ to disable all rules or /* eslint-disable rule-name */ /* eslint-enable rule-name*/ to disable specific rule. Everything between disable and enable comments will be ignored. You can also use inline comments as well: // eslint-disable-line to disable all rules for current line, or // eslint-disable-line rule-name to disable specific rule. Same applies to // eslint-disable-next-line but for the following line.

If all else fails, you can just disable the rule in the configuration. If you find yourself in the situation were you need to use comments constantly, most likely this rule just doesn't fit your coding style. Not every rule fits everyone, and there's no prize for enabling as many rules as possible.

like image 82
Ilya Volodin Avatar answered Oct 25 '22 07:10

Ilya Volodin