代码规范
VScode配置 (适用于airbnb-base)
{
"editor.quickSuggestions": {
//开启自动显示建议
"other": true,
"comments": true,
"strings": true
},
// vscode默认启用了根据文件类型自动设置tabsize的选项
"editor.detectIndentation": false,
// 重新设定tabsize,一个tab=2个空格
"editor.tabSize": 2,
// 每次保存的时候自动格式化
"editor.formatOnSave": true,
"editor.rulers": [100, 120], // 显示代码列标
"editor.fontSize": 20,
"html.format.wrapLineLength": 100,
"html.format.endWithNewline": true,
"html.format.wrapAttributes": "aligned-multiple",
"vetur.format.defaultFormatter.html": "prettier",
// "vetur.format.defaultFormatter.js": "prettier",
// js对象去除最后一个逗号
// "prettier.trailingComma": "none",
// js单引号
"prettier.singleQuote": true,
// 禁止更新提示
"update.mode": "none",
// 使用prettier格式化js代码
"editor.defaultFormatter": "esbenp.prettier-vscode",
"[javascript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"editor.codeActionsOnSave": {
"source.fixAll": true,
"source.fixAll.eslint": false
},
"prettier.printWidth": 100,
"http.proxyAuthorization": null,
"files.associations": {
}
}
husky (绑定git hook)+ lint-staged(在你提交的文件中,执行自定义的指令)
推荐链接:(husky + lint-staged) https://blog.csdn.net/csdn_yudong/article/details/106884274
(husky): https://zhuanlan.zhihu.com/p/366786798
(lint-staged):https://juejin.cn/post/6844904048525574152
-------------------------husky
安装husky
npm install -D husky
2. 在packgae.json中添加prepare脚本
{
"scripts": {
"prepare": "husky install"
}
}
prepare脚本会在npm install(不带参数)之后自动执行。也就是说当我们执行npm install安装完项目依赖后会执行 husky install命令,该命令会创建.husky/目录并指定该目录为git hooks所在的目录。
3. 添加git hooks,运行一下命令创建git hooks
npx husky add .husky/pre-commit "npm run test"
运行完该命令后我们会看到.husky/目录下新增了一个名为pre-commit的shell脚本。也就是说在在执行git commit命令时会先执行pre-commit这个脚本。pre-commit脚本内容如下:
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"
npm run test
该脚本的功能就是执行npm run test这个命令
------------------------lint-staged
"lint-staged": {
// 此处可以配置文件夹和文件类型的范围
"src/**/*.{jsx,txs,ts,js,json,css,md}": [
"prettier --write", // 先使用prettier进行格式化
"eslint --fix", // 再使用eslint进行自动修复
"git add" // 所有通过的话执行git
]
}
*如果想跳过校验
使用 --no-verify
指令可以跳过检验规则
git add . && git commit --no-verify -m "代码规范强制提交测试"