Created
January 5, 2018 20:29
-
-
Save wpcarro/bdd84190200afe04e3b6e15ec9301fca to your computer and use it in GitHub Desktop.
Use this as your `.git/hooks/pre-commit` to enforce ESLint-ing for your JavaScript code. Modify the grep filtering to customize your whitelist. Other examples of ESLint pre-commit hooks exist online, but many seemed too complicated for what I was looking for. Enjoy!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
################################################################################ | |
# This script will run ESLint over all staged files with js or jsx extensions. | |
# Author: William Carroll | |
################################################################################ | |
for file in $(git diff --name-only --cached | grep -E '\.jsx?$'); do | |
npx eslint ${file} | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Realizing that it is more performant to run:
npx eslint $(git diff --name-only --cached --diff-filter=d | grep -E '\.jsx?$')
This allows ESLint to handle the file batching, which it could do better than serially. It also uses the
--diff-filter
option to blacklist deleted files, since these cannot be linted.