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 prettier config to support format-on-save #2323

Merged
merged 2 commits into from
Apr 4, 2023
Merged

Conversation

stufreen
Copy link
Contributor

@stufreen stufreen commented Feb 22, 2023

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:

  1. Add prettierrc.json file with settings that matched up closely with our existing code
  2. Added workspace VSCode settings to the .vscode directory to help VSCode users get set up quickly
  3. Saved all files in the assets directory to auto-format them

Other considerations

Decision log

# Decision Alternatives Rationale Downsides
1 Run all css and js files through prettier Do this on an ad-hoc basis in future PRs If we don't do this we'll likely see a lot changes in future PRs that are unrelated to the code being changed. Better to have all of the noise in this PR. This PR is very large and has some changes to almost every file. Any work in progress will need to be rebased which may be non-trivial.

Visual impact on existing themes

There should be no changes.

Generally speaking the changes fall in to a few categories:

  1. Double quote to single quote
  2. Add 0 to decimals, e.g. .13 becomes 0.13
  3. Add commas to final items in arrays
  4. Brackets around arrow function params (e.g. e => { becomes (e) => {)
  5. Indentation changes

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

@@ -0,0 +1,8 @@
{
"printWidth": 120,
Copy link
Contributor Author

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)

Copy link
Contributor

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)

Copy link
Contributor

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.

@stufreen
Copy link
Contributor Author

@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"]
Copy link
Contributor Author

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
Copy link
Contributor Author

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.

Copy link
Contributor

@vfonic vfonic Feb 24, 2023

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.

Copy link
Contributor

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.

Copy link
Contributor

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.

Copy link
Contributor

@vfonic vfonic left a 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,
Copy link
Contributor

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
}
Copy link
Contributor

@vfonic vfonic Feb 24, 2023

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
Copy link
Contributor

@vfonic vfonic Feb 24, 2023

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
Comment on lines 336 to 353
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))
);
Copy link
Contributor

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)

Copy link
Contributor

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.

Comment on lines -405 to +426
if (this.mainDetailsToggle.hasAttribute('open') && !this.mainDetailsToggle.contains(document.activeElement)) this.closeMenuDrawer();
if (this.mainDetailsToggle.hasAttribute('open') && !this.mainDetailsToggle.contains(document.activeElement))
this.closeMenuDrawer();
Copy link
Contributor

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

Copy link
Contributor Author

@stufreen stufreen Feb 24, 2023

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.

@kmeleta
Copy link
Contributor

kmeleta commented Feb 24, 2023

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.

If we add the prettier config file should we go ahead and format every asset?

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.

@stufreen stufreen force-pushed the stu/prettier-config branch from f2407c8 to c9a5f7c Compare March 31, 2023 18:59
@stufreen stufreen marked this pull request as ready for review March 31, 2023 19:44
Copy link
Contributor

@KaichenWang KaichenWang left a 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!

Copy link
Contributor

@kmeleta kmeleta left a 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

Stu Freen added 2 commits April 4, 2023 13:30
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)
@stufreen stufreen force-pushed the stu/prettier-config branch from 72645b8 to 5d95e0e Compare April 4, 2023 17:34
@stufreen stufreen merged commit 1880145 into main Apr 4, 2023
@stufreen stufreen deleted the stu/prettier-config branch April 4, 2023 17:35
pangloss added a commit to pangloss/dawn that referenced this pull request Apr 5, 2023
* 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)
@kmeleta kmeleta mentioned this pull request Apr 7, 2023
8 tasks
pangloss added a commit to pangloss/dawn that referenced this pull request Apr 12, 2023
* 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
phapsidesGT pushed a commit to Gravytrain-UK/gt-shopify-dawn-theme that referenced this pull request Sep 3, 2024
* 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)
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.

4 participants