Skip to content

fix(prettier): lay out assignments, arrow functions, and call arguments like Prettier (#340, #332, #316, #337, #335, #336, #325, #326, #298) - #383

Merged
leonidaz merged 8 commits into
mainfrom
fix/prettier-assignment-arrow-332
Sep 25, 2026

Conversation

@leonidaz

@leonidaz leonidaz commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

Closes #340
Closes #332
Closes #316
Closes #337
Closes #335
Closes #336
Closes #325
Closes #326
Closes #298
Closes #354
Closes #363
Closes #380

Summary

Assignments, arrow functions, and call arguments now lay out the way Prettier lays them out. This ports three Prettier modules into the plugin: assignment.js (printAssignment and chooseLayout), arrow-function.js (printArrowFunction and its chain and body helpers), and call-arguments.js (printCallArguments and its hugging rules), plus printVariableDeclaration.

Input Before Now
const a = 1, b = 2, c = 3; one line one declarator per line
const message = "…long string…"; one line past the print width breaks after =
type T = Foo<aaa…, bbb…, ccc…>; type T =, then Foo< with the arguments indented twice type T = Foo<, the arguments, >;
export const parse = (_Err) => (schema, …) => { … }; breaks the last parameter list export const parse =, then the chain indented
const create = (key, opts) => makeKmac(blockLen, …); => makeKmac(, then the arguments =>, then makeKmac(…) on the next line
const f = (a) => a ? b : c; unchanged const f = (a) => (a ? b : c);
items.map((item) => ({ id: item.id, … })) items.map(, then the arrow on its own line items.map((item) => ({, the properties, }));
makeObserver((entries) => { … }, { threshold: 0.5 }) hugs the function, the object after } one argument per line
useEffect(() => { … }, [aaa…, bbb…]) the dependency array stays on a 99-character line the array breaks
build.onLoad({ filter: …, namespace: "file" }, async (args) => { … }) breaks the object and hugs the function; the second pass changes it one argument per line, the object on one line
it("…long test name…", async () => { … }) one argument per line unchanged
declare type A = string; type A = string; unchanged

Changes

Output differences from Prettier that remain

These come from printers other groups own, which the new layouts now rely on to break:

Verification

  • pnpm vitest run --project prettier-plugin: 944 pass, including 37 new cases. 29 of them fail with main's printer; the other 8 pin layouts that already matched (for example a for head, a hugged setTimeout(() => { … }, 500), an object body on the => line, and a curried call). The plugin's format helper also checks that a second pass changes nothing.

  • The reformatted .tsrx test files still pass: tsrx-{react,preact,solid,vue,hono}-runtime (326 pass).

  • tsgo passes for prettier-plugin, as do Prettier checks of the changed files and of all tracked .tsrx files, and pnpm changeset:check.

  • A probe formatted 12,699 statements from .ts and .js files in node_modules (declarations, assignments, arrow functions, calls with function or object arguments, type aliases, class fields; 5 sets, including long statements and statements with comments) with main's plugin, this branch's, and Prettier's typescript or babel parser:

    Set Formatted Matched Prettier on main Now Fixed Regressed
    .ts 2816 2467 2635 168 0
    .js 2484 2282 2386 104 0
    .ts with comments 1469 1108 1247 139 0
    long .ts statements 1963 1371 1649 278 0
    long .js statements 3967 2805 3201 396 0

    No comment is lost. The 5 outputs that aren't idempotent and the 27 whose Babel AST differs from the input's do the same on main (function type parameters with a comment ([Bug]: Formatter never breaks the parameters of class methods, TypeScript signatures, and function types #324), for await ([Bug]: Formatter drops await from for await loops #344), and keys that lose their quotes). The probe caught two regressions along the way, both fixed before this PR: a function expression hugged as the last argument broke its parameters (now Prettier's printFunction rule), and AMD define calls (now [Bug]: prettier-plugin breaks the arguments of test calls, require calls, and AMD define calls that Prettier keeps on one line #363).

  • Group D's probe (13,957 snippets from 98 packages) found three more differences from main; two are fixed here (the prettier-ignore declarations and the type literal parameter). With them, it reports 0 regressions, 0 new non-idempotent outputs, and 493 newly matching snippets with default options (10,709 → 11,202 match Prettier), and 331 with tabs, single quotes, and width 100 (11,340 → 11,671). Its one remaining new flag with those options is the function type with an object return type above ([Bug]: Formatter never breaks the parameters of class methods, TypeScript signatures, and function types #324); it's a layout difference, not an AST change.

Related findings

🤖 Generated with Claude Code

leonidaz and others added 6 commits September 24, 2026 20:00
A declaration joined its declarators with a plain `, `, so they never
broke. Port Prettier's printVariableDeclaration: once any declarator has a
value, every declarator after the first starts its own indented line;
without values, or in a `for` head, they break only when they don't fit.
A line comment after a declarator now stays in place.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
)

Declarators, assignments, class fields, and object properties each picked
a layout by the value's type, and a type alias always broke after `=`.
Port Prettier's printAssignment and chooseLayout for all five, so a long
string or member chain breaks after `=`, a type that can break by itself
stays on the `=` line, a complex destructuring breaks its pattern first,
and an assignment chain of three or more puts each part on its own line.
The value learns its layout through the `assignmentLayout` print argument.

`isMemberChain` ports only the decision of Prettier's printMemberChain
(whether the call prints as a member chain), which the poorly-breakable
chain check needs. A binary value of a class field or object property no
longer indents twice under the new break after the operator.

Type aliases and interfaces also keep their `declare` modifier (#354).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
#336)

Port Prettier's printArrowFunction. A chain of arrows collects its
signatures and prints them together: as an assigned value it moves below
the operator as a whole, in a call argument or elsewhere each arrow gets
its own line, and a return type or a non-identifier parameter always
breaks it. The line after `=>` and the whole body now share one group, so
an expression body that doesn't fit moves below `=>` instead of breaking
inside itself, while objects, arrays, blocks, and templates stay on the
`=>` line. A conditional body prints in parentheses only while it stays on
the `=>` line, so nodeNeedsParens no longer keeps the written ones.

As a hugged call argument (`expandLastArg`/`expandFirstArg`), the arrow
keeps its signature on one line or throws ArgExpansionBailout, and adds
the trailing comma and the line before `)`. The call arguments printer
starts passing those in the next commit.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Port Prettier's printCallArguments, extending the existing cast-aware
expansion. In Prettier's order: a React hook call keeps its callback and
dependency array on the call's line and lets the array break by itself;
function compositions break every argument; a leading function hugs only
when one short argument that can't expand follows it (shouldExpandFirstArg);
and the last argument hugs when couldExpandArg allows it, which now takes
arrows with an expression body too, including a single argument. A head
argument that breaks puts every argument on its own line, so a function
after a broken object no longer hugs it (and formats the same way twice).
A function expression hugged as the last argument keeps its parameters on
one line like Prettier's printFunction, or every argument breaks.

Test calls (`it("…", () => { … })`), `require("…")` calls, AMD `define`
calls, and a lone template literal argument keep their arguments on one
line, and a test call's function keeps its parameters on one line (#363).
A call on a call groups its argument lists so a long curried call breaks
its own arguments first.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The printVariableDeclaration port (#340) already fixes this: a trailing
line comment's break puts each declarator on its own line with its
comments.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@cursor cursor Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

Bugbot Autofix prepared a fix for the issue found in the latest run.

  • ✅ Fixed: Logical values indent twice after =
    • Logical expressions no longer indent their right operand when an assignment layout already indented the value after = or :, so &&, ||, and ?? values match the binary indent.

Create PR

Or push these changes by commenting:

@cursor push ca6921a478
Preview (ca6921a478)
diff --git a/packages/prettier-plugin/src/index.js b/packages/prettier-plugin/src/index.js
--- a/packages/prettier-plugin/src/index.js
+++ b/packages/prettier-plugin/src/index.js
@@ -2803,6 +2803,18 @@ function printTsrxNode(node, path, options, print, args) {
 					node.operator,
 					[line, path.call((childPath) => print(childPath, { isConditionalTest: true }), 'right')],
 				]);
+			} else if (
+				// `printAssignment` already indents this value after `=` or `:`.
+				// Indenting the right operand again would place it one level deeper.
+				args?.assignmentLayout === 'break-after-operator' ||
+				args?.assignmentLayout === 'chain-tail'
+			) {
+				logicalResult = group([
+					path.call(print, 'left'),
+					' ',
+					node.operator,
+					[line, path.call(print, 'right')],
+				]);
 			} else {
 				logicalResult = group([
 					path.call(print, 'left'),

@@ -2803,6 +2803,18 @@ function printTsrxNode(node, path, options, print, args) {
 					node.operator,
 					[line, path.call((childPath) => print(childPath, { isConditionalTest: true }), 'right')],
 				]);
+			} else if (
+				// `printAssignment` already indents this value after `=` or `:`.
+				// Indenting the right operand again would place it one level deeper.
+				args?.assignmentLayout === 'break-after-operator' ||
+				args?.assignmentLayout === 'chain-tail'
+			) {
+				logicalResult = group([
+					path.call(print, 'left'),
+					' ',
+					node.operator,
+					[line, path.call(print, 'right')],
+				]);
 			} else {
 				logicalResult = group([
 					path.call(print, 'left'),

diff --git a/packages/prettier-plugin/src/index.test.js b/packages/prettier-plugin/src/index.test.js
--- a/packages/prettier-plugin/src/index.test.js
+++ b/packages/prettier-plugin/src/index.test.js
@@ -8899,6 +8899,36 @@ class A {
 			expect(result).toBeWithNewline(expected);
 		});
 
+		it('breaks after : or = before a logical value without indenting it twice', async () => {
+			const input = `const options = {
+  description: someVeryLongVariableNameNumberOne && someVeryLongVariableNameNumberTwoooooooo,
+};
+class A {
+  description = someVeryLongVariableNameNumberOne || someVeryLongVariableNameNumberTwoooooooo;
+}
+description = someVeryLongVariableNameNumberOne ?? someVeryLongVariableNameNumberTwoooooooo;
+window.aaaaaaaaaaaaaaaaaa = window.bbbbbbbbbbbbbbbbbbbbbbbb = window.cccccccccccccccccccc = someVeryLongVariableNameNumberOne && someVeryLongVariableNameNumberTwoooooooo;`;
+			const expected = `const options = {
+  description:
+    someVeryLongVariableNameNumberOne &&
+    someVeryLongVariableNameNumberTwoooooooo,
+};
+class A {
+  description =
+    someVeryLongVariableNameNumberOne ||
+    someVeryLongVariableNameNumberTwoooooooo;
+}
+description =
+  someVeryLongVariableNameNumberOne ?? someVeryLongVariableNameNumberTwoooooooo;
+window.aaaaaaaaaaaaaaaaaa =
+  window.bbbbbbbbbbbbbbbbbbbbbbbb =
+  window.cccccccccccccccccccc =
+    someVeryLongVariableNameNumberOne &&
+    someVeryLongVariableNameNumberTwoooooooo;`;
+			const result = await format(input);
+			expect(result).toBeWithNewline(expected);
+		});
+
 		it('keeps a value that can break by itself on the operator line', async () => {
 			const source = `const result = someFunction(
   argumentNumberOne,

@@ -8899,6 +8899,36 @@ class A {
 			expect(result).toBeWithNewline(expected);
 		});
 
+		it('breaks after : or = before a logical value without indenting it twice', async () => {
+			const input = `const options = {
+  description: someVeryLongVariableNameNumberOne && someVeryLongVariableNameNumberTwoooooooo,
+};
+class A {
+  description = someVeryLongVariableNameNumberOne || someVeryLongVariableNameNumberTwoooooooo;
+}
+description = someVeryLongVariableNameNumberOne ?? someVeryLongVariableNameNumberTwoooooooo;
+window.aaaaaaaaaaaaaaaaaa = window.bbbbbbbbbbbbbbbbbbbbbbbb = window.cccccccccccccccccccc = someVeryLongVariableNameNumberOne && someVeryLongVariableNameNumberTwoooooooo;`;
+			const expected = `const options = {
+  description:
+    someVeryLongVariableNameNumberOne &&
+    someVeryLongVariableNameNumberTwoooooooo,
+};
+class A {
+  description =
+    someVeryLongVariableNameNumberOne ||
+    someVeryLongVariableNameNumberTwoooooooo;
+}
+description =
+  someVeryLongVariableNameNumberOne ?? someVeryLongVariableNameNumberTwoooooooo;
+window.aaaaaaaaaaaaaaaaaa =
+  window.bbbbbbbbbbbbbbbbbbbbbbbb =
+  window.cccccccccccccccccccc =
+    someVeryLongVariableNameNumberOne &&
+    someVeryLongVariableNameNumberTwoooooooo;`;
+			const result = await format(input);
+			expect(result).toBeWithNewline(expected);
+		});
+
 		it('keeps a value that can break by itself on the operator line', async () => {
 			const source = `const result = someFunction(
   argumentNumberOne,

You can send follow-ups to the cloud agent here.

Reviewed by Cursor Bugbot for commit 7134831. Configure here.

Comment thread packages/prettier-plugin/src/index.js Resolved
`printAssignment` breaks after the operator before a logical value and
indents it, but the logical printer indented its right operand again.
Like Prettier's `shouldIndentIfInlining`, a logical expression that is
the value of a declarator, an assignment, a class field, or an object
property now leaves the indentation to its parent, as a binary one
already did. A logical expression whose right side is an object or array
literal keeps its own layout.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…340, #326)

Two regressions a second corpus probe found:

- A declaration followed by `// prettier-ignore` on its line is ignored by
  Prettier (`hasNodeIgnoreComment` checks trailing comments too). The new
  declarator layout split such declarations and moved the comment, so
  printVariableDeclaration now prints them as written. Other statements
  still need this: #395.
- A hugged callback whose parameter has a type literal type printed the
  type broken in the first pass and every argument broken in the second.
  The plugin's type literal offers its broken layout as a later
  conditionalGroup state, which removeLines leaves in place, so the hugged
  signature could still break. removeLinesForHug keeps only the first state,
  and the call breaks every argument like Prettier.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment