fix(js_formatter): remove conditional expanded content in RemoveSoftLinesBuffer
#1032
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
In #934 I tried fixing an issue where arrow chains as call arguments wouldn't cause the parent to expand even if the signatures broke over multiple lines. In doing that, I saw that it was using the
RemoveSoftLinesBuffer
, which should've matched Prettier's behavior, but wasn't working for some reason. So, I introducedParameterLayout::Compact
, which did some additional forcing to remove potential line breaks, and that did the trick.But, when running Biome on our monorepo, this peculiar issue came up: Playground Link
Biome breaks the call arguments, even though they fit exactly in the line width (80 chars here), while Prettier correctly fits the line.
After some investigation, it turns out this is because the object destructuring pattern was adding an
if_group_breaks(",")
entry in the doc for the trailing comma on the object pattern. But that entry shouldn't get rendered when the object fits on a single line. I'm not totally sure why it was being printed when checking if the line fits, but that extra character caused it to not fit and forced the parent to expand. Prettier doesn't have this entry in the IR for the same code, so I started looking at that.Taking a closer look at their removeLinesFn, it also checks for
DOC_TYPE_IF_BREAK
and forces the flat content to be rendered instead of the expanded content...Ah! Biome'sRemoveSoftLinesBuffer
was only checking for actual soft line breaks, but it didn't check for this conditional content.So I added in similar logic to remove any conditional content for when
mode == PrintMode::Expanded
and...suddenly it all works! That turned out to be the missing piece all along. In fact, it actually solved the previous issue as well, meaningParameterLayout::Compact
wasn't actually necessary.So, this PR fixes the
RemoveSoftLinesBuffer
logic to match Prettier, and also removs theParameterLayout::Compact
change from that other PR.Test Plan
Added a spec test to cover this specific case, and running the test suite didn't cause any other changes to existing snapshots.