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

Run prettier on JavaScript via pre-commit #1838

Merged
merged 2 commits into from
Dec 20, 2024

Conversation

adamzap
Copy link
Member

@adamzap adamzap commented Dec 20, 2024

This patch removes the global djangoproject/static/* exclude rule in .pre-commit-config.yaml to enable prettier to run on this project's JavaScript files. The djangoproject/static/js/lib/ directory is now excluded in the prettier hook because there is no value in reformatting a minified JavaScript file.

I also added a .prettierrc file to the root of the project to control JavaScript formatting.

@bmispelon Is this what you had in mind as a requirement to get #1835 in?

@adamzap adamzap force-pushed the use-prettier-on-js-in-pre-commit branch 6 times, most recently from 448add0 to 4ea09b4 Compare December 20, 2024 05:38
@adamzap adamzap marked this pull request as ready for review December 20, 2024 05:45
@adamzap
Copy link
Member Author

adamzap commented Dec 20, 2024

I added a commit to show that pre-commit will reformat JavaScript.

@@ -1,4 +1,4 @@
exclude: '(^djangoproject\/static\/.*$)'
exclude: '^djangoproject\/static\/(css\/|fonts\/|img\/|robots).*$'
Copy link
Member Author

@adamzap adamzap Dec 20, 2024

Choose a reason for hiding this comment

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

This makes pre-commit only run on the js directory in static. In a series of future PRs I'd like to remove this exclude line and fix all of the warnings generated by the other directories.

@@ -19,6 +19,7 @@ repos:
- id: debug-statements
- id: detect-private-key
- id: end-of-file-fixer
exclude: '(^djangoproject\/static\/js\/lib\/.*$)'
Copy link
Member Author

Choose a reason for hiding this comment

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

We don't need to modify minified JavaScript files to add a newline at the end.

@@ -46,6 +47,7 @@ repos:
hooks:
- id: prettier
exclude_types: [html, json, scss]
exclude: '(^djangoproject\/static\/js\/lib\/.*$)'
Copy link
Member Author

Choose a reason for hiding this comment

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

We don't want to run the formatter on minified JavaScript files.

Comment on lines +5 to +8
"options": {
"tabWidth": 2,
"singleQuote": true
}
Copy link
Member Author

Choose a reason for hiding this comment

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

These options are my personal preference. Let me know if this should be modified.

https://prettier.io/docs/en/options

This patch removes the global `djangoproject/static/*` exclude rule in
`.pre-commit-config.yaml` to enable `prettier` to run on this project's
JavaScript files. The `djangoproject/static/js/lib/` directory is now
excluded in the `prettier` hook because there is no value in
reformatting a minified JavaScript file.

I also added a `.prettierrc` file to the root of the project to control
JavaScript formatting.
@adamzap adamzap force-pushed the use-prettier-on-js-in-pre-commit branch from 1ffb7f1 to 091ca2c Compare December 20, 2024 05:56
@adamzap
Copy link
Member Author

adamzap commented Dec 20, 2024

You can see in 0091a87 that pre-commit/prettier has reformatted all relevant JavaScript according to the new rules I added.

Copy link
Member

@pauloxnet pauloxnet left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Member

@bmispelon bmispelon left a comment

Choose a reason for hiding this comment

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

This is great, I'm very happy about having a an automatically enforced standard now 🎸

@bmispelon bmispelon merged commit 6dc8f41 into django:main Dec 20, 2024
4 checks passed
@adamzap adamzap deleted the use-prettier-on-js-in-pre-commit branch December 20, 2024 21:46
ulgens added a commit to ulgens/code.djangoproject.com that referenced this pull request Dec 21, 2024
bmispelon pushed a commit to django/code.djangoproject.com that referenced this pull request Dec 28, 2024
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.

3 participants