Skip to content

fix(prettier): drop redundant type parentheses, parenthesize element operands, and break around continued strings like Prettier (#396, #425, #407) - #451

Merged
leonidaz merged 15 commits into
mainfrom
fix/prettier-parens-strings-396
Sep 25, 2026

Conversation

@leonidaz

@leonidaz leonidaz commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

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.

Input Before Now
type A = (B | C); unchanged type A = B | C;
let x: (A | B) = 1; / type D = ((E)); unchanged let x: A | B = 1; / type D = E;
type A = (Bbbb… | Cccc…); past the print width type A = ( + | Bbbb… + | Cccc… + ); type A = + | Bbbb… + | Cccc…;
function foo(options: ({ a: string; … })) {} past the print width breaks the parameter list, and the object type keeps its parentheses hugs the object type: function foo(options: { … }) {}
type A = (B | C)[]; / keyof (B | C) / (() => void) | null unchanged unchanged (the type needs them)
async function f() { await <div />; } await <div />; await (<div />);
x = !<div />; / x = typeof <div />; unchanged x = !(<div />); / x = typeof (<div />);
x = <b /> as any; / x = [...<b />]; / x = { ...<b /> }; unchanged x = (<b />) as any; / x = [...(<b />)]; / x = { ...(<b />) };
const a = (<style>.a {}</style>).a; the parentheses are dropped kept
const message = "first line \ + second line"; unchanged const message = + "first line \ + second line";
foo("first line \ + second line", other); unchanged foo( + "first line \ + second line", + other, + );

Changes

  • Type parentheses ([Bug]: prettier-plugin keeps redundant parentheses around types #396). Prettier's parser postprocess drops TSParenthesizedType, and needsParens adds 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's print entry, replaces the wrappers in the node's children and grandchildren and throughout every type among them (a WeakSet keeps 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. printTSUnionType asks needsParens instead of checking for a TSParenthesizedType parent, as Prettier's printUnionType does. The unwrapped type is marked metadata.parenthesized, like a parenthesized expression, so a type kept by prettier-ignore keeps its written parentheses (keyof /* prettier-ignore */ (B | C)), and it takes over the parser's metadata.prettierIgnore mark ([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, printTSConditionalType adds Prettier's ternary parentheses around a conditional true type that stays on one line (T extends string ? (T extends "a" ? 1 : 2) : 3).
  • Element operands ([Bug]: prettier-plugin leaves out Prettier's parentheses around an element operand of await, unary operators, casts, and spreads #425). nodeNeedsParens ports the JSX branch of Prettier's needsParens: 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 and new arguments, 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 an ExpressionStatement, so isStatementSlot keeps them bare in a @{ } code block's statements and output, in a block, case, or if or loop body, and in Program/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/@try aren't elements and are unchanged.
  • Continued strings ([Bug]: prettier-plugin doesn't break around a string literal that continues onto the next line #407). The Literal case and printKey print a string containing a line break with replaceEndOfLine, like Prettier's printLiteral (printMultilineString; a single-line string stays a plain string). The literalline breaks the enclosing groups, so an assignment breaks after =, and call arguments, arrays, objects, and a return argument break. Directives stay plain, like printDirective.
  • Reformatted files. pnpm format changes nothing in the repo.

Output differences from Prettier that remain

Verification

  • pnpm vitest run --project prettier-plugin (with main at bff5325 merged): 1557 pass, including 80 new cases. 41 of them fail with main'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/@catch bodies). The plugin's format helper also checks that a second pass changes nothing.

  • pnpm prettier --check "**/*.tsrx", pnpm format:check, node --check, and pnpm changeset:check pass; pnpm format changes nothing. CI runs the other projects and the typecheck.

  • A probe formatted statements from node_modules with main's plugin (at d734bfa), this branch's, and Prettier (typescript parser, .tsx for JSX, babel for .js):

    Snippets (statements) Formatted Matched Prettier on main Here Fixed Regressed
    with a parenthesized type, .ts 289 248 269 21 0
    with a parenthesized type, .d.ts 1489 1325 1419 94 0
    arrow return types, typeof, type operators, conditional, infer, and rest types, .ts (H's set) 622 557 557 0 0
    strings containing quotes, .ts (H's set) 109 106 106 0 0
    with JSX, .tsx 21 12 12 0 0

    No output loses a comment, and Babel's AST of every output matches the input's apart from type parentheses, except 9 .tsx statements whose JSX text whitespace differs the same way on main. The one output that isn't idempotent is the type-fest case above, where Prettier isn't idempotent either. Before a fix for the conditional true types, the probe caught 7 regressions (A ? (B ? C : D) : E lost 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

🤖 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-plugin formatting with Prettier 3.9.6 for type parentheses, JSX/element operands, and line-continued string literals.

Type parentheses: Before printing, dropParenthesizedTypes strips parser TSParenthesizedType wrappers (comments and prettier-ignore/parenthesized metadata 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 dedicated TSParenthesizedType print path is removed.

Element operands: nodeNeedsParens for JSXElement / JSXFragment / JSXStyleElement follows Prettier’s bare-parent list plus TSRX statement slots (isStatementSlot) so template elements stay unwrapped in blocks and control-flow bodies but get parens for await, unary ops, as/satisfies, spreads, class fields, etc.

Continued strings: printMultilineString uses replaceEndOfLine on 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.

leonidaz and others added 13 commits September 24, 2026 22:23
… 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]>
… 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]>

@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 2 potential issues.

Fix All in Cursor

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.

Comment thread packages/prettier-plugin/src/index.js Resolved
Comment thread packages/prettier-plugin/src/index.js Resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant