前言
ESLint 是在 ECMAScript/JavaScript 代码中识别和报告模式匹配的工具,它的目标是保证代码的一致性和避免错误。在许多方面,它和 JSLint、JSHint 相似,除了少数的例外:
- ESLint 使用 Espree 解析 JavaScript。
- ESLint 使用 AST 去分析代码中的模式 ESLint是完全插件化的。
- 每一个规则都是一个插件并且你可以在运行时添加更多的规则。
在项目中使用了ESLint后,有些规则会让人抓破脑袋,但是我们有时会觉得某个规则完全没有意义,就想禁用某些没有意义或者你觉得很扯的规则,听起来有些自暴自弃,但ESLint其实是允许我们自定义规则的。
配置语法
把自定义规则加入到 rules 配置项中即可:
// 配置参数
rules: {
"规则名1": [规则值, 规则配置],
"规则名2": [规则值, 规则配置]
}
规则值(决定了是否开启/关闭):
"off"或者0 //关闭规则
"warn"或者1 //在打开的规则作为警告(不影响退出代码)
"error"或者2 //把规则作为一个错误(退出代码触发时为1)
规则名(每个规则对应一个规则名,就像前言提到的空格缩进规则对应 indent 一样):
always or never
- “never” (default) disallows space between the function name and the opening parenthesis.
- “always” requires space between the function name and the opening parenthesis.
Further, in “always” mode, a second object option is available that contains a single boolean allowNewlines property.
never
1、Examples of incorrect code for this rule with the default “never” option:
/*eslint func-call-spacing: ["error", "never"]*/
fn ();
fn
();
2、Examples of correct code for this rule with the default “never” option:
/*eslint func-call-spacing: ["error", "never"]*/
fn();
always
1、Examples of incorrect code for this rule with the “always” option:
/*eslint func-call-spacing: ["error", "always"]*/
fn();
fn
();
2、Examples of correct code for this rule with the “always” option:
/*eslint func-call-spacing: ["error", "always"]*/
fn ();