Skip to content

Instantly share code, notes, and snippets.

@shettayyy
Last active December 10, 2021 05:27
Show Gist options
  • Save shettayyy/328da46a99a9d7c746636df1cf769675 to your computer and use it in GitHub Desktop.
Save shettayyy/328da46a99a9d7c746636df1cf769675 to your computer and use it in GitHub Desktop.
Pre-commit hook for Linting JS with ESLint before commit.
#!/bin/sh
STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep ".jsx\{0,1\}$")
ESLINT="$(git rev-parse --show-toplevel)/node_modules/.bin/eslint"
if [[ "$STAGED_FILES" = "" ]]; then
exit 0
fi
PASS=true
printf "\nValidating Javascript:\n"
# Check for eslint
if [[ ! -x "$ESLINT" ]]; then
printf "\t\033[41mPlease install ESlint\033[0m (npm i --save-dev eslint)"
exit 1
fi
for FILE in $STAGED_FILES
do
"$ESLINT" "$FILE"
if [[ "$?" == 0 ]]; then
printf "\t\033[32mESLint Passed: $FILE\033[0m"
else
printf "\t\033[41mESLint Failed: $FILE\033[0m"
PASS=false
fi
done
printf "\nJavascript validation completed!\n"
if ! $PASS; then
printf "\033[41mCOMMIT FAILED:\033[0m Your commit contains files that should pass ESLint but do not. Please fix the ESLint errors and try again.\n"
exit 1
else
printf "\033[42mCOMMIT SUCCEEDED\033[0m\n"
fi
exit $?
@jamesperi
Copy link

I had the same problem at @cowCrazy and changing if [[ "$?" == 0 ]]; then to if "$ESLINT" "$FILE"; then resolved it for me.

@jbjorge
Copy link

jbjorge commented Jan 25, 2018

This code will allow committing files that fail eslint. If you have lint errors in a staged file, but have unstaged changes to the file that contains no errors, the code above will lint the file, not the staged changes - which allows you to commit a staged change that contains errors.
I went with this instead

@thesquaremedia
Copy link

@jbjorge i dont necessarily agree. i think committing the intended work in one commit and all the code formatting in another one makes sense. specially if for whatever reason some ill formatted code made it to the repo. this way you can split the formatting from the rest of the code making it easier to review. In the case of avoiding having people to push non formatted code to either a specific branch or all branches the pre-push hook made be better.

@wwwebman
Copy link

wwwebman commented Feb 18, 2018

Maybe:

[[ -z "$STAGED_FILES" ]] && exit 0

Instead of:

if [[ "$STAGED_FILES" = "" ]]; then
  exit 0
fi

@masives
Copy link

masives commented May 24, 2018

@thesquaremedia @jbjorge
I agree that it's possible to commit unlinted code through mistake. However I found a way to check if staged file differs from working directory (git diff --color --name-only $file_name). Below is code that works quite well for me:


# Check if staged files differ from working tree
PASS_STAGING_COMPARATION=true
ALL_STAGED_FILES="$(git diff --cached --name-only)"

for file in $ALL_STAGED_FILES 
	do
	compared_file=$(git diff --color --name-only $file)
	if [[ -n $compared_file ]]; then
		printf "${RED}Staged file differs from working directory:${NC} $file. \n"
		PASS_STAGING_COMPARATION=false
	fi

	if $PASS_STAGING_COMPARATION; then
		printf "${GREEN}Staged files don't differ from working directory${NC} \n"
	fi
done

And when checking if linting went correctly just add

elif ! $PASS_STAGING_COMPARATION; then
	printf "${RED}COMMIT FAILED:${NC} Your commit contains files that have been changed after staging. Please stage them and try again. \n"
	exit 1
else

RED, GREEN and NC simply holds the color

@r17x
Copy link

r17x commented Jun 1, 2018

if you are use Tmux+Zsh & have an error with message

.git/hooks/pre-commit: 7: .git/hooks/pre-commit: [[: not found

Validating Javascript:
.git/hooks/pre-commit: 16: .git/hooks/pre-commit: [[: not found

this my solution, cange line 1 on pre-commit-eslint from

#!/bin/sh

To

#!/bin/bash

i tested on my ubuntu with Tmux+Zsh

thanks

@scottkidder
Copy link

scottkidder commented Jun 15, 2018

+1 @ri7nz solution even though I'm just on Zsh.

@chriscauley
Copy link

One more tip: instead of saving this to .git/hooks/pre-commit save it to scripts/pre-commit (or somewhere in your repo). Then run ln -s $PWD/scripts/pre-commit .git/hooks/ and add a note about this to your README.md or CONTRIBUTORS.md file. This way anyone who clones the repo will have the hook as well.

@adnanaliarshad
Copy link

adnanaliarshad commented Nov 28, 2018

@rashtay any idea why I am facing this issue?

This is occurring on source tree.

Validating Javascript:
env: node: No such file or directory

@agilles28
Copy link

thank you @rashtay, thank you all for this!

@shettayyy
Copy link
Author

@adnanaliarshad Need more details

@agilles28 Most welcome :)

@streichsbaer Awesome

@rajneshbiz
Copy link

rajneshbiz commented Mar 16, 2020

if you are use Tmux+Zsh & have an error with message

.git/hooks/pre-commit: 7: .git/hooks/pre-commit: [[: not found

Validating Javascript:
.git/hooks/pre-commit: 16: .git/hooks/pre-commit: [[: not found

this my solution, cange line 1 on pre-commit-eslint from

#!/bin/sh

To

#!/bin/bash

i tested on my ubuntu with Tmux+Zsh

thanks

Hi
I tried it but every time it result succeed whereas ng lint in angular project having eslint issue then why it all time result succeed.
please help me. I am facing this since days.

@massifaqiri
Copy link

It fails on error, but not on warnings. What to change in this script so that it fails on warnings too?

Copy link

ghost commented Mar 8, 2021

It fails on error, but not on warnings. What to change in this script so that it fails on warnings too?

You should configure your .eslintrc to treat rules you consider important as errors. You can also change the command to "$ESLINT" "$FILE" --max-warnings 0 instead.

@shettayyy
Copy link
Author

Hello Everyone,

I created this a long time ago and I no more write such manual scripts. I rely on husky for everything. So pardon me for my delayed response or if I am unable to help. I see there are a lot of helpful comments. I thank each and everyone for making this better. Cheers.

@julix-unity
Copy link

julix-unity commented Nov 19, 2021

Hello Everyone,

I created this a long time ago and I no more write manual scripts. I rely on husky for everything. So pardon me for my delayed response or if I am unable to help. I see there are a lot of helpful comments. I thank each and everyone for making this better. Cheers.

Could you update this then? I think that's why you're still getting so much traffic here.
https://levelup.gitconnected.com/how-to-run-eslint-using-pre-commit-hook-25984fbce17e

Edit: nevermind :D lol big notice right at the top, I skimmed past it - my bad.

@shettayyy
Copy link
Author

Could you update this then? I think that's why you're still getting so much traffic here.
https://levelup.gitconnected.com/how-to-run-eslint-using-pre-commit-hook-25984fbce17e

Edit: nevermind :D lol big notice right at the top, I skimmed past it - my bad.

Edited the notice to clearly indicate I am not maintaining this gist anymore. Thank you for the suggestion @julix-unity ;)

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