I need a way to run ESLint on only the code lines or blocks that have changed on a branch. The idea is, that we have a big old code base and we want to enforce ESLint rules at least moving forward.
I implemented it in CI linting all the files that changed in a PR. But people are touching one line in a file and all of a sudden they have to fix 77 ESLint errors.
Personally I'm not at all concerned for them, but they are bigger and stronger than me and I need to make some baby steps here I guess.
So I can get a list of all the files changed for a particular branch. And I suspect I can do a diff and parse it and pull out all the line numbers that changed and then run ESLint and filter the results by the line numbers but this sounds like a colossal pain.
My question is is there not some better real way to do this? I'm not the first person to need this.
Just to be clear I understand that this "requirement" is strictly for edge cases such as mine where the burden of a whole file change is just too great. It's not optimal. It is sub-optimal, but if I don't do this I don't get anything so I have to pick my battles.
eslint goes over your code and reports stylistic errors. Usually going over the whole code is unneccessary, because you only changed a few files since the last run. To be able to track which files changed, and which errors occured in the files which were not changed, eslint is writing a cache (into the file you found).
If you have the ESLint extension installed you can use CTRL+SHIFT+P to open the Command Palette. Then search for ESLint: Fix all auto-fixable Problems and press ENTER (or RETURN ).
eslint --fix will fix every rule violation it is capable of fixing, actually overwrite the code, and print out any warnings or errors it was incapable of fixing. Most errors are not actually automatically fixable.
I wrote an ESLint plugin for this exact use-case. We had an existing config and wanted to do a major overhaul of the config, but didn’t want to have to fix all errors (which were previously defined as not being errors) in one go, because it would’ve been a terrible RIO.
This plugin instead allows you to only diff whatever you’ve staged and we run it in a pre-commit. Our regular “lint”-script in package.json doesn’t use the plugin, so it still shows all errors/warnings there.
Here’s a link to it: https://www.npmjs.com/package/eslint-plugin-diff
I think you can use some tool like Review Dog. It will add review feedbacks on your PR/MR based on the linter result that are part of the diff only:
reviewdog provides a way to post review comments to code hosting service, such as GitHub, automatically by integrating with any linter tools with ease. It uses an output of lint tools and posts them as a comment if findings are in diff of patches to review.
Btw, this article helped me figure this out and propose alternative solutions for your use case if you're interested.
Cheers
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With