上一篇文章,把eslint引入了項目中作代碼規範檢查。 可是在團隊協做中,仍是可能有同事誤提交不合規範的代碼,因而有了eslint + pre-commit 的方案。node
pre-commit是git的鉤子,顧名思義就是在提交前運行,因此通常用於代碼檢查、單元測試。git還有其餘鉤子,好比prepare-commit-msg、pre-push等,具體可查看git官網。git
npm install -D pre-commit
配置項以下:npm
"scripts": { "start": "cross-env NODE_ENV=development node build/dev.js", "build": "cross-env NODE_ENV=production node build/prod.js", "lint": "eslint --ext .jsx,.js src/ --fix ./src --cache" }, "pre-commit": [ "lint" ],
就這樣,就實現了在每次 commit 以前進行代碼檢查。咱們能夠試一下 在有不合規範代碼的狀況下 commit,出現以下:json
7:12 error Parsing error: Unexpected token 5 | 6 | export class About extends Component { > 7 | console.log(111); | ^ 8 | 9 | render () { 10 | return ( ✖ 1 problem (1 error, 0 warnings) pre-commit: pre-commit: We've failed to pass the specified git pre-commit hooks as the `lint` pre-commit: hook returned an exit code (1). If you're feeling adventurous you can pre-commit: skip the git pre-commit hooks by adding the following flags to your commit: pre-commit: pre-commit: git commit -n (or --no-verify) pre-commit: pre-commit: This is ill-advised since the commit is broken.