Skip to content

fix(core): keep comments in object method type parameters, before a file's final semicolon, and in spread attributes (#458, #488, #489) - #633

Merged
leonidaz merged 3 commits into
mainfrom
fix/core-comment-attachment
Sep 25, 2026
Merged

leonidaz merged 3 commits into
mainfrom
fix/core-comment-attachment

Conversation

@leonidaz

@leonidaz leonidaz commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

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 (typescript parser):

Input Before Now, and Prettier
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) {}: deleted m< / // c / T, / >(b: T) {},
const x = 1 / // c / ; (no final newline) const x = 1;: deleted const x = 1; / // c
foo() / // c / ; (no final newline) foo();: deleted foo(); / // c
a = <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

  • Object method type parameters ([Bug]: core: A comment in the type parameters of an object literal method is deleted #458, core). The FunctionExpression of a generic object method, getter, or setter started at its (, after its own typeParameters, 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's estree plugin, so the comment leads or trails a type parameter, as it does in a class method. #startMethodAtTypeParameters moves start, loc.start (and range) with acorn-typescript's resetStartLocationFromNode, the helper it uses for a generic arrow function. The plain m<T>() {} branch is TSRX's own code in parsePropertyValue; async, generator, get, and set methods come from acorn-typescript, and the same override fixes them after super.parsePropertyValue (see the override below).
  • Comments that lead an object method's function ([Bug]: core: A comment in the type parameters of an object literal method is deleted #458, formatter). With the function starting at the <, a comment between the key and the type parameters (m /* c */ <T>() {}) now leads the function, as in Prettier. printProperty printed the function without its comments, so it now prints the function's leading comments right after the key, as Prettier's printMethod does through print("value"): m/* c */ <T>() {}. The same printer gap deleted the comment in "m" /* c */ () {} on main (a string key has no method-name rule), which now prints like Prettier too.
  • A comment before a file's final ; ([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 ;, the Program ends where its last statement does. A node that ends where its parent does leaves its remaining comments to the parent, and the Program keeps 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.
  • Comments in a spread attribute ([Bug]: core: A comment inside a spread attribute attaches after the attribute, and prettier-ignore there prints twice #489, core). isCommentInsideUnvisitedAttribute stops at an enclosing JSXAttribute, but not at a JSXSpreadAttribute, 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, which printJSXSpread already prints inside the braces before the ..., like Prettier's printJsxSpreadAttributeOrChild. A prettier-ignore there 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

tsrx-prettier-plugin

The new plugin passes Prettier its own comment list, but reads core's node ranges.

Issue Input On main With this PR
#458 const o = { m</* c */ T>(b: T): T { return b; } }; m /* c */<T>(b: T): T { matches Prettier
#458 const o = { async m</* c */ T>(b: T) {} }; async m /* c */<T>(b: T) {} matches Prettier
#458 const o = { m< / // c / T, / >(b: T) {} }; m// c / <T>(b: T) {}, matches Prettier
#488 the three inputs of the issue match Prettier match Prettier
#489 both inputs of the issue match Prettier match Prettier

It still moves a comment in a class method's type parameters (class A { m</* c */ T>(a: T) {} } gives m /* 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

  • Parse comparison, before (main at e82305f) and after, strict and with { collect: true, errors: [], comments: [], preserveParens: true }: 17,432 inputs, which are 70 tracked .tsrx files, 2,040 Prettier snapshot inputs and 2,040 outputs under packages/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:
    • 8 Prettier snapshot entries (4 inputs, 4 outputs): jsx/ignore spread.js and jsx/spread attribute.js (spread comments now lead the argument), typescript/generic object-method.ts and typescript/type-parameters-arguments constraint (the object method's function starts at its type parameters).
    • 9 existing test strings: 5 generic object methods (value.start only) and 4 in the spread-attribute location tests in parser.test.js, where the comment before ...props moves from the next attribute's name to the argument. Those tests check positions only.
    • 48 strings from this PR's new tests.
    • No tracked .tsrx file 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. No tsrx-prettier-plugin known failure started passing or failing.
    • 30 new prettier-plugin tests; all 30 fail on main.
    • 4 new parser tests in parser.test.js, through the worker helpers; 3 fail on main, and the fourth pins where methods without type parameters and class methods start.
  • eslint-parser and eslint-plugin pass (after building eslint-parser).
  • pnpm format:check, pnpm changeset:check, and pnpm typecheck pass. tsrx-tsc isn't linked after --ignore-scripts, so the vite-plugin-hono test typecheck ran through packages/typescript-plugin/dist/tsc.js after building @tsrx/typescript-plugin.

Findings

Filed separately (distinct causes):

Same causes as this PR:

  • Prettier isn't idempotent on two inputs this PR touches, and the formatter now prints Prettier's first pass: "m" /* c */ () {} gives m/* c */ () {} and then m /* c */() {}, and a line comment on its own line between a method's name and < (m / // c / <T>() {}) gives m// c / <T>() {} and then m<T>() {}, // c. On main, both comments were deleted.
  • A JSDoc cast in a spread ({.../** @type {any} */ (b)}) moved after the spread on main. It now stays on its parentheses, which printJSXSpread keeps for a cast. Prettier's typescript parser prints {/** @type {any} */ ...b}, which drops the parentheses, and its babel parser {/** @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/core and one printing gap in @tsrx/prettier-plugin so 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 FunctionExpression to start at its type parameters (#startMethodAtTypeParameters in plugin.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 the Program ends at the same offset.

JSX spread attributes (#489): Comment placement inside {.../* c */ b} is fixed by treating JSXSpreadAttribute like other attributes in isCommentInsideUnvisitedAttribute, so the comment leads the spread argument (and prettier-ignore no 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.

leonidaz and others added 2 commits September 25, 2026 15:40
…ile's final semicolon, and in spread attributes (#458, #488, #489)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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