eslint配置

835 阅读10分钟

最近一直苦恼eslint配置问题,找了很多的配置都不怎么完整,最后自己一点点的添加,最后整合成以下的配置,可以参考:

// eslint官方链接 https://eslint.org/docs/user-guide/configuring

module.exports = {
  root: true,
  parserOptions: {
    parser: 'babel-eslint',
    sourceType: 'module'
  },
  env: {
    browser: true,
    node: true,
    es6: true,
  },
  'extends': [
    'plugin:vue/recommended',
    'eslint:recommended'
  ],
  // required to lint *.vue files
  plugins: ['vue'],
  // add your custom rules here
  rules: {
    "vue/html-indent": [2, 2, {
      "attribute": 1,
      "baseIndent": 1,
      "closeBracket": 0,
      "alignAttributesVertically": true,
      "ignores": []
    }], // html的缩进
    "vue/max-attributes-per-line": [2, {
      "singleline": 10,
      "multiline": {
        "max": 1,
        "allowFirstLine": false
      }
    }], // 强制每行的最大属性数
    "vue/no-use-v-if-with-v-for": ["error", {
      "allowUsingIterationVar": false
    }], // 防止将v-for指令与v-if同一元素上的指令一起使用
    "vue/name-property-casing": ["error", "PascalCase"], // 为 Vue 组件中的 name 属性强制执行特定大小写
    "vue/return-in-computed-property": ["error", {
      "treatUndefinedAsUnspecified": false
    }], // 强制return声明存在于computed属性和函数中
    "vue/no-unused-components": ["error", {
      "ignoreWhenBindingPresent": true
    }], // 报告模板中尚未使用的组件
    "vue/attribute-hyphenation": ["error", "always", {
      "ignore": []
    }], // 自定义组件上使用带连字符的属性名称,例:is-prop
    "vue/component-name-in-template-casing": ["error", "PascalCase", {
      "ignores": []
    }], // 组件名用驼峰命名
    "vue/html-closing-bracket-newline": ["error", {
      "singleline": "never",
      "multiline": "never"
    }], // 警告位于配置位置以外的位置的右​​尖括号。
    "vue/html-closing-bracket-spacing": ["error", {
      "startTag": "never",
      "endTag": "never",
      "selfClosingTag": "always"
    }], // 在关闭标签 > 的括号之前强制执行一致的间距样式
    "vue/html-indent": ["error", 2, {
      "attribute": 1,
      "baseIndent": 1,
      "closeBracket": 0,
      "alignAttributesVertically": true,
      "ignores": []
    }],
    "vue/html-quotes": ["error", "double",{
      "avoidEscape": true
    }], // 如果属性的引号与配置的引号不同,则此规则会报告属性的引号
    "vue/html-self-closing": ["error", {
      "html": {
        "void": "always",
        "normal": "always",
        "component": "always"
      },
      "svg": "always",
      "math": "always"
    }], // 强制将自动关闭标志作为配置的样式
    "vue/max-attributes-per-line": ["error", {
      "singleline": 3,
      "multiline": {
        "max": 3,
        "allowFirstLine": true
      }
    }], // 强制执行模板中每行的多个属性
    "vue/multiline-html-element-content-newline": ["error", {
      "ignoreWhenEmpty": true,
      "ignores": ["pre", "textarea"]
    }], // 在多行元素的内容前后强制换行
    "vue/mustache-interpolation-spacing": ["error", "always"], // 在胡须插值中强制执行统一间距
    "vue/no-multi-spaces": ["error", {
      "ignoreProperties": false
    }], // 删除标签中不用于缩进的多个空格。
    "vue/no-spaces-around-equal-signs-in-attribute": ["error"], // 不允许属性中的等号周围有空格
    "vue/no-template-shadow": ["error"], // 消除 v-for 指令或范围属性的阴影变量声明。
    "vue/prop-name-casing": ["error", "camelCase"], // 在 vue 组件(camelCase)中对 props 进行适当的大小写。
    "vue/require-default-prop": ["error"], // 要求为每个未标记为required(Boolean道具除外)的道具设置默认值
    "vue/v-bind-style": ["error", "shorthand"], // 强制执行v-bind指令样式,您应该使用简写或长格式
    "vue/v-on-style": ["error", "shorthand"], // 强制执行v-on指令样式,您应该使用简写或长格式
    "vue/attributes-order": ["error", {
      "order": [
        "DEFINITION",
        "LIST_RENDERING",
        "CONDITIONALS",
        "RENDER_MODIFIERS",
        "GLOBAL",
        "UNIQUE",
        "TWO_WAY_BINDING",
        "OTHER_DIRECTIVES",
        "OTHER_ATTR",
        "EVENTS",
        "CONTENT"
      ]
    }], // 强制对组件属性进行排序
    "vue/order-in-components": ["error", {
      "order": [
        "el",
        "name",
        "parent",
        "functional",
        ["delimiters", "comments"],
        ["components", "directives", "filters"],
        "extends",
        "mixins",
        "inheritAttrs",
        "model",
        ["props", "propsData"],
        "data",
        "computed",
        "watch",
        "LIFECYCLE_HOOKS",
        "methods",
        ["template", "render"],
        "renderError"
      ]
    }], // 确保您保持组件中属性的声明顺序
    "vue/this-in-template": ["error", "never"], // 防止this在 Vue 模板中使用。
    'accessor-pairs': 2,
    'arrow-spacing': [2, {
      'before': true,
      'after': true
    }], // 要求箭头函数的箭头之前或之后有空格
    'block-spacing': [2, 'always'], // 在打开块之后和关闭块之前禁止或强制块内的空格
    'block-scoped-var': 2, //将变量声明放在合适的代码块里
    'brace-style': [2, '1tbs', { allowSingleLine: false }], //大括号风格
    'complexity': 0, //限制条件语句的复杂度
    'consistent-return': 2, //无论有没有返回值都强制要求return语句返回一个值
    'default-case': 0, //在switch语句中需要有default语句
    'dot-notation': ['error', { allowKeywords: false, allowPattern: '' }], //获取对象属性的时候使用点号
    'camelcase': [0, {
        'properties': 'always'
    }], //强制驼峰命名规则
    'comma-dangle': [2, 'never'], //是否允许对象中出现结尾逗号
    'comma-spacing': [2, {
        'before': false,
        'after': true
    }], // 强制逗号周围的间距
    'comma-style': [2, 'last'], //逗号风格
    'constructor-super': 2, // 验证super()构造函数中的调用
    'consistent-this': [0, 'self'], //当获取当前环境的this是用一样的风格
    'curly': ['error', 'all'], //强制使用花括号的风格
    'dot-location': [2, 'property'], // 在点之前和之后强制换行
    'eol-last': 0,
    'eqeqeq': [2, 'smart'], //比较的时候使用严格等于
    'func-names': 0, //函数表达式必须有名字
    'func-style': 0, //函数风格,规定只能使用函数声明或者函数表达式
    'func-call-spacing': ['error', 'never'], //函数调用时,函数名与()之间不能有空格
    'generator-star-spacing': [2, {
        'before': true,
        'after': true
    }], // 在生成器函数中在 * 周围强制间距
    'handle-callback-err': [2, '^(err|error)$'], // 强制回调错误处理
    'guard-for-in': 0, //监视for in循环,防止出现不可预料的情况
    "indent": [2, 2, { "SwitchCase": 1 }], //强制一致的缩进风格
    'jsx-quotes': [2, 'prefer-single'], // 强制在 JSX 属性(jsx-quotes)中一致使用双引号或单引号
    'key-spacing': [2, {
        'beforeColon': false,
        'afterColon': true
    }], //对象字面量中冒号的前后空格
    'keyword-spacing': [2, {
        'before': true,
        'after': true
    }], //关键字前后的空格
    'max-nested-callbacks': 0, //回调嵌套深度
    'max-depth': 0, //嵌套块深度
    'max-len': [2, { "code": 100 }], //一行最大长度,单位为字符
    'max-params': 0, //函数最多能有多少个参数
    'max-statements': 0, //函数内最多有几个声明
    'newline-after-var': 0, //变量声明后必须空一行
    'new-cap': [2, {
        'newIsCap': true,
        'capIsNew': false
    }], //构造函数名字首字母要大写
    'new-parens': 2, //new时构造函数必须有小括号
    'no-bitwise': 0, //不允许使用位运算符
    // 'generator-star-spacing': [2, 'both'], //生成器函数前后空格
    // 'standard/object-curly-even-spacing': [2, 'either'],
    'no-plusplus': 0, //不允许使用++ --运算符
    'no-debugger': process.env.NODE_ENV === 'production' ? 'error' : 'off',
    'no-cond-assign': 2, //条件语句的条件中不允许出现赋值运算符
    'no-console': 'off', //不允许出现console语句
    'no-constant-condition': 2, //条件语句的条件中不允许出现恒定不变的量
    'no-control-regex': 2, //正则表达式中不允许出现控制字符
    // 'no-debugger': 2, //不允许出现debugger语句
    'no-dupe-args': 2, //函数定义的时候不允许出现重复的参数
    'no-dupe-keys': 2, //对象中不允许出现重复的键
    'no-duplicate-case': 2, //switch语句中不允许出现重复的case标签
    'no-empty': 2, //不允许出现空的代码块
    'no-empty-character-class': 2, //正则表达式中不允许出现空的字符组
    'no-ex-assign': 2, //在try catch语句中不允许重新分配异常变量
    'no-extra-boolean-cast': 2, //不允许出现不必要的布尔值转换
    'no-extra-parens': 0, //不允许出现不必要的圆括号
    'no-extra-semi': 2, //不允许出现不必要的分号
    'no-func-assign': 2, //不允许重新分配函数声明
    'no-inner-declarations': ['error', 'functions'], //不允许在嵌套代码块里声明函数
    'no-invalid-regexp': 2, //不允许在RegExp构造函数里出现无效的正则表达式
    'no-irregular-whitespace': 2, //不允许出现不规则的空格
    'no-negated-in-lhs': 2, //不允许在in表达式语句中对最左边的运算数使用取反操作
    'no-obj-calls': 2, //不允许把全局对象属性当做函数来调用
    'no-regex-spaces': 2, //正则表达式中不允许出现多个连续空格
    'no-sparse-arrays': 2, //数组中不允许出现空位置
    'no-unreachable': 2, //在return,throw,continue,break语句后不允许出现不可能到达的语句
    'no-alert': 1, //不允许使用alert,confirm,prompt语句
    'no-caller': 2, //不允许使用arguments.callee和arguments.caller属性
    'no-class-assign': 2, // 禁止修改类声明的变量
    'no-div-regex': 2, //不能使用看起来像除法的正则表达式
    'no-else-return': 0, //如果if语句有return,else里的return不用放在else里
    'no-labels': [
        'error',
        {
            allowLoop: false,
            allowSwitch: false
        }
    ], //不允许标签语句
    'no-eq-null': 2, //不允许对null用==或者!=
    'no-eval': 2, //不允许使用eval()
    'no-extend-native': 2, //不允许扩展原生对象
    'no-extra-bind': 2, //不允许不必要的函数绑定
    'no-fallthrough': 2, //不允许switch按顺序全部执行所有case
    'no-floating-decimal': 2, //不允许浮点数缺失数字
    'no-implied-eval': 2, //不允许使用隐式eval()
    'no-iterator': 2, //不允许使用__iterator__属性
    'no-lone-blocks': 2, //不允许不必要的嵌套代码块
    'no-loop-func': 2, //不允许在循环语句中进行函数声明
    'no-multi-spaces': 2, //不允许出现多余的空格
    'no-multi-str': 2, //不允许用\来让字符串换行
    'no-global-assign': 2, //不允许重新分配原生对象
    'no-new': 2, //不允许new一个实例后不赋值或者不比较
    'no-new-func': 2, //不允许使用new Function
    'no-new-wrappers': 2, //不允许使用new String,Number和Boolean对象
    'no-octal': 2, //不允许使用八进制字面值
    'no-octal-escape': 2, //不允许使用八进制转义序列
    'no-param-reassign': 0, //不允许重新分配函数参数"no-proto": 2, //不允许使用__proto__属性
    'no-redeclare': 2, //不允许变量重复声明
    'no-return-assign': 2, //不允许在return语句中使用分配语句
    'no-script-url': 2, //不允许使用javascript:void(0)
    'no-self-compare': 2, //不允许自己和自己比较
    'no-sequences': 2, //不允许使用逗号表达式
    'no-throw-literal': 2, //不允许抛出字面量错误 throw "error"
    'no-unused-expressions': 2, //不允许无用的表达式
    'no-void': 2, //不允许void操作符
    'no-warning-comments': [1, { terms: ['todo', 'fixme', 'any other term'] }], //不允许警告备注
    'no-with': 2, //不允许使用with语句
    'no-catch-shadow': 2, //不允许try catch语句接受的err变量与外部变量重名"no-delete-var": 2, //不允许使用delete操作符
    'no-label-var': 2, //不允许标签和变量同名
    // 'no-shadow': 2, //外部作用域中的变量不能与它所包含的作用域中的变量或参数同名
    'no-shadow-restricted-names': 2, //js关键字和保留字不能作为函数名或者变量名
    'no-undef': 2, //不允许未声明的变量
    'no-undef-init': 2, //不允许初始化变量时给变量赋值undefined
    // 'no-undefined': 2, //不允许把undefined当做标识符使用
    'no-unused-vars': [1, { vars: 'all', args: 'after-used' }], //警告有声明后未使用的变量或者参数
    'no-use-before-define': [2, 'nofunc'], //不允许在未定义之前就使用变量
    // 'eol-last': 2, //文件以换行符结束
    'no-array-constructor': 2, //不允许使用数组构造器
    'no-inline-comments': 0, //不允许行内注释
    'no-lonely-if': 0, //不允许else语句内只有if语句
    'no-mixed-spaces-and-tabs': [2, 'smart-tabs'], //不允许混用tab和空格
    'no-multiple-empty-lines': [2, { max: 2 }], //空行最多不能超过两行
    'no-nested-ternary': 2, //不允许使用嵌套的三目运算符
    'no-new-object': 2, //禁止使用new Object()
    'no-ternary': 0, //不允许使用三目运算符
    'no-trailing-spaces': 2, //一行最后不允许有空格
    'no-underscore-dangle': 2, //不允许标识符以下划线开头
    'no-var': 0, //使用let和const代替var
    'one-var': 0, //强制变量声明放在一起
    'operator-assignment': 0, //赋值运算符的风格
    'padded-blocks': [2, 'never'], //块内行首行尾是否空行
    'prefer-const': 2,
    'quote-props': 0, //对象字面量中属性名加引号
    'quotes': [2, 'single', {
        'avoidEscape': true,
        'allowTemplateLiterals': true
    }], //引号风格
    'radix': 1, //使用parseInt时强制使用基数来指定是十进制还是其他进制
    'semi': [2, 'never'], // 禁止末尾使用分号
    'semi-spacing': [2, {
        'before': false,
        'after': true
    }], //分后前后空格
    'sort-vars': 0, //变量声明时排序
    'space-before-blocks': [2, 'always'], //块前的空格
    'space-before-function-paren': 0, //函数定义时括号前的空格
    'space-in-parens': [2, 'never'], // 禁止或强制使用括号内的空格
    'space-infix-ops': [2, { 'int32Hint': true }], //操作符周围的空格
    'space-unary-ops': [2, { 'words': true, 'nonwords': false }], //一元运算符前后不要加空格
    'spaced-comment': [2, 'always', {
        'markers': ['global', 'globals', 'eslint', 'eslint-disable', '*package', '!', ',']
    }], // 要求或禁止以空格(空格或制表符)开始评论
    'strict': [2, 'function'], //使用严格模式
    'template-curly-spacing': [2, 'never'], // 在模板字符串中强制使用间距
    'wrap-regex': 2, //正则表达式字面量用括号括起来
    'use-isnan': 2, //要求检查NaN的时候使用isNaN()
    'valid-jsdoc': [
        'error',
        {
            'requireReturn': false,
            'requireParamDescription': false,
            'requireReturnDescription': true
        }
    ], //强制JSDoc注释
    'valid-typeof': [
        2,
        {
            'requireStringLiterals': true
        }
    ], //在使用typeof表达式比较的时候强制使用有效的字符串
    'vars-on-top': 0, //var必须放在作用域顶部
    'wrap-iife': [2, 'any'], //立即执行表达式的括号风格
    'yoda': [2, 'never', { exceptRange: true }], //不允许在if条件中使用yoda条件
  }
}