Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

feat(rome_js_formatter): elide quotes in object/class members #2536

Merged
merged 6 commits into from
Jun 3, 2022

Conversation

ematipico
Copy link
Contributor

@ematipico ematipico commented May 4, 2022

Summary

This PR closes #2405

I cherry-picked the commits of the previous PR that introduced these changes in the first place, then applied the logic for checking the source type.

Test Plan

I created new test cases.

Unfortunately it's not easy to compare it with Prettier output, that's why I suggest to compare their playground with our snapshots.

@ematipico ematipico temporarily deployed to aws May 4, 2022 10:17 Inactive
}

type B = { "string": "B" };
type B = { string: "B" };
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 matches prettier's output

@github-actions
Copy link

github-actions bot commented May 4, 2022

Parser conformance results on ubuntu-latest

js/262

Test result main count This PR count Difference
Total 45878 45878 0
Passed 44938 44938 0
Failed 940 940 0
Panics 0 0 0
Coverage 97.95% 97.95% 0.00%

jsx/babel

Test result main count This PR count Difference
Total 39 39 0
Passed 36 36 0
Failed 3 3 0
Panics 0 0 0
Coverage 92.31% 92.31% 0.00%

symbols/microsoft

Test result main count This PR count Difference
Total 5946 5946 0
Passed 350 350 0
Failed 5590 5591 ❌ ⏫ +1
Panics 6 5 ✅ ⏬ -1
Coverage 5.89% 5.89% 0.00%
⁉️ Panic To Failed (1):
typeGuardsAsAssertions.symbols

ts/babel

Test result main count This PR count Difference
Total 588 588 0
Passed 519 519 0
Failed 69 69 0
Panics 0 0 0
Coverage 88.27% 88.27% 0.00%

ts/microsoft

Test result main count This PR count Difference
Total 16257 16257 0
Passed 12391 12391 0
Failed 3866 3866 0
Panics 0 0 0
Coverage 76.22% 76.22% 0.00%

@cloudflare-workers-and-pages
Copy link

cloudflare-workers-and-pages bot commented May 4, 2022

Deploying with  Cloudflare Pages  Cloudflare Pages

Latest commit: 125ad84
Status: ✅  Deploy successful!
Preview URL: https://a0ddff2a.tools-8rn.pages.dev

View logs

@github-actions
Copy link

github-actions bot commented May 4, 2022

@ematipico ematipico temporarily deployed to aws May 5, 2022 12:33 Inactive
Copy link
Contributor

@MichaReiser MichaReiser left a comment

Choose a reason for hiding this comment

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

It turns out that prettier applies the same logic to both JS and TS.

This isn't the case for all number

crates/rome_js_formatter/src/utils/mod.rs Outdated Show resolved Hide resolved
text_to_check.chars().enumerate().all(|(index, c)| {
// Members that starts with numbers in their name are not valid syntax,
// hence we can't elide the quotes
if index == 0 && c.is_numeric() {
Copy link
Contributor

Choose a reason for hiding this comment

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

I just checked this again and we forgot that we're talking about members, not identifiers.

A all digits string member can be turned into a number literal that.

{
  "123": true,
  // same as
  123: true
}

However, Prettier doesn't unquote all number members in TypeScript.

So the rule must support:

  • string forming a valid identifier -> print as identifier
  • string forming a valid number -> print as number literal (only for JS)

Copy link
Contributor Author

@ematipico ematipico May 5, 2022

Choose a reason for hiding this comment

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

That is true, but prettier doesn't seem to remove the quotes from numeric-like members (that can be valid members): playground

Copy link
Contributor

Choose a reason for hiding this comment

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

You need to change the parser to babel.

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 should be flagged now, I added a new test that should cover the case

@ematipico
Copy link
Contributor Author

ematipico commented May 5, 2022

This isn't the case for all number

Could you please share some cases that I might have missed, please? I couldn't find one

@ematipico ematipico temporarily deployed to aws May 5, 2022 15:18 Inactive
@ematipico ematipico requested a review from MichaReiser May 5, 2022 15:19
@MichaReiser
Copy link
Contributor

This isn't the case for all number

Could you please share some cases that I might have missed, please? I couldn't find one

Sorry, I didn't intend to submit that comment. I first started this comment before I then wrote the more detailed comment in utils/mod.rs

crates/rome_js_formatter/src/utils/mod.rs Outdated Show resolved Hide resolved
text_to_check.chars().enumerate().all(|(index, c)| {
// Members that starts with numbers in their name are not valid syntax,
// hence we can't elide the quotes
if index == 0 && c.is_numeric() {
Copy link
Contributor

Choose a reason for hiding this comment

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

You need to change the parser to babel.

@ematipico ematipico added the PR: on hold A PR that needs some upstream work before getting merged. label May 6, 2022
@ematipico
Copy link
Contributor Author

PR on hold as at the moment the formatter doesn't have the understanding if it's parsing a TS file or a JS file.

In order to give this information to the formatter, we have to extract it from the SourceType. We will need to pass this information first, and then we might be able to achieve this.

@ematipico ematipico force-pushed the fix/keep-quotes-on-numbers branch from ee6d28e to 9872483 Compare May 31, 2022 16:42
@ematipico ematipico temporarily deployed to aws May 31, 2022 16:43 Inactive
@ematipico ematipico changed the title fix(rome_js_formatter): keep quotes for numeric members feat(rome_js_formatter): elide quotes in object/class members May 31, 2022
@ematipico ematipico removed the PR: on hold A PR that needs some upstream work before getting merged. label May 31, 2022
@ematipico ematipico force-pushed the fix/keep-quotes-on-numbers branch from 9872483 to 250a139 Compare May 31, 2022 16:54
@ematipico ematipico temporarily deployed to aws May 31, 2022 16:54 Inactive
@ematipico ematipico force-pushed the fix/keep-quotes-on-numbers branch from 250a139 to a79ceef Compare May 31, 2022 16:58
@ematipico ematipico temporarily deployed to aws May 31, 2022 16:58 Inactive
@ematipico ematipico requested a review from MichaReiser May 31, 2022 16:59
Copy link
Contributor

@MichaReiser MichaReiser left a comment

Choose a reason for hiding this comment

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

I'll need to review the rest tomorrow. This is some sophisticated stuff that my brain isn't capable of handling at this time of a day :D

crates/rome_js_formatter/src/utils/mod.rs Outdated Show resolved Hide resolved
crates/rome_js_formatter/src/utils/string_utils.rs Outdated Show resolved Hide resolved
@ematipico ematipico temporarily deployed to aws June 1, 2022 14:44 Inactive
@ematipico ematipico requested a review from MichaReiser June 1, 2022 14:44
Copy link
Contributor

@MichaReiser MichaReiser 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. One thing that I'm wondering is if it would be better to have small helper functions for the string cleaning but then have different

  • FormatLiteralMemberName
  • FormatDirectiveLiteral
  • ...

Functions that call into this helpers. It could help to remove some branching and may help to split this code into some smaller files.

I'm considering this because it feels like it's becoming this one string manipulation that is able to handle ALL cases which makes it harder to reason about how the code works (for a specific case)

crates/rome_js_formatter/src/utils/mod.rs Outdated Show resolved Hide resolved
crates/rome_js_formatter/src/utils/string_utils.rs Outdated Show resolved Hide resolved
crates/rome_js_formatter/src/utils/string_utils.rs Outdated Show resolved Hide resolved
@ematipico ematipico temporarily deployed to aws June 3, 2022 11:24 Inactive
@ematipico ematipico force-pushed the fix/keep-quotes-on-numbers branch from 74dc656 to 125ad84 Compare June 3, 2022 11:29
@ematipico ematipico requested a review from MichaReiser June 3, 2022 11:29
@ematipico ematipico temporarily deployed to aws June 3, 2022 11:29 Inactive
@ematipico
Copy link
Contributor Author

ematipico commented Jun 3, 2022

Looks good. One thing that I'm wondering is if it would be better to have small helper functions for the string cleaning but then have different

* `FormatLiteralMemberName`

* `FormatDirectiveLiteral`

* ...

Functions that call into this helpers. It could help to remove some branching and may help to split this code into some smaller files.

I'm considering this because it feels like it's becoming this one string manipulation that is able to handle ALL cases which makes it harder to reason about how the code works (for a specific case)

Makes sense. Allow me to address these concerns in the next PR. The works on these PR are not finished yet. The cases are not over unfortunately (we still have the escaping of other chars) . At this point it might make more sense to also have a small module for each case (directive, member, literal string)

@MichaReiser
Copy link
Contributor

Looks good. One thing that I'm wondering is if it would be better to have small helper functions for the string cleaning but then have different

* `FormatLiteralMemberName`

* `FormatDirectiveLiteral`

* ...

Functions that call into this helpers. It could help to remove some branching and may help to split this code into some smaller files.
I'm considering this because it feels like it's becoming this one string manipulation that is able to handle ALL cases which makes it harder to reason about how the code works (for a specific case)

Makes sense. Allow me to address these concerns in the next PR. The works on these PR are not finished yet. The cases are not over unfortunately (we still have the escaping of other chars) . At this point it might make more sense to also have a small module for each case (directive, member, literal string)

I'm not opposed to it. Is this something you're planning to work on soon?

@ematipico
Copy link
Contributor Author

I'm not opposed to it. Is this something you're planning to work on soon?

Yes, it's in my bucket list :)

@ematipico ematipico merged commit d46fcfd into main Jun 3, 2022
@ematipico ematipico deleted the fix/keep-quotes-on-numbers branch June 3, 2022 15:19
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Only add quotes around object properties where needed
3 participants