Analysis of Eslint configuration rules

foreword

ESLint is a tool for identifying and reporting pattern matching in ECMAScript/JavaScript code, and its goal is to ensure code consistency and avoid errors. In many respects, it is similar to JSLint and JSHint, with a few exceptions:

  • ESLint uses Espree to parse JavaScript.
  • ESLint uses AST to analyze patterns in code ESLint is fully pluggable.
  • Each rule is a plugin and you can add more rules at runtime.

After using ESLint in the project, some rules will make people scratch their heads, but sometimes we feel that a certain rule is completely meaningless, so we want to disable some rules that are meaningless or you think are ridiculous. It sounds a bit self-defeating, but ESLint actually allows us to customize the rules.

configuration syntax

Add custom rules to the rules configuration item:

// 配置参数
rules: {
    
    
    "规则名1": [规则值, 规则配置],
    "规则名2": [规则值, 规则配置]
}

Rule value (determines whether to enable/disable):

"off"或者0    //关闭规则
"warn"或者1    //在打开的规则作为警告(不影响退出代码)
"error"或者2    //把规则作为一个错误(退出代码触发时为1)

Rule name (each rule corresponds to a rule name, just like the space indentation rule mentioned in the preface corresponds to indent):

Chinese document

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 ();

Guess you like

Origin blog.csdn.net/haoaiqian/article/details/115915235