Skip to content

fix(prettier): pick string quotes, break heritage clauses, and add type parentheses like Prettier (#373, #385, #386, #405, #410) - #413

Merged
leonidaz merged 11 commits into
mainfrom
fix/prettier-quotes-heritage-373
Sep 25, 2026
Merged

leonidaz merged 11 commits into
mainfrom
fix/prettier-quotes-heritage-373

Conversation

@leonidaz

@leonidaz leonidaz commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

Closes #373
Closes #385
Closes #386
Closes #405
Closes #410

Summary

String quotes, class and interface headings, and parentheses around types now format the way Prettier 3.9.6 formats them.

Input Before Now
const b = "say \"hi\""; unchanged const b = 'say "hi"';
const c = 'it\'s'; with singleQuote: true unchanged const c = "it's";
const e = "it\'s"; const e = "it's"; unchanged, like Prettier (the quote doesn't change, so the source text stays)
export class BrowserPerformanceClient extends PerformanceClient implements IPerformanceClient, IDisposable { one line, however long extends … and implements … on their own indented lines, { on its own line
interface AbortSignal extends EventTarget, InternalEventTargetEventProperties<AbortSignalEventMap> { breaks inside <…> extends … on its own indented line; one type per line under extends when they don't fit either
const f = (journal: Journal): () => void => { unchanged (journal: Journal): (() => void) => {
type A = typeof a[]; unchanged type A = (typeof a)[];
Foo = class extends Some.Very.Long.Base { past the print width one line Foo = class extends ( + the superclass on its own line + ) {

Changes

  • String quotes ([Bug]: prettier-plugin keeps escaped quotes where Prettier switches to the other quote #373). printStringLiteral ports Prettier's printString: the configured quote, unless the string's source text holds more of it than of the other quote (the existing getPreferredQuote, shared with JSX attribute strings, now serves both). When the quote doesn't change, the literal prints exactly as written, as in Prettier, so "it\'s" keeps its escape. Otherwise makeString, ported from Prettier, escapes only the chosen quote and drops the backslash of an escaped other quote. Every other escape stays as written, so the lone-surrogate and escape handling from [Bug]: Formatter --write changes escaped lone surrogates and merges distinct object keys #271 is unchanged, and directives still go through printDirective ([Bug]: Formatter turns an escaped string into a strict-mode directive and breaks compilation #268). The value-based fallback for literals without source text picks its quote the same way. A string in a JSX expression container (title={"It's \"both\""}) is an ordinary string literal, so it now prints title={'It\'s "both"'} like Prettier; the attribute-string printer itself is unchanged.
  • Heritage clauses ([Bug]: prettier-plugin never breaks the extends and implements clauses of a class or interface #385). printClassDeclaration and printTSInterfaceDeclaration port the heading layout of Prettier's printClass. shouldPrintHeritageInGroupMode ports shouldPrintClassInGroupMode: a heading with more than one heritage type, a single qualified name (ns.Base, which this parser gives as a TSQualifiedName in implements and interface extends), or a commented id, type parameter list, or superclass groups its clauses. printHeritageClauses ports Prettier's function of the same name: with several heritage types, implements/extends starts its own line, and its types follow on that line or, when they don't fit, one per line below it. A class whose heading breaks puts { on its own line (ifBreak on the heading's group id) unless its body is empty. An interface keeps { on the last heading line, as Prettier 3.9.6 does.
  • Superclass of an assigned class expression ([Bug]: prettier-plugin doesn't wrap a long superclass in parentheses when a class expression is assigned #410). Like Prettier's printSuperClass, when the class is the value of an assignment expression, its superclass moves into parentheses on its own line if the heading doesn't fit. A superclass that needs its own parentheses ((A || B)) gets both pairs when broken, as in Prettier.
  • Type parentheses ([Bug]: prettier-plugin leaves a function type in an arrow function's return type without parentheses #386, [Bug]: prettier-plugin leaves out Prettier's parentheses around typeof and keyof types in array, indexed access, and rest types #405). nodeNeedsParens gets the type cases of Prettier's needsParens (TSFunctionType through TSTypeQuery). Prettier writes them as switch cases that fall through; the website's tsconfig checks the plugin with noFallthroughCasesInSwitch, so the shared chain is a helper, typeOperandNeedsParens, that applies the same rules in order. The rules: a function type as an arrow function's return type, a conditional type as a check type, extends type, or type parameter constraint, a function or constructor type whose return type is a constrained infer, unions and intersections inside each other, any of these or a type operator or infer inside an array, optional, or rest type or another type operator (except infer in a rest type), and typeof as an array element or indexed object type. Parentheses written in the source stay a TSParenthesizedType, so the node's parent is that wrapper, no rule fires, and nothing is doubled. The parser keeps a mapped type's in type as a type parameter's constraint, so the constraint rule skips that position (Prettier's AST has no type parameter there).
  • Reformatted files. Four website-tsrx/src/pages/*.tsrx files had strings with more single than double quotes; they now use double quotes without the escapes.

Output differences from Prettier that remain

Verification

  • pnpm vitest run --project prettier-plugin: 1344 pass, including 49 new cases (one old test is replaced and one JSX row now expects Prettier's output). 17 of the new cases and the changed row fail with main's printer; the other 32 pin layouts that already matched (headings that fit, single simple clauses, written type parentheses, directives, quotes that don't change). The plugin's format helper also checks that a second pass changes nothing.

  • pnpm format:check (the repo formats itself with this plugin), node --check, and pnpm changeset:check pass; pnpm format changes nothing beyond the four pages above. CI runs the other projects and the typecheck.

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

    Snippets (statements) Formatted Matched Prettier on main Here Fixed Regressed
    strings containing quotes, .ts 109 17 106 89 0
    strings containing quotes, .js 1835 560 1797 1240 3
    the same with singleQuote: true, .ts 109 88 106 18 0
    the same with singleQuote: true, .js 1835 1131 1797 666 0
    classes and interfaces with heritage clauses, .ts 547 508 539 31 0
    classes and interfaces with heritage clauses, .d.ts 3998 3672 3857 185 0
    arrow return types, typeof, type operators, conditional, infer, and rest types, .ts 622 545 548 3 0
    the same, .d.ts 3835 3295 3341 46 0

    The 3 regressions are generated files (entities, linkedom) that put // prettier-ignore and then /* #__PURE__ */ before a long string. The formatter never honored that prettier-ignore ([Bug]: prettier-plugin ignores a prettier-ignore comment that another comment follows #412, filed); main printed the string as written only because it already used the default double quote. Every output this branch changes is idempotent, and no comment is lost. The statements that aren't idempotent or lose a comment do the same on main. Babel's AST of the output matches the input's, apart from the added type parentheses and, in .js files, object keys that are unquoted on main too. No class or interface heading differs from Prettier any more; the remaining differences in those snippets are in the class and interface bodies.

Related findings

🤖 Generated with Claude Code

leonidaz and others added 11 commits September 24, 2026 21:21
)

Port Prettier's printString / getPreferredQuote / makeString: a string
literal takes the configured quote unless its source text holds more of
that quote than of the other, is kept as written when its quote does not
change, and otherwise escapes only the chosen quote. Directives keep
their own printDirective rules.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…row return types (#386)

Port the TypeScript type cases of Prettier's needsParens into
nodeNeedsParens: a function type as an arrow function's return type,
typeof in an array or indexed access type, a type operator inside
another, a union in a rest type, and a conditional type as a type
parameter constraint. Written parentheses stay a TSParenthesizedType,
so none are doubled.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…er (#385)

Port Prettier's printClass heading layout: a heading with more than one
heritage type, or a single qualified name, groups its extends and
implements clauses so each starts its own indented line when the
heading does not fit, with the types of a multi-type clause one per
line below the keyword when they do not fit either. A class whose
heading breaks puts { on its own line; an interface keeps it on the
last heading line.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… in parentheses (#410)

Port the AssignmentExpression branch of Prettier's printSuperClass: when
the class is the value of an assignment, its superclass moves into
parentheses on its own line if the heading does not fit.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ice (#373)

Strings that hold more single than double quotes now print in double
quotes, so their escaped quotes go away.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ized (#405)

The parser keeps the `in` type of a mapped type as the constraint of a
type parameter, so the constraint rule from Prettier's needsParens must
skip it: Prettier's AST has no type parameter there.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The website's tsconfig checks the plugin with noFallthroughCasesInSwitch,
so Prettier's fallthrough chain of type cases becomes a helper,
typeOperandNeedsParens, that applies the same rules in order.

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

# Conflicts:
#	packages/prettier-plugin/src/index.test.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment