question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Please add "--diff" command line flag that just runs eslint on the diff

See original GitHub issue

The problem you want to solve. There is currently no way to run eslint on a diff. So there is no reliable way to use it as pre-commit hook. Eslint even points people to this gist claiming it “only lints staged changes”. That is a dang lie! It only lints staged files. And it lints them as they are on disk, not in git, so the suggested pre-commit hook can fail for something that isn’t even in your commit. This effects my workflow on a daily basis, and I suspect many others too.

Your take on the correct solution to problem. Another linter (but for Python) already implements this feature. I’m not sure how it is implemented, but I would assume it works something like:

  • take all files relevant files
  • lint them in their entirety
  • then only report the lines which are +'d in the diff

There is some nuance here around linting the file on disk, or linting the file as staged.

Ultimately the output of this would be something like

git diff | eslint --diff

Beautiful.

Are you willing to submit a pull request to implement this change? Yeah but I’m gonna need help!

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
mdjermanoviccommented, Dec 11, 2020

Hi @cowlicks, thanks for the issue!

I’m confused by the proposal. The problem you want to solve is about linting staged versions instead of local files, but the solution is about showing linting errors only for diff between the local file and git.

Another linter (but for Python) already implements this feature.

This doesn’t look related to staged changes.

  • then only report the lines which are +'d in the diff

A changed line can produce linting errors elsewhere, so it doesn’t seem that filtering errors by diff lines would provide much value. For example, renaming a variable in a declaration can produce no-undef and other errors across the file.

0reactions
mdjermanoviccommented, Jan 29, 2021

git diff --cached | flake8 --diff

I don’t think that any linter can work on fragments of code, the whole file is needed. How can it get the staged versions from this diff, assuming that the working tree isn’t clean? I believe this still lints file versions from the working tree, just like the gist you mentioned in the original post.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Command Line Interface - ESLint - Pluggable JavaScript Linter
A pluggable and configurable linter tool for identifying and reporting on patterns in JavaScript. Maintain your code quality with ease.
Read more >
How to run eslint --fix from npm script - Stack Overflow
To run the newly added script to auto-fix the linting issues on the command line you should run the command like as below:...
Read more >
Lint-staged NPM | npm.io
--diff-filter : By default only files that are added, copied, modified, or renamed are included. Use this flag to override the default ACMR...
Read more >
Linting in TypeScript using ESLint and Prettier - LogRocket Blog
You can include the above command in your scripts to make it easier to run. Go to package.json and add the --watch flag...
Read more >
pre-commit
Git hook scripts are useful for identifying simple issues before submission to code review. We run our hooks on every commit to automatically...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found