官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 尼勒克县| 那坡县| 山阳县| 宾川县| 永登县| 祁门县| 嵊州市| 宝坻区| 文山县| 陆良县| 简阳市| 东丰县| 镇远县| 浏阳市| 新河县| 五峰| 南昌县| 赤城县| 扶绥县| 抚顺县| 林甸县| 耒阳市| 景宁| 惠安县| 乡宁县| 长顺县| 黑山县| 双城市| 六安市| 盐亭县| 外汇| 阿荣旗| 新泰市| 萍乡市| 南京市| 冀州市| 和龙市| 太原市| 建德市| 涡阳县| 布尔津县|