eslint配置方式有两种:
有几种东西是可以配置的:
我们这里使用配置文件.eslintrc.js来配置,它导出一个模块供ESLint识别。
// http://eslint.org/docs/user-guide/configuring module.exports = { root: true, parser: ‘babel-eslint‘,//解析器,这里我们使用babel-eslint parserOptions: { sourceType: ‘module‘//类型为module,因为代码使用了使用了ECMAScript模块 }, env: { browser: true,//预定义的全局变量,这里是浏览器环境 }, // https://github.com/feross/standard/blob/master/RULES.md#javascript-standard-style //extends: ‘standard‘, //扩展,可以通过字符串或者一个数组来扩展规则 // required to lint *.vue files plugins: [ ‘html‘ //插件,此插件用于识别文件中的js代码,没有MIME类型标识没有script标签也可以识别到,因此拿来识别.vue文件中的js代码 ], // add your custom rules here ‘rules‘: { //这里写自定义规则 } }
ESLint的规则有三种级别:
有时候代码里有些特殊情况需要我们在某一行或者某几行关闭ESLint检测,可以使用注释:
下面的代码会关闭所有规则
/* eslint-disable */ alert(‘foo‘); /* eslint-enable */
下面的代码会关闭某一行的所有规则
alert(‘foo‘); // eslint-disable-line // eslint-disable-next-line alert(‘foo‘);
下面的代码在某一行关闭指定的规则
alert(‘foo‘); // eslint-disable-line no-alert // eslint-disable-next-line no-alert alert(‘foo‘);
常用规则:
‘rules‘: { "comma-dangle": ["error", "never"], //是否允许对象中出现结尾逗号 "no-cond-assign": 2, //条件语句的条件中不允许出现赋值运算符 "no-console": 2, //不允许出现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, //正则表达式中不允许出现多个连续空格 "quote-props": 2, //对象中的属性名是否需要用引号引起来 "no-sparse-arrays": 2, //数组中不允许出现空位置 "no-unreachable": 2, //在return,throw,continue,break语句后不允许出现不可能到达的语句 "use-isnan": 2, "valid-jsdoc": ["error", { "requireReturn": false, "requireParamDescription": false, "requireReturnDescription": true }], "valid-typeof": ["error", { "requireStringLiterals": true }], "block-scoped-var": 2, "complexity": 0, "consistent-return": 2, "curly": ["error", "all"], "default-case": 0, "dot-notation": ["error", {"allowKeywords": false, "allowPattern": ""}], "eqeqeq": ["error", "smart"], "no-alert": 1, "no-caller": 2, "guard-for-in": 0, "no-div-regex": 2, "no-else-return": 0, "no-labels": ["error", { "allowLoop": false, "allowSwitch": false }], "no-eq-null": 2, "no-eval": 2, "no-extend-native": 2, "no-extra-bind": 2, "no-fallthrough": 2, "no-floating-decimal": 2, "no-implied-eval": 2, "no-iterator": 2, "no-lone-blocks": 2, "no-loop-func": 2, "no-multi-spaces": 2, "no-multi-str": 2, "no-native-reassign": 2, "no-new": 2, "no-new-func": 2, "no-new-wrappers": 2, "no-octal": 2, "no-octal-escape": 2, "no-param-reassign": 0, "no-process-env": 0, "no-proto": 2, "no-redeclare": 2, "no-return-assign": 2, "no-script-url": 2, "no-self-compare": 2, "no-sequences": 2, "no-throw-literal": 2, "no-unused-expressions": 2, "no-void": 2, "no-warning-comments": [1, {"terms": ["todo", "fixme", "any other term"]}], "no-with": 2, "radix": 1, "vars-on-top": 0, "wrap-iife": [2, "any"], "yoda": [2, "never", {"exceptRange": true}], "strict": [2, "function"], "no-catch-shadow": 2, "no-extra-strict": 2, "no-delete-var": 2, "no-label-var": 2, "no-shadow": 2, "no-shadow-restricted-names": 2, "no-undef": 2, "no-undef-init": 2, "no-undefined": 2, "no-unused-vars": [2, {"vars": "all", "args": "after-used"}], "no-use-before-define": [2, "nofunc"], "handle-callback-err": [2, "^(err|error|anySpecificError)$"], "no-mixed-requires": [1, true], "no-new-require": 2, "no-path-concat": 0, "no-process-exit": 2, "no-restricted-modules": 0, "no-sync": 0, "indent": 2, "brace-style": [2, "1tbs", { "allowSingleLine": false}], "camelcase": [2, {"properties": "never"}], "comma-style": [2, "last"], "consistent-this": [0, "self"], "eol-last": 2, "func-names": 0, "func-style": 0, "key-spacing": [2, {"beforeColon": false, "afterColon": true}], "max-nested-callbacks": 0, "new-cap": [2, {"newIsCap": true, "capIsNew": false}], "new-parens": 2, "newline-after-var": 0, "no-array-constructor": 2, "no-inline-comments": 0, "no-lonely-if": 0, "no-mixed-spaces-and-tabs": [2, "smart-tabs"], "no-multiple-empty-lines": [2, {"max": 2}], "no-nested-ternary": 2, "no-new-object": 2, "no-spaced-func": 2, "no-ternary": 0, "no-trailing-spaces": 2, "no-underscore-dangle": 2, "no-wrap-func": 0, "one-var": 0, "operator-assignment": 0, "padded-blocks": [2, "never"], "quote-props": 0, "quotes": [1, "single", "avoid-escape"], "semi": [2, "always"], "semi-spacing": [2, {"before": false, "after": true}], "sort-vars": 0, "space-before-blocks": [2, "always"], "space-before-function-paren": [2, {"anonymous": "always", "named": "never"}], "space-infix-ops": [2, {"int32Hint": true}], "space-return-throw-case": 2, "space-unary-ops": [2, { "words": true, "nonwords": false}], "wrap-regex": 2, "no-var": 0, "generator-star-spacing": [2, "both"], "max-depth": 0, "max-len": 0, "max-params": 0, "max-statements": 0, "no-bitwise": 0, "no-plusplus": 0 }
原文:http://www.cnblogs.com/hahazexia/p/6393212.html