简体   繁体   English

如何在eslint规则中添加异常?

[英]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. 我正在尝试使用linting规则 - 在我的eslint配置文件中禁止多个空格(无多个空格) I am using current latest version of eslint. 我正在使用当前最新版本的eslint。

I know that this rule does not allows multiple spaces in statments eg 我知道这条规则不允许在法规中使用多个空格,例如

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? 在这种变量声明的情况下,有什么方法可以阻止lint规则被应用吗?

This rule ( no-multi-spaces ) has an option exceptions that you can configure to ignore all variable declarations. 此规则( no-multi-spaces )具有一个选项exceptions ,您可以将其配置为忽略所有变量声明。 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规则。 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. ESLint支持块样式注释: /* eslint-disable */ /* eslint-enable */禁用所有规则或/* eslint-disable rule-name */ /* eslint-enable rule-name*/禁用特定规则。 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. 您还可以使用内联注释: // eslint-disable-line禁用当前行的所有规则,或// eslint-disable-line rule-name禁用特定规则。 Same applies to // eslint-disable-next-line but for the following line. 这同样适用于// eslint-disable-next-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. 不是每个规则都适合每个人,并且没有奖励尽可能多的规则。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM