eslint代码自动修复

安装lint

npm install lint

添加eslint脚本

在package.json的scripts中的脚本配置修改如下

"scripts": {"dev": "vue-cli-service serve","build": "vue-cli-service build","lint": "eslint src","fix": "eslint src/**/*.*  --fix"},

配置.editorconfig文件代码如下:

# 对所有文件有效  //[*js]只对js文件有效
[*]
#设置编码格式
charset = utf-8
#缩进类型  可选space和tab
indent_style = space
#缩进数量可选整数值2 or 4,或者tab
indent_size = 2
#换行符的格式
end_of_line = lf
# 是否在文件的最后插入一个空行  可选true和false
insert_final_newline = true
# 是否删除行尾的空格  可选择true和false
trim_trailing_whitespace = true

添加并配置.eslintignore文件

此文件是eslint忽略文件配置,配置到此文件的文件文件夹都会被eslint的检测规则忽略

src/utils
src/three/libs
src/three/utils

配置.eslintrc.js

项目配置如下

module.exports = {root: true,env: {browser: true,node: true,es6: true,},parserOptions: {parser: 'babel-eslint',sourceType: 'module'},parser: 'vue-eslint-parser',extends: 'eslint:recommended',rules: {'indent': ['error',2,{SwitchCase: 1,},],'indent-legacy': 'off','generator-star-spacing': ['error',{'before': false,'after': true,},],// 该规则在数组括号内强制实现一致的间距。'array-bracket-spacing': ['error','never',],// {} 中间是否需要空格'object-curly-spacing': [0,'always',{objectsInObjects: false}],// 该规则都会在箭头函数参数周围加上括号'arrow-parens': ['error','as-needed',],'no-constant-condition': 'warn','comma-dangle': [0,{'arrays': 'always-multiline','objects': 'always-multiline','imports': 'always-multiline','exports': 'always-multiline','functions': 'never',},],// 禁止的debugger'no-debugger': process.env.NODE_ENV === 'production' ? 'error' : 0,// 不允许在对象文字中使用重复键。'no-dupe-keys': 'warn','no-empty-character-class': 'error','no-ex-assign': 'error','no-extra-boolean-cast': 'warn','no-func-assign': 'warn','no-inner-declarations': 'warn','no-invalid-regexp': 'error','no-negated-in-lhs': 'error',// 不允许调用Math,JSON和Reflect对象作为功能'no-obj-calls': 'error','no-sparse-arrays': 'error',// 不允许可达代码后return,throw,continue,和break语句'no-unreachable': 'error','use-isnan': 'error','valid-typeof': 'error','curly': ['error','all',],'eqeqeq': ['error','allow-null',],'guard-for-in': 'warn','no-else-return': 'warn','no-labels': ['warn',{'allowLoop': true,},],'no-eval': 'warn','no-extend-native': 'error','no-extra-bind': 'warn','no-implied-eval': 'warn','no-iterator': 'error','no-irregular-whitespace': 'warn','no-lone-blocks': 'warn','no-loop-func': 'warn','no-multi-str': 'warn','no-native-reassign': 'error','no-new-wrappers': 'error','no-octal': 'warn','no-octal-escape': 'warn','no-proto': 'error','no-redeclare': 'warn','no-self-compare': 'error','no-unneeded-ternary': 'error','no-with': 'warn','wrap-iife': ['error','any',],'no-delete-var': 'warn','no-dupe-args': 'error','no-duplicate-case': 'error','no-label-var': 'warn','no-shadow-restricted-names': 'error','no-undef': 'error','no-undef-init': 'warn','no-unused-vars': ['warn',{'vars': 'local','args': 'none',},],'no-use-before-define': ['error','nofunc',],'brace-style': ['warn','1tbs',{},],'comma-spacing': ['error',{'before': false,'after': true,},],'comma-style': ['error','last',],'new-parens': 'warn','no-array-constructor': 'error','no-multi-spaces': ['error',{'exceptions': {'Property': true,'BinaryExpression': true,'VariableDeclarator': true,},},],'no-new-object': 'error','no-trailing-spaces': 'error','no-extra-parens': 'off','no-mixed-spaces-and-tabs': 'error','one-var': ['error','never',],'operator-linebreak': ['error','before',],'quotes': ['error','single',],'semi': ['error','always',],'semi-spacing': 'error','keyword-spacing': 'error','key-spacing': ['error',{'beforeColon': false,'afterColon': true,},],'space-before-function-paren': ['error',{'anonymous': 'always','named': 'never',},],'space-before-blocks': ['error','always',],'computed-property-spacing': ['error','never',],'space-in-parens': ['error','never',],'space-unary-ops': 'warn','spaced-comment': ['error','always',{'exceptions': ['-','+','\'',],'block': {'balanced': true,},},],'max-nested-callbacks': ['warn',4,],'max-depth': ['warn',6,],'max-len': ['error',200,4,{'ignoreUrls': true,'ignoreComments': true,},],'max-params': ['warn',6,],'space-infix-ops': 'error','dot-notation': ['error',{'allowKeywords': true,'allowPattern': '^catch$',},],'arrow-spacing': 'error','constructor-super': 'error','no-confusing-arrow': ['error',{'allowParens': true,},],'no-class-assign': 'warn','no-const-assign': 'error','no-dupe-class-members': 'warn','no-this-before-super': 'warn','no-var': 'warn','no-duplicate-imports': 'warn','prefer-rest-params': 'error','unicode-bom': 'warn','max-statements-per-line': 'error','no-useless-constructor': 'warn',}
};

如果对具体配置规则不清楚,可以参照https://cloud.tencent.com/developer/chapter/12618的文档说明

执行脚本

执行npm run lint ,可以查看问题文件及数量

执行npm run fix ,可以自动修复大部分问题

需要注意的是,脚本执行完了之后,可能会有部分未能解决的错误,这时需要手动修复解决

配置vscode的eslint

安装eslint插件之后,打开配置文件(文件-首选项-设置),搜索setting.json并打开

修改setting.json中的配置如下:

  {// vscode默认启用了根据文件类型自动设置tabsize的选项"editor.detectIndentation": false,// 重新设定tabsize"editor.tabSize": 2,// #每次保存的时候自动格式化 "editor.formatOnSave": true,// #每次保存的时候将代码按eslint格式进行修复"eslint.autoFixOnSave": true,// 添加 vue 支持"eslint.validate": [{"language": "javascript","autoFix": true},"javascriptreact",{"language": "vue","autoFix": true}],//  #让prettier使用eslint的代码格式进行校验 "prettier.eslintIntegration": true,//  #去掉代码结尾的分号 "prettier.semi": true,//  #使用带引号替代双引号 "prettier.singleQuote": true,//  #让函数(名)和后面的括号之间加个空格"javascript.format.insertSpaceBeforeFunctionParenthesis": false,// #这个按用户自身习惯选择 "vetur.format.defaultFormatter.html": "js-beautify-html",// #让vue中的js按编辑器自带的ts格式进行格式化 "vetur.format.defaultFormatter.js": "vscode-typescript","vetur.format.defaultFormatterOptions": {"js-beautify-html": {"wrap_attributes": "force-aligned",// #vue组件中html代码格式化样式"wrap_line_length": 200, // 每行数"end_with_newline": true,"semi": false,"singleQuote": true}},// 格式化stylus, 需安装Manta's Stylus Supremacy插件"stylusSupremacy.insertColons": false, // 是否插入冒号"stylusSupremacy.insertSemicolons": false, // 是否插入分好"stylusSupremacy.insertBraces": false, // 是否插入大括号"stylusSupremacy.insertNewLineAroundImports": false, // import之后是否换行"stylusSupremacy.insertNewLineAroundBlocks": false,"editor.suggestSelection": "first","vsintellicode.modify.editor.suggestSelection": "automaticallyOverrodeDefaultValue","editor.codeActionsOnSave": {"source.fixAll.eslint": true},"editor.fontLigatures": null // 两个选择器中是否换行
}

目前暂定为以上配置项,后续根据情况进行更新调整

eslint代码自动修复相关推荐

  1. vue关闭eslint代码检测

    在vue项目中安装了eslint代码检测,会使你的代码得到规范,但有时候可能会阻止你的下一步程序编写和运行,这时候需要关闭代码检测  需要在项目下的根目录中修改vue.config.js,没有这个文件 ...

  2. 二、为uniapp项目配置基于airbnb风格的Eslint代码检查规则

    上一节我们一起创建了基于Vue3 + TS + Vite的uniapp项目并配置了自动导入,接下来让我们一起来为项目配置基于基于airbnb规范的Eslint代码检查规则. 1.为什么要为项目配置Es ...

  3. VsCode Eslint 代码检测工具

    VsCode Eslint 代码检测工具 Vscode中安装ESLint 1.安装eslint插件 2.进入设置,添加ESlint设置代码 {"eslint.autoFixOnSave&qu ...

  4. (开发)ESLint - 代码规范

    参考文档:http://eslint.cn/ ESLint 是在 ECMAScript/JavaScript 代码中识别和报告模式匹配的工具,它的目标是保证代码的一致性和避免错误.在许多方面,它和 J ...

  5. ESLint代码检查

    代码检查 esLint是一种代码检查工具.会帮助我们自动修复错误代码. 错误提示: 代码修复前: 代码修复后:

  6. vue eslint 代码自动格式化

    vue-cli 代码风格为 JavaScript Standard Style 代码检查规范严格,一不小心就无法运行,使用eslint的autoFixOnSave可以在保存代码的时候自动格式化代码 V ...

  7. 编辑器eslint格式_VScode格式化代码,开启ESlint代码检测方法,eslint代码配置

    ESlint:是用来统一JavaScript代码风格的工具,不包含css.html等. 背景: 近来研究前端,然后一直在百度上找VScode格式化(ESlint)的插件,结果找了半天都不靠谱.目前没有 ...

  8. eslint代码检查工具

    what?什么是eslint? Eslint是一个 ECMAScript/JavaScript代码检查工具,使用node.js编写.可以使用eslint默认规则,也可以创建自己的检测规则. why?为 ...

  9. Eslint代码规范

    转载于:https://www.cnblogs.com/souleigh-hong/p/9050217.html

最新文章

  1. P3391 【模板】文艺平衡树(Splay)
  2. iOS之深入解析内存对齐的底层原理
  3. 遍历某个文件夹下的所有文件并格式化显示出来
  4. GIS集成技术之二:数据集成
  5. Asp.Net自学笔记...(运算符)
  6. gson java 对象_Gson把json串转换成java实体对象
  7. MyBatis教程目录
  8. 2020-11-01
  9. linux dd 命令拷贝iso到U盘
  10. 英伟达显卡不同架构_NVIDIA显卡架构代号的另一面:他们都是伟大的科学先驱
  11. 软考架构设计师论文(微服务)
  12. Web安全班作业 | WireShark抓包ARP报文分析并实施ARP中间人攻击
  13. WLW blog 小技巧
  14. ORACLE中单引号和双引号使用区别详解
  15. 基于Keras实现鸢尾花分类
  16. 2022年度《福布斯旅游指南》公布,123家中国酒店上榜 | 美通社头条
  17. Java之Properties集合
  18. 程序员30岁前,该如何规划自己的职业发展?
  19. 华为基本配置命令(整理)
  20. 华为进入公有云的为与不为

热门文章

  1. 315护眼灯测试哪一款合格?盘点315合格儿童护眼灯品牌
  2. html5电路模拟器,eda仿真软件
  3. Android材料设计之材料主题
  4. 【操作】NUC电脑升级BIOS
  5. jquery点击获取当前图片地址
  6. 工业数据采集与处理-1. 绪论
  7. 计算24点游戏精化算法剖析
  8. 数学英语不好可以想计算机吗,高中数学和英语不好可以学计算机专业吗
  9. A卡速度表以及软件的设置参数(二…
  10. Kotlin学习笔记(三):Kotlin中的函数