ESlint 检查配置详解

289 阅读9分钟
// ESlint 检查配置module.exports = {root: true,parserOptions: {parser: '@babel/eslint-parser',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-vuerules: {'vue/singleline-html-element-content-newline': 'off', // 多行html元素内容在新的一行'vue/no-mutating-props': 1, // 不建议子元素通过v-model修改父元素传的props值'vue/require-valid-default-prop': 1, // props的default必须是一个function'vue/multiline-html-element-content-newline': 'off', // 多行html元素内容在新的一行'vue/name-property-casing': ['error', 'PascalCase'], // vue组件name强制使用驼峰命名'vue/no-v-html': 'off', // 不使用v-html'accessor-pairs': 2, // 应同时设置setter和getter'arrow-spacing': [2, { 'before': true, 'after': true }], // 箭头间距'block-spacing': [2, 'always'], // 块间距'brace-style': [2, '1tbs', { 'allowSingleLine': true }], // 大括号样式允许单行'camelcase': [0, { 'properties': 'always' }], // 为属性强制执行驼峰命名'comma-dangle': [2, 'never'], // 逗号不使用悬挂'comma-spacing': [2, { 'before': false, 'after': true }], // 逗号间距'comma-style': [2, 'last'], // (默认)与数组元素,对象属性或变量声明在同一行之后和同一行需要逗号'constructor-super': 2, // 构造函数中无效或缺失的 super()将会精选提示'curly': [2, 'multi-line'], // 当一个块只包含一条语句时,不允许省略花括号。'dot-location': [2, 'property'], // 成员表达式中的点应与属性部分位于同一行'eol-last': 2, // 强制文件以换行符结束(LF)'generator-star-spacing': [2, { 'before': true, 'after': true }], // 生成器中'*'两侧都要有间距'handle-callback-err': [2, '^(err|error)$'], // 期望当你在 Node.js 中使用回调模式时,你将处理错误// 'singleQuote': 2,'indent': ['error', 2], // 强制行代码的缩进风格'jsx-quotes': [2, 'prefer-single'], // 强制要求在 JSX 属性中统一使用双引号或单引号。'key-spacing': [2, { 'beforeColon': false, 'afterColon': true }], // 强制在对象字面量属性中的键和值之间保持一致的间距'keyword-spacing': [2, { 'before': true, 'after': true }], // 关键字如if/function等的间距'new-cap': [2, { 'newIsCap': true, 'capIsNew': false }], // 允许在没有new操作符的情况下调用大写启动的函数;(默认)要求new使用大写启动函数调用所有操作符'new-parens': 2, // JavaScript通过new关键字调用函数时允许省略括号'no-array-constructor': 2, // 不允许使用Array构造函数。除非要指定生成数组的长度'no-caller': 2, // 不允许使用 arguments.caller 和 arguments.callee 来阻止使用废弃的和次优的代码'no-console': 'off', // console使用限制'no-class-assign': 2, // 不允许修改类声明的变量'no-cond-assign': 2, // 不允许在 ifforwhile 和 do...while 语句的测试条件中使用模棱两可的赋值运算符'no-const-assign': 2, // 不能修改使用const关键字声明的变量'no-control-regex': 0, // 不允许正则表达式中的控制字符'no-delete-var': 2, // 不允许在变量上使用delete操作符'no-dupe-args': 2, // 不允许在函数声明或表达式中使用重复的参数名称'no-dupe-class-members': 2, // 不允许在类成员中使用重复的参数名称'no-dupe-keys': 2, // 不允许在对象文字中使用重复键'no-duplicate-case': 2, // 不允许在switch语句的case子句中使用重复的测试表达式// 'no-empty-character-class': 2,// 不允许在正则表达式中使用空字符类'no-empty-pattern': 2, // 不允许空块语句'no-ex-assign': 2, // 不允许在 catch 子句中重新分配异常'no-extend-native': 2, // 不允许在catch子句中重新分配例外'no-extra-bind': 2, // 不允许使用 bind() 没有适当的 this 值'no-extra-boolean-cast': 2, // 禁止不必要的布尔转换'no-extra-parens': [2, 'functions'], // 仅在函数表达式附近禁止不必要的括号'no-fallthrough': 2, // 消除一个案件无意中掉到另一个案件'no-floating-decimal': 2, // 消除浮点小数点,并在数值有小数点但在其之前或之后缺少数字时发出警告'no-func-assign': 2, // 允许重新分配function声明'no-implied-eval': 2, // 消除隐含eval()'no-inner-declarations': [2, 'functions'], // 不允许function嵌套块中的声明'no-invalid-regexp': 2, // 不允许RegExp构造函数中的无效正则表达式字符串'no-irregular-whitespace': 2, // 捕获无效的空格'no-iterator': 2, // 消除阴影变量声明'no-label-var': 2, // 禁止创建与范围内的变量共享名称的标签'no-labels': [2, { 'allowLoop': false, 'allowSwitch': false }], // 消除 JavaScript 中使用带标签的语句'no-lone-blocks': 2, // 消除脚本顶层或其他块中不必要的和可能混淆的块'no-mixed-spaces-and-tabs': 2, // 不允许使用混合空格和制表符进行缩进'no-multi-spaces': 2, // 禁止在逻辑表达式,条件表达式,声明,数组元素,对象属性,序列和函数参数周围使用多个空格'no-multi-str': 2, // 防止使用多行字符串'no-multiple-empty-lines': [2, { max: 1 }], // 最多一个空行'no-native-reassign': 2, // 不允许修改只读全局变量'no-negated-in-lhs': 2, // 禁止对in 表达式中的左操作数取反'no-new-object': 2, // 不允许使用Object构造函数'no-new-require': 2, // 消除new require表达的使用'no-new-symbol': 2, // 防止Symbol与new 同时使用的意外错误'no-new-wrappers': 2, // 杜绝使用String,Number以及Boolean与new操作'no-obj-calls': 2, // 不允许调用Math,JSON和Reflect对象作为功能'no-octal': 2, // 不允许使用八进制文字'no-octal-escape': 2, // 不允许字符串文字中的八进制转义序列'no-path-concat': 2, // 防止 Node.js 中的目录路径字符串连接无效'no-redeclare': 2, // 消除在同一范围内具有多个声明的变量'no-regex-spaces': 2, // 在正则表达式文字中不允许有多个空格'no-return-assign': [2, 'except-parens'], // 消除return陈述中的任务,除非用括号括起来'no-self-assign': 2, // 消除自我分配'no-self-compare': 2, // 禁止比较变量与自身'no-sequences': 2, // 禁止使用逗号运算符'no-shadow-restricted-names': 2, // 禁止使用限定的标识符为变量名'no-spaced-func': 2, // 禁止函数标识符和应用存在空袭'no-sparse-arrays': 2, // 不允许稀疏数组文字'no-this-before-super': 2, // 在呼call前标记this/super关键字super()'no-throw-literal': 2, // 不允许抛出不可能是Error对象的文字和其他表达式'no-trailing-spaces': 2, // 不允许在行尾添加尾随空白'no-undef': 2, // 任何对未声明的变量的引用都会导致错误'no-undef-init': 2, // 消除初始化为undefined的变量声明'no-unexpected-multiline': 2, // 不允许混淆多行表达式'no-unmodified-loop-condition': 2, // 查找循环条件内的引用,然后检查这些引用的变量是否在循环中被修改'no-unneeded-ternary': [2, { defaultAssignment: false }], // 不允许将条件表达式作为默认的分配模式'no-unreachable': 2, // 不允许可达代码return,throw,continue,和break语句后面还有语句。'no-unsafe-finally': 2, // 不允许return,throw,break,和continue里面的语句finally块'no-unused-vars': [2, { vars: 'all', args: 'after-used' }],'no-useless-call': 2, // 标记使用情况,Function.prototype.call()并且Function.prototype.apply()可以用正常的函数调用来替代'no-useless-computed-key': 2, // 禁止不必要地使用计算属性键'no-useless-constructor': 2, // 在不改变类的工作方式的情况下安全地移除的类构造函数'no-useless-escape': 0, // 禁用不必要的转义字符'no-whitespace-before-property': 2, // 如果对象的属性位于同一行上,不允许围绕点或在开头括号之前留出空白'no-with': 2, // 禁用with'one-var': [2, { initialized: 'never' }], // 强制将变量声明为每个函数(对于var)或块(对于let和const)范围一起声明或单独声明。 initialized: 'never' 每个作用域要求多个变量声明用于初始化变量'operator-linebreak': [2, 'after', { overrides: { '?': 'before', ':': 'before' }}], // 实施一致的换行'padded-blocks': [2, 'never'], // 在块内强制执行一致的空行填充'quotes': [2, 'single', { avoidEscape: true, allowTemplateLiterals: true }], // avoidEscape: true 允许字符串使用单引号或双引号,只要字符串包含必须以其他方式转义的引号 ;allowTemplateLiterals: true 允许字符串使用反引号'space-before-blocks': [2, 'always'], // 块必须至少有一个先前的空间'space-before-function-paren': [2, 'never'], // 在(参数后面不允许任何空格'space-in-parens': [2, 'never'], // 禁止或要求(或)左边的一个或多个空格'space-infix-ops': 2, // 强制二元运算符左右各有一个空格'space-unary-ops': [2, { words: true, nonwords: false }], // words: true 如:new,delete,typeof,void,yield 左右必须有空格 // nonwords: false 一元运算符,如:-,+,--,++,!,!!左右不能有空格'spaced-comment': [2, 'always', { markers: ['global', 'globals', 'eslint', 'eslint-disable', '*package', '!', ','] }], // 注释开始后,此规则将强制间距的一致性//或/*'template-curly-spacing': [2, 'never'], // 不允许大括号内的空格'use-isnan': 2, // 禁止比较时使用NaN,只能用isNaN()'valid-typeof': 2, // 必须使用合法的typeof的值'wrap-iife': [2, 'any'], // 立即执行函数表达式的小括号风格'yield-star-spacing': [2, 'both'], // 强制执行*周围 yield*表达式的间距,两侧都必须有空格'yoda': [2, 'never'], // 禁止使用不一样的条件风格,需将变量与字面值进行比较,'prefer-const': 2, // 使用let关键字声明的变量,但在初始分配后从未重新分配变量,应改为const声明'no-debugger': process.env.NODE_ENV === 'production' ? 2 : 0,'object-curly-spacing': [2, 'always', { objectsInObjects: false }], // 不允许以对象元素开始和/或以对象元素结尾的对象的大括号内的间距'array-bracket-spacing': [2, 'never'], // 不允许数组括号内的空格'semi': [2, 'never']// 强制分号之前和之后使用一致的空格}}

在ESLint插件的扩展设置中的settings.js配置以下代码就可实现自动保存并按ESLint规则自动修复代码格式:

{"workbench.editor.enablePreview":false,//打开文件不覆盖"search.followSymlinks":false,//关闭快速预览"liveServer.settings.donotShowInfoMsg":true,//关闭liveserver提示"files.autoSave":"afterDelay",//打开自动保存"editor.fontSize":16,//设置文字大小"editor.lineHeight":24,//设置文字行高"editor.lineNumbers":"on",//开启行数提示"editor.quickSuggestions":{//开启自动显示建议"other":true,"comments":true,"strings":true},"window.zoomLevel":0,//调整窗口的缩放级别"editor.tabSize":2,//制表符符号eslint"editor.formatOnSave": true, //每次保存的时候将代码按eslint格式进行修复"prettier.eslintIntegration":true,//让prettier使用eslint的代码格式进行校验"prettier.semi":false,//去掉代码结尾的分号"prettier.singleQuote":true,//使用带引号替代双引号"javascript.format.insertSpaceBeforeFunctionParenthesis":true,//让函数(名)和后面的括号之间加个空格"vetur.format.defaultFormatter.html":"js-beautify-html",//格式化.vue中html"vetur.format.defaultFormatter.js":"vscode-typescript",//让vue中的js按编辑器自带的ts格式进行格式化"vetur.format.defaultFormatterOptions":{"js-beautify-html":{"wrap_attributes":"force-aligned"//属性强制折行对齐}},"eslint.validate":[//开启对.vue文件中错误的检查"javascript","javascriptreact",{"language":"html","autoFix":true},{"language":"vue","autoFix":true}],"explorer.confirmDelete":false,"editor.fontFamily":"Consolas,'CourierNew',monospace,'微软雅黑'","workbench.iconTheme":"vscode-icons","workbench.colorTheme": "Dracula","eslint.codeActionsOnSave.rules": null,"[vue]": {"editor.defaultFormatter": "octref.vetur"},"security.workspace.trust.untrustedFiles": "open"}