Repository navigation
fix(prettier): lay out assignments, arrow functions, and call arguments like Prettier (#340, #332, #316, #337, #335, #336, #325, #326, #298) - #383
Merged
Conversation
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]>
…youts 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]>
Contributor
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.
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.
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.
`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]>
Closed
1 task done
…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]>
This was referenced Sep 25, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


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(printAssignmentandchooseLayout),arrow-function.js(printArrowFunctionand its chain and body helpers), andcall-arguments.js(printCallArgumentsand its hugging rules), plusprintVariableDeclaration.const a = 1, b = 2, c = 3;const message = "…long string…";=type T = Foo<aaa…, bbb…, ccc…>;type T =, thenFoo<with the arguments indented twicetype T = Foo<, the arguments,>;export const parse = (_Err) => (schema, …) => { … };export const parse =, then the chain indentedconst create = (key, opts) => makeKmac(blockLen, …);=> makeKmac(, then the arguments=>, thenmakeKmac(…)on the next lineconst f = (a) => a ? b : c;const f = (a) => (a ? b : c);items.map((item) => ({ id: item.id, … }))items.map(, then the arrow on its own lineitems.map((item) => ({, the properties,}));makeObserver((entries) => { … }, { threshold: 0.5 })}useEffect(() => { … }, [aaa…, bbb…])build.onLoad({ filter: …, namespace: "file" }, async (args) => { … })it("…long test name…", async () => { … })declare type A = string;type A = string;Changes
printVariableDeclarationports Prettier's: once any declarator has a value, every declarator after the first starts its own indented line; without values, or in aforhead, they break only when they don't fit. A line comment after a declarator stays in place, and comments between declarators keep their order ([Bug]: prettier-plugin reorders and merges the comments between declarators and is not idempotent #380, found by the comments group; the port fixes it without more code). Like Prettier'shasNodeIgnoreComment, a declaration followed by// prettier-ignoreon its line prints as written, so the new layout doesn't split it (other statements: [Bug]: prettier-plugin ignores a prettier-ignore comment at the end of a statement's line #395).printAssignmentandchooseAssignmentLayoutportprintAssignmentandchooseLayout, withshouldBreakAfterOperator,isPoorlyBreakableMemberOrCallChain,isComplexDestructuring,hasComplexTypeAnnotation,isComplexTypeAliasParams,isObjectPropertyWithShortKey,isLoneShortArgument, andshouldHugUnionType. Declarators, assignment expressions, class fields, object properties, and type aliases all go through it, replacing the per-printer choices by value type. So a long string or poorly breakable member or call chain breaks after=, a type alias keeps a type that can break by itself on the=line, a complex destructuring breaks its pattern first,require(…)never breaks after=, and a chain of three or more assignments puts each on its own line. The layout reaches the value as theassignmentLayoutprint argument, which the arrow chain reads. The plugin prints comments ahead of the value itself, so the existingprintValueAfterLeadingCommentstill runs first, and the own-line comment check skips comments inside a type cast's parentheses.isMemberChainports only the decisionprintMemberChainmakes (whether a call prints as a member chain, from its link groups and comments), which the poorly breakable chain check needs. The member chain printer itself is [Bug]: Formatter never breaks a chain of method calls at the dots #350.PropertyDefinitionandPropertyjoin the parents the binary printer leaves the indentation to, and the logical printer now does the same for declarators, assignments, class fields, and object properties, like Prettier'sshouldIndentIfInlining(a logical value whose right side is an object or array literal keeps its layout).printArrowFunctionports Prettier's. A chain collects the signatures of every arrow in it and prints the comments of the inner ones. As an assigned value it moves below the operator as a whole (chain-tail-arrow-chainin a longer assignment chain); in a call argument or a binary operand the first signature leads and the rest indent; anywhere else each arrow gets its own line. A return type with parameters, type parameters, or a parameter that isn't a plain name always breaks the chain. The line after=>and the body share one group, so an expression body moves below=>as a whole, and objects, arrays, blocks, arrows, sequences, templates, and multi-line template literals stay on the=>line (mayBreakAfterShortPrefix). A conditional body prints in parentheses only while it stays on the=>line (shouldAddParensIfNotBreak), sonodeNeedsParensno longer keeps the written ones. A TSRX template body keeps its existing layout (on the=>line while it fits, otherwise on the next line), and a TSRX@{ … }body counts as a block.printCallArgumentsfollows Prettier's order: the React hook shape (isReactHookCallWithDepsArray) prints as a plain concatenation so the dependency array can break by itself; function compositions (isFunctionCompositionArguments) break every argument; a leading function hugs only undershouldExpandFirstArg(two arguments, the second short and not expandable); and the last argument hugs undershouldExpandLastArg, whosecouldExpandArgnow also takes an arrow with an expression body (call, conditional, object, array, or an expandable chain) and applies to a lone argument too. The existing cast handling becamecouldExpandArg's cast case. A head argument that will break puts every argument on its own line, which fixes [Bug]: Formatter hugs a broken object argument to a trailing function argument and is not idempotent #298's first pass; its second pass then depends only on the object's own layout ([Bug]: Formatter keeps an object expanded whenever it spans lines, not only when a newline follows { #314). A hugged arrow or function keeps its parameters on the call's line or throwsArgExpansionBailout, and every argument breaks instead.removeLinesForHugalso drops the later states of conditional groups (the plugin's type literal offers its broken layout that way, whichremoveLineswould keep), so a callback with a type literal parameter breaks every argument like Prettier instead of breaking the type. A function expression hugged as the last argument does the same when Prettier'sprintFunctionasks for it, andisDecoratedFunctionkeepsdecorator(a)((…) => { … })hugged. A call on a call groups its argument lists so a long curried call breaks its own arguments first (isLongCurriedCallExpression).printCallExpression, a test call (isTestCall), arequireof one module (isSimpleModuleImport), an AMDdefine/requirecall, and a call on one template literal that starts on its line keep their arguments joined with", ", and a test call's function keeps its parameters on one line. Without this, the ported hugging would have brokendefine([…], (…) => { … }), whichmainalready printed like Prettier.declare([Bug]: prettier-plugin drops declare from type aliases and interfaces #354). Type aliases and interfaces print theirdeclaremodifier.vite-plugin-*/tests/*.test.tsrxfiles andwebsite-tsrx/src/components/compiler-demo.tsrxnow keep test names on theit(line, break arrow bodies after=>, and keep an assigned template literal on the=line.Output differences from Prettier that remain
These come from printers other groups own, which the new layouts now rely on to break:
=(or inside a type argument of a parameter) instead of breaking the parameter list:type Listener<T> = (result: Result</T/>) => void;, where Prettier printstype Listener<T> = (/result: Result<T>,/) => void;. The same keepstype Mapper<A extends X, B = A> = (input: A) => Bfrom matching, and a function type whose return type is an object type breaks the object instead (= (issue: I, ctx: C) => {, then the members).&([Bug]: prettier-plugin never breaks an intersection type at its & operators #381, filed here). A type alias whose value is one of these and doesn't fit still breaks after=; with [Bug]: prettier-plugin never breaks an intersection type at its & operators #381,type T = Generic<Arg> & Other & Morethat doesn't fit now breaks insideGeneric<…>.=,:, or=>like Prettier, but a chain of three or more operands still packs them onto fewer lines ([Bug]: Formatter breaks only before the last operand of a long chain of same-precedence operators #347), and after=>the right operand still indents ([Bug]: Formatter indents the continuation line of a broken binary or logical expression where Prettier doesn't #334).=line), but the chain then breaks its last call's arguments, as onmain, where Prettier puts each call on its own line.isTemplateOnItsOwnLinecases [Bug]: prettier-plugin: A multi-line template literal doesn't break the call arguments or array around it #374 mentions for the call printer and the assignment layout.items.map((item) => <li>…</li>)breaks every argument). Prettier hugs JSX there and wraps it in parentheses, which is part of the JSX layout ([Bug]: prettier-plugin lays out JSX children and multi-line elements differently from Prettier #369).forhead keeps its clauses on one line ([Bug]: Formatter breaks long for headers inside a clause instead of one clause per line #321).=line where Prettier's switched quotes fit ([Bug]: prettier-plugin keeps escaped quotes where Prettier switches to the other quote #373).Verification
pnpm vitest run --project prettier-plugin: 944 pass, including 37 new cases. 29 of them fail withmain's printer; the other 8 pin layouts that already matched (for example aforhead, a huggedsetTimeout(() => { … }, 500), an object body on the=>line, and a curried call). The plugin'sformathelper also checks that a second pass changes nothing.The reformatted
.tsrxtest files still pass:tsrx-{react,preact,solid,vue,hono}-runtime(326 pass).tsgopasses forprettier-plugin, as do Prettier checks of the changed files and of all tracked.tsrxfiles, andpnpm changeset:check.A probe formatted 12,699 statements from
.tsand.jsfiles innode_modules(declarations, assignments, arrow functions, calls with function or object arguments, type aliases, class fields; 5 sets, including long statements and statements with comments) withmain's plugin, this branch's, and Prettier'stypescriptorbabelparser:main.ts.js.tswith comments.tsstatements.jsstatementsNo 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'sprintFunctionrule), and AMDdefinecalls (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 (theprettier-ignoredeclarations 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
declare. Fixed here.requirecalls, and AMDdefinecalls. Fixed here, see above.awaitoryieldthat is a callee or member object hugs its parentheses when it breaks, where Prettier indents it between them. Left for later.&. Left for later.// prettier-ignorecomment at the end of a statement's line is ignored; this PR handles it for variable declarations only. Left for later.🤖 Generated with Claude Code