eslint配置方式javascript
注释配置:使用js注释来直接嵌入ESLint配置信息到一个文件里html
配置文件:使用一个js文件,JSON或者YAML文件来给整个目录和它的子目录指定配置信息。这些配置能够写在一个文件名为.eslintrc.*的文件或者在package.json文件里的eslintConfig项里,vue
这两种方式ESLint都会自动寻找而后读取,或者你也能够在命令行里指定一个配置文件。java
有几种东西是能够配置的node
环境:你的脚本会在哪一种环境下运行。每一个环境带来了一组特定的预约义的全局变量。react
全局变量:脚本运行期间会访问额外的全局变量。git
规则:使用那些规则,而且规则的等级是多少。es6
ESLint的规则有三种级别github
"off"或者0,不启用这个规则正则表达式
"warn"或者1,出现问题会有警告
"error"或者2,出现问题会报错
关闭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');
本身经常使用配置:
module.exports = { root: true, env: { /** * 规则适用 */ browser: true, node: true, commonjs: true, es6: true, amd: true }, 'extends': [ 'plugin:vue/essential', '@vue/standard' ], rules: { /** * 运行环境 */ 'no-console': process.env.NODE_ENV === 'production' ? 'error' : 'off', 'no-debugger': process.env.NODE_ENV === 'production' ? 'error' : 'off', /** * 不须要 */ 'indent': 0, 'quotes': 0, // 不区分单双引号 'key-spacing': 0, // 冒号前不用放空格 'semi': 0, 'eqeqeq': 0, // 必须使用全等 'arrow-parens': 0, // 箭头函数有参数时不用加圆括号 'space-before-function-paren': 0, // 函数前面不须要空格 'one-var': 0, //容许var实现多个变量 /** * 警告 */ 'no-extra-boolean-cast': 1, // 没必要要的bool转换 'no-extra-parens': 1, // 非必要的括号 'no-empty': 1, // 块语句中的内容不能为空 'no-use-before-define': [1, 'nofunc'], // 未定义前不能使用 'complexity': [1, 10], // 循环复杂度 'no-unused-vars': 1 // 不能有声明后未被使用的变量或参数 }, parserOptions: { parser: 'babel-eslint', ecmaFeatures: { 'jsx': true } } }
给项目配置eslintrc.js
在vue-cli@3.0脚手架下会自动建立eslintrc.js文件,按照如下配置:
按 Ctrl+C 复制代码 // https://eslint.org/docs/user-guide/configuring module.exports = { //此项是用来告诉eslint找当前配置文件不能往父级查找 root: true, //此项是用来指定eslint解析器的,解析器必须符合规则,babel-eslint解析器是对babel解析器的包装使其与ESLint解析 parser: 'babel-eslint', //此项是用来指定javaScript语言类型和风格,sourceType用来指定js导入的方式,默认是script,此处设置为module,指某块导入方式 parserOptions: { // 设置 script(默认) 或 module,若是代码是在ECMASCRIPT中的模块 sourceType: 'module', "ecmaVersion": 6, "ecmaFeatures": { "jsx": true } }, // 此项指定环境的全局变量,下面的配置指定为浏览器环境 env: { "browser": true, "node": true, "commonjs": true, "es6": true, "amd": true }, // https://github.com/standard/standard/blob/master/docs/RULES-en.md // 此项是用来配置标准的js风格,就是说写代码的时候要规范的写,若是你使用vs-code我以为应该能够避免出错 extends: 'vue', // 此项是用来提供插件的,插件名称省略了eslint-plugin-,下面这个配置是用来规范html的 plugins: [ 'html', "flow-vars", "react" ], /* 下面这些rules是用来设置从插件来的规范代码的规则,使用必须去掉前缀eslint-plugin- 主要有以下的设置规则,能够设置字符串也能够设置数字,二者效果一致 "off" -> 0 关闭规则 "warn" -> 1 开启警告规则 "error" -> 2 开启错误规则 */ rules: { // 不须要 "space-before-function-paren": 0, // 函数定义时括号前面要不要有空格 "eol-last": 0, // 文件以单一的换行符结束 "no-extra-semi": 0, // 能够多余的冒号 "semi": 0, // 语句能够不须要分号结尾 "eqeqeq": 0, // 必须使用全等 "one-var": 0, // 连续声明 "no-undef": 0, // 能够 有未定义的变量 // 警告 "no-extra-boolean-cast": 1, // 没必要要的bool转换 "no-extra-parens": 1, // 非必要的括号 "no-empty": 1, // 块语句中的内容不能为空 "no-use-before-define": [1, "nofunc"], // 未定义前不能使用 "complexity": [1, 10], // 循环复杂度 "no-unused-vars": 1, // 不能有声明后未被使用的变量或参数 // vue "flow-vars/define-flow-type": 1, "flow-vars/use-flow-type": 1, // react "react/jsx-uses-react": 2, "react/jsx-uses-vars": 2, // 错误 "comma-dangle": [2, "never"], // 对象字面量项尾不能有逗号 "no-debugger": 2, // 禁止使用debugger "no-constant-condition": 2, // 禁止在条件中使用常量表达式 if(true) if(1) "no-dupe-args": 2, // 函数参数不能重复 "no-dupe-keys": 2, // 在建立对象字面量时不容许键重复 {a:1,a:1} "no-duplicate-case": 2, // switch中的case标签不能重复 "no-empty-character-class": 2, // 正则表达式中的[]内容不能为空 "no-invalid-regexp": 2, // 禁止无效的正则表达式 "no-func-assign": 2, // 禁止重复的函数声明 "valid-typeof": 2, // 必须使用合法的typeof的值 "no-unreachable": 2, // 不能有没法执行的代码 "no-unexpected-multiline": 2, // 避免多行表达式 "no-sparse-arrays": 2, // 禁止稀疏数组, [1,,2] "no-shadow-restricted-names": 2, // 严格模式中规定的限制标识符不能做为声明时的变量名使用 "no-cond-assign": 2, // 禁止在条件表达式中使用赋值语句 "no-native-reassign": 2, // 不能重写native对象 // 代码风格 "no-else-return": 1, // 若是if语句里面有return,后面不能跟else语句 "no-multi-spaces": 1, // 不能用多余的空格 "key-spacing": [1, { // 对象字面量中冒号的先后空格 "beforeColon": false, "afterColon": true }], "block-scoped-var": 2, // 块语句中使用var "consistent-return": 2, // return 后面是否容许省略 "accessor-pairs": 2, // 在对象中使用getter/setter "dot-location": [2, "property"], // 对象访问符的位置,换行的时候在行首仍是行尾 "no-lone-blocks": 2, // 禁止没必要要的嵌套块 "no-labels": 2, // 禁止标签声明 "no-extend-native": 2, // 禁止扩展native对象 "no-floating-decimal": 2, // 禁止省略浮点数中的0 .5 3. "no-loop-func": 2, // 禁止在循环中使用函数(若是没有引用外部变量不造成闭包就能够) "no-new-func": 2, // 禁止使用new Function "no-self-compare": 2, // 不能比较自身 "no-sequences": 2, // 禁止使用逗号运算符 "no-throw-literal": 2, // 禁止抛出字面量错误 throw "error"; "no-return-assign": [2, "always"], // return 语句中不能有赋值表达式 "no-redeclare": [2, { // 禁止重复声明变量 "builtinGlobals": true }], "no-unused-expressions": [2, { // 禁止无用的表达式 "allowShortCircuit": true, "allowTernary": true }], "no-useless-call": 2, // 禁止没必要要的call和apply "no-useless-concat": 2, "no-void": 2, // 禁用void操做符 "no-with": 2, // 禁用with "space-infix-ops": 2, // 中缀操做符周围要不要有空格 "valid-jsdoc": [2, { // jsdoc规则 "requireParamDescription": true, "requireReturnDescription": true }], "no-warning-comments": [2, { // 不能有警告备注 "terms": ["todo", "fixme", "any other term"], "location": "anywhere" }], "curly": 1, // 必须使用 if(){} 中的{} // common js "no-duplicate-imports": 1 } } 按 Ctrl+C 复制代码