fix(js_formatter): fix propagate_expand
for interned elements with best_fitting
#1141
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
Playground Link
This one was fun! The problem:
When an expression breaks over multiple lines, it expands. That expansion is propagated up to all of the enclosing groups, forcing them to expand as well. This only stops when a
best_fitting
element is encountered in the enclosing ancesstry.In this case, the
{ brokenObject }
expression expands, meaning everything above it should also expand: the function call expression, the arrow expression, and thethen
call expression at the top. When this happens, the output matches Prettier.However, Biome was not properly propagating the expansion in this case, specifically because of the presence of a
best_fitting
element and the use ofinterned
elements. Specifically, when propagating expansion,BestFitting
would setexpands = false
, causing the interned element to think it did not expand, and any references to it to lose the propagation.The rest of this description is taken from the comment added inline in the code:
BestFitting acts as a boundary, meaning there is no need to continue processing this element and we can move onto the next. However, we don't set
expands = false
, because that ends up negating the expansion when processingInterned
elements.Only interned lists are affected, because they cache the expansion value based on the value of
expands
at the end of iterating the children. If abest_fitting
element occurs after the last expanding element, and we end up settingexpands = false
here, then the interned element ends up thinking that its content doesn't expand, even though it might. Example:Here,
group(1)
gets expanded directly by theexpand_parent
element. This happens immediately, and thenexpands = true
is set. The interned element continues processing, and encounters thebest_fitting
. If we setexpands = false
there, then the interned element's result ends up beingfalse
, even though it does actually expand. Then, whengroup(2)
checks for expansion, it looks at the ref tointerned 1
, which thinks it doesn't expand, and sogroup(2)
stays flat.By not setting
expands = false
, even thoughbest_fitting
is a boundary for expansion, we ensure that any references to the interned element will get the correct value for whether the contained content actually expands, regardless of the order of elements within it.Instead, just returning false here enforces that
best_fitting
doesn't think it expands itself, but allows other sibling elements to still propagate their expansion.Test Plan
Added an inline test to cover this case of propagation for interned
best_fitting
elements. No other test cases seemed to hit this, but I saw it come up a few times when running Biome on another large codebase.