Skip to content

Instantly share code, notes, and snippets.

@wpcarro
Created January 5, 2018 20:29
Show Gist options
  • Save wpcarro/bdd84190200afe04e3b6e15ec9301fca to your computer and use it in GitHub Desktop.
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!
#!/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
@wpcarro
Copy link
Author

wpcarro commented Jan 5, 2018

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.

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