Run linters against staged git files and don't let π© slip into your code base!
Linting makes more sense when running before commiting you 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 want to lint only files that will be committed.
This project contains a script that will run arbitary npm tasks against staged files, filtered by a spicified glob pattern.
npm install --save-dev lint-staged
npm install --save-dev pre-commit
(recommended way of adding a git hook)- Install and setup your linters just like you would do normally. Add appropriate
.eslintrc
and.stylelintrc
etc. configs (see ESLint and Stylelint docs if you need help here). - Add
"lint-staged": { "eslint": "*.js" }
topackage.json
(see configuration) - Add
{ "lint-staged": "lint-staged" }
toscripts
section ofpackage.json
- Add
"pre-commit": [ "lint-staged" ]
topackage.json
You can configure lint-staged by adding a lint-staged
section to your package.json
. It should
be an object where each key is a command to run and value is a glob pattern to use for this
command. This package uses minimatch for glob patterns.
See the documentation for it in case you have question.
{
"scripts": {
"my-cool-linter": "eslint"
},
"lint-staged": {
"my-cool-linter": "*"
}
}
This config will run my-cool-linter
with all staged files passed as argument. Every script that
can be run via npm run-script
or installed via npm
is supported. This package is
using npm-which to locate scripts, so you don't need to
add { "eslint": "eslint" }
to the scripts
section of your pacakge.json
when running with default
parameters. So the above example becomes:
{
"scripts": {
},
"lint-staged": {
"eslint": "*"
}
}
If you want to pass some custom parameters to your linter, you can add it to the
scripts
section and then add it to the lint-staged
configuration. See examples below.
{
"name": "My project",
"version": "0.1.0",
"scripts": {
"lint-staged": "lint-staged"
},
"lint-staged": {
"eslint": "*.@(js|jsx)",
},
"pre-commit": [ "lint-staged" ]
}
{
"name": "My project",
"version": "0.1.0",
"scripts": {
"lint-staged": "lint-staged",
"stylelint-staged": "stylelint --syntax=scss"
},
"lint-staged": {
"eslint": "*.js",
"stylelint-staged": "*.scss"
},
"pre-commit": [ "lint-staged" ]
}