#!/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 |
This comment has been minimized.
This comment has been minimized.
Option --diff-filter=d should be specified in git diff to exclude deleted files. |
This comment has been minimized.
This comment has been minimized.
@Fer0x Where would that go? |
This comment has been minimized.
This comment has been minimized.
@Alberth8 It'd be like this -> |
This comment has been minimized.
This comment has been minimized.
Thanks @dahjelle! Great stuff :) |
This comment has been minimized.
This comment has been minimized.
Thank you very much |
This comment has been minimized.
This comment has been minimized.
eslint is quite slow when it runs against my entire project, this is much faster, thanks!! |
This comment has been minimized.
This comment has been minimized.
Has anyone figured out a workflow for using the --fix flag during this pre-commit hook? What I've come across is that when the pre-commit hook is triggered for files that are staged, the new changes aren't included in the commit. It makes sense why it wouldn't be included: this is changing a staged file, and those changes would also have to be staged. lint-staged has a partly-working fix for this, however, and as they note, this doesn't work for partially-staged files, AKA files whose changes have been cherry-picked for a commit. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Thank you very much. |
This comment has been minimized.
This comment has been minimized.
One-liner version of the above:
|
This comment has been minimized.
This comment has been minimized.
you can do this in one line |
This comment has been minimized.
This comment has been minimized.
nice, @richistron |
This comment has been minimized.
This comment has been minimized.
@jancimajek's one-liner is great but does not handle spaces (and other funny chars) in the filename. Using The @richistron one liner will lint the changed files in the local checkout, which is not necessarily what is being committed to Git if you have not staged every hunk in a file. |
This comment has been minimized.
This comment has been minimized.
thanks |
This comment has been minimized.
This comment has been minimized.
Thanks @dahjelle. |
This comment has been minimized.
This comment has been minimized.
The scripts above works well enough for a handful of changes, but for large projects when things like a project-wide search & replace is performed, it takes ages because the files are sent one by one to eslint. Running over the whole project only takes 20 seconds, but the commit hook takes 4 minutes! Is there a way to batch the files instead of calling eslint for every file one by one? |
This comment has been minimized.
This comment has been minimized.
This is great! @lifenstein That seems like a less common case, so when it happens you could run |
This comment has been minimized.
This comment has been minimized.
I had to change the @jancimajek version a little bit, because
|
This comment has been minimized.
This comment has been minimized.
Thank you to OP -- very useful. I adapted and my #!/bin/bash
clear
fileList=$(git diff --diff-filter=d --cached --name-only | grep -E '\.(js|vue)$')
if [ ${#fileList} -lt 1 ]; then
echo -e "You have no staged .js or .vue files to test\n"
exit
fi
npx eslint ${fileList[*]} "$@"
if [ $? -ne 0 ]; then
echo -e "\nPlease fix the above linting issues before committing.\n"
exit 1
fi Benefits are:
|
This comment has been minimized.
This comment has been minimized.
Thanks @nemesarial! This works like a charm |
This comment has been minimized.
This comment has been minimized.
This does not lint "only staged changes" it lints each file that is changed. Which is annoying because it can block your commit for stuff that isn't even in the commit! Eslint should just have a |
This comment has been minimized.
Thanks. Very helpful.