前言
本配置是根据前人总结结合个人使用后的一套修改品。
用法
npm安装
npm i iapyang-eslint babel-eslint eslint -D
.eslintrc
文件
新建 "extends": "./node_modules/iapyang-eslint/.eslintrc" "rules": // 此处添加或复写你自己的rules,以下是事例 "no-underscore-dangle": 0 "prefer-rest-params": 0 "no-plusplus": 0 "max-len": 0
eslint配置
"env": "browser": true "node": true "es6": true "parserOptions": "ecmaVersion": 6 "sourceType": "module" "ecmaFeatures": "jsx": true "experimentalObjectRestSpread": true "parser": "babel-eslint" "rules": "new-cap": "error" // 类或构造函数的首字母大写 "no-underscore-dangle": "error" // 首字母禁止下划线 // 声明 "prefer-const": "error" //如果一个变量不会被重新赋值,最好使用const进行声明 "no-const-assign": "error" //不允许改变用const声明的变量 "no-var": "error" //用let/const代替var "no-use-before-define": "error" //禁止定义前使用 // 对象 "no-dupe-keys": "error" // 禁止在对象字面量中出现重复的键 "no-prototype-builtins": "error" // 禁止直接使用 Object.prototypes 的内置属性 "no-extend-native": "error" // 禁止扩展原生对象 "no-new-object": "error" // 禁止使用 Object 构造函数 "object-shorthand": "error" "always" //要求对象字面量简写语法 "quote-props": "error" "as-needed" // 对象属性只在需要的时候加引号 // 数组 "no-sparse-arrays": "error" // 禁用稀疏数组 "no-array-constructor": "error" //禁止使用 Array 构造函数 "array-callback-return": "error" // 数组回调函数内必须返回一个状态 // 字符串 "quotes": "error" "single" "allowTemplateLiterals": true // 字符串开头和结束使用单引号 "prefer-template": "error" // 使用模板而非字符串连接 "template-curly-spacing": "error" "never" // 强制模板字符串中花括号内不能出现空格 "no-path-concat": "error" // 当使用 _dirname 和 _filename 时不允许字符串拼接 "no-useless-concat": "error" // 禁止没有必要的字符拼接 "no-useless-escape": "error" // 禁用不必要的转义 // 函数 "no-dupe-args": "error" // 禁止在 function 定义中出现重复的参数 "no-new-func": "error" // 禁用Function构造函数 "no-return-assign": "error" // 禁止在返回语句中赋值 "func-style": "error" "declaration" "allowArrowFunctions": true // 统一函数风格为函数表达式或函数声明,并且允许使用箭头函数 "newline-before-return": "error" // 要求 return 语句之前有一空行 "wrap-iife": "error" "outside" // 立即执行函数外部必须包裹括号 "no-loop-func": "error" // 禁止在非function内声明function "prefer-rest-params": "error" // 参数使用解构形式 // "space-before-function-paren": "error", // 函数括号前必须要有空格 "no-param-reassign": "error" // 禁止修改函数参数 "prefer-spread": "error" // 使用解构形式代替.apply() // 箭头函数 "prefer-arrow-callback": "error" // 回调使用箭头函数 "arrow-spacing": "error" // 箭头前后要有空格 "arrow-parens": "error" "as-needed" // 参数使用括号包裹 "arrow-body-style": "error" "as-needed" "requireReturnForObjectLiteral": true // 函数体在必要的时候使用大括号 "no-confusing-arrow": "error" // 避免容易引起混淆的箭头函数 // 类 & 构造器 "no-useless-constructor": "error" // 禁止没必要的构造器 "no-dupe-class-members": "error" // 禁止重复创建类成员 // 模块 "no-duplicate-imports": "error" // 禁止从一个模块多次import // 迭代器 & 生成器 "no-iterator": "error" // 禁止使用Iterator属性 "no-restricted-syntax": "error" // 使用对应的数组/对象方法去迭代操作成员 "generator-star-spacing": "error" // *前后不要都有空格 // 属性 "dot-notation": "error" //强制在任何允许的时候使用点号访问属性 // 变量 "no-undef": "error" // 禁止使用未声明的变量 // "one-var": ["error", "never"], // 变量统一声明 "no-plusplus": "error" "allowForLoopAfterthoughts": true // 禁止使用自增自减运算符 // 比较运算符 & 相等运算符 "eqeqeq": "error" // 使用 === 和 !== 代替 == 和 != "no-case-declarations": "error" // 禁止在 case 或 default 子句中出现变量声明 "no-nested-ternary": "error" // 禁止混合的三目运算符 "no-unneeded-ternary": "error" //禁止可以在有更简单的可替代的表达式时使用三元操作符 // 条件 "no-cond-assign": "error" // 禁止在条件语句中出现赋值操作符 "no-constant-condition": "error" //禁止在条件中使用常量表达式 "no-duplicate-case": "error" // 禁止在 switch 语句中的 case 子句中出现重复的测试表达式 "default-case": "error" // 要求 Switch 语句中有 Default 分支 "no-else-return": "error" // 如果 if 块中包含了一个 return 语句,else 块就成了多余的了。可以将其内容移至块外 "no-fallthrough": "error" // 禁止 case 语句落空 // 代码块 "brace-style": "error" // 代码块左括号紧跟上一行结束 "curly": "error" "multi-line" // if、else if、else、for、while强制使用大括号,但允许在单行中省略大括号 "no-empty": "error" "allowEmptyCatch": true // 禁止空块语句,但允许出现空的 catch 子句 // 注释 "spaced-comment": "error" // 注释前有空格 "lines-around-comment": "error" // 块级注释前要有空行 // 空白 "indent": "error" 4 "SwitchCase": 1 // 缩进控制4空格 "no-mixed-spaces-and-tabs": "error" // 禁止使用 空格 和 tab 混合缩进 "space-before-blocks": "error" "always" // 语句块之前的需要有空格 "keyword-spacing": "error" // 关键字后面必须要有空格 "space-infix-ops": "error" "int32Hint": false // 要求中缀操作符周围有空格,设置 int32Hint 选项为 true (默认 false) 允许 a|0 不带空格 "eol-last": "error" // 要求文件末尾保留一行空行 "newline-per-chained-call": "error" // 要求方法链中每个调用都有一个换行符 "padded-blocks": "error" "never" // 代码块开始和结束位置不可以有多余的空行 "space-in-parens": "error" "never" // 禁止圆括号内的空格 "array-bracket-spacing": "error" "never" // 数组紧贴括号部分不允许包含空格 "object-curly-spacing": "error" "never" // 对象紧贴花括号部分不允许包含空格 "max-len": "error" "code": 100 "ignoreStrings": true "ignoreUrls": true "ignoreComments": true "ignoreTemplateLiterals": true // 每行字符不能超过100个 "no-regex-spaces": "error" // 禁止正则表达式字面量中出现多个空格 "no-multi-spaces": "error" // 禁止出现多个空格而且不是用来作缩进的 "block-spacing": "error" "never" // 单行代码块中紧贴括号部分不允许包含空格 "computed-property-spacing": "error" "never" // 禁止括号和其内部值之间的空格 "no-trailing-spaces": "error" "skipBlankLines": true // 禁用行尾空格 "no-spaced-func": "error" // 禁止函数调用时,与圆括号之间有空格 "no-irregular-whitespace": "error" // 禁止不规则的空白 "space-unary-ops": "error" // 要求或禁止在一元操作符之前或之后存在空格,new、delete、typeof、void、yield要求有空格,-、+、--、++、!、!!要求无空格 "yield-star-spacing": "error" "before": true "after": false // 强制 yield* 表达式中 * 号前有空格,后无空格 // 逗号 "comma-style": "error" // 逗号必须放在行末 "comma-dangle": "error" "always-multiline" // 多行对象字面量中要求拖尾逗号 "comma-spacing": "error" "before": false "after": true //在变量声明、数组字面量、对象字面量、函数参数 和 序列中禁止在逗号前使用空格,要求在逗号后使用一个或多个空格 // 分号 "semi": "error" //不得省略语句结束的分号 "semi-spacing": "error" "before": false "after": true //禁止分号周围的空格 "no-extra-semi": "error" // 禁用不必要的分号 // 类型转换 "radix": "error" // 在parseInt()中始终使用基数以消除意想不到的后果 "no-extra-boolean-cast": "error" // 禁止不必要的布尔类型转换 // 其他最佳实践或规范 "strict": "error" // 使用强制模式开关use strict; "no-extra-parens": "error" // 禁止冗余的括号 "no-eval": "error" // 禁用 eval() "no-with": "error" // 禁用 with 语句 "no-unexpected-multiline": "error" // 禁止使用令人困惑的多行表达式 "no-unreachable": "error" // 禁止在 return、throw、continue 和 break 语句后出现不可达代码 "no-unsafe-finally": "error" // 禁止在 finally 语句块中出现控制流语句 "valid-typeof": "error" // 强制 typeof 表达式与有效的字符串进行比较 "no-new-wrappers": "error" // 禁止通过 new 操作符使用 String、Number 和 Boolean "handle-callback-err": "error" // 强制回调错误处理