2021-5-20 (代码规范)

代码规范

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 "代码规范强制提交测试"
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值