Skip to content

Instantly share code, notes, and snippets.

@oroce
Created April 25, 2014 08:42
Show Gist options
  • Star 37 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save oroce/11282380 to your computer and use it in GitHub Desktop.
Save oroce/11282380 to your computer and use it in GitHub Desktop.
run eslint only on changed (*.js files) files using pre-commit
{
"scripts": {
"eslint": "LIST=`git diff-index --name-only HEAD | grep .*\\.js | grep -v json`; if [ \"$LIST\" ]; then eslint $LIST; fi"
},
"devDependencies": {
"pre-commit": "0.0.7",
"eslint": "~0.5.1"
},
"pre-commit": [
"eslint"
]
}
@saalmaan
Copy link

saalmaan commented Mar 2, 2016

Mac osx terminal above shell script throws an error bellow works

{
  "scripts": {
    "eslint": "LIST=`git diff-index --name-only HEAD | grep -f *.js | grep -v json;`; if [ $LIST ]; then eslint $LIST; fi"
  },
  "devDependencies": {
    "pre-commit": "0.0.7",
    "eslint": "~0.5.1"
  },
  "pre-commit": [
    "eslint"
  ]
}

@dlutwuwei
Copy link

dlutwuwei commented Aug 10, 2017

@saalmaan you should change 'grep -f .js' to 'grep .\.js', grep files is wrong.

@hemikak
Copy link

hemikak commented Sep 14, 2017

I am getting this error :

grep: brackets ([ ]) not balanced

Any idea why ?

@M-Porter
Copy link

@EddieOne
Copy link

When I run npm run eslint it doesn't do anything

@scottwarren
Copy link

When I run npm run eslint it doesn't do anything

@EddieOne do you have any modified/changed (according to Git) *.js files locally? If not, it won't (and shouldn't) do anything

@kelvien
Copy link

kelvien commented Jul 10, 2018

Just adding to this thread

git diff-index --name-only HEAD | grep -E "(.*)\.(jsx|js)" | xargs node_modules/eslint/bin/eslint.js -c .eslintrc

@grebenyuksv-preply
Copy link

A tiny thing: you might want to grep -E "(.*)\.(jsx|js)$"

@vogelino
Copy link

vogelino commented Oct 24, 2018

This includes files that where deleted. Eslint obviously cannot check deleted files and therefor crashes. Is there a way to exclude these?

PS: Thx for this very useful snippet!

@vogelino
Copy link

I found the solution, apparently you can exclude deleted filed using --diff-filter=d.
This gives us:

LIST=`git diff-index --name-only --diff-filter=d HEAD | grep .*\\.js | grep -v json`; if [ \"$LIST\" ]; then eslint --fix $LIST; fi

@lifenstein
Copy link

This works well enough if there are only a handful of files, but in a large project with hundreds if not thousands of files, a large merge commit can include a lot of ofiles. LIST is too big in this case - I'm getting ENAMETOOLONG error.

@Vhndaree
Copy link

Vhndaree commented Jan 10, 2020

@grebenyuksv-preply I think it is great idea to run lint on all possible ext but I got this err
/bin/sh: 1: [: src/services/acc.js: unexpected operator
my setup "LIST=`git diff-index --name-only HEAD | grep -E \"(.*)\\.(jsx|js|scss)$\" | grep -v json;`; if [ $LIST ]; then eslint $LIST; fi"
You have any idea, Why I'm having this?

@protoEvangelion
Copy link

This worked for me on mac:

        "eslint": "LIST=`git diff-index --name-only --diff-filter=d HEAD | grep -E \"(.*)\\.(tsx|ts)\" | grep -v json`; if [ \"$LIST\" ]; then eslint $LIST --fix; fi",

@Bananen1234
Copy link

Awesome dudes! Wont need lint-staged anymore

@Bananen1234
Copy link

Problem here with using eslint --fix, is that the script is passing successfully, but the fixed file is now unstaged, and git is commiting the erronous change still unfixed. Can I reuse the list of staged files and git add them??

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