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

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.

主站蜘蛛池模板: 台中县| 台州市| 邻水| 卓资县| 静海县| 张家川| 海林市| 常山县| 于田县| 广元市| 连平县| 泗洪县| 上杭县| 南康市| 保定市| 子洲县| 犍为县| 团风县| 大宁县| 治多县| 乐至县| 巴彦县| 名山县| 定襄县| 舞阳县| 行唐县| 霍林郭勒市| 汉沽区| 贺州市| 湾仔区| 金华市| 拉萨市| 运城市| 扎鲁特旗| 开封县| 德惠市| 蛟河市| 留坝县| 名山县| 巴东县| 辰溪县|