Skip to content

Commit 6912d1f

Browse files
committed
fix(formatter): keep an arrow signature that exactly fills the line for any body (#27989)
Revert #24440 and reclassify DIVERGENCES.md#arrow-exact-fill-signature as "uniform-rule". --- > It seems like oxc is correct here and Prettier has a bug? > #24416 Having the opportunity to revisit the details..., I found that this is indeed the truth.
1 parent 3244321 commit 6912d1f

11 files changed

Lines changed: 222 additions & 237 deletions

File tree

‎crates/oxc_formatter/DIVERGENCES.md‎

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -547,30 +547,36 @@ Prettier 3.9 changed this to first retry the whole union FLAT on the indented ne
547547
Community pushback on the 3.9 style (users migrated to Oxfmt over it) put following on hold:
548548
ours deliberately matches Prettier 3.8's output for this construct.
549549

550-
## arrow-chain-exact-fill-signature
550+
## arrow-exact-fill-signature
551551

552-
- Why: cost
553-
- Pin: `tests/fixtures/js/arrow-function/chain-exact-fill-signature.js`
552+
- Why: uniform-rule (same construct, same output: arrow with a non-conditional body)
553+
- Pin: `tests/fixtures/js/arrow-function/exact-fill-signature.js`
554554

555555
```js
556556
/* input (printWidth 80) */
557+
const exactlyFillsThePrintWidthEightySignatureLinePaddedToLengthOkOkOkO = (x) => cond ? longlonglong : shortshort;
557558
const exactlyFillsThePrintWidthEightySignatureLinePaddedToLength = (x) => (y) => cond ? longlonglong : shortshort;
558559

559560
/* ours */
561+
const exactlyFillsThePrintWidthEightySignatureLinePaddedToLengthOkOkOkO = (x) =>
562+
cond ? longlonglong : shortshort;
560563
const exactlyFillsThePrintWidthEightySignatureLinePaddedToLength = (x) => (y) =>
561564
cond ? longlonglong : shortshort;
562565

563566
/* prettier */
567+
const exactlyFillsThePrintWidthEightySignatureLinePaddedToLengthOkOkOkO = (
568+
x,
569+
) => (cond ? longlonglong : shortshort);
564570
const exactlyFillsThePrintWidthEightySignatureLinePaddedToLength =
565571
(x) => (y) => (cond ? longlonglong : shortshort);
566572
```
567573

568-
An arrow chain with a parens-adding conditional body keeps a signature line that EXACTLY fills the print width (the signature alone is measured; both idempotent).
569-
Prettier counts the hug layout's literal trailing space, so the exact fill overflows:
570-
it breaks the assignment and retries the chain flat on the indented next line.
571-
One char over and the outputs converge;
572-
a naive port of the literal-space structure regresses `js/arrows/currying-4.js` (Prettier gates the hug on the chain's expand state),
573-
so matching costs more than this exact-width edge is worth.
574+
An arrow (single or chain) whose signature line EXACTLY fills the print width keeps it and puts the body on the next line, whatever the body.
575+
Prettier breaks the signature only for a parens-adding conditional body:
576+
its hug layout puts a literal space after `=>`, outside the group that may break the body onto the next line,
577+
so the space is counted although it is trimmed whenever the body breaks.
578+
As the last call argument, Prettier breaks all the arguments instead of hugging the arrow.
579+
One char over and the outputs converge.
574580

575581
## suppressed-for-head-declaration
576582

‎crates/oxc_formatter/src/print/arrow_function_expression.rs‎

Lines changed: 24 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,8 @@ impl<'a, 'b> FormatJsArrowFunctionExpression<'a, 'b> {
169169
}),
170170
};
171171
// A cast-wrapped body has no kind (see `is_cast_target`)
172+
// Prettier also keeps a parens-adding conditional on the same line (its hug layout);
173+
// we don't, see DIVERGENCES.md#arrow-exact-fill-signature
172174
body_kind_hugs && !is_cast_target(expression.span(), f)
173175
});
174176

