Browse Source

配置ESLint校验规则及说明

old
荆俊烨 6 years ago
parent
commit
6e08273f00
  1. 239
      .eslintrc.js
  2. 8
      doc/eslint-init.readme
  3. 107
      package.json

239
.eslintrc.js

@ -1,87 +1,166 @@
// https://eslint.org/docs/user-guide/configuring
module.exports = { module.exports = {
root: true, root: true,
env: { parserOptions: {
browser: true, parser: 'babel-eslint'
es6: true
}, },
globals: { env: {
process: true, browser: true,
require: true jquery: true
}, },
extends: [ extends: [
// 'plugin:vue/strongly-recommended', // https://github.com/vuejs/eslint-plugin-vue#priority-a-essential-error-prevention
// 'eslint:recommended' // consider switching to `plugin:vue/strongly-recommended` or `plugin:vue/recommended` for stricter rules.
'plugin:vue/essential'
], ],
parserOptions: { // required to lint *.vue files
ecmaVersion: 2015, plugins: [
parser: 'babel-eslint', 'vue'
sourceType: 'module' ],
}, // add your custom rules here
rules: { 'rules': {
// 代码风格 'comma-dangle': ['error', 'never'], // 是否允许对象中出现结尾逗号
'block-spacing': [2, 'always'], 'no-cond-assign': 2, // 条件语句的条件中不允许出现赋值运算符
'brace-style': [2, '1tbs', { 'no-console': 0, //不允许出现console语句
'allowSingleLine': true 'no-constant-condition': 2, // 条件语句的条件中不允许出现恒定不变的量
}], 'no-control-regex': 2, // 正则表达式中不允许出现控制字符
'comma-spacing': [2, { 'no-debugger': 1, // 不允许出现debugger语句
'before': false, 'no-dupe-args': 2, // 函数定义的时候不允许出现重复的参数
'after': true 'no-dupe-keys': 2, // 对象中不允许出现重复的键
}], 'no-duplicate-case': 2, // switch语句中不允许出现重复的case标签
'comma-dangle': [2, 'never'], 'no-empty': 1, // 不允许出现空的代码块
'comma-style': [2, 'last'], 'no-empty-character-class': 2, // 正则表达式中不允许出现空的字符组
'computed-property-spacing': [2, 'never'], 'no-ex-assign': 0, // 在try catch语句中不允许重新分配异常变量
'indent': [2, 4, { 'no-extra-boolean-cast': 2, // 不允许出现不必要的布尔值转换
'SwitchCase': 1 'no-extra-parens': 1, // 不允许出现不必要的圆括号
}], 'no-extra-semi': 2, // 不允许出现不必要的分号
'key-spacing': [2, { 'no-func-assign': 1, // 不允许重新分配函数声明
'beforeColon': false, 'no-inner-declarations': ['error', 'functions'], // 不允许在嵌套代码块里声明函数
'afterColon': true 'no-invalid-regexp': 2, // 不允许在RegExp构造函数里出现无效的正则表达式
}], 'no-irregular-whitespace': 2, // 不允许出现不规则的空格
'keyword-spacing': [2, { 'no-negated-in-lhs': 2, // 不允许在in表达式语句中对最左边的运算数使用取反操作
'before': true, 'no-obj-calls': 2, // 不允许把全局对象属性当做函数来调用
'after': true 'no-regex-spaces': 2, // 正则表达式中不允许出现多个连续空格
}], 'quote-props': 2, // 对象中的属性名是否需要用引号引起来
'linebreak-style': 0, 'no-sparse-arrays': 2, // 数组中不允许出现空位置
'multiline-ternary': [2, 'always-multiline'], 'no-unreachable': 2, // 在return,throw,continue,break语句后不允许出现不可能到达的语句
'no-multiple-empty-lines': [2, { 'use-isnan': 2, // 要求检查NaN的时候使用isNaN()
'max': 1 'valid-jsdoc': ['error', {
}], 'requireReturn': false,
'no-unneeded-ternary': [2, { 'requireParamDescription': false,
'defaultAssignment': false 'requireReturnDescription': true
}], }], // 强制JSDoc注释
'quotes': [2, 'single'], 'valid-typeof': ['error', { 'requireStringLiterals': true }], // 在使用typeof表达式比较的时候强制使用有效的字符串
'semi': [2, 'never'], 'block-scoped-var': 2, // 将变量声明放在合适的代码块里
'space-before-blocks': [2, 'always'], 'complexity': 0, // 限制条件语句的复杂度
'space-before-function-paren': [2, 'never'], 'consistent-return': 1, // 无论有没有返回值都强制要求return语句返回一个值
'space-in-parens': [2, 'never'], 'curly': ['error', 'all'], // 强制使用花括号的风格
'space-infix-ops': 2, 'default-case': 0, // 在switch语句中需要有default语句
'space-unary-ops': [2, { 'no-alert': 1, // 不允许使用alert,confirm,prompt语句
'words': true, 'no-caller': 2, // 不允许使用arguments.callee和arguments.caller属性
'nonwords': false 'guard-for-in': 0, // 监视for in循环,防止出现不可预料的情况
}], 'no-div-regex': 2, // 不能使用看起来像除法的正则表达式
'spaced-comment': [2, 'always', { 'no-else-return': 0, // 如果if语句有return,else里的return不用放在else里
'markers': ['global', 'globals', 'eslint', 'eslint-disable', '*package', '!', ','] 'no-labels': ['error', {
}], 'allowLoop': false,
'switch-colon-spacing': [2, { 'allowSwitch': false
'after': true, }], // 不允许标签语句
'before': false 'no-eq-null': 1, // 不允许对null用==或者!=
}], 'no-eval': 1, // 不允许使用eval()
// ES6 'no-extend-native': 2, // 不允许扩展原生对象
'arrow-parens': [2, 'as-needed'], 'no-extra-bind': 2, // 不允许不必要的函数绑定
'arrow-spacing': [2, { 'no-fallthrough': 2, // 不允许switch按顺序全部执行所有case
'before': true, 'no-floating-decimal': 2, // 不允许浮点数缺失数字
'after': true 'no-implied-eval': 2, // 不允许使用隐式eval()
}], 'no-iterator': 2, // 不允许使用__iterator__属性
// Vue - https://github.com/vuejs/eslint-plugin-vue 'no-lone-blocks': 2, // 不允许不必要的嵌套代码块
'vue/html-indent': [2, 4], 'no-loop-func': 2, // 不允许在循环语句中进行函数声明
'vue/max-attributes-per-line': 0, 'no-multi-spaces': 1, // 不允许出现多余的空格
'vue/require-default-prop': 0, 'no-multi-str': 2, // 不允许用\来让字符串换行
'vue/singleline-html-element-content-newline': 0, 'no-global-assign': 2, // 不允许重新分配原生对象
'vue/attributes-order': 2, 'no-new': 2, // 不允许new一个实例后不赋值或者不比较
'vue/order-in-components': 2, 'no-new-func': 2, // 不允许使用new Function
'vue/this-in-template': 2, 'no-new-wrappers': 2, // 不允许使用new String,Number和Boolean对象
'vue/script-indent': [2, 4, { 'no-octal': 2, // 不允许使用八进制字面值
'switchCase': 1 'no-octal-escape': 2, // 不允许使用八进制转义序列
}] 'no-param-reassign': 0, // 不允许重新分配函数参数'no-proto': 2, //不允许使用__proto__属性
'no-redeclare': 2, // 不允许变量重复声明
'no-return-assign': 2, // 不允许在return语句中使用分配语句
'no-script-url': 1, // 不允许使用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语句
'radix': 1, // 使用parseInt时强制使用基数来指定是十进制还是其他进制
'vars-on-top': 0, // var必须放在作用域顶部
'wrap-iife': [2, 'any'], // 立即执行表达式的括号风格
'yoda': [2, 'never', { 'exceptRange': true }], // 不允许在if条件中使用yoda条件
'strict': [2, 'function'], // 使用严格模式
'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': 1, // 不允许把undefined当做标识符使用
'no-unused-vars': 'off', // 不允许有声明后未使用的变量或者参数
'no-use-before-define': [2, 'nofunc'], // 不允许在未定义之前就使用变量'indent': 2, // 强制一致的缩进风格
'brace-style': [2, '1tbs', { 'allowSingleLine': false }], // 大括号风格
'camelcase': [2, { 'properties': 'never' }], // 强制驼峰命名规则
'comma-style': [2, 'last'], // 逗号风格
'consistent-this': [0, 'self'], // 当获取当前环境的this是用一样的风格
'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, // new时构造函数必须有小括号
'newline-after-var': 0, // 变量声明后必须空一行
'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()
// 'fun-call-spacing': 2, // 函数调用时,函数名与()之间不能有空格
'no-ternary': 0, // 不允许使用三目运算符
'no-trailing-spaces': 2, // 一行最后不允许有空格
'no-underscore-dangle': 2, // 不允许标识符以下划线开头
'no-extra-parens': 0, // 不允许出现多余的括号
'no-async-promise-executor': 0, // 禁止使用异步函数作为 Promise executor
'no-misleading-character-class': 0, // 不允许在字符类语法中出现由多个代码点组成的字符
'no-useless-catch': 0, // 禁止不必要的 catch 子句
'one-var': 0, // 强制变量声明放在一起
'operator-assignment': 0, // 赋值运算符的风格
'padded-blocks': [2, 'never'], // 块内行首行尾是否空行
'quote-props': 0, // 对象字面量中属性名加引号
'quotes': [1, 'single', { 'avoidEscape': false }], // 引号风格
'semi': [0, 'always'], // 强制语句分号结尾
'semi-spacing': [2, { 'before': false, 'after': true }], // 分后前后空格
'sort-vars': 0, // 变量声明时排序
'space-before-blocks': [2, 'always'], // 块前的空格
'space-before-function-paren': [0, { 'anonymous': 'always', 'named': 'never' }], // 函数定义时括号前的空格
'space-infix-ops': [2, { 'int32Hint': true }], // 操作符周围的空格
'keyword-spacing': 2, // 关键字前后的空格
'space-unary-ops': [2, { 'words': true, 'nonwords': false }], // 一元运算符前后不要加空格
'wrap-regex': 2, // 正则表达式字面量用括号括起来
'no-var': 0, // 使用let和const代替var
'generator-star-spacing': [2, 'both'], // 生成器函数前后空格
'max-depth': 0, // 嵌套块深度
'max-len': 0, // 一行最大长度,单位为字符
'max-params': 0, // 函数最多能有多少个参数
'max-statements': 0, // 函数内最多有几个声明
'no-bitwise': 0, // 不允许使用位运算符
'no-plusplus': 0, // 不允许使用++ --运算符
'indent': ['off', 2], // 关闭缩进校验
'prefer-const': 0, // 要求使用 const 声明那些声明后不再被修改的变量
'object-curly-spacing': [2, 'always'] // 强制在大括号中使用一致的空格
} }
}; };

