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

Lint changed files by default in pre-push hook #4261

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

mcmire
Copy link
Contributor

@mcmire mcmire commented May 6, 2024

Explanation

Currently if you install the pre-push hook by running yarn setup-git-hooks, then each time you push, every file that can be linted will be linted. This is incredibly time-consuming if you want to push a branch and you've only changed a handful of files on your branch.

To address this, this commit adds a shell script which replaces the existing lint and lint:fix package scripts. This new script not only runs in "check" and "fix" modes (that is, either check and exit without fixes, or check and automatically make fixes), it also has the ability to detect which files have changed within the branch that you're on and only run ESLint and Prettier on those files.

To accomplish this, the argument to eslint and prettier have been simplified so that instead of excluding non-lintable files when these commands are called, ignore files are used instead. This required updating to Prettier 3, which reads from both .prettierignore and .gitignore, a useful task Prettier 2 did not do.

Of course, you don't need to install the Git hook to take advantage of the changes in this commit. The lint and lint:fix package scripts now take advantage of the new shell script, and there are also two new package scripts that delegate to the script, namely, lint:only-branch and lint:fix:only-branch.

Finally, CI will still run yarn lint to ensure that all files in the repo still pass lint.

References

Fixes #1333.

Testing

You can test this script by checking out this branch, making a change to a file that would violate an ESLint rule or violate Prettier format and then running yarn lint:only-branch. This should work regardless of whether or not you commit the change.

You can also test that the script only considers the changes to the current branch by cutting a new branch off of this branch and running yarn lint:only-branch. ESLint and Prettier shouldn't do anything since no changes have occurred on this branch relative to its parent.

Finally, if you want to know what happens on main, you can merge this branch into main (but don't push) and then run yarn lint:only-branch. You should get the same results as the first test.

Changelog

(No functional changes)

Checklist

  • I've updated the test suite for new or updated code as appropriate
  • I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate
  • I've highlighted breaking changes using the "BREAKING" category above as appropriate

@mcmire mcmire requested a review from a team as a code owner May 6, 2024 22:51
@@ -127,5 +128,8 @@ module.exports = {
'import/resolver': {
typescript: {},
},
jsdoc: {
Copy link
Contributor Author

@mcmire mcmire May 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added because .prettierrc.js is now being linted (it was being ignored before, for some reason) and ESLint doesn't know what to do with TypeScript syntax in JSDoc by default.

@@ -2,6 +2,7 @@
* @type {import('prettier').Options}
*/
module.exports = {
plugins: ['prettier-plugin-packagejson'],
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prettier does not seem to autodetect plugins in version 3. I couldn't find any mention of this in the release notes, but I found this necessary.

@@ -0,0 +1,4 @@
!.*
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prettier seems to ignore dotfiles by default.

@mcmire mcmire marked this pull request as draft May 6, 2024 23:00
scripts/lint.sh Show resolved Hide resolved
@mcmire mcmire force-pushed the add-lint-changed branch 2 times, most recently from f6072a8 to 2775ada Compare May 7, 2024 03:07
@mcmire mcmire marked this pull request as ready for review May 7, 2024 03:09
Currently if you install the pre-push hook by running `yarn
setup-git-hooks`, then each time you push, every file that can be linted
will be. This is incredibly time-consuming if you want to push a branch
and you've only changed a handful of files on your branch.

To address this, this commit adds a shell script which replaces the
existing `lint` and `lint:fix` package scripts. This new script not only
runs in "check" and "fix" modes (that is, either check and exit without
fixes, or check and automatically make fixes), it also has the ability
to detect which files have changed within the branch that you're on and
only run ESLint and Prettier on those files.

To accomplish this, the argument to `eslint` and `prettier` have been
simplified so that instead of excluding non-lintable files when these
commands are called, ignore files are used instead. This required
updating to Prettier 3, which reads from both `.prettierignore` and
`.gitignore`, a useful task Prettier 2 did not do.

Of course, you don't need to install the Git hook to take advantage of
the changes in this commit. The `lint` and `lint:fix` package scripts
now take advantage of the new shell script, and there are also two new
package scripts that delegate to the script, namely, `lint:only-branch`
and `lint:fix:only-branch`.

Finally, CI will still run `yarn lint` to ensure that all files in the
repo still pass lint.
@mcmire mcmire mentioned this pull request May 7, 2024
3 tasks
@mcmire
Copy link
Contributor Author

mcmire commented May 7, 2024

Lint violations will be fixed in #4264.

@mcmire mcmire marked this pull request as draft May 7, 2024 18:08
@mcmire
Copy link
Contributor Author

mcmire commented May 7, 2024

Putting back in draft due to Prettier 3 upgrade. Perhaps I will roll that back since that doesn't seem feasible right now without first upgrading Jest.

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.

Update lint hook to only lint files that have been changed for the current branch
1 participant