Run linters against staged git files and don't let 💩 slip into your code base!
Linting makes more sense when running before committing your code. By doing that you can ensure no errors are going into repository and enforce code style. But running a lint process on a whole project is slow and linting results can be irrelevant. Ultimately you only want to lint files that will be committed.
This project contains a script that will run arbitrary npm and shell tasks with a list of staged files as an argument, filtered by a specified glob pattern.
npm install --save-dev lint-flow husky
- Install and setup your linters just like you would do normally. Add appropriate
.eslintrc
,.stylelintrc
, etc. - Update your
package.json
like this:
{
"scripts": {
"precommit": "lint-flow"
}
}
Now change a few files, git add
some of them to your commit and try to git commit
them.
See examples and configuration below.
I recommend using husky to manage git hooks but you can use any other tool.
{
"scripts": {
"precommit": "lint-flow eslint **/*.{js,jsx}"
}
}
This will run eslint --fix
and automatically add changes to the commit. Please note, that it doesn’t work well with committing hunks (git add -p
).
{
"scripts": {
"precommit": "lint-flow stylelint **/*.{css,scss}"
}
}