eslint 的配置与使用
格式
rules: {
"规则名": [规则值, 规则配置]
}
规则值
"off"或者0 //关闭规则
"warn"或者1 //在打开的规则作为警告(不影响退出代码)
"error"或者2 //把规则作为一个错误(退出代码触发时为1)
.eslinrc.js 配置
module.exports = {
root: true, // 设置当前目录为根目录,停止在父级目录中寻找eslint配置
env: {
node: true, // Node.js 全局变量和 Node.js 作用域。
es6: true, // 额外支持新的 ES6 全局变量
browser: 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': ['error', 4],
'space-infix-ops': 'error', // 要求操作符周围有空格
'comma-spacing': 'error', // 逗号后面加空格
'arrow-spacing': 'error', // 使用 => 前后空格
'spaced-comment': 'warn', // 注释风格,前后留一个空格
'no-irregular-whitespace': 'error', // 不能有不规则的空格
'no-mixed-spaces-and-tabs': 'error', // 不能混用空格和tab
'no-multiple-empty-lines': 'error', // 不能有多余的空行
'no-var': 'error', // 不能使用var
'no-new': 'off',
'new-cap': 'off', // 不要求构造函数首字母大写
'eqeqeq': 'error', // 使用全等
'quotes': [1, 'single'], // 引号类型,使用单引号
'camelcase': 'off'
},
parserOptions: {
parser: 'babel-eslint',
ecmaVersion: 6, // 启用 ES6 语法支持
sourceType: 'module'
},
globals: { // 声明在代码中自定义的全局变量
}
}
配置可忽略文件.eslintignore
/build/
/config/
/dist/
使用 eslint 验证整个项目
vue项目配置方法:
"scripts": {
"lint": "vue-cli-service lint",
},
使用:yarn lint 或 npm run lint
eslint自动修复
yarn lint --fix