formatter/js: member chain with a breaking object in the last call is not idempotent with `objectWrap: preserve`
Root issue for #23852 and #27053. Both are the same cause.
A member chain whose last call arguments hold an object that does not fit is not idempotent. Pass 1 splits the chain, pass 2 joins it back.
// input
const { error } = await client.from('sent_messages').insert({ id: sid, account_id: accountId, subject: 'x', body: 'x', message_id: id, recipient_count: count });
// pass 1
const { error } = await client
.from("sent_messages")
.insert({
id: sid,
account_id: accountId,
subject: "x",
body: "x",
message_id: id,
recipient_count: count,
});
// pass 2 (fixpoint)
const { error } = await client.from("sent_messages").insert({
id: sid,
account_id: accountId,
subject: "x",
body: "x",
message_id: id,
recipient_count: count,
});Shapes:
- Object directly in the last call:
a.b().c({ ...long })- #27053
- Object wrapped in a call:
a.b().c(ww({ ...long }))- #23852
- Mapped type in the last call type arguments:
a.b().c<{ [K in keyof T]: ... }>(x)
Prettier 3.9.9 does the same.
Known upstream: typescript/method-chain/object/issue-17239.ts is in unstableTests (objectWrap !== "collapse") since prettier/prettier#17251
prettier/prettier#19968 is the same kind of issue for function signatures (#26240).
Its fix prettier/prettier#19970 (open) ignores breaks that come only from objectWrap: preserve when deciding the layout.
Cause
Two rules interact.
objectWrap: preserve: an object with a newline right after{in the source is printed expanded- Member chain:
best_fitting!(one_line, expanded), measured up to the first line break- Last group without a forced break:
one_lineis measured flat, too long, so the chain splits - Last group with a forced break:
one_lineis measured up to.insert({, fits, so the chain joins
- Last group without a forced break:
Pass 1 prints the object expanded, so pass 2 reads that {\n as authored and takes the other branch.
Whether pass 1 breaks the object depends on the column it is printed at, so the decision can not be made from the AST or IR alone.
Function signatures have the same kind of issue (single parameter + object in the return type), tracked in #26240. It is decided by a different rule (hugging the only parameter), so it is fixed separately. Other constructs (assignments, call arguments, arrow chains, JSX, TS types, ...) were checked and are idempotent.
Options
| Option | Idempotent | Prettier compat | Cost |
|---|---|---|---|
| Speculative printing (#27178): print the split layout, roll back and join if it breaks a preserved object | Yes (the mapped type case is not handled yet) | Prints Prettier's pass 2 directly | New printer primitive in oxc_formatter_core with a single consumer, ~-1% perf |
| Always split when the last group has a forced break (#27184) | Yes, all shapes | No: callbacks, template literals, JSX in the last call get split | Small, but a style change (conformance JS 765 → 760) |
| Always join when the first line up to the last call fits | Yes | No: chains that fit split get joined | Small, 8 conformance regressions |
Ignore breaks from objectWrap: preserve in the chain decision (same direction as prettier/prettier#19970) |
Expected, not prototyped | Prints Prettier's pass 1 | The best_fitting! measurement also has to ignore them, likely a printer change. Authored .insert({ chains get split |
Not viable:
- Expanding objects whose source is wider than
printWidth: ignores the printed column, not fixed at width 100, expands padded objects that fit- #26851
- Joining when the last call's source is wider than
printWidth: object wrapped in call case not covered, no fallback to split makes other chains non-idempotent- #27108
Source: oxc-project/oxc