强迫症的eslint配置(初学者)

480 阅读1分钟

.eslintrc.js

// eslint-disable-next-line no-undef
module.exports = {
   "env": {
      "browser": true,
      "es2021" : true
   },
   "extends": "eslint:recommended",

   "parser" : "@typescript-eslint/parser",
   "plugins": ["@typescript-eslint"],

   "parserOptions": {"ecmaVersion": 12},

   "rules": {
      // # Possible Errors
      //
      "no-constant-condition": ["warn"],
      // 允许 if(...){带有花括号的空代码}
      "no-empty"             : ["off"],
      // 数组, 对象 逗号后面强制一空格
      "comma-spacing"        : [
         "error",
         {"after": true}
      ],
      // 对象单行 多行对齐模式
      "key-spacing": [
         "error",
         {
            "singleLine": {},
            "multiLine" : {"align": "colon"}
         }
      ],
      // # Best Practices
      // 对象换行策略 , 如果多行, `{`和`}` 前后不能跟值
      "object-curly-newline": [
         "error",
         {"multiline": true}
      ],
      "array-bracket-newline": [
         "error",
         {"multiline": true}
      ],
      // if后面强制花括号
      "curly": [
         "error",
         "all"
      ],
      // # Strict Mode
      // # Variables
      "no-unused-vars": ["warn"],

      // # Stylistic Issues

      "quotes": [
         "error",
         "double"
      ],
      "indent": [
         "error",
         "tab"
      ],
      "linebreak-style": [
         "error",
         "windows"
      ],
      // if else try catch 换行
      "brace-style": [
         "error",
         "stroustrup",
         {"allowSingleLine": false}
      ],
      // 操作符周围前后必须空格
      "space-infix-ops": ["error"],
      // 禁止尾逗号 , 微信浏览器 , uc浏览器的问题
      "comma-dangle"   : [
         "error",
         "never"
      ]
      // # ECMAScript 6
      // # Deprecated
      // # Removed

   }
}

验证示例 :

 
var a = 123;
console.log(a);
let b;
b = "123";

a = [1, 2, 3, 4]
a = [
   1,
   2,
   3,
   4
]
let c; 
c = {
   x : "1",
   yy: "3"
}
c = {}
c = {x: "1", yy: "3"} 
function f() {
   let x = 1;
   if (1) {
      x += 1
   }
   else {
   }
   try {

   }
   catch (e) {}
}

let d = "foo"
   ? "bar"
   : "baz";


let exp = {
   "env": {
      "browser": true,
      "es2021" : true
   },
   "extends": "eslint:recommended",

   "parser" : "@typescript-eslint/parser",
   "plugins": ["@typescript-eslint"]
}