Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new feature to lint against committed files #59

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

forivall
Copy link

@forivall forivall commented Dec 13, 2023

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.

@forivall forivall changed the title Use git-diff-index plumbing instead of git-diff porcelain Add new feature to lint against committed files Dec 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant