Repository navigation
fix(prettier): lay out JSDoc-cast operands, comments after =, and import() arguments like Prettier (#580, #582, #581, #553, #591, #597) - #605
Merged
Conversation
…ort() arguments like Prettier (#580, #582, #581, #553) - shouldInlineLogicalExpression, the ConditionalExpression case of shouldBreakAfterOperator, and the JSX, same-precedence, and lone-operator checks of the binaryish printers treat an operand in JSDoc-cast parentheses as Prettier's ParenthesizedExpression (#580, #582, #597). - printValueAfterLeadingComment prints a block comment that ends the `=` line in the break-after-operator layout, and leaves an assignment chain's value to the chain layouts (#581, #591). - ImportExpression prints its source and options through printCallArguments, without a trailing comma, and a lone string source stays on the line (#553). Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
The parser now puts the options of import(source, options) on options (#602), so the printer drops its arguments[0] fallback. Co-Authored-By: Claude Opus 5.5 <[email protected]>
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 #580
Closes #582
Closes #581
Closes #553
Closes #591
Closes #597
Summary
Logical and assigned values with JSDoc casts, comments after an
=, and the arguments ofimport()now format like Prettier 3.9.6. Prettier'sbabelparser keeps a JSDoc cast's parentheses as aParenthesizedExpression, so several layout checks that look at an operand's type now treat a cast operand as a node of its own. A block comment that ends the=line now takes Prettier'sbreak-after-operatorlayout, andimport()lays out its source and options withprintCallArguments.⏎is a line break. Outputs use Prettier's default options;…shortens long names.x = aaa && /** @type {X} */ ({ aaa: 1, bbb: 2 });(too long)x = aaa && /** @type {X} */ ({⏎ aaa: 1,⏎ bbb: 2,⏎});x =⏎ aaa &&⏎ /** @type {X} */ ({ aaa: 1, bbb: 2 });(Prettier)const f = /** @type {…} */ (items || foo) ? foo : bar;const f =⏎ /** @type {…} */ (items || foo)⏎ ? foo⏎ : bar;const f = /** @type {…} */ (⏎ items || foo⏎)⏎ ? foo⏎ : bar;(Prettier)x = /** @type {X} */ (aaa && bbb) && { aaa: 1, bbb: 2 };… && {⏎ aaa: 1,⏎ bbb: 2,⏎};(Prettier)foo(/** @type {X} */ (aaa && bbb) && c);(the cast breaks)) &&⏎ c,) && c,(Prettier)const test = /* note */⏎ goog.partial(…);(doesn't fit after the comment)const test = /* note */⏎ goog.partial(…);const test =⏎ /* note */⏎ goog.partial(…);(Prettier)a = b = c = /* note */⏎ compute(…);c = /* note */ compute(…);, thenc =⏎ /* note */ compute(…);on the next formatc =⏎ /* note */⏎ compute(…);(Prettier)await import("./long/…/module.js", options);await import(⏎ "./long/…/module.js",⏎ options⏎);(Prettier)const p = import(⏎ // comment⏎ "./foo"⏎);const p = import(// comment⏎"./foo");Changes
shouldInlineLogicalExpressiontakes the expression's path andoptions, and doesn't inline a right operand in cast parentheses. The newhasTypeCastParens(path, options, key)asksgetTypeCastParensabout a child.printBinaryishExpression,printBinaryishExpressions,shouldBreakAfterOperator) pass the path. The value then takesbreak-after-operator, and the operand moves to its own line, in call arguments too.printBinaryishExpressionno longer gives a cast element on the right its own group (hasJsx), since Prettier'sisJsxElementsees the parentheses.prettier-ignoreinside the cast follows: the expression breaks after the=.ConditionalExpressioncase ofshouldBreakAfterOperatorrequires the test to have no cast parentheses, AB's 4-line fix. The conditional stays after the=and breaks inside the cast's parentheses, in an object property too.samePrecedenceSubExpressioninprintBinaryishExpressionandshouldGroupinprintBinaryishExpressionstreat a cast operand as neither binaryish nor of the node's type.=line ([Bug]: prettier-plugin: A block comment that ends the line after = stays there when the value doesn't fit, where Prettier breaks after the = #581).printValueAfterLeadingCommentprints such a comment and the value asgroup(indent([line, valueDoc])), Prettier'sbreak-after-operatordoc. The comment's ownline(fromprintLeadingComments) and the one after the operator break together, so the comment stays on the=line only when the value fits after it.=line keeps the old doc,[' ', group(indent(valueDoc))], since Prettier's default gives it to the left side. The newisLineEndedByLineCommentchecks the rest of the line, so= /* a */ // bcounts as a line comment.=of an assignment chain link ([Bug]: prettier-plugin: A block comment after the = of a link in an assignment chain stays on the = line and can format differently on the next pass #591, new).printValueAfterLeadingCommenttakes the layout. Forchainandchain-tailit returnsnull, so the chain's own layout prints the comment and the value, as in Prettier, whosechooseLayoutpicks those layouts before it looks at comments. A line comment on the=line still stays there.import()arguments ([Bug]: @tsrx/prettier-plugin: Formatter never breaks between the arguments ofimport(source, options)like Prettier #553).ImportExpressioncase printsgroup([import or import.<phase>, printCallArguments(…)]).getCallArgumentsreturns[source, options]. Since fix(core): accept catch var redeclarations, type and namespace exports, quoted attribute keys, and import() trailing commas (#225, #253, #342, #554) #602, the parser puts the options of everyimport()onoptionsand accepts a trailing comma, which the printer drops like Prettier.printCallArgumentsprints the arguments throughsourceandoptionsand adds no trailing comma forimport().isSimpleModuleImportcoversimport()with one string source, so it stays on the line.keepsArgumentsOnCallLinereads the arguments throughgetCallArguments.import(// comment⏎"./foo"); the comment now stays on its own line.import("x", { with: … })in a type, [Bug]: tsrx-core: Parser rejects import attributes in import types (import("foo", { with: { type: "json" } })) #422) print as before.Output differences from Prettier that remain
:or a class field's=: Prettier moves it before the key or the=([Bug]: prettier-plugin: A comment at the end of the line after an object key's colon or a class field's = leads the value, where Prettier moves it before the key or the = #593). With this PR, a block comment there that doesn't fit with the value moves below the operator, like in a declaration, where it used to stay on the operator's line.const test = /* a */ // b⏎ value;: Prettier's line comment trails the name and prints first (= // b⏎ /* a */ value). The comments keep their order here ([Bug]: prettier-plugin: A comment at the end of the line after an object key's colon or a class field's = leads the value, where Prettier moves it before the key or the = #593).=of a type alias, or before an object, array, or template: Prettier moves it below the=([Bug]: prettier-plugin: A line comment after the = of a type alias, or before an object, array, or template value, stays on the = line, where Prettier moves it below #598). A printer-only fix isn't idempotent, since a line comment before the=prints after it, so this needs the core's comment attachment.=before an arrow chain that ends an assignment chain stays on the=line, as before. Prettier's output there drops the space (c =/* x */). The arrow chain's body is indented one level more than Prettier's there, with a line comment too (as onmain).typescriptparser readsimport.defer(…)as a call on a meta property and adds a trailing comma. Itsbabelparser prints it as animport(), which this matches.Verification
pnpm vitest run --project prettier-plugin: 2441 pass after mergingmain(e82305f8). 22 of the 35 new tests fail withmain's printer (1af65cc6), and 13 pin layouts that already matched Prettier.pnpm formatchanges only the line wrapping of one new test (committed asstyle:).pnpm format:check,pnpm changeset:check, andnode --checkpass.babeloutput onmain, 118 here. The 12 changed outputs all now match Prettier. None changes a cast or a comment, and all are idempotent.comments-closure-typecastsnapshot (19 cases): 16 pass onmain, 18 here (issue-4124.js,issue-9358.js).issue-8045.jsdeclares a function twice, which doesn't parse as a module. With the names changed, its output matches Prettier.tsrx-prettier-plugin): 1596 pass onmain, 1604 here. 11 outputs change: 8 now pass, and 3 still fail for other reasons ([Bug]: prettier-plugin: A line comment after the = of a type alias, or before an object, array, or template value, stays on the = line, where Prettier moves it below #598, [Bug]: prettier-plugin: A comment after the leading & of a one-member intersection type moves to the = line and formats differently on the next pass #603, [Bug]: prettier-plugin: A comment on its own line between an import attribute's key and value moves to the key's line #604). The one flagged as not idempotent (typescript/intersection/consistent-with-flow/single-member.ts, [Bug]: prettier-plugin: A comment after the leading & of a one-member intersection type moves to the = line and formats differently on the next pass #603) isn't idempotent onmaineither.packages/*/src/**/*.jsfiles: the output is the same asmain's for all of them. Babel's AST withcreateParenthesizedExpressionsand every comment are unchanged.escaping.jsdoesn't parse on either ([Bug]: core: A regular expression that starts with > fails to parse unless it ends the input #584).node_modules(default options): the same snippets match Prettier onmainand here (none fixed, none regressed), and the same 4 aren't idempotent on both.Related findings
Fixed here:
=of an assignment chain link stayed on the=line with the value indented one level too many. Joined with a value that fits, it moved below the=on the next format.Filed separately:
:or a class field's=leads the value, where Prettier moves it before the key or the=(handlePropertyCommentsisn't ported).=of a type alias, or before an object, array, or template value, stays on the=line (handleAssignmentLikeComments). It needs the core attachment to stay idempotent.&of a one-member intersection type moves to the=line with the member at column 0, and the next format changes it. Prettier'spostprocessunwraps one-member unions and intersections.printAssignment.🤖 Generated with Claude Code
Note
Low Risk
Changes are limited to print-layout docs in the Prettier plugin with broad new regression tests; no runtime or security behavior is affected, though formatted output will shift for the covered edge cases.
Overview
Formatting parity with Prettier 3.9.6 for JSDoc type casts, values after
=/:, and dynamicimport().JSDoc cast parentheses are now treated like Prettier’s separate
ParenthesizedExpressionnodes in layout logic.hasTypeCastParensand an updatedshouldInlineLogicalExpression(path-aware) stop inlining object/array/JSX on the right of&&/||/??when they sit inside a cast, fix flattening/grouping for cast operands that share the parent operator, and make assigned conditionals with a cast binary/logical test break inside the cast instead of after=.printValueAfterLeadingCommentfollows Prettier’sbreak-after-operatorrules (block comments on the operator line only when the value fits; otherwise comment and value move down) and defers to assignment chain layouts so chained=comments stay stable across re-formats.ImportExpressionprinting now routes source/options throughprintCallArguments(like ordinary calls, no trailing comma), extendsgetCallArguments,isSimpleModuleImport, andkeepsArgumentsOnCallLinefor single-stringimport()/import.defer(), and preserves own-line comments before the module string.Reviewed by Cursor Bugbot for commit 6889b56. Bugbot is set up for automated code reviews on this repo. Configure here.