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 pre-commit hooks for formatting #10737

Open
wants to merge 18 commits into
base: dev/7.6.x
Choose a base branch
from

Conversation

jacobtylerwalls
Copy link
Member

@jacobtylerwalls jacobtylerwalls commented Apr 3, 2024

Types of changes

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Description of Change

Hand-formatting code is a drain on team productivity:

  • slows development
  • easily forgotten
  • bloats diffs
  • causes merge conflicts
  • prolongs code reviews or encourages bikeshedding
  • perhaps most importantly: can even discourage people from contributing to the project!

This PR adds prettier to format the frontend. Run it with yarn format.

It also adds a GitHub action that fails PRs without formatting. A hint is given:

[warn] Code style issues found in the above file. Run Prettier to fix.

Of course, that's a last resort, as pull request time is too late to get the real benefit. We can socialize the knowledge to enable either "format on save" in VS Code or install a pre-commit hook (included here). We might eventually have a pre-commit hook for a backend formatter also.

Demonstration

Sample changes that would be made in other pending work:
1e79453
7629b6d

Issues Solved

Closes #10575

Checklist

  • Unit tests pass locally with my changes
  • I have added tests that prove my fix is effective or that my feature works
  • I have added necessary documentation (if appropriate)

Ticket Background

  • Sponsored by: Getty Conservation Institute
  • Found by: @
  • Tested by: @
  • Designed by: @

.eslintrc.js Show resolved Hide resolved
.eslintrc.js Show resolved Hide resolved
.prettierrc Outdated Show resolved Hide resolved
package.json Outdated Show resolved Hide resolved
@@ -20,7 +21,7 @@
"type": "git"
},
"devDependencies": {
"arches-dev-dependencies": "archesproject/arches-dev-dependencies#dev/7.6.x"
"arches-dev-dependencies": "archesproject/arches-dev-dependencies#jtw/prettier"
Copy link
Member Author

Choose a reason for hiding this comment

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

Of course I'll revert this once archesproject/arches-dev-dependencies#15 is merged.

Copy link
Member Author

Choose a reason for hiding this comment

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

To test the pre-commit hook in a new project, you'll want to add this change to the project template file (and potentially reinstall arches) before creating it (or manually edit afterward and nuke/reinstall yarn)

Copy link
Contributor

@chrabyrd chrabyrd left a comment

Choose a reason for hiding this comment

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

Overall I like this feature. There are some merge conflicts that need to be taken care of.

I'm wondering if instead of something like yarn format , this could be added as a pre-commit hook to remove the friction of using this

.github/workflows/main.yml Outdated Show resolved Hide resolved
@jacobtylerwalls
Copy link
Member Author

I'm wondering if instead of something like yarn format , this could be added as a pre-commit hook to remove the friction of using this

Sure, I was planning to add something like that as a follow-up, but I'm happy to add a little PoC here.

@jacobtylerwalls
Copy link
Member Author

Added a pre-commit hook so that folks can have a frictionless formatting experience.

To use, assuming you already have the new js dependencies like prettier itself:

pip install -r arches/install/requirements_dev.txt
pre-commit install

Then write some vue/ts by hand, attempt to commit, see the auto-fix happen as the commit aborts. You can always git commit --no-verify.

I can walk interested folks through this on the forum and update the contributor docs if this lands. It is truly optional, if you like waiting for Github Actions to fail and asking you to make more commits ;)

@jacobtylerwalls jacobtylerwalls changed the title Add command to format vue/ts files #10575 Add pre-commit hook to format vue/ts files #10575 Apr 12, 2024
@jacobtylerwalls jacobtylerwalls changed the title Add pre-commit hook to format vue/ts files #10575 Add pre-commit hooks for formatting Apr 26, 2024
@jacobtylerwalls
Copy link
Member Author

@chrabyrd added black, but decided to wait on eslint/ts until #10758.

@jacobtylerwalls
Copy link
Member Author

(I'll leave this in a failing state until approval, and then I can add one more formatting commit to clean up black and add it to the ignored commit hashes.)

Copy link
Contributor

@chrabyrd chrabyrd left a comment

Choose a reason for hiding this comment

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

Is it possible to config the formatter so this pattern doesn't happen?

@jacobtylerwalls
Copy link
Member Author

jacobtylerwalls commented May 1, 2024

Unfortunately not, there's an open issue for it.

I added a playground to toy with it. You can fix it yourself by putting the closing tag on a new line, and prettier is OK with it.

Copy link
Contributor

@chrabyrd chrabyrd left a comment

Choose a reason for hiding this comment

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

Overall looks good just a couple of minor things.

Also holding off on merging this until #10758 is merged since it's blocking eslint/ts formatting hooks.

Also seeing the amount of files that require formatting in the GH action failures, does it make sense to have a separate commit that formats the codebase and then gets added to .git-blame-ignore-revs ?

package.json Outdated
"eslint:check": "NODE_PATH=./arches/app/media/node_modules ./arches/app/media/node_modules/.bin/eslint ./arches/app/src --resolve-plugins-relative-to ./arches/app/media --ext .vue,.ts --parser ./arches/app/media/node_modules/vue-eslint-parser/index.js",
"eslint:fix": "NODE_PATH=./arches/app/media/node_modules ./arches/app/media/node_modules/.bin/eslint ./arches/app/src --resolve-plugins-relative-to ./arches/app/media --ext .vue,.ts --parser ./arches/app/media/node_modules/vue-eslint-parser/index.js --fix",
"eslint:watch": "NODE_PATH=./arches/app/media/node_modules ./arches/app/media/node_modules/.bin/nodemon --watch ./arches/app/src --ext ts,vue --exec yarn --silent eslint:check",
"format": "yarn prettier arches/app/src --write",
Copy link
Contributor

Choose a reason for hiding this comment

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

can this be renamed to something like prettier:format? All other yarn commands, save actual build processes, denote which dependency they're using

.github/workflows/main.yml Show resolved Hide resolved
@@ -70,6 +70,11 @@ jobs:
echo "package.json not found, skipping yarn commands."
fi

- name: Check formatting
run: |
yarn format --check --no-write
Copy link
Contributor

Choose a reason for hiding this comment

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

This reduces to

./arches/app/media/node_modules/.bin/prettier ./arches/app/src --write --check --no-write

The --write --no-write isn't ideal. Is it worth taking the --write flag out of yarn format? Or adding a second yarn command to package.json for exclusively checking?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good catch. If I go with prettier:check and prettier:fix it will rhyme with the eslint commands.

Add formatting step to template CI workflow
@jacobtylerwalls
Copy link
Member Author

Also seeing the amount of files that require formatting in the GH action failures, does it make sense to have a separate commit that formats the codebase and then gets added to .git-blame-ignore-revs ?

Yeah that was my plan. Will wait until right before merge, as we're going to merge more python code until then.

Comment on lines +25 to +27
"resolutions": {
"@vue/compiler-sfc": "^3.4.21"
},
Copy link
Contributor

Choose a reason for hiding this comment

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

does this do anything? I'm reticent to include it at the project level if it's just quieting noise

@@ -1,5 +1,5 @@
[tool.black]
line-length = 140
line-length = 100
Copy link
Contributor

@chrabyrd chrabyrd May 8, 2024

Choose a reason for hiding this comment

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

why shorten to 100? If we're going to shorten it why not follow PEP-8 or black standard line length?

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm with you there. I'm pretty sure the black team did a huge amount of research on this, everything from accessibility to psychology to real-world usage stats, and an arbitrary value just invites more 🚲 'shedding when the whole promise of black is to liberate your team 🏇.

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.

Add prettier configuration to format vue/ts files
2 participants