diff --git a/crates/oxc_formatter/src/formatter/format_element/document.rs b/crates/oxc_formatter/src/formatter/format_element/document.rs index 262e409c41413..3047e6b40976e 100644 --- a/crates/oxc_formatter/src/formatter/format_element/document.rs +++ b/crates/oxc_formatter/src/formatter/format_element/document.rs @@ -58,20 +58,23 @@ impl Document<'_> { _ => false, }, FormatElement::Interned(interned) => { - if let Some(interned_expands) = checked_interned.get(interned) { - *interned_expands - } else { - let interned_expands = - propagate_expands(interned, enclosing, checked_interned); - checked_interned.insert(interned, interned_expands); - interned_expands - } + // if let Some(interned_expands) = checked_interned.get(interned) { + // *interned_expands + // } else { + let interned_expands = + propagate_expands(interned, enclosing, checked_interned); + checked_interned.insert(interned, interned_expands); + interned_expands + // } } FormatElement::BestFitting(best_fitting) => { enclosing.push(Enclosing::BestFitting); - - for variant in best_fitting.variants() { - propagate_expands(variant, enclosing, checked_interned); + let mut expanded = true; + for variant in best_fitting.non_expanded_variants() { + let res = propagate_expands(variant, enclosing, checked_interned); + if expanded { + expanded = res; + } } enclosing.pop(); @@ -110,7 +113,7 @@ impl Document<'_> { // 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. - false + expanded } // `FormatElement::Token` cannot contain line breaks FormatElement::Text { text: _, width } => width.is_multiline(), diff --git a/crates/oxc_formatter/src/formatter/format_element/mod.rs b/crates/oxc_formatter/src/formatter/format_element/mod.rs index 4df64b70f9dc9..b142a040b25f7 100644 --- a/crates/oxc_formatter/src/formatter/format_element/mod.rs +++ b/crates/oxc_formatter/src/formatter/format_element/mod.rs @@ -351,6 +351,10 @@ impl<'a> BestFittingElement<'a> { self.variants } + pub fn non_expanded_variants(&self) -> &[&'a [FormatElement<'a>]] { + &self.variants[..self.variants.len() - 1] + } + /// Returns the least expanded variant pub fn most_flat(&self) -> &[FormatElement<'a>] { self.variants.first().expect(