@@ -201,34 +203,18 @@ impl<'a, 'b> FormatJsArrowFunctionExpression<'a, 'b> {
201203
|| (matches!(self.arrow.parent(), AstNodes::JSXExpressionContainer(container)
202204
if !f.context().comments().has_comment_in_range(arrow.span.end, container.span.end)));
203205

204-
if should_add_parens {
205-
// The leading space must be a literal space rather than a soft line:
206-
// it is counted when measuring whether the signature fits,
207-
// so a signature that fills the line exactly gets broken,
208-
// matching Prettier's `printArrowFunctionBody`.
209-
write!(
210-
f,
211-
[
212-
space(),
213-
group(&format_args!(
214-
if_group_fits_on_line(&token("(")),
215-
indent(&format_args!(soft_line_break(), format_body)),
216-
if_group_fits_on_line(&token(")")),
217-
is_last_call_arg.then_some(&FormatTrailingCommas::All),
218-
should_add_soft_line.then_some(soft_line_break())
219-
))
220-
]
221-
);
222-
} else {
223-
write!(
224-
f,
225-
group(&format_args!(
226-
soft_line_indent_or_space(&format_body),
227-
is_last_call_arg.then_some(&FormatTrailingCommas::All),
228-
should_add_soft_line.then_some(soft_line_break())
229-
))
230-
);
231-
}
206+
write!(
207+
f,
208+
group(&format_args!(
209+
soft_line_indent_or_space(&format_args!(
210+
should_add_parens.then_some(if_group_fits_on_line(&token("("))),
211+
format_body,
212+
should_add_parens.then_some(if_group_fits_on_line(&token(")")))
213+
)),
214+
is_last_call_arg.then_some(&FormatTrailingCommas::All),
215+
should_add_soft_line.then_some(soft_line_break())
216+
))
217+
);
232218
}
233219
}
234220
}
@@ -442,6 +428,8 @@ impl<'a> Format<'a, JsFormatContext<'a>> for ArrowChain<'a, '_> {
442428
// soft line break before the body so that it prints on a separate line
443429
// in its entirety.
444430
// A cast-wrapped body has no kind (see `is_cast_target`).
431+
// Prettier also keeps a parens-adding conditional on the same line (its hug layout) unless the chain breaks;
432+
// we don't, see DIVERGENCES.md#arrow-exact-fill-signature.
445433
let body_on_separate_line = !tail.get_expression().is_none_or(|expression| {
446434
matches!(
447435
expression,
@@ -597,25 +585,14 @@ impl<'a> Format<'a, JsFormatContext<'a>> for ArrowChain<'a, '_> {
597585
}
598586
} else {
599587
let should_add_parens = tail_body.as_expression().is_some_and(should_add_parens);
600-
if should_add_parens {
601-
// Known divergence from Prettier: with a signature that exactly fills the line,
602-
// Prettier breaks it because the hug layout's literal space is counted
603-
// when measuring fits (see the single-arrow branch above), while this soft-line
604-
// wrapping (`soft_line_indent_or_space` in `format_tail_body`) stops the measurement.
605-
// Porting the literal-space structure here is NOT enough: Prettier gates the hug
606-
// on `!shouldBreakChain` (`expand_signatures` here) and otherwise breaks without
607-
// parens; a naive port regresses `js/arrows/currying-4.js`.
608-
write!(
609-
f,
610-
[
611-
if_group_fits_on_line(&token("(")),
612-
format_tail_body,
613-
if_group_fits_on_line(&token(")"))
614-
]
615-
);
616-
} else {
617-
write!(f, [format_tail_body]);
618-
}
588+
write!(
589+
f,
590+
[
591+
should_add_parens.then_some(if_group_fits_on_line(&token("("))),
592+
format_tail_body,
593+
should_add_parens.then_some(if_group_fits_on_line(&token(")")))
594+
]
595+
);
619596
}
620597
});
621598

‎crates/oxc_formatter/tests/fixtures/js/arrow-function/chain-exact-fill-signature.js‎

Lines changed: 0 additions & 12 deletions
This file was deleted.

‎crates/oxc_formatter/tests/fixtures/js/arrow-function/chain-exact-fill-signature.js.snap‎

