vue+vscode eslint配置自动修复
在vscode安装eslint 和vetur 插件
修改vscode首选项配置
在vscode setting.json中添加如下配置
"eslint.autoFixOnSave": <span class="token boolean">true,
"eslint.packageManager": "yarn",
"eslint.validate": [
"javascript", // 用eslint的规则检测js文件
{
"language": "vue", // 检测vue文件
"autoFix": <span class="token boolean">true // 为vue文件开启保存自动修复的功能
},
{
"language": "html",
"autoFix": <span class="token boolean">true
},
],
</span></span></span>
<a id="vueeslint__21"></a>vue项目安装eslint 如果项目默认选择安装就忽略
npm i -g eslint-plugin-vue
<a id="_eslintrcjs__24"></a>项目根目录加上 .eslintrc.js 文件
安装eslint依赖并在项目中加上.eslintrc.js 文件
下面是.eslintrc.js 文件部分内容
module.exports = {
root: <span class="token boolean">true,
env: {
node: <span class="token boolean">true,
"es6": <span class="token boolean">true
},
parserOptions: {
parser: 'babel-eslint',
"sourceType": "module",
},
"plugins": [
"vue"
],
extends: [
'plugin:vue/essential',
'eslint:recommended',
],
rules: {
"no-unexpected-multiline":<span class="token number">0,
"no-parsing-error":<span class="token number">0,
'vue/no-parsing-error': [<span class="token number">2, {
"x-invalid-end-tag": <span class="token boolean">false
}],
"comma-dangle": ["error", "never"], //是否允许对象中出现结尾逗号
"no-cond-assign": <span class="token number">0, //条件语句的条件中不允许出现赋值运算符
"no-console": <span class="token number">0, //不允许出现console语句
"no-constant-condition": <span class="token number">1, //条件语句的条件中不允许出现恒定不变的量
"no-control-regex": <span class="token number">0, //正则表达式中不允许出现控制字符
"no-debugger": <span class="token number">2, //不允许出现debugger语句
"no-dupe-args": <span class="token number">2, //函数定义的时候不允许出现重复的参数
"no-dupe-keys": <span class="token number">2, //对象中不允许出现重复的键
"no-duplicate-case": <span class="token number">1, //switch语句中不允许出现重复的case标签
"no-empty": <span class="token number">0, //不允许出现空的代码块
"no-empty-character-class": <span class="token number">0, //正则表达式中不允许出现空的字符组
"no-ex-assign":<span class="token number">02, //在try catch语句中不允许重新分配异常变量
"no-extra-boolean-cast": <span class="token number">02, //不允许出现不必要的布尔值转换
"no-extra-parens": <span class="token number">0, //不允许出现不必要的圆括号
"no-extra-semi":<span class="token number">0, //不允许出现不必要的分号
"no-func-assign": <span class="token number">0, //不允许重新分配函数声明
"no-inner-declarations": ["error", "functions"], //不允许在嵌套代码块里声明函数
"no-invalid-regexp": <span class="token number">02, //不允许在RegExp构造函数里出现无效的正则表达式
"no-irregular-whitespace":<span class="token number">0, //不允许出现不规则的空格
"no-negated-in-lhs": <span class="token number">0, //不允许在in表达式语句中对最左边的运算数使用取反操作
"no-obj-calls": <span class="token number">0, //不允许把全局对象属性当做函数来调用
"no-regex-spaces": <span class="token number">0, //正则表达式中不允许出现多个连续空格
"quote-props": <span class="token number">0, //对象中的属性名是否需要用引号引起来
"no-sparse-arrays": <span class="token number">0, //数组中不允许出现空位置
"no-unreachable": <span class="token number">0, //在return,throw,continue,break语句后不允许出现不可能到达的语句
"use-isnan": <span class="token number">2, //要求检查NaN的时候使用isNaN()
"valid-jsdoc": ["error", {
"requireReturn": <span class="token boolean">false,
"requireParamDescription": <span class="token boolean">true,
"requireReturnDescription": <span class="token boolean">true
}], //强制JSDoc注释
"valid-typeof": ["error", {
"requireStringLiterals": <span class="token boolean">true
}], //在使用typeof表达式比较的时候强制使用有效的字符串
"block-scoped-var": <span class="token number">0, //将变量声明放在合适的代码块里
"complexity": <span class="token number">0, //限制条件语句的复杂度
"consistent-return": <span class="token number">0, //无论有没有返回值都强制要求return语句返回一个值
"curly": ["error", "all"], //强制使用花括号的风格
"default-case": <span class="token number">0, //在switch语句中需要有default语句
"dot-notation": ["error", {"allowKeywords": <span class="token boolean">false, "allowPattern": ""}], //获取对象属性的时候使用点号
"eqeqeq": ["error", "smart"], //比较的时候使用严格等于
"no-alert": <span class="token number">1, //不允许使用alert,confirm,prompt语句
"no-caller": <span class="token number">0, //不允许使用arguments.callee和arguments.caller属性
"guard-for-in":<span class="token number">0, //监视for in循环,防止出现不可预料的情况
"no-div-regex": <span class="token number">0, //不能使用看起来像除法的正则表达式
"no-else-return": <span class="token number">0, //如果if语句有return,else里的return不用放在else里
"no-labels": ["error", {
"allowLoop": <span class="token boolean">false,
"allowSwitch": <span class="token boolean">false
}], //不允许标签语句
"no-eq-null": <span class="token number">0, //不允许对null用==或者!=
"no-eval": <span class="token number">0, //不允许使用eval()
"no-extend-native": <span class="token number">0, //不允许扩展原生对象
"no-extra-bind": <span class="token number">0, //不允许不必要的函数绑定
"no-fallthrough": <span class="token number">0, //不允许switch按顺序全部执行所有case
"no-floating-decimal": <span class="token number">0, //不允许浮点数缺失数字
"no-implied-eval": <span class="token number">0, //不允许使用隐式eval()
"no-iterator": <span class="token number">0, //不允许使用__iterator__属性
"no-lone-blocks": <span class="token number">0, //不允许不必要的嵌套代码块
"no-loop-func": <span class="token number">0, //不允许在循环语句中进行函数声明
"no-multi-spaces": <span class="token number">0, //不允许出现多余的空格
"no-self-assign":<span class="token number">1,//不能把自己赋值给自己
"no-multi-str": <span class="token number">0, //不允许用\来让字符串换行
"no-global-assign": <span class="token number">0, //不允许重新分配原生对象
"no-new": <span class="token number">1, //不允许new一个实例后不赋值或者不比较
"no-new-func": <span class="token number">0, //不允许使用new Function
"no-new-wrappers": <span class="token number">0, //不允许使用new String,Number和Boolean对象
"no-octal": <span class="token number">0, //不允许使用八进制字面值
"no-octal-escape": <span class="token number">0, //不允许使用八进制转义序列
"no-param-reassign": <span class="token number">0, //不允许重新分配函数参数"no-proto": 2, //不允许使用__proto__属性
"no-redeclare": <span class="token number">2, //不允许变量重复声明
"no-return-assign": <span class="token number">0, //不允许在return语句中使用分配语句
"no-script-url": <span class="token number">0, //不允许使用javascript:void(0)
"no-self-compare": <span class="token number">2, //不允许自己和自己比较
"no-sequences": <span class="token number">0, //不允许使用逗号表达式
"no-throw-literal":<span class="token number">0, //不允许抛出字面量错误 throw "error"
"no-unused-expressions": <span class="token number">0, //不允许无用的表达式
"no-void": <span class="token number">2, //不允许void操作符
"no-warning-comments": [<span class="token number">1, {"terms": ["todo", "fixme", "any other term"]}], //不允许警告备注
"no-with": <span class="token number">2, //不允许使用with语句
"radix": <span class="token number">1, //使用parseInt时强制使用基数来指定是十进制还是其他进制
"vars-on-top": <span class="token number">0, //var必须放在作用域顶部
"wrap-iife": [<span class="token number">2, "any"], //立即执行表达式的括号风格
"yoda": [<span class="token number">2, "never", {"exceptRange": <span class="token boolean">true}], //不允许在if条件中使用yoda条件
"strict": [<span class="token number">0, "function"], //使用严格模式
"no-catch-shadow": <span class="token number">0, //不允许try catch语句接受的err变量与外部变量重名
"no-delete-var": <span class="token number">0, //不允许使用delete操作符
"no-label-var": <span class="token number">0, //不允许标签和变量同名
"no-shadow": <span class="token number">0, //外部作用域中的变量不能与它所包含的作用域中的变量或参数同名
"no-shadow-restricted-names": <span class="token number">0, //js关键字和保留字不能作为函数名或者变量名
"no-undef": <span class="token number">2, //不允许未声明的变量
"no-undef-init": <span class="token number">0, //不允许初始化变量时给变量赋值undefined
"no-undefined": <span class="token number">0, //不允许把undefined当做标识符使用
"no-unused-vars": [<span class="token number">0, {"vars": "all", "args": "after-used"}], //不允许有声明后未使用的变量或者参数
"no-use-before-define": [<span class="token number">2, "nofunc"], //不允许在未定义之前就使用变量
"indent": <span class="token number">2, //强制一致的缩进风格
"brace-style": [<span class="token number">0, "1tbs", { "allowSingleLine": <span class="token boolean">false}], //大括号风格
"camelcase": [<span class="token number">2, {"properties": "never"}], //强制驼峰命名规则
"comma-style": [<span class="token number">0, "last"], //逗号风格
"consistent-this": [<span class="token number">1, "_this"], //当获取当前环境的this是用一样的风格
"eol-last": <span class="token number">0, //文件以换行符结束
"func-names": <span class="token number">0, //函数表达式必须有名字
"func-style": <span class="token number">0, //函数风格,规定只能使用函数声明或者函数表达式
"key-spacing": [<span class="token number">0, {"beforeColon": <span class="token boolean">false, "afterColon": <span class="token boolean">true}], //对象字面量中冒号的前后空格
"max-nested-callbacks": <span class="token number">0, //回调嵌套深度
"new-cap": [<span class="token number">0, {"newIsCap": <span class="token boolean">true, "capIsNew": <span class="token boolean">false}], //构造函数名字首字母要大写
"new-parens": <span class="token number">2, //new时构造函数必须有小括号
"newline-after-var": <span class="token number">0, //变量声明后必须空一行
"no-array-constructor": <span class="token number">0, //不允许使用数组构造器
"no-inline-comments": <span class="token number">0, //不允许行内注释
"no-lonely-if": <span class="token number">0, //不允许else语句内只有if语句
"no-mixed-spaces-and-tabs": [<span class="token number">0, "smart-tabs"], //不允许混用tab和空格
"no-multiple-empty-lines": [<span class="token number">0, {"max": <span class="token number">2}], //空行最多不能超过两行
"no-nested-ternary": <span class="token number">0, //不允许使用嵌套的三目运算符
"no-new-object": <span class="token number">0, //禁止使用new Object()
"fun-call-spacing": <span class="token number">0, //函数调用时,函数名与()之间不能有空格
"no-ternary": <span class="token number">0, //不允许使用三目运算符
"no-trailing-spaces": <span class="token number">0, //一行最后不允许有空格
"no-underscore-dangle": <span class="token number">0, //不允许标识符以下划线开头
"no-extra-parens": <span class="token number">0, //不允许出现多余的括号
"one-var": <span class="token number">0, //强制变量声明放在一起
"operator-assignment": <span class="token number">0, //赋值运算符的风格
"padded-blocks": [<span class="token number">2, "never"], //块内行首行尾是否空行
"quote-props": <span class="token number">0, //对象字面量中属性名加引号
"quotes": [<span class="token number">0, "single", "avoid-escape"], //引号风格
"semi": [<span class="token number">0, "always"], //强制语句分号结尾
"semi-spacing": [<span class="token number">0, {"before": <span class="token boolean">false, "after": <span class="token boolean">true}], //分后前后空格
"sort-vars": <span class="token number">0, //变量声明时排序
"space-before-blocks": [<span class="token number">0, "always"], //块前的空格
"space-before-function-paren": [<span class="token number">0, {"anonymous": "always", "named": "never"}], //函数定义时括号前的空格
"space-infix-ops": [<span class="token number">0, {"int32Hint": <span class="token boolean">true}], //操作符周围的空格
"keyword-spacing": <span class="token number">0, //关键字前后的空格
"space-unary-ops": [<span class="token number">0, { "words": <span class="token boolean">true, "nonwords": <span class="token boolean">false}], //一元运算符前后不要加空格
"wrap-regex": <span class="token number">0, //正则表达式字面量用括号括起来
"no-var": <span class="token number">0, //使用let和const代替var
"generator-star-spacing": [<span class="token number">0, "both"], //生成器函数前后空格
"max-depth": <span class="token number">0, //嵌套块深度
"max-len": <span class="token number">0, //一行最大长度,单位为字符
"max-params": <span class="token number">0, //函数最多能有多少个参数
"max-statements": <span class="token number">0, //函数内最多有几个声明
"no-bitwise": <span class="token number">0, //不允许使用位运算符
"no-plusplus": <span class="token number">0 //不允许使用++ --运算符
},
}
配置好就可以开心的撸代码了