ESLint as Git Hook

I came across this great post by my friend Ran Bar-Zik. He explains how to easily set up eslint to run as a git hook.

Use huskey

First, you’ll nee huskey:

npm i -D huskey

Now, just add precommit script:

"scripts": {
  "precommit": "eslint ."
}

Save Time

While this is great, the problem is that running eslint on your whole project, might take a while… So, since you obviously use a build process that’s running eslint on the full code base, on precommit you can just run it on modified files!

Get modified *.js files:

git ls-files --modified | grep .js$

Now, in package.json:

"scripts": {
  "precommit": "eslint `git diff --name-only --staged | grep .js$` --fix"
}

Note

The backtick ` allows us to run a “nested” command.

Note 2

I love the --fix flag..