Lines changed: 0 additions & 51 deletions
This file was deleted.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// 10 20 30 40 50 60 70 80
2+
// 45678901234567890123456789012345678901234567890123456789012345678901234567890
3+
4+
// DIVERGENCES.md#arrow-exact-fill-signature
5+
6+
// Conditional body: diverges at printWidth 80
7+
const exactlyFillsThePrintWidthEightySignatureLinePaddedToLengthOkOkOkO = (x) =>
8+
cond ? longlonglong : shortshort;
9+
const exactlyFillsThePrintWidthEightySignatureLinePaddedToLength = (x) => (y) =>
10+
cond ? longlonglong : shortshort;
11+
vi.mocked(useCommandStore().formatKeySequenceXY).mockImplementation((command) =>
12+
cond ? longlonglong : shortshort,
13+
);
14+
15+
// Call body: Prettier keeps the signature too
16+
const exactlyFillsThePrintWidthEightySignatureLinePaddedToLengthOkOkOkO = (x) =>
17+
callSomething(longlonglong, shortshort);
18+
const exactlyFillsThePrintWidthEightySignatureLinePaddedToLength = (x) => (y) =>
19+
callSomething(longlonglong, shortshort);
20+
vi.mocked(useCommandStore().formatKeySequenceXY).mockImplementation((command) =>
21+
callSomething(longlonglong, shortshort),
22+
);
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
---
2+
source: crates/oxc_formatter/tests/fixtures/mod.rs
3+
---
4+
==================== Input ====================
5+
// 10 20 30 40 50 60 70 80
6+
// 45678901234567890123456789012345678901234567890123456789012345678901234567890
7+
8+
// DIVERGENCES.md#arrow-exact-fill-signature
9+
10+
// Conditional body: diverges at printWidth 80
11+
const exactlyFillsThePrintWidthEightySignatureLinePaddedToLengthOkOkOkO = (x) =>
12+
cond ? longlonglong : shortshort;
13+
const exactlyFillsThePrintWidthEightySignatureLinePaddedToLength = (x) => (y) =>
14+
cond ? longlonglong : shortshort;
15+
vi.mocked(useCommandStore().formatKeySequenceXY).mockImplementation((command) =>
16+
cond ? longlonglong : shortshort,
17+
);
18+
19+
// Call body: Prettier keeps the signature too
20+
const exactlyFillsThePrintWidthEightySignatureLinePaddedToLengthOkOkOkO = (x) =>
21+
callSomething(longlonglong, shortshort);
22+
const exactlyFillsThePrintWidthEightySignatureLinePaddedToLength = (x) => (y) =>
23+
callSomething(longlonglong, shortshort);
24+
vi.mocked(useCommandStore().formatKeySequenceXY).mockImplementation((command) =>
25+
callSomething(longlonglong, shortshort),
26+
);
27+
28+
==================== Output ====================
29+
------------------
30+
{ printWidth: 80 }
31+
------------------
32+
// 10 20 30 40 50 60 70 80
33+
// 45678901234567890123456789012345678901234567890123456789012345678901234567890
34+
35+
// DIVERGENCES.md#arrow-exact-fill-signature
36+
37+
// Conditional body: diverges at printWidth 80
38+
const exactlyFillsThePrintWidthEightySignatureLinePaddedToLengthOkOkOkO = (x) =>
39+
cond ? longlonglong : shortshort;
40+
const exactlyFillsThePrintWidthEightySignatureLinePaddedToLength = (x) => (y) =>
41+
cond ? longlonglong : shortshort;
42+
vi.mocked(useCommandStore().formatKeySequenceXY).mockImplementation((command) =>
43+
cond ? longlonglong : shortshort,
44+
);
45+
46+
// Call body: Prettier keeps the signature too
47+
const exactlyFillsThePrintWidthEightySignatureLinePaddedToLengthOkOkOkO = (x) =>
48+
callSomething(longlonglong, shortshort);
49+
const exactlyFillsThePrintWidthEightySignatureLinePaddedToLength = (x) => (y) =>
50+
callSomething(longlonglong, shortshort);
51+
vi.mocked(useCommandStore().formatKeySequenceXY).mockImplementation((command) =>
52+
callSomething(longlonglong, shortshort),
53+
);
54+
55+
-------------------
56+
{ printWidth: 100 }
57+
-------------------
58+
// 10 20 30 40 50 60 70 80
59+
// 45678901234567890123456789012345678901234567890123456789012345678901234567890
60+
61+
// DIVERGENCES.md#arrow-exact-fill-signature
62+
63+
// Conditional body: diverges at printWidth 80
64+
const exactlyFillsThePrintWidthEightySignatureLinePaddedToLengthOkOkOkO = (x) =>
65+
cond ? longlonglong : shortshort;
66+
const exactlyFillsThePrintWidthEightySignatureLinePaddedToLength = (x) => (y) =>
67+
cond ? longlonglong : shortshort;
68+
vi.mocked(useCommandStore().formatKeySequenceXY).mockImplementation((command) =>
69+
cond ? longlonglong : shortshort,
70+
);
71+
72+
// Call body: Prettier keeps the signature too
73+
const exactlyFillsThePrintWidthEightySignatureLinePaddedToLengthOkOkOkO = (x) =>
74+
callSomething(longlonglong, shortshort);
75+
const exactlyFillsThePrintWidthEightySignatureLinePaddedToLength = (x) => (y) =>
76+
callSomething(longlonglong, shortshort);
77+
vi.mocked(useCommandStore().formatKeySequenceXY).mockImplementation((command) =>
78+
callSomething(longlonglong, shortshort),
79+
);
80+
81+
===================== End =====================
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// 10 20 30 40 50 60 70 80
2+
// 45678901234567890123456789012345678901234567890123456789012345678901234567890
3+
4+
// Each child line fills the print width (80) exactly, then `{" "}` overflows it:
5+
// the child stays on its line, the fill entry boundary does not count the pending space
6+
<p>
7+
Run <span className="dynamic-info">{toolNameToolNameToolNameToolNameTo}</span>{" "}
8+
with args:{" "}
9+
</p>;
10+
<p>
11+
Abort {switchBlocked?.blockingOp === "merge" ? "MergeMergeMergeMe" : "Rebase"}{" "}
12+
&amp; Switch
13+
</p>;
14+
<p>
15+
<ValueText style={valueStyle}>{formatValue(num1)}</ValueText> {t("andandand")}{" "}
16+
<ValueText style={valueStyle}>{formatValue(num2)}</ValueText>
17+
</p>;
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
---
2+
source: crates/oxc_formatter/tests/fixtures/mod.rs
3+
---
4+
==================== Input ====================
5+
// 10 20 30 40 50 60 70 80
6+
// 45678901234567890123456789012345678901234567890123456789012345678901234567890
7+
8+
// Each child line fills the print width (80) exactly, then `{" "}` overflows it:
9+
// the child stays on its line, the fill entry boundary does not count the pending space
10+
<p>
11+
Run <span className="dynamic-info">{toolNameToolNameToolNameToolNameTo}</span>{" "}
12+
with args:{" "}
13+
</p>;
14+
<p>
15+
Abort {switchBlocked?.blockingOp === "merge" ? "MergeMergeMergeMe" : "Rebase"}{" "}
16+
&amp; Switch
17+
</p>;
18+
<p>
19+
<ValueText style={valueStyle}>{formatValue(num1)}</ValueText> {t("andandand")}{" "}
20+
<ValueText style={valueStyle}>{formatValue(num2)}</ValueText>
21+
</p>;
22+
23+
==================== Output ====================
24+
------------------
25+
{ printWidth: 80 }
26+
------------------
27+
// 10 20 30 40 50 60 70 80
28+
// 45678901234567890123456789012345678901234567890123456789012345678901234567890
29+
30+
// Each child line fills the print width (80) exactly, then `{" "}` overflows it:
31+
// the child stays on its line, the fill entry boundary does not count the pending space
32+
<p>
33+
Run <span className="dynamic-info">{toolNameToolNameToolNameToolNameTo}</span>{" "}
34+
with args:{" "}
35+
</p>;
36+
<p>
37+
Abort {switchBlocked?.blockingOp === "merge" ? "MergeMergeMergeMe" : "Rebase"}{" "}
38+
&amp; Switch
39+
</p>;
40+
<p>
41+
<ValueText style={valueStyle}>{formatValue(num1)}</ValueText> {t("andandand")}{" "}
42+
<ValueText style={valueStyle}>{formatValue(num2)}</ValueText>
43+
</p>;
44+
45+
-------------------
46+
{ printWidth: 100 }
47+
-------------------
48+
// 10 20 30 40 50 60 70 80
49+
// 45678901234567890123456789012345678901234567890123456789012345678901234567890
50+
51+
// Each child line fills the print width (80) exactly, then `{" "}` overflows it:
52+
// the child stays on its line, the fill entry boundary does not count the pending space
53+
<p>
54+
Run <span className="dynamic-info">{toolNameToolNameToolNameToolNameTo}</span> with args:{" "}
55+
</p>;
56+
<p>Abort {switchBlocked?.blockingOp === "merge" ? "MergeMergeMergeMe" : "Rebase"} &amp; Switch</p>;
57+
<p>
58+
<ValueText style={valueStyle}>{formatValue(num1)}</ValueText> {t("andandand")}{" "}
59+
<ValueText style={valueStyle}>{formatValue(num2)}</ValueText>
60+
</p>;
61+
62+
===================== End =====================

0 commit comments

Comments
 (0)