Repository navigation
fix(prettier): pick string quotes, break heritage clauses, and add type parentheses like Prettier (#373, #385, #386, #405, #410) - #413
Merged
Conversation
) 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]>
…rentheses 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
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 #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.
const b = "say \"hi\"";const b = 'say "hi"';const c = 'it\'s';withsingleQuote: trueconst c = "it's";const e = "it\'s";const e = "it's";export class BrowserPerformanceClient extends PerformanceClient implements IPerformanceClient, IDisposable {extends …andimplements …on their own indented lines,{on its own lineinterface AbortSignal extends EventTarget, InternalEventTargetEventProperties<AbortSignalEventMap> {<…>extends …on its own indented line; one type per line underextendswhen they don't fit eitherconst f = (journal: Journal): () => void => {(journal: Journal): (() => void) => {type A = typeof a[];type A = (typeof a)[];Foo = class extends Some.Very.Long.Base {past the print widthFoo = class extends (+ the superclass on its own line +) {Changes
printStringLiteralports Prettier'sprintString: the configured quote, unless the string's source text holds more of it than of the other quote (the existinggetPreferredQuote, 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. OtherwisemakeString, 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 throughprintDirective([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 printstitle={'It\'s "both"'}like Prettier; the attribute-string printer itself is unchanged.printClassDeclarationandprintTSInterfaceDeclarationport the heading layout of Prettier'sprintClass.shouldPrintHeritageInGroupModeportsshouldPrintClassInGroupMode: a heading with more than one heritage type, a single qualified name (ns.Base, which this parser gives as aTSQualifiedNameinimplementsand interfaceextends), or a commented id, type parameter list, or superclass groups its clauses.printHeritageClausesports Prettier's function of the same name: with several heritage types,implements/extendsstarts 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 (ifBreakon the heading's group id) unless its body is empty. An interface keeps{on the last heading line, as Prettier 3.9.6 does.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.nodeNeedsParensgets the type cases of Prettier'sneedsParens(TSFunctionTypethroughTSTypeQuery). Prettier writes them asswitchcases that fall through; the website's tsconfig checks the plugin withnoFallthroughCasesInSwitch, 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 constrainedinfer, unions and intersections inside each other, any of these or a type operator orinferinside an array, optional, or rest type or another type operator (exceptinferin a rest type), andtypeofas an array element or indexed object type. Parentheses written in the source stay aTSParenthesizedType, so the node's parent is that wrapper, no rule fires, and nothing is doubled. The parser keeps a mapped type'sintype as a type parameter's constraint, so the constraint rule skips that position (Prettier's AST has no type parameter there).website-tsrx/src/pages/*.tsrxfiles had strings with more single than double quotes; they now use double quotes without the escapes.Output differences from Prettier that remain
{, or afterimplements, isn't moved the way Prettier'shandleClassCommentsmoves it: [Bug]: prettier-plugin: Comments between a class or interface heading and its body don't move like Prettier and need two passes #406. With this PR,class A extends B // c+{printsclass A+extends B // c+{(idempotent; it used to need two passes), and a comment afterimplementsprints above the first type instead of pulling the next line to column 0. The interface case still needs two passes, as onmain.(): (new () => Foo) =>andtype A = (string);: [Bug]: prettier-plugin keeps redundant parentheses around types #396.// prettier-ignorecomment followed by another comment (/* #__PURE__ */) isn't honored, so the ignored node is formatted, and with this PR that includes its quotes: [Bug]: prettier-plugin ignores a prettier-ignore comment that another comment follows #412.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 withmain'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'sformathelper also checks that a second pass changes nothing.pnpm format:check(the repo formats itself with this plugin),node --check, andpnpm changeset:checkpass;pnpm formatchanges nothing beyond the four pages above. CI runs the other projects and the typecheck.A probe formatted statements from
node_moduleswithmain's plugin (at a7246b9), this branch's, and Prettier (typescriptparser,babelfor.js):main.ts.jssingleQuote: true,.tssingleQuote: true,.js.ts.d.tstypeof, type operators, conditional,infer, and rest types,.ts.d.tsThe 3 regressions are generated files (
entities,linkedom) that put// prettier-ignoreand then/* #__PURE__ */before a long string. The formatter never honored thatprettier-ignore([Bug]: prettier-plugin ignores a prettier-ignore comment that another comment follows #412, filed);mainprinted 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 onmain. Babel's AST of the output matches the input's, apart from the added type parentheses and, in.jsfiles, object keys that are unquoted onmaintoo. 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
printClass.handleClassComments).replaceEndOfLine).// prettier-ignorecomment followed by another comment isn't honored (hasPrettierIgnorechecks only the last leading comment; [Bug]: prettier-plugin ignores a prettier-ignore comment at the end of a statement's line #395 is a different gap in the same function). Found by the string probe.🤖 Generated with Claude Code