shekhardtu
shekhardtu

Reputation: 5370

Enable eslint only for edited files

Recently, I configured eslint one of my projects. This project was consisting of hundreds of files, as soon as I configured eslint starts showing errors in all files.

IMO, eslint traverses through all js files, which is not required currently. Is there any way we can limit eslint only for edited files?

Upvotes: 57

Views: 50446

Answers (9)

madprops
madprops

Reputation: 4023

Somebody might find this useful in the future.

Only run eslint on files that have changed since the last git tag:

#!/usr/bin/env bash

# Only check files that have changed recently
last_tag=$(git describe --tags --abbrev=0)
changed_files=$(git diff --name-only $last_tag HEAD -- '*.js')

if [ -n "$changed_files" ]; then
  npx eslint --cache $changed_files
else
  echo "No files have changed since the last tag."
fi

Upvotes: 0

Yaroslav Larin
Yaroslav Larin

Reputation: 335

The answer is: do not run Eslint globally for all files. I think what you want is to prevent the offending code to be commited to repository (of course, it's already there, commited before linting was introduced, but you want to prevent new commits to be made that contain eslint errors).
This means that it makes sense to run Eslint only on those files, that have changes in them and are about to be commited to repo, i.e. staged files. There is a package that does exactly that, lint-staged. In your package.json, you add a configuration like this:

"scripts": {
  "lint": "eslint --fix",
},
"lint-staged": {
  "*.{js,mjs,cjs,jsx,ts,tsx,vue}": [
     "npm run lint"
  ]
}

Then when you run npm run lint-staged, only the files that are staged will be passed as argument to lint command, so only those will be linted. Usually it makes sense to combine lint-staged with husky to automatically run linting and some other checks like formatting as a pre-commit hook (i.e. each time when you try to make a commit those checks will be executed) This way you can make sure that no code that contains errors will be commited.

Upvotes: 6

Purkhalo Alex
Purkhalo Alex

Reputation: 3627

The following command is very convenient for me:

git diff --name-only | xargs eslint --fix

It applys eslint --fix for each row of the git diff --name-only output via xargs command.

Upvotes: 5

Reza
Reza

Reputation: 475

  1. If you want to lint staged and edited files, use:

    eslint $(git diff --name-only HEAD | grep -E '\.(js|jsx)$' | xargs)
    

    or run it with yarn if you want to use it in the terminal:

    yarn run eslint $(git diff --name-only HEAD | grep -E '\.(js|jsx)$' | xargs)
    

    Explanation:

    • git diff --name-only HEAD: name of edited files
    • grep -E '\.(js|jsx)$': Filter js and jsx files. You can add your file formats here.
    • xargs: get names as arguments
  2. Also you can use lint-staged library to lint staged files. It's useful if you want to run it before each commit and make sure your repo always remains pretty and linted.
    Read more about it here.

  3. If you want to lint last committed files, use:

    eslint $(git diff --name-only HEAD HEAD~1 | grep -E '\.(js|jsx)$' | xargs)
    

Upvotes: 27

Mikeumus
Mikeumus

Reputation: 3878

For larger files where doing a pull-request with thousands of line changes isn't practical, you can use lint-diff

This tool just lints the lines that were changed:

https://github.com/grvcoelho/lint-diff

Upvotes: 0

Tony
Tony

Reputation: 19171

eslint --fix $(git diff --name-only HEAD | xargs)

Upvotes: 47

Seeliang
Seeliang

Reputation: 2949

similar to @ELCAPITANBETO 's feedback

we can do

eslint -c eslintrc.js $(git diff --name-only --diff-filter=ACMRTUXB master | grep -E \"(.js$|.ts$|.tsx$)\")

https://gist.github.com/seeliang/0f0de424d1cdc4541c338f4ee93b7e6a

Upvotes: 1

ELCAPITANBETO
ELCAPITANBETO

Reputation: 94

I'm using this and it works like a charm

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

I've added it to the package.json as a script under the name of lint:git and now I execute it with yarn lint:git

Taken from https://gist.github.com/oroce/11282380#gistcomment-2741146

Upvotes: 5

vsemozhebuty
vsemozhebuty

Reputation: 13812

Maybe ESLint caching is what you need?

https://eslint.org/docs/user-guide/command-line-interface#caching

Upvotes: 7

Related Questions