From ee0756b5c62258549bcd622a5248b2683229a5f1 Mon Sep 17 00:00:00 2001
From: Dunqing <29533304+Dunqing@users.noreply.github.com>
Date: Wed, 3 Dec 2025 06:59:20 +0000
Subject: [PATCH] fix(formatter): JSX text wrapping incorrect 2 (#16320)
* close: #16199
~~Two tests regressed, but I kind of think they are correct. I created https://github.com/prettier/prettier/issues/18384 to confirm the issue in Prettier.~~ Aligned with Prettier's current behavior
---
.../src/formatter/printer/mod.rs | 18 ++++---
.../tests/fixtures/js/jsx/issue-16199.jsx | 12 +++++
.../fixtures/js/jsx/issue-16199.jsx.snap | 49 +++++++++++++++++++
3 files changed, 72 insertions(+), 7 deletions(-)
create mode 100644 crates/oxc_formatter/tests/fixtures/js/jsx/issue-16199.jsx
create mode 100644 crates/oxc_formatter/tests/fixtures/js/jsx/issue-16199.jsx.snap
diff --git a/crates/oxc_formatter/src/formatter/printer/mod.rs b/crates/oxc_formatter/src/formatter/printer/mod.rs
index 64d432aa8eccc..570c314d53309 100644
--- a/crates/oxc_formatter/src/formatter/printer/mod.rs
+++ b/crates/oxc_formatter/src/formatter/printer/mod.rs
@@ -48,7 +48,7 @@ impl<'a> Printer<'a> {
}
};
let buffer = CodeBuffer::with_indent(indent_char, indent_width);
- Self { options, state: PrinterState { buffer, ..Default::default() } }
+ Self { options, state: PrinterState::new(buffer) }
}
/// Prints the passed in element as well as all its content
@@ -456,8 +456,6 @@ impl<'a> Printer<'a> {
measurer.finish();
- self.state.measured_group_fits = true;
-
// Print all pairs that fit in flat mode.
for _ in 0..flat_pairs {
self.print_fill_item(
@@ -655,10 +653,6 @@ impl<'a> Printer<'a> {
}
self.state.line_width = 0;
-
- // Fit's only tests if groups up to the first line break fit.
- // The next group must re-measure if it still fits.
- self.state.measured_group_fits = false;
} else {
let char_width = if char == '\t' {
// SAFETY: `'\t'` is an valid ASCII character
@@ -714,6 +708,16 @@ struct PrinterState<'a> {
fits_queue: Vec<&'a [FormatElement<'a>]>,
}
+impl PrinterState<'_> {
+ pub fn new(buffer: CodeBuffer) -> Self {
+ Self {
+ buffer,
+ // Initialize `measured_group_fits` to true to indicate that groups are initially assumed to fit.
+ measured_group_fits: true,
+ ..Default::default()
+ }
+ }
+}
/// Tracks the mode in which groups with ids are printed. Stores the groups at `group.id()` index.
/// This is based on the assumption that the group ids for a single document are dense.
#[derive(Debug, Default)]
diff --git a/crates/oxc_formatter/tests/fixtures/js/jsx/issue-16199.jsx b/crates/oxc_formatter/tests/fixtures/js/jsx/issue-16199.jsx
new file mode 100644
index 0000000000000..61e102b0a9220
--- /dev/null
+++ b/crates/oxc_formatter/tests/fixtures/js/jsx/issue-16199.jsx
@@ -0,0 +1,12 @@
+export default function ProTip() {
+ return (
+
+
+ Pro tip: See more{' '}
+
+ BREAK THIS
+ {' '}
+ on the MUI documentation.
+
+ )
+}
\ No newline at end of file
diff --git a/crates/oxc_formatter/tests/fixtures/js/jsx/issue-16199.jsx.snap b/crates/oxc_formatter/tests/fixtures/js/jsx/issue-16199.jsx.snap
new file mode 100644
index 0000000000000..4fdd369c92453
--- /dev/null
+++ b/crates/oxc_formatter/tests/fixtures/js/jsx/issue-16199.jsx.snap
@@ -0,0 +1,49 @@
+---
+source: crates/oxc_formatter/tests/fixtures/mod.rs
+---
+==================== Input ====================
+export default function ProTip() {
+ return (
+
+
+ Pro tip: See more{' '}
+
+ BREAK THIS
+ {' '}
+ on the MUI documentation.
+
+ )
+}
+==================== Output ====================
+------------------
+{ printWidth: 80 }
+------------------
+export default function ProTip() {
+ return (
+
+
+ Pro tip: See more{" "}
+
+ BREAK THIS
+ {" "}
+ on the MUI documentation.
+
+ );
+}
+
+-------------------
+{ printWidth: 100 }
+-------------------
+export default function ProTip() {
+ return (
+
+
+ Pro tip: See more
+ BREAK THIS
+ on
+ the MUI documentation.
+
+ );
+}
+
+===================== End =====================