当前位置: 首页 > news >正文

vue项目使用eslint+prettier管理项目格式化

代码格式化、规范化说明

使用eslint+prettier进行格式化,vscode中需要安装插件ESLint、Prettier - Code formatter,且格式化程序选择为后者(vue文件、js文件要分别设置)  
对于eslint规则,在格式化时不会全部自动调整,建议的处理方式为:   
1.手工按提示进行处理  
2.vsocode中在源码上右键---源代码操作--Fix all ESLint auto-fixable problems(主要包含attribute小写、顺序、js代码块顺序等)  
使用此方式要注意,有时会处理错误,建议先格式化一次后再进行此操作  
3.执行npm run lint,此种方式针对所有文件,但有的错误或警告有时候无法修复  
4.对单行取消提示:在前面加注释如// eslint-disable-next-line vue/require-default-prop,如果是vscode可以通过鼠标悬浮出现修复提示后自动添加。但要注意,对有的代码,如html元素有多个attribute换行时,前面增加 // ....这样的注释并不被认可。  
5.对文件取消息提示:在vue文件第一行增加,在js文件第1行增加// eslint-disable  
注意:错误(红色)必须按提示进行处理,不能取消提示,警告(黄色)尽量处理,实在不好处理的,可按行或按文件取消提示

1、将指定版本的这些依赖安装到 devDependencies 中的 npm 命令:

npm install --save-dev @vue/eslint-config-standard@^4.0.0 eslint@^8.33.0 eslint-config-airbnb-base@^15.0.0 eslint-config-prettier@^8.6.0 eslint-plugin-import@^2.27.5 eslint-plugin-prettier@^3.1.4 eslint-plugin-prettier-vue@^2.1.1 eslint-plugin-vue@^9.9.0 prettier@^2.1.2 prettier-vue@^1.1.2

2、项目根路径引入相关配置文件

.prettir的全部规范只有十来个,详情可参考https://www.prettier.cn/docs/options.html)

3、scripts中引入lint

"lint": "eslint \"src/**/*.{js,vue}\" --quiet --fix",
"lint:prettier":"prettier --write \"src/**/*.{js,vue}\"",

4、使用lint

npm run lint
!!!!注意:eslint会自动修复一些错误,但有时会引入新的错误,为了确保不影响现有功能,请手动检查每个被修复的文件。!!!!

5、使用prettier脚本

npm run lint:prettier
会自动使用 Prettier 格式化项目文件errc.json:Prettier 的配置文件,用于规定代码格式化规则,如缩进、引号使用、分号添加等。
.prettierignore:指定 Prettier 不处理的文件或目录。
.eslintrc.json:ESLint 的配置文件,定义代码检查规则,包括语法错误、风格问题等。
.eslintignore:指定 ESLint 不检查的文件或目录。prettie

在这里插入图片描述
在这里插入图片描述

.prettierrc.json代码

{"printWidth": 120,"singleQuote": true,"useTabs": false,"semi": false,"tabWidth": 2,"trailingComma": "none","bracketSpacing": true,"jsxBracketSameLine": false,"arrowParens": "avoid","endOfLine": "lf"
}

.prettierignore代码

dist/*
node_modules

.eslintrc.json代码

{"root": true,"env": {"browser": true,"node": true,"es2021": true},"extends": ["airbnb-base/legacy","plugin:vue/recommended","prettier"],"parser": "vue-eslint-parser","parserOptions": {"ecmaVersion": "latest","sourceType": "module","ecmaFeatures": {"jsx": true}},"plugins": ["vue","import"],"rules": {"prefer-exponentiation-operator": "off","eqeqeq": "off","no-underscore-dangle": 0,"vue/multi-word-component-names": 0,"func-names": 0,"import/no-extraneous-dependencies": ["error",{"devDependencies": true,"optionalDependencies": true,"peerDependencies": true}],"no-console": ["warn",{"allow": ["warn","error","log"]}],"no-debugger": 2,"no-alert": 2,"no-param-reassign": ["error",{"props": false}],"lines-between-class-members": 0,"dot-notation": 0,"no-plusplus": ["error",{"allowForLoopAfterthoughts": true}]}
}

.eslintignore

dist/*
vue.config.js
vite.config.js
http://www.lryc.cn/news/486662.html

相关文章:

  • Java基础-组件及事件处理(中)
  • UNIX网络编程-TCP套接字编程(实战)
  • python编写一个自动清理三个月以前的邮件脚本
  • C++组合复用中,委托的含义与作用
  • 自制C++游戏头文件:C++自己的游戏头文件!!!(后续会更新)
  • java 读取 有时需要sc.nextLine();读取换行符 有时不需要sc.nextLine();读取换行符 详解
  • Redis知识分享(三)
  • python安装包报错
  • Linux性能优化之火焰图简介
  • Unity类银河战士恶魔城学习总结(P129 Craft UI 合成面板UI)
  • linux基础笔试练习题笔记(2)
  • Android OpenGL ES详解——glTexImage2D方法
  • Redisson 中开启看门狗(watchdog)机制
  • 【JSOO】设计模式
  • 本草纲目数字化:Spring Boot在中药实验管理中的应用
  • java八股-jvm入门-程序计数器,堆,元空间,虚拟机栈,本地方法栈,类加载器,双亲委派,类加载执行过程
  • 重构Action-cli前端脚手架
  • 华为USG5500防火墙配置NAT
  • 【大数据学习 | HBASE高级】hive操作hbase
  • 集群聊天服务器(9)一对一聊天功能
  • 《FreeRTOS列表和列表项篇》
  • C++:哈希拓展-位图
  • 【数据结构与算法】查找
  • 从零开始学习 sg200x 多核开发之 milkv-duo256 编译运行 sophpi
  • LLM - 使用 LLaMA-Factory 微调大模型 Qwen2-VL SFT(LoRA) 图像数据集 教程 (2)
  • 基于STM32设计的大棚育苗管理系统(4G+华为云IOT)_265
  • 深入浅出《钉钉AI》产品体验报告
  • 2020年计挑赛往届真题(C++)
  • ES6进阶知识二
  • 大语言模型通用能力排行榜(2024年10月8日更新)