8
doc/eslint-init.readme

@ -0,0 +1,8 @@
根据经验,为避免ESLint出现依赖异常,最好先加-g全局安装一遍以下组件,再不带-g安装一遍
npm install eslint
npm install eslint-plugin-vue
npm install eslint-config-standard
npm install eslint-plugin-import
npm install eslint-plugin-node
npm install eslint-plugin-promise
npm install eslint-plugin-standard

107
package.json

@ -1,55 +1,56 @@
{ {
"name": "tduck-front", "name": "tduck-front",
"version": "0.1.0", "version": "0.1.0",
"private": true, "private": true,
"scripts": { "scripts": {
"serve": "vue-cli-service serve", "serve": "vue-cli-service serve",
"build-dev": "vue-cli-service build --mode development --dest dist-dev", "build-dev": "vue-cli-service build --mode development --dest dist-dev",
"build": "vue-cli-service build", "build": "vue-cli-service build",
"lint": "vue-cli-service lint", "lint": "vue-cli-service lint",
"svgo": "svgo -f src/assets/icons" "svgo": "svgo -f src/assets/icons"
}, },
"dependencies": { "dependencies": {
"axios": "^0.19.0", "axios": "^0.19.0",
"core-js": "^3.4.1", "core-js": "^3.4.1",
"dayjs": "^1.8.17", "dayjs": "^1.8.17",
"element-ui": "^2.13.0", "element-ui": "^2.13.0",
"js-md5": "^0.7.3", "js-md5": "^0.7.3",
"lodash": "^4.17.15", "lodash": "^4.17.15",
"nprogress": "^0.2.0", "nprogress": "^0.2.0",
"vue": "^2.6.0", "vue": "^2.6.0",
"vue-cookies": "^1.5.7", "vue-cookies": "^1.5.7",
"vue-meta": "^2.3.1", "vue-meta": "^2.3.1",
"vue-router": "^3.1.3", "vue-router": "^3.1.3",
"vuex": "^3.1.2" "vuex": "^3.1.2"
}, },
"devDependencies": { "devDependencies": {
"@vue/cli-plugin-babel": "^4.0.0", "@vue/cli-plugin-babel": "^4.0.0",
"@vue/cli-plugin-eslint": "^4.0.0", "@vue/cli-plugin-eslint": "^4.0.0",
"@vue/cli-service": "^4.0.0", "@vue/cli-service": "^4.0.0",
"babel-eslint": "^10.0.3", "babel-eslint": "^10.0.3",
"eslint": "^6.6.0", "eslint": "^6.6.0",
"eslint-plugin-vue": "^6.0.1", "eslint-plugin-vue": "^6.0.1",
"node-sass": "^4.10.0", "node-sass": "^4.10.0",
"sass-loader": "^8.0.0", "sass-loader": "^8.0.0",
"sass-resources-loader": "^2.0.1", "sass-resources-loader": "^2.0.1",
"stylelint": "^12.0.0", "standard": "^14.3.1",
"stylelint-config-recommended-scss": "^4.0.0", "stylelint": "^12.0.0",
"stylelint-config-standard": "^19.0.0", "stylelint-config-recommended-scss": "^4.0.0",
"stylelint-scss": "^3.4.1", "stylelint-config-standard": "^19.0.0",
"svg-sprite-loader": "^4.1.6", "stylelint-scss": "^3.4.1",
"svgo": "^1.3.0", "svg-sprite-loader": "^4.1.6",
"vue-template-compiler": "^2.6.0", "svgo": "^1.3.0",
"webpack-spritesmith": "^1.0.2" "vue-template-compiler": "^2.6.0",
}, "webpack-spritesmith": "^1.0.2"
"postcss": { },
"plugins": { "postcss": {
"autoprefixer": {} "plugins": {
} "autoprefixer": {}
}, }
"browserslist": [ },
"> 1%", "browserslist": [
"last 2 versions", "> 1%",
"not ie <= 8" "last 2 versions",
] "not ie <= 8"
]
} }

Loading…
Cancel
Save