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-staged uses prettier before git commit #1098

Merged
merged 5 commits into from
Jan 4, 2024

Conversation

abhcs
Copy link
Collaborator

@abhcs abhcs commented Jan 2, 2024

Description

Fix #1097.

Changes

  • deleted husky post-commit because it takes too long.
  • modified husky pre-commit to use only prettier on all files (.prettierignore should ensure that the correct files are fixed).
  • added a script for installing husky, as described here.
  • removed eslintconfig from package.json since we use .eslintrc.json for eslint settings.

Tests

Note that you must use npm run prepare-husky to enable husky (this should be in PolicyEngine readme).
I tested the change by trying to commit a file with the wrong format. The format was correct after commit.

@anth-volk
Copy link
Collaborator

Can you expound a bit more on the benefits of moving husky to pre-commit? I'm hoping to weigh the advantages here, because I see two potential major disadvantages:

  1. Contributors who make, say, only a few contributions will almost certainly forget to run npm run prepare-husky, though this could be included in some sort of pre-commit make rule; if it's not included in a make rule, then it's also inconsistent with what we currently do for most other actions
  2. I have to wonder if moving husky expands these changes beyond the target of this issue; is it possible to move the prettier check without moving husky around, then open a new issue for husky?

@abhcs
Copy link
Collaborator Author

abhcs commented Jan 2, 2024

husky is only there to manage git commits. lint-staged is there to ensure that the scope of lint during pre-commit is restricted to staged files. lint-staged depends on husky.

No one is using husky or lint-staged right now. You only use husky if you have installed it through husky install.

We can add npm run prepare-husky to make install.

@abhcs
Copy link
Collaborator Author

abhcs commented Jan 2, 2024

husky post-commit is doing too much. Why lint the entire code after commit? It slows down commits by a lot. Right now since husky is not installed by anyone, we do nothing pre- or post-commit.

It is best to start with a minimal pre-commit file and add more things if deemed necessary.

@anth-volk
Copy link
Collaborator

Okay, I get what you're saying - also, strange to have husky config files without installation. I know this might come off as nitpicky, but I just want to clarify a couple more things on this so that we can get it right:

  1. I'm curious why you removed the --ignore-unknown flag on prettier in lint-staged
  2. Is there a benefit to dropping eslint from lint-staged?
  3. By removing the eslintConfig option from the package.json, we also lose the two presets that were declared there, as they don't exist within .eslintrc.json at the moment - could you add those in there?

@abhcs
Copy link
Collaborator Author

abhcs commented Jan 3, 2024

Okay, I get what you're saying - also, strange to have husky config files without installation. I know this might come off as nitpicky, but I just want to clarify a couple more things on this so that we can get it right:

  1. I'm curious why you removed the --ignore-unknown flag on prettier in lint-staged

Because prettier does not use the flag in the check in the lint or fix commands. .prettierignore is deciding what prettier ignores.

  1. Is there a benefit to dropping eslint from lint-staged?

We can use eslint --fix as well. prettier is just a lightweight formatter while eslint can do much more depending on how it is configured, and hence can be much slower. I found that the annoying lint failures in the PR workflow usually stem from prettier checks -- so adding just the prettier fix here made sense to me.

  1. By removing the eslintConfig option from the package.json, we also lose the two presets that were declared there, as they don't exist within .eslintrc.json at the moment - could you add those in there?

This section in the file is overridden by .eslintrc.json -- it may have been used in the past when the file did not exist. Enabling the react-app config is the point of PR #1081.

Copy link
Collaborator

@anth-volk anth-volk left a comment

Choose a reason for hiding this comment

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

I gotcha. In that case, would you mind fixing the merge conflict and adding eslint --fix back into the script? Otherwise, I think it'd be good to go.

@abhcs abhcs requested a review from anth-volk January 4, 2024 21:18
Copy link
Collaborator

@anth-volk anth-volk left a comment

Choose a reason for hiding this comment

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

Thanks so much! Looks great.

@anth-volk anth-volk merged commit cc71ccf into PolicyEngine:master Jan 4, 2024
2 checks passed
@abhcs abhcs deleted the fix-issue-1097 branch January 4, 2024 23:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

lint-staged should use prettier before git commit
2 participants