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

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.

主站蜘蛛池模板: 垦利县| 泰和县| 时尚| 福鼎市| 井陉县| 监利县| 邹平县| 阿克| 峡江县| 双柏县| 黄骅市| 诸城市| 桑日县| 扶绥县| 三河市| 安图县| 永福县| 山东省| 项城市| 和平县| 彭山县| 腾冲县| 旬阳县| 汉中市| 裕民县| 额济纳旗| 枣强县| 麻阳| 辽中县| 利辛县| 高安市| 株洲县| 顺义区| 朝阳区| 鄯善县| 麻栗坡县| 合川市| 柘荣县| 长子县| 嘉黎县| 台州市|