最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to add exception to a eslint rule? - Stack Overflow

programmeradmin7浏览0评论

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?

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?

Share Improve this question asked Jun 5, 2016 at 21:14 WitVaultWitVault 24.1k20 gold badges106 silver badges137 bronze badges 3
  • Hmm, I suspect that rule was created specifically to catch the code you want to allow. – Barmar Commented Jun 5, 2016 at 22:18
  • @Barmar Should I remove this rule then? – WitVault Commented Jun 6, 2016 at 12:48
  • Probably, I doubt there's any way to get it to distinguish the two cases. – Barmar Commented Jun 6, 2016 at 15:53
Add a comment  | 

1 Answer 1

Reset to default 16

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.

发布评论

评论列表(0)

  1. 暂无评论