Repository navigation
fix(prettier): drop redundant type parentheses, parenthesize element operands, and break around continued strings like Prettier (#396, #425, #407) - #451
Merged
Conversation
… next line (#407) Print a string literal or string key that contains a line break with replaceEndOfLine, like Prettier's printLiteral, so its literalline breaks the enclosing assignment, call arguments, array, or object. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…396) The parser keeps the parentheses written around a type as a TSParenthesizedType node, which the printer printed as written. Like Prettier's parser postprocess, the printer now drops them as it reaches each node (its children, grandchildren, and every type among them, so the layout checks see the type inside), moves their comments to that type, and lets needsParens add back the pairs the type needs from its real parent. The union printer asks needsParens instead of checking for the wrapper. A type kept by prettier-ignore keeps its written parentheses. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Port the JSX branch of Prettier's needsParens: an element or fragment is
parenthesized unless its parent prints it bare, so the operand of await,
a unary operator, a cast, a spread, a template literal, or a class
property value gets its parentheses. TSRX elements in statement
positions (template bodies, @{ } code blocks, cases, if and loop bodies)
stay bare, and a <style> block follows the same rule, which also restores
the parentheses it needs as a member object.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ttier (#396) With the written type parentheses dropped, a conditional type nested in another's true type gets Prettier's ternary parentheses: only when it stays on one line. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…inued strings Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
…it parenthesized Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
… member (#396) The parser marks the union member after an own-line prettier-ignore comment (#433). When that member is written in parentheses, the mark is on the TSParenthesizedType, so the type inside it takes it over when the parentheses are dropped. Co-Authored-By: Claude Opus 5.5 <[email protected]>
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 2 potential issues.
Bugbot Autofix is ON. A cloud agent has been kicked off to fix the reported issues. You can view the agent here.
Reviewed by Cursor Bugbot for commit aad47cd. Configure here.
…t slot (#425) Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 task done
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 #396
Closes #425
Closes #407
Summary
Parentheses around types and around element operands, and strings continued onto the next line, now format the way Prettier 3.9.6 formats them.
type A = (B | C);type A = B | C;let x: (A | B) = 1;/type D = ((E));let x: A | B = 1;/type D = E;type A = (Bbbb… | Cccc…);past the print widthtype A = (+| Bbbb…+| Cccc…+);type A =+| Bbbb…+| Cccc…;function foo(options: ({ a: string; … })) {}past the print widthfunction foo(options: {…}) {}type A = (B | C)[];/keyof (B | C)/(() => void) | nullasync function f() { await <div />; }await <div />;await (<div />);x = !<div />;/x = typeof <div />;x = !(<div />);/x = typeof (<div />);x = <b /> as any;/x = [...<b />];/x = { ...<b /> };x = (<b />) as any;/x = [...(<b />)];/x = { ...(<b />) };const a = (<style>.a {}</style>).a;const message = "first line \+second line";const message =+"first line \+second line";foo("first line \+second line", other);foo(+"first line \+second line",+other,+);Changes
TSParenthesizedType, andneedsParensadds back the parentheses a type needs from its real parent. The printer now does the same without a walk of its own:dropParenthesizedTypes, called from the printer'sprintentry, replaces the wrappers in the node's children and grandchildren and throughout every type among them (aWeakSetkeeps each type from being visited twice), and the node itself when a parent printed it through a path that skips a level (path.call(print, 'typeParameter', 'constraint')). Grandchildren are needed because layout checks look through a parameter or declarator into its annotated type (isHuggableParameterType,isComplexTypeAnnotation); with only children unwrapped,function foo(options: ({ … })) {}wasn't idempotent. The wrapper's comments move to the type inside it. The type rules H ported in fix(prettier): pick string quotes, break heritage clauses, and add type parentheses like Prettier (#373, #385, #386, #405, #410) #413 (typeOperandNeedsParens) now see the real parent and add every pair a type prints with.printTSUnionTypeasksneedsParensinstead of checking for aTSParenthesizedTypeparent, as Prettier'sprintUnionTypedoes. The unwrapped type is markedmetadata.parenthesized, like a parenthesized expression, so a type kept byprettier-ignorekeeps its written parentheses (keyof /* prettier-ignore */ (B | C)), and it takes over the parser'smetadata.prettierIgnoremark ([Bug]: prettier-plugin prints a prettier-ignore comment between union members after the next | #433) when the ignored union member is written in parentheses (| (C & D)after an own-line// prettier-ignore). With the written parentheses gone,printTSConditionalTypeadds Prettier's ternary parentheses around a conditional true type that stays on one line (T extends string ? (T extends "a" ? 1 : 2) : 3).nodeNeedsParensports the JSX branch of Prettier'sneedsParens: an element or fragment is parenthesized as a callee, as the left of<, and under every parent that isn't in Prettier's list (ELEMENT_BARE_PARENTS, call andnewarguments, object property values,return/throw,export default). The old rule's cases (tag, member object,!, instantiation) are all under that. TSRX elements are statements of their own in a template, without anExpressionStatement, soisStatementSlotkeeps them bare in a@{ }code block's statements and output, in a block,case, orifor loop body, and inProgram/StaticBlock/TSModuleBlock. A<style>block follows the same rule, which also restores the parentheses it needs as a member object. Code blocks and@if/@for/@switch/@tryaren't elements and are unchanged.Literalcase andprintKeyprint a string containing a line break withreplaceEndOfLine, like Prettier'sprintLiteral(printMultilineString; a single-line string stays a plain string). Theliterallinebreaks the enclosing groups, so an assignment breaks after=, and call arguments, arrays, objects, and areturnargument break. Directives stay plain, likeprintDirective.pnpm formatchanges nothing in the repo.Output differences from Prettier that remain
mainkept the written parentheses in the same places, so these outputs are unchanged or gain only the one-line parentheses.!(<div>…</div>)instead of Prettier's!(+ indented element +): the multi-line JSX parentheses of [Bug]: prettier-plugin lays out JSX children and multi-line elements differently from Prettier #369.[.../* c */ (<b />)], Prettier[...(/* c */ <b />)]);maindoes the same for a callee or member object: [Bug]: prettier-plugin prints an element's comments outside its parentheses #449 (filed here).// prettier-ignorebefore a union written in parentheses (type A =+// prettier-ignore+(B | C);) keeps the whole union, where Prettier ignores only its first member, as [Bug]: prettier-plugin prints a prettier-ignore comment between union members after the next | #433 now does for a bare union: [Bug]: prettier-plugin: prettier-ignore before a union written in parentheses keeps the whole union #463 (filed here; the parser's rule doesn't look throughTSParenthesizedType). Same output asmain.needsParens(Foo</* prettier-ignore */ (B | C)>, PrettierFoo</* prettier-ignore */ B | C>): [Bug]: prettier-plugin: A node kept by prettier-ignore keeps its written parentheses instead of Prettier's #436.type-fest'sInternalIsUnion); it's the one non-idempotent output in the probe below.Verification
pnpm vitest run --project prettier-plugin(withmainat bff5325 merged): 1557 pass, including 80 new cases. 41 of them fail withmain's printer; the other 39 pin layouts that already matched (type parentheses the type needs, elements that stay bare, and a template element as a statement of every statement slot: program, block,if/else,case, loop, label, static block, namespace, and the@for/@empty/@try/@pending/@catchbodies). The plugin'sformathelper also checks that a second pass changes nothing.pnpm prettier --check "**/*.tsrx",pnpm format:check,node --check, andpnpm changeset:checkpass;pnpm formatchanges nothing. CI runs the other projects and the typecheck.A probe formatted statements from
node_moduleswithmain's plugin (at d734bfa), this branch's, and Prettier (typescriptparser,.tsxfor JSX,babelfor.js):main.ts.d.tstypeof, type operators, conditional,infer, and rest types,.ts(H's set).ts(H's set).tsxNo output loses a comment, and Babel's AST of every output matches the input's apart from type parentheses, except 9
.tsxstatements whose JSX text whitespace differs the same way onmain. The one output that isn't idempotent is thetype-festcase above, where Prettier isn't idempotent either. Before a fix for the conditional true types, the probe caught 7 regressions (A ? (B ? C : D) : Elost its parentheses on one line); they're fixed and pinned in the tests. The remaining differences from Prettier in the type snippets are mostly [Bug]: prettier-plugin breaks each nested conditional type on its own instead of with its chain #448.Related findings
printTernaryOldtoTSConditionalType). Found by the type probe.willPrintOwnCommentsfor JSX).prettier-ignorebefore a parenthesized union keeps the whole union (the union-level rule of [Bug]: prettier-plugin prints a prettier-ignore comment between union members after the next | #433 doesn't see through the parentheses).🤖 Generated with Claude Code
Note
Medium Risk
Large formatter changes across type AST mutation at print time and JSX paren rules; behavior is heavily tested but any missed edge case could alter formatted output repo-wide.
Overview
Aligns
@tsrx/prettier-pluginformatting with Prettier 3.9.6 for type parentheses, JSX/element operands, and line-continued string literals.Type parentheses: Before printing,
dropParenthesizedTypesstrips parserTSParenthesizedTypewrappers (comments andprettier-ignore/parenthesizedmetadata move to the inner type).needsParens/ union and conditional-type printing then re-add only required or readability parens—e.g.(B | C)→B | C, while(B | C)[]stays. The dedicatedTSParenthesizedTypeprint path is removed.Element operands:
nodeNeedsParensforJSXElement/JSXFragment/JSXStyleElementfollows Prettier’s bare-parent list plus TSRX statement slots (isStatementSlot) so template elements stay unwrapped in blocks and control-flow bodies but get parens forawait, unary ops,as/satisfies, spreads, class fields, etc.Continued strings:
printMultilineStringusesreplaceEndOfLineon literals whose printed form contains a newline (backslash continuation), so assignments and calls break around them like Prettier.Extensive tests and a patch changeset document the behavior.
Reviewed by Cursor Bugbot for commit 83fac5c. Bugbot is set up for automated code reviews on this repo. Configure here.