React Design Patterns and Best Practices(Second Edition)
上QQ阅读APP看书,第一时间看更新

Git hooks

To avoid having unlinted code in our repository, what we can do is add ESLint at one point of our process using Git hooks. For example, we can use Husky to run our linter in a Git hook called pre-commit, and it is also useful to run our unit tests on the hook pre-push.

To install husky, you need to run:

  npm install --save-dev husky

Then in our package.json file, we can add this node to configure the tasks we want to run in the Git hooks:

  {
"scripts": {
"lint": "eslint --ext .jsx,.js src",
"lint:fix": "eslint --ext .jsx,.js --fix src",
"test": "jest src"
},
"husky": {
"hooks": {
"pre-commit": "npm lint",
"pre-push": "npm test"
}
}
}

There is a special option (flag) for the ESlint command called --fix – with this option, ESLint will try to fix all our linter errors automatically (not all of them), be careful with this option because sometimes can affect a little bit of our code style. Another useful flag is --ext to specify the extensions of the files we want to validate, in this case just the .jsx and .js files.