一、为什么需要编程规范
- 团队开发,没有统一的代码标准,呈现出各种乱想
需求:让程序自动处理规范化的内容
ESLint 是 2013年6月创建的一个开源项目,它的目标非常简单,只有一个,那就是提供一个插件化的 javascript 代码检测工具,说白了就是做代码格式检测使用的
修改setting设置【恢复原始状态】
{"workbench.colorTheme": "One Dark Pro","workbench.iconTheme": "material-icon-theme","explorer.confirmDelete": false,"liveServer.settings.donotShowInfoMsg": true,"explorer.confirmDragAndDrop": false,"breadcrumbs.enabled": false,"editor.minimap.enabled": false,"editor.tabSize": 2,"[vue]": {"editor.defaultFormatter": "esbenp.prettier-vscode"},"[javascript]": {"editor.defaultFormatter": "esbenp.prettier-vscode"}}
一个代码格式化工具
- 开箱即用
- 可以集成到VSCode
- 官网:https://www.prettier.c
添加prettierrc配置文件
{"printWidth":120,//不使用分号"semi":false,//使用字符串单引号"singleQuote":true,//多行最后一个逗号不添加"trailingComma": "none"}
四、ESLint与Prettier配合解决代码格式化问题
安装eslint和Prettier插件
ESLint和Prettier的冲突问题
Angular团队规范延伸出的 Conventional Commits specification(约定式提交)
- 规范格式
- 类型可选范围
问题:
作用:
全局安装
npm install -g commitizen
配置当前项目cz生效
npm i cz-customizable --save-dev
package.json添加配置
- 指定自定义提交规范路径
"config": {"commitizen": {"path": "node_modules/cz-customizable"}}
- 指定自定义提交规范路径
新建配置文件【自定义提示文件】——.cz-config.js
module.exports = {// 可选类型types: [{ value: 'feat', name: 'feat: 新功能' },{ value: 'fix', name: 'fix: 修复' },{ value: 'docs', name: 'docs: 文档变更' },{ value: 'style', name: 'style: 代码格式(不影响代码运行的变动)' },{value: 'refactor',name: 'refactor: 重构(既不是增加feature,也不是修复bug)'},{ value: 'perf', name: 'perf: 性能优化' },{ value: 'test', name: 'test: 增加测试' },{ value: 'chore', name: 'chore: 构建过程或辅助工具的变动' },{ value: 'revert', name: 'revert: 回退' },{ value: 'build', name: 'build: 打包' }],// 消息步骤messages: {type: '请选择提交类型:',customScope: '请输入修改范围(可选):',subject: '请简要描述提交(必填):',body: '请输入详细描述(可选):',footer: '请输入要关闭的issue(可选):',confirmCommit: '确认使用以上信息提交?(y/n/e/h)'},// 跳过问题skipQuestions: ['body', 'footer'],// subject文字长度默认是72subjectLimit: 72}
实现效果【可以自动选择】
-
七、Git Hooks
提供一些钩子,检验提交是不是规范
- 完整Hooks地址: https://git-scm.com/docs/githooks
八、使用husky+commitlint检查提交描述是否符合规范
- 完整Hooks地址: https://git-scm.com/docs/githooks
commitlint:检查提交信息
npm install --save-dev @commitlint/config-conventional@12.1.4 @commitlint/cli@12.1.4
创建配置文件【commitlint.config.js】提交配置
module.exports = {// 继承的规则extends: ['@commitlint/config-conventional'],// 定义规则类型rules: {// type 类型定义,表示 git 提交的 type 必须在以下类型范围内'type-enum': [2,'always',['feat', // 新功能 feature'fix', // 修复 bug'docs', // 文档注释'style', // 代码格式(不影响代码运行的变动)'refactor', // 重构(既不增加新功能,也不是修复bug)'perf', // 性能优化'test', // 增加测试'chore', // 构建过程或辅助工具的变动'revert', // 回退'build' // 打包]],// subject 大小写不做校验'subject-case': [0]}}
commitlint配置官网: https://github.com/conventional-changelog/commitlint/blob/master/@commitlint/config-conventional/index.js
husky:git hooks工具
npm install husky@7.0.1 --save-dev
启动git hooks【.husky文件夹出现】
npm husky install
修改package.json 添加新的启动命令
npm set-script prepare "husky install"
执行 npm run prepare

- git hook安装成功
添加commit-msg
npx husky add .husky/commit-msg 'npx --no-install commitlint --edit "$1"'
九、通过pre-commit 检测提交时代码规范
使用husky配合eslint,在代码提交之前检查代码规范【per-commit】
npx husky add .husky/pre-commit "npx eslint --ext .js,.vue src"
十、lint-staged自动修复格式错误
lint-staged可以检测本次代码格式是否符合本地eslint提交规则,并修改错误代码【commit之前】
- 无需单独安装,生成项目时,vue-cli 已经帮助我们安装过了,可以直接使用就可以了
//config同级"lint-staged": {"src/**/*.{js,vue}": ["eslint --fix","git add"]}
- 无需单独安装,生成项目时,vue-cli 已经帮助我们安装过了,可以直接使用就可以了
修改pre-commit
npx lint-staged
总结
代码格式规范
- 通过ESLint+Prettier+VScode配合进行处理
- 保存代码时,自动规范化代码格式
- git提交规范
- 使用husky来检测Git hooks,并且通过以下插件完成对应的配置
- 约定式提交规范
- commitizen:git提交规范化工具
- commitlint:检查提交信息
- pre-commit:git hooks钩子
- lint-staged:只检查本次修改更新的代码,并在出现错误的时候,自动修复并且推送



