Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Pre-commit hook for eslint, linting *only* staged changes.
#!/bin/bash
for file in $(git diff --cached --name-only | grep -E '\.(js|jsx)$')
do
git show ":$file" | node_modules/.bin/eslint --stdin --stdin-filename "$file" # we only want to lint the staged changes, not any un-staged changes
if [ $? -ne 0 ]; then
echo "ESLint failed on staged file '$file'. Please check your code and try again. You can run ESLint manually via npm run eslint."
exit 1 # exit with failure status
fi
done

gichuwil commented Sep 9, 2016

Thanks. Very helpful.

Fer0x commented Jan 20, 2017

Option --diff-filter=d should be specified in git diff to exclude deleted files.

@Fer0x Where would that go?

rashtay commented Apr 24, 2017

@alberth8 It'd be like this -> git diff --diff-filter=d --cached --name-only

Thanks @dahjelle! Great stuff :)

sagar12861 commented Aug 8, 2017 edited

Thank you very much

eslint is quite slow when it runs against my entire project, this is much faster, thanks!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment