eslint项目配置流痕

211 阅读2分钟
module.exports = {
    env: {
        browser: true,
        commonjs: true,
        es2020: true,
        node: true
    },
    parserOptions: {
        //对于ES6+的语法和用import/export的语法必须用module
        sourceType: "module",
        //ECMAScript的版本
        ecmaVersion: 10
    },
    parser: "babel-eslint",
    extends: ["eslint:recommended"],
    rules: {
        //禁止对function声明重新赋值
        "no-func-assign": "off",
        //禁止出现未使用过的变量
        "no-unused-vars": "error",
        //禁止未声明的变量,除非它们在/*global*/注释中被提到
        "no-undef": "error",
        //禁止在条件中使用常量表达式
        "no-constant-condition": "off",

        //规则
        //缩进
        "indent": ["warn", 4, { SwitchCase: 1 }],

        //文件最大行数
        // "max-lines": ["error", { max: 600, skipBlankLines: true, skipComments: true }],
        //最大变量声明数量 10->20
        "max-statements": ["error", 20],
        //每行最大声明数量 1->10
        "max-statements-per-line": ["error", { max: 10 }],
        //单行120个字符长度
        "max-len": ["error", { code: 200 }],
        //块语句最大嵌套深度
        "max-depth": ["error", 5],
        //参数最大个数 4->6
        "max-params": ["error", 4],
        //每个函数最大行数(不包含空行,不包含注释)
        "max-lines-per-function": ["error", { max: 60, skipBlankLines: true, skipComments: true }],
        //回调函数最大嵌套层数
        "max-nested-callbacks": ["error", 5],
        //每个函数圈复杂度
        "complexity": ["error", { max: 20 }],
        //禁止使用脱尾逗号
        "comma-dangle": ["error", "never"],
        //变量驼峰命名
        "camelcase": "warn",
        //禁止 if 语句作为唯一语句出现在 else 语句块中
        "no-lonely-if": "off",
        //对一对混合操作符发出一个错误
        "no-mixed-operators": "warn",
        //禁止在含有 else 分支的 if 语句 的情况使用否定表达式
        "no-negated-condition": "off",
        //禁止使用嵌套的三元表达式
        "no-nested-ternary": "error",

        //变量
        //强制变量在声明时进行初始化
        "init-declarations": ["warn", "always"],
        //禁止变量声明覆盖外层作用域的变量
        // "no-shadow": "warn",
        //先声明,后引用
        "no-use-before-define": ["error", { functions: false }],

        //最佳实践
        //强制把变量的使用限制在其定义的作用域范围内
        "block-scoped-var": "warn",
        //确保代码块使用了大括号包裹以避免bug的发生,并且增加代码的清晰度
        "curly": "off",
        //switch 必须有默认分支
        "default-case": "error",
        //点号与属性放一行
        "dot-location": ["warn", "property"],
        //不允许空函数
        "no-empty-function": "warn",
        //因为 null 其本身以及 null 与 undefined 比较结果都为 true
        "no-eq-null": "error",
        //循环条件中的变量在循环中是要经常改变的。如果不是这样,那么可能是个错误。
        "no-unmodified-loop-condition": "error"
    }
};