paleite / eslint-plugin-diff

Run ESLint on your changes only
https://npm.im/eslint-plugin-diff
MIT License
181 stars 14 forks source link

Add new feature to lint against committed files #59

Open forivall opened 11 months ago

forivall commented 11 months ago

Background

Because of various factors, my team prefers to run lints in a git pre-push hook. While the diff or staged config could work, an ideal solution is to only lint the changes that are actually being pushed to the remote.

Solution

This PR adds a new config and processor, "committed" that allows linting only changes that have already been committed. It requires that the ESLINT_PLUGIN_DIFF_COMMIT environment variable is set, otherwise there will be nothing in the diff to lint.

Additionally, if a file has been changed on disk compared to the HEAD commit, the preprocessor will fetch the commmitted contents from git and lint that instead -- this preprocessor is why the autofix is disabled. This technique could be used in an alternative version of the staged linter which would lint the file as it has been committed rather than throwing the "unstaged changes" error.

Other notes

I've done a bit of refactoring to my tastes, although those can be omitted. They're available in separate branches: 1st, swapping out git porcelain commands for git plumbing commands (more stable interface), and 2nd, some miscellaneous changes (use filter instead of reduce, using string encoding in execFileSync instead of .toString(), and changing getPreProcessor so that it fetches the diff file list when linting is run, instead of when the file is loaded, allowing for other plugins to change process.env.ESLINT_PLUGIN_DIFF_COMMIT if they need to, before eslint-plugin-diff.

I would like to expand this into an independent CLI tool to allow ideal pre-push linting, but hopefully you consider merging my changes! If not, I'm happy to maintain a friendly fork -- This has been published as @forivall/eslint-plugin-diff.