perf(formatter_core): avoid exponential will_break check on nested interned (#27211)

Remove redundant and also inefficient check.
This commit is contained in:
leaysgur committed 2026-09-30 01:48:01 +00:00
1 parent 5914a24cfa
commit cea47e3eac
1 file changed
+27 -3
@@ -215,9 +215,6 @@ impl FormatElements for [FormatElement<'_>] {
FormatElement::Tag(EndLineSuffix) => {
ignore_depth -= 1;
}
FormatElement::Interned(interned) if ignore_depth == 0 && interned.will_break() => {
return true;
}
// No `ignore_depth` guard on purpose: like Prettier's `willBreak`,
// any always-breaking line counts — even directly inside a line suffix,
// and independently of whether it propagates expansion
@@ -327,3 +324,30 @@ impl FormatElements for [FormatElement<'_>] {
self.last().and_then(|element| element.end_tag(kind))
}
}
#[cfg(test)]
mod tests {
use oxc_allocator::{Allocator, ArenaVec};
use crate::{FormatElement, FormatElements, LineMode, format_element::Interned};
#[test]
fn will_break_deeply_nested_interned() {
let allocator = &Allocator::new();
for (line, expected) in [(LineMode::Soft, false), (LineMode::Hard, true)] {
let mut elements = ArenaVec::from_array_in([FormatElement::Line(line)], &allocator);
// The non-breaking path must traverse each interned slice only once.
// Repeating it after a failed match guard makes this exponential in depth.
for _ in 0..64 {
elements = ArenaVec::from_array_in(
[FormatElement::Interned(Interned::new(elements))],
&allocator,
);
}
assert_eq!(elements.will_break(), expected);
}
}
}