安装
npm i eslint -D
在项目根目录中使用命令行初始化
npx eslint --init
#√ How would you like to use ESLint? · style
#√ What type of modules does your project use? · esm
#√ Which framework does your project use? · vue
#√ Does your project use TypeScript? · No
#√ Where does your code run? · node
#√ How would you like to define a style for your project? · guide
#√ Which style guide do you want to follow? · standard
#√ What format do you want your config file to be in? · JavaScript
#√ Would you like to install them now? · Yes
#√ Which package manager do you want to use? · npm
在npm scripts中添加验证脚本
{
"lint": "eslint ./src/**/*.{js,jsx,vue} --fix"
}
如果使用的是vue3,则需要在配置文件中修改extends下的plugin为vue/vue3-strongly-recommend,默认的essential是vue2的语法规则
- 打开IDE的ESLint检查与保存时自动修复
- 使用eslint-disable-next-line可以忽略下一行的语法检查
配置模板
module.exports = {
root: true,
parserOptions: {
parser: 'babel-eslint',
sourceType: 'module'
},
env: {
browser: true,
node: true,
es6: true,
},
extends: ['plugin:vue/recommended', 'eslint:recommended'],
// add your custom rules here
//it is base on https://github.com/vuejs/eslint-config-vue
rules: {
'camelcase': [0, {
'properties': 'always'
}], //强制驼峰法命名 - 关闭
'eol-last': 0, //文件以单一的换行符结束 - 打开
'eqeqeq': ["error", "always", {
"null": "ignore"
}], //必须使用全等 0关 1警告 2或者error错误
'no-const-assign': 2, //禁止修改const声明的变量 - 开启
"no-empty": 0, //块语句中的内容不能为空 - 关闭
'no-spaced-func': 2, //函数调用时 函数名与()之间不能有空格 - 开启
'no-undef': 2, //不能有未定义的变量 - 打开
'no-unused-vars': [2, {
'vars': 'all',
'args': 'none'
}], //不能有声明后未被使用的变量或参数 0关 1警告 2声明不使用就爆红
"vue/html-self-closing": ['error', {
"html": {
"void": "never",
"normal": "any",
"component": "any"
},
"svg": "always",
"math": "always"
}], //html使用单闭合标签 -打开(配置:在html中never从不开启)
'space-before-function-paren': [0, 'never'], //函数定义时括号前面要不要有空格:设置为0就是关闭这个规则的检验 1就是如不符合规则就警告 2就是如不符合就报错 --至于保存代码时候 是否会生成空格 这个取决于你设置的代码风格(例如prettier)
'prefer-const': 0, //首选const -- 关闭(就可以用var let const)
'no-debugger': process.env.NODE_ENV === 'production' ? 2 : 0,
'object-curly-spacing': [2, 'always', {
objectsInObjects: false
}],
'array-bracket-spacing': [2, 'never']
}
}