Repository navigation
fix(core): keep comments in object method type parameters, before a file's final semicolon, and in spread attributes (#458, #488, #489) - #633
Merged
Conversation
…ile's final semicolon, and in spread attributes (#458, #488, #489) 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 #458
Closes #488
Closes #489
Summary
The parser attached three kinds of comments where Prettier doesn't, and the formatter, which prints from that attachment, moved or deleted them. Each now matches Prettier 3.9.6 (
typescriptparser):const o = { m</* c */ T>(b: T): T { return b; } };m /* c */<T>(b: T): T {m</* c */ T>(b: T): T {const o = { async m</* c */ T>(b: T) {} };async m /* c */<T>(b: T) {}async m</* c */ T>(b: T) {}const o = { m</// c/T,/>(b: T) {} };m<T>(b: T) {}: deletedm</// c/T,/>(b: T) {},const x = 1/// c/;(no final newline)const x = 1;: deletedconst x = 1;/// cfoo()/// c/;(no final newline)foo();: deletedfoo();/// ca = <div {.../* note */b}/>;<div {...b} /* note */ /><div {/* note */ ...b} />a = <div {.../* prettier-ignore */b}/>;<div {.../* prettier-ignore */b} /* prettier-ignore */ />, and a third copy on the next pass<div {/* prettier-ignore */ ...b} />(
/separates lines.)Changes
FunctionExpressionof a generic object method, getter, or setter started at its(, after its owntypeParameters, so a comment in them lay outside the function. The walker gave it to the key before it (block comments) or to the whole function (a line comment on its own line), and the method printer prints neither there. The function now starts at its type parameters, as in typescript-estree (fixParentLocation) and Babel'sestreeplugin, so the comment leads or trails a type parameter, as it does in a class method.#startMethodAtTypeParametersmovesstart,loc.start(andrange) with acorn-typescript'sresetStartLocationFromNode, the helper it uses for a generic arrow function. The plainm<T>() {}branch is TSRX's own code inparsePropertyValue;async, generator,get, andsetmethods come from acorn-typescript, and the same override fixes them aftersuper.parsePropertyValue(see the override below).<, a comment between the key and the type parameters (m /* c */ <T>() {}) now leads the function, as in Prettier.printPropertyprinted the function without its comments, so it now prints the function's leading comments right after the key, as Prettier'sprintMethoddoes throughprint("value"):m/* c */ <T>() {}. The same printer gap deleted the comment in"m" /* c */ () {}onmain(a string key has no method-name rule), which now prints like Prettier too.;([Bug]: core: A comment on its own line before the final ; of a file without a final newline is deleted #488, core). Without a line break after the;, theProgramends where its last statement does. A node that ends where its parent does leaves its remaining comments to the parent, and theProgramkeeps none after its statements, so the comment was dropped. The last statement of the program now takes them as trailing comments, exactly as it does when a line break follows the;. Only a statement that ends at the end of the file is affected.isCommentInsideUnvisitedAttributestops at an enclosingJSXAttribute, but not at aJSXSpreadAttribute, so a comment in a spread's braces before its argument looked like one in a not-yet-visited attribute of the element. The argument skipped it, and it then trailed the spread (or led the next attribute). The check now stops at a spread attribute too, so the comment leads the argument, whichprintJSXSpreadalready prints inside the braces before the..., like Prettier'sprintJsxSpreadAttributeOrChild. Aprettier-ignorethere now ignores the argument, which prints as written, instead of printing after the ignored spread's source that already holds it.The compilers print no comments, and their output and Volar mappings don't change (checked for all five targets on inputs with generic object methods, spread comments, and a final
;comment).Override
parsePropertyValue(already overridden inTSRXPlugin) now also calls#startMethodAtTypeParametersaftersuper.parsePropertyValue, marked// UPSTREAM(sveltejs/acorn-typescript#127): remove once a release includes the fix. acorn-typescript'sparsePropertyValueandparseGetterSetterattach the type parameters to a function thatparseMethodstarted at the(. I filed The function of a generic object method, getter or setter starts after its type parameters sveltejs/acorn-typescript#127 with a plainacorn+@sveltejs/acorn-typescriptrepro and the fix direction (resetStartLocationFromNode(prop.value, typeParameters)). The override produces the AST that fix produces, so removing it later doesn't change the AST. No open upstream issue or PR covered this; feat: remove lazy destructuring (RFC #106) #110 doesn't touch these methods.tsrx-prettier-pluginThe new plugin passes Prettier its own comment list, but reads core's node ranges.
mainconst o = { m</* c */ T>(b: T): T { return b; } };m /* c */<T>(b: T): T {const o = { async m</* c */ T>(b: T) {} };async m /* c */<T>(b: T) {}const o = { m</// c/T,/>(b: T) {} };m// c/<T>(b: T) {},It still moves a comment in a class method's type parameters (
class A { m</* c */ T>(a: T) {} }givesm /* c */<T>(a: T) {}): its conversion moves the method's type parameters onto the function without moving the function's start. That's filed as #630; this PR doesn't change the package.Verification
mainat e82305f) and after, strict and with{ collect: true, errors: [], comments: [], preserveParens: true }: 17,432 inputs, which are 70 tracked.tsrxfiles, 2,040 Prettier snapshot inputs and 2,040 outputs underpackages/tsrx-prettier-plugin/tests/prettier/, and 13,282 strings from the repo's test files. 65 inputs changed, the same 65 in both modes, with no change to any error, thrown message, or hang:jsx/ignorespread.jsandjsx/spreadattribute.js(spread comments now lead the argument),typescript/genericobject-method.tsandtypescript/type-parameters-argumentsconstraint(the object method's function starts at its type parameters).value.startonly) and 4 in the spread-attribute location tests inparser.test.js, where the comment before...propsmoves from the next attribute's name to the argument. Those tests check positions only..tsrxfile changed.Every AST difference is one of: an object method's
value.start, a comment moving into a spread's argument, a comment moving from a method's key (or its function) into the type parameters or onto the function, or a comment trailing the last statement of a file.pnpm vitest run --project prettier-plugin --project tsrx-prettier-plugin --project tsrx-utils --project tsrx-react --project tsrx-preact --project tsrx-solid --project tsrx-vue --project tsrx-hono --maxWorkers=4: 8,517 pass, 77 skipped. Notsrx-prettier-pluginknown failure started passing or failing.prettier-plugintests; all 30 fail onmain.parser.test.js, through the worker helpers; 3 fail onmain, and the fourth pins where methods without type parameters and class methods start.eslint-parserandeslint-pluginpass (after buildingeslint-parser).pnpm format:check,pnpm changeset:check, andpnpm typecheckpass.tsrx-tscisn't linked after--ignore-scripts, so thevite-plugin-honotest typecheck ran throughpackages/typescript-plugin/dist/tsc.jsafter building@tsrx/typescript-plugin.Findings
Filed separately (distinct causes):
tsrx-prettier-pluginmoves a comment in a class method's type parameters after the name (itsMethodDefinitionconversion).const o = { m<const T>(x: T) {} }fails to parse (Unexpected keyword 'const'): TSRX'sparsePropertyValuebranch doesn't passtsParseConstModifier.compile_to_volar_mappingsthrows for it in every target.Same causes as this PR:
"m" /* c */ () {}givesm/* c */ () {}and thenm /* c */() {}, and a line comment on its own line between a method's name and<(m/// c/<T>() {}) givesm// c/<T>() {}and thenm<T>() {}, // c. Onmain, both comments were deleted.{.../** @type {any} */ (b)}) moved after the spread onmain. It now stays on its parentheses, whichprintJSXSpreadkeeps for a cast. Prettier'stypescriptparser prints{/** @type {any} */ ...b}, which drops the parentheses, and itsbabelparser{/** @type {any} */ ...(b)}, where the comment no longer casts(b).🤖 Generated with Claude Code
Note
Low Risk
Changes are limited to comment attachment and print order in the parser/formatter; AST span tweaks are localized and heavily tested, with no auth, data, or runtime behavior impact.
Overview
Fixes three comment-attachment bugs in
@tsrx/coreand one printing gap in@tsrx/prettier-pluginso formatting matches Prettier instead of moving or dropping comments (closes #458, #488, #489).Generic object methods (#458): The parser now sets a method/getter/setter
FunctionExpressionto start at its type parameters (#startMethodAtTypeParametersinplugin.js), so comments inside<T>attach to the type parameter list rather than the key or an orphaned function span. The prettier plugin prints leading comments on the method value right after the key (e.g."m" /* c */ () {},m /* c */ <T>() {}).EOF semicolon (#488): When the file ends with
statement/ comment /;and no trailing newline, the comment walker now lets the last statement keep that comment as trailing, instead of losing it because theProgramends at the same offset.JSX spread attributes (#489): Comment placement inside
{.../* c */ b}is fixed by treatingJSXSpreadAttributelike other attributes inisCommentInsideUnvisitedAttribute, so the comment leads the spread argument (andprettier-ignoreno longer duplicates).Tests and a changeset document the behavior; an acorn-typescript upstream workaround remains until sveltejs/acorn-typescript#127 lands.
Reviewed by Cursor Bugbot for commit 9b8cb28. Bugbot is set up for automated code reviews on this repo. Configure here.