-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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 prettier config to support format-on-save #2323
Conversation
@@ -0,0 +1,8 @@ | |||
{ | |||
"printWidth": 120, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seemed to be the least disruptive in our codebase. Prettier docs recommend 80 but I found that it resulted in many changes (e.g. global.js increased by 200 lines)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd recommend at least 120. 80 seems like a setting from the 90s (no offense to Prettier team!) I recently started using 140 and can see it being better option (for me obviously)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't really mind 120. The liquid prettier plugin defaults to 120 as well, so the consistency there is kind of nice. But we can always make file type exceptions if we liked 80 for css but 120 for js.
@KaichenWang @kmeleta Any thoughts on this so far? This config seems to result in not too many changes, and most of the changes it introduces are from inconsistencies in our code style. If we add the prettier config file should we go ahead and format every asset? I think so, since otherwise we'll be having PRs for weeks with prettier changes. |
@@ -1,3 +1,3 @@ | |||
{ | |||
"recommendations": ["shopify.theme-check-vscode"] | |||
"recommendations": ["shopify.theme-check-vscode", "esbenp.prettier-vscode"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will prompt users to install the Prettier plugin if they're using VSCode.
@@ -0,0 +1,3 @@ | |||
{ | |||
"editor.formatOnSave": true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm on the fence about this -- if the user has a different formatter installed it might screw up.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"We" also recommend [liquid]
-specific settings which includes Shopify Prettier formatter. I don't think this will be an issue.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd like to keep it. If it becomes an issue we can always change it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Though if we keep this and didn't want to format all our files as part of this PR for some reason, we could consider adding editor.formatOnSaveMode": "modifications"
to limit the scope of the changes as we go.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd urge you to run the formatter on all files within this PR. 🙏
Not sure if my approval is worth anything (I'm not Shopify employee)
@@ -0,0 +1,8 @@ | |||
{ | |||
"printWidth": 120, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd recommend at least 120. 80 seems like a setting from the 90s (no offense to Prettier team!) I recently started using 140 and can see it being better option (for me obviously)
"tabWidth": 2, | ||
"useTabs": false, | ||
"semi": true | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
trailingComma
default is "es5"
: https://prettier.io/docs/en/options.html#trailing-commas
tabWidth
default is 2
: https://prettier.io/docs/en/options.html#tab-width
useTabs
default is false
: https://prettier.io/docs/en/options.html#tabs
semi
default is true
: https://prettier.io/docs/en/options.html#semicolons
I don't think these defaults recently (or ever) changed so you can omit them, but you can also keep them if you think it shouldn't be implicit.
@@ -0,0 +1,3 @@ | |||
{ | |||
"editor.formatOnSave": true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"We" also recommend [liquid]
-specific settings which includes Shopify Prettier formatter. I don't think this will be an issue.
assets/global.js
Outdated
this.querySelectorAll('summary').forEach(summary => summary.addEventListener('click', this.onSummaryClick.bind(this))); | ||
this.querySelectorAll('button').forEach(button => button.addEventListener('click', this.onCloseButtonClick.bind(this))); | ||
this.querySelectorAll('summary').forEach((summary) => | ||
summary.addEventListener('click', this.onSummaryClick.bind(this)) | ||
); | ||
this.querySelectorAll('button').forEach((button) => | ||
button.addEventListener('click', this.onCloseButtonClick.bind(this)) | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These, previously 2, now 6 lines, are 124 and 125 long respectively. These both would fit under 140 printWidth
. Just sayin' (your teammates already used, without formatter, 120+ printWidth
)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I actually don't find the single line version of these lines to be very readable though. Without any formatting enforce otherwise, I'd opt to break those lines right at the fat arrow myself.
if (this.mainDetailsToggle.hasAttribute('open') && !this.mainDetailsToggle.contains(document.activeElement)) this.closeMenuDrawer(); | ||
if (this.mainDetailsToggle.hasAttribute('open') && !this.mainDetailsToggle.contains(document.activeElement)) | ||
this.closeMenuDrawer(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is 139 chars long. Fits within 140 printWidth
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
140 seemed a bit long when I was experimenting (it created a few mega-long logic statements and CSS rules), but 120 seems to fit in well with existing line lengths.
I think what you have is looking good. As mentioned above, all or most of those appear to be the defaults so we can remove those as long as that still takes preference over any user-specific settings (which I believe is the case). Not sure if there's any significant motivation to be explicit with them other than easy reference.
Yeah ideally. We eventually did that with the liquid prettier, because the random formatting in subsequent PRs became distracting. But we batched those changes into several PRs so we could more easily test for regressions, given that it wasn't prod-tested at the time. I wouldn't think we have the same concern prettifying css and js. |
f2407c8
to
c9a5f7c
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good! Thanks for doing this!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Everything looks to me. Thanks Stu
Run base.css through prettier Run global.js through prettier Add prettier to list of recommended settings and enable format on save Prettify base and global again Remove unnecessary params for prettierrc Run all files through prettier Fix rule re-added by bad rebase Revery mask-blobs (don't prettier it)
72645b8
to
5d95e0e
Compare
* shopify/main: [Video] Add fade in on scroll animation (Shopify#2495) Animate rich text and email signup (Shopify#2408) [Gift card] Add help doc link and change label for translation (Shopify#2471) Add prettier config to support format-on-save (Shopify#2323) Announcements slideshow (Shopify#2394) fix: Update Follow on Shop Option Text (Shopify#2463) Enable "per-PR" async PR mode (Shopify#2488) Animate multicolumn (Shopify#2409) Fix improper image sizes in the collage section (Shopify#2478) Replaced depreciated liquid property/value (Shopify#2480)
* next: [Video] Add fade in on scroll animation (Shopify#2495) Animate rich text and email signup (Shopify#2408) [Gift card] Add help doc link and change label for translation (Shopify#2471) Add prettier config to support format-on-save (Shopify#2323) Announcements slideshow (Shopify#2394) fix: Update Follow on Shop Option Text (Shopify#2463) Enable "per-PR" async PR mode (Shopify#2488) Animate multicolumn (Shopify#2409) Fix improper image sizes in the collage section (Shopify#2478) Replaced depreciated liquid property/value (Shopify#2480) Update from Shopify for theme dawn/next Update from Shopify for theme dawn/next Update from Shopify for theme dawn/next Update from Shopify for theme dawn/next Update from Shopify for theme dawn/next Update from Shopify for theme dawn/next
* Add prettier config file * Prettify JS and CSS files Run base.css through prettier Run global.js through prettier Add prettier to list of recommended settings and enable format on save Prettify base and global again Remove unnecessary params for prettierrc Run all files through prettier Fix rule re-added by bad rebase Revery mask-blobs (don't prettier it)
PR Summary:
Adds Prettier config file so that contributors will have the same code formatting configuration by default.
Why are these changes introduced?
Fixes https://github.com/Shopify/dawn-private/issues/105
Using an auto-formatter will help keep the code style uniform and speed up development.
What approach did you take?
My process was the following:
prettierrc.json
file with settings that matched up closely with our existing code.vscode
directory to help VSCode users get set up quicklyassets
directory to auto-format themOther considerations
Decision log
Visual impact on existing themes
There should be no changes.
Generally speaking the changes fall in to a few categories:
0
to decimals, e.g..13
becomes0.13
e => {
becomes(e) => {
)Testing steps/scenarios
The changes should all be inconsequential, but smoke testing is recommended. Ensure no new console errors are logged or other issues detected in the inspector.
Demo links
Checklist