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

feat(rome_js_formatter): Heuristic for fill arrays #3126

Merged
merged 5 commits into from
Aug 29, 2022

Conversation

MichaReiser
Copy link
Contributor

@MichaReiser MichaReiser commented Aug 29, 2022

feat(rome_js_formatter): Heuristic for flat arrays

Align Rome's heuristic with Prettier's of when use fill vs a group.

There's one difference that I intend not to fix because I believe it is a bug in Prettier.

Rome's printer automatically inserts a group around fill elements so that fits returns false if it encounters any hard line break.
Prettier doesn't do so and any hard line break will result in fits return true.

This difference is meaningful for

[
// comment2
-380014,
-253951682
  ]

where the first element contains hard line breaks because of the //comment. Prettier assumes that the content fits and, thus, prints -253... on the same line. Rome does not and istead, prints a line break after -3800...

I can demonstrate that this leads to issues where Prettier incorrectly exceeds the line width: example

[
// comment2
-380014333333333333333333333333333333333333333333333333333333333333333333,
-253951682
  ]

Tests

Average compatibility: 83.70 -> 84.00
Compatible lines: 80.79 -> 81.75

@MichaReiser MichaReiser requested a review from a team August 29, 2022 10:06
@MichaReiser MichaReiser temporarily deployed to aws August 29, 2022 10:06 Inactive
@MichaReiser MichaReiser marked this pull request as draft August 29, 2022 10:06
@github-actions
Copy link

github-actions bot commented Aug 29, 2022

@github-actions
Copy link

Parser conformance results on ubuntu-latest

js/262

Test result main count This PR count Difference
Total 45879 45879 0
Passed 44939 44939 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 1621 1621 0
Failed 4325 4325 0
Panics 0 0 0
Coverage 27.26% 27.26% 0.00%

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 12397 12397 0
Failed 3860 3860 0
Panics 0 0 0
Coverage 76.26% 76.26% 0.00%

@cloudflare-workers-and-pages
Copy link

cloudflare-workers-and-pages bot commented Aug 29, 2022

Deploying with  Cloudflare Pages  Cloudflare Pages

Latest commit: 67b33ee
Status:⚡️  Build in progress...

View logs

@MichaReiser MichaReiser changed the base branch from main to feat/parentheses-transform August 29, 2022 12:13
@MichaReiser MichaReiser added the A-Formatter Area: formatter label Aug 29, 2022
@MichaReiser MichaReiser added this to the 0.9.0 milestone Aug 29, 2022
@MichaReiser MichaReiser marked this pull request as ready for review August 29, 2022 12:17
"TG", "TH", "TJ", "TK", "TL", "TM", "TN", "TO", "TR", "TT", "TV", "TW",
"TZ", "UA", "UG", "US", "UY", "UZ", "VA", "VC", "VE", "VG", "VN", "VU",
"WF", "WS", "XK", "YE", "YT", "ZA", "ZM", "ZW", "ZZ",
"AC",
Copy link
Contributor

Choose a reason for hiding this comment

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

Personal thought: isn't it weird arrays of numbers fill the array, while the array of string literals does not? I suppose prettier has their reasons for doing so

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 considered keeping that special handling for string arrays as it isn't too hard for us to do it but then decided against it, simply to optimize for compatibility for now. I think this is something that we can consider adding again in the future.

Base automatically changed from feat/parentheses-transform to main August 29, 2022 15:13
Align Rome's heuristic with Prettier's of when use `fill` vs a group.

There's one difference that I intend not to fix because I believe it is a bug in Prettier.

Rome's printer automatically inserts a `group` around `fill` elements so that `fits` returns `false` if it encounters any hard line break.
Prettier doesn't do so and any hard line break will result in `fits` return true.

This difference is meaningful for

```javascript
[
// comment2
-380014,
-253951682
  ]

```

where the first element contains hard line breaks because of the `//comment`. Prettier assumes that the content fits and, thus, prints `-253...` on the same line. Rome does not and istead, prints a line break after `-3800..`.

I can demonstrate that this leads to issues where Prettier incorrectly exceeds the line widths: [example](https://prettier.io/playground/#N4Igxg9gdgLgprEAuEBtAOlA9FgBJAWwIRgCZMBaAZgA4AGOgRgBYq32POvufe-+ANJVIBWKgE4RjAGw1yUXLgC6mEAJAQADjACW0AM7JQAQwBOpiAHcACmYSGUxgDaXjAT0PqARqeNgA1nAwAMrGxAAyOlBwyABmzvpw3r4BQcGaflEA5sgwpgCuSSBwBF5wACblFeHGUFn5xllwAGIQpgTGMLp1yCDG+TAQaiAAFjAETgDqIzrw+hlgcMH2szoAbrNuvWD6niBRiaYw1r5ZHXEJRQBW+gAewdlOcACK+RDwF06J6hmmh70wNyaOD6MCmHTaYaacGwSY6cowEbIeg-CyJSa+TS9aEguCmNYxdQARze8BOWgcIBxhwJw1McBJOnpJ0a5yQ8S+RUSBB0uQKXMeL1JMXZl3UMGMXjhCKRSFI4t8Oic2QAwhAiMZes4nMN8okACqShwc74gNaFACSUCqsGCYIhMAAgtbgoCnp9EgBfT1AA)

```javascript
[
// comment2
-380014333333333333333333333333333333333333333333333333333333333333333333,
-253951682
  ]
```
@MichaReiser MichaReiser temporarily deployed to aws August 29, 2022 15:15 Inactive
@MichaReiser MichaReiser temporarily deployed to aws August 29, 2022 15:16 Inactive
@MichaReiser MichaReiser temporarily deployed to aws August 29, 2022 15:18 Inactive
@MichaReiser MichaReiser changed the title feat(rome_js_formatter): Heuristic for flat arrays feat(rome_js_formatter): Heuristic for fill arrays Aug 29, 2022
@MichaReiser MichaReiser merged commit 100acb4 into main Aug 29, 2022
@MichaReiser MichaReiser deleted the feat/array-expression branch August 29, 2022 15:30
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
A-Formatter Area: formatter
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

2 participants