Add new feature to lint against committed files #59
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Background
Because of various factors, my team prefers to run lints in a git pre-push hook. While the
diff
orstaged
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 theautofix
is disabled. This technique could be used in an alternative version of thestaged
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 changinggetPreProcessor
so that it fetches the diff file list when linting is run, instead of when the file is loaded, allowing for other plugins to changeprocess.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
.