This repository has been archived by the owner on Aug 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 660
feat(rome_js_formatter): Member chain formatting #3283
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
✅ Deploy Preview for rometools ready!
To edit notification comments on pull requests, go to your Netlify site settings. |
!bench_formatter |
Comparing refactor(rome_formatter): Best fitting fits definition Snapshot #4 to median since last deploy of rome.tools.
1 page testedHomeBrowser previews
Most significant changes5 other significant changes: Number of Requests on iPhone, 4G LTE, Number of Requests on Motorola Moto G Power, 3G connection, Total Page Size in Bytes on Chrome Desktop, Total Page Size in Bytes on iPhone, 4G LTE, Total Page Size in Bytes on Motorola Moto G Power, 3G connection Calibre: Site dashboard | View this PR | Edit settings | View documentation |
Formatter Benchmark Results
|
ematipico
approved these changes
Sep 28, 2022
Co-authored-by: Emanuele Stoppa <my.burning@gmail.com>
Co-authored-by: Emanuele Stoppa <my.burning@gmail.com>
MichaReiser
changed the title
refactor(rome_formatter): Best fitting fits definition
feat(rome_js_formatter): Member chain formatting
Sep 29, 2022
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR refactors the definition of when a
best_fitting
element fits. I plan to merge #3273 into this branch before merging to main because chaining the fits definition requires changes to the member chain which are already part of #3273 (reason why some tests are currently failing on this branch).The existing definition of when a best fitting element fits is the same as for groups:
expand_parent
, any non-soft line break, ...This PR changes the definition for best fitting to:
This is important to support use cases like:
Where the whole member chain is a best-fitting element and it should take the first variant regardless of that the
{...}
object expression is printed over multiple lines.The main challenge of this change comes from that groups inside of a best fitting variant that contains an element that forces them to break must be measured in expanded mode. This complicates things because the fits function would need to "unwind" to the outermost group (best fitting variants form a boundary) if it finds any content that causes it to expand and then continue to measure in expanded mode. The problem with "unwinding" is that it would require restoring the fits queue and fits stack to the point right before measuring the group.
I haven't found a way to do so with reasonable runtime which is why I opted for the same approach as prettier by pre-processing the IR before printing (or, in our case, after formatting). The pre-processing traverses through the whole document and sets
expanded
topropagated
onGroup
if it contains any content that forces it to expand but only if there's no best-fitting element in between. The downside of this approach is that it does not respect "if_group_breaks" nor "if_group_fits" (could potentially be added) but it seems to be working reasonably well. It also comes with the advantage that less back-tracking is required in the printer when measuring if a group fits because it only has to measure groups that contain no hard line break.Test Plan
See #3273 that fixes many prettier compatibility issues. I also added a new printer test.