Skip to content

fix: disambiguate generic arrows in the TSX printer - #188

Merged
Rich-Harris merged 2 commits into
sveltejs:mainfrom
leonidaz:fix/tsx-type-parameter-commas
Sep 21, 2026
Merged

Rich-Harris merged 2 commits into
sveltejs:mainfrom
leonidaz:fix/tsx-type-parameter-commas

Conversation

@leonidaz

@leonidaz leonidaz commented Sep 20, 2026 •

Copy link
Copy Markdown
Contributor

Generic arrows such as const identity = <T,>(value: T): T => value lose their trailing comma when printed with tsx(), producing invalid TSX. Override TSTypeParameterDeclaration in the TSX printer so single-parameter declarations always include the comma by default.

A trailing comma is valid TypeScript syntax in a type-parameter declaration and does not change its meaning. It is therefore safe to emit consistently, including for constrained/defaulted parameters and single-parameter function, class, interface, and type declarations where it is not required to resolve JSX ambiguity. Multiple-parameter lists and type arguments retain their existing formatting. Both public function signatures and the shared TS printer are unchanged.

Supersedes #155 and addresses the reviewer's API concern using the existing visitor overrides, without adding a parameter or moving the TS implementation.

The sample runner now uses tsx() for jsx-*/tsx-* fixture directories and ts() for the rest. AST comparisons ignore Acorn's trailing-comma source offsets. Regression coverage includes code and sourcemap snapshots, nested and async arrows, const parameters, constraints/defaults, and TS input with no original comma.

Update the @sveltejs/acorn-typescript development dependency from 1.0.5 to 1.0.13, which includes the upstream fixes for generic async arrows. The dedicated TSX regression tests use Acorn, including the async cases.

Validation:

  • pnpm test — 316 tests passed
  • pnpm check — passed
  • pnpm lint — passed

@pkg-svelte-dev

pkg-svelte-dev Bot commented Sep 20, 2026 •

Copy link
Copy Markdown

Install the latest version of esrap from e246144:

pnpm add https://pkg.svelte.dev/esrap/c/e246144bd35338cbfceb755ab80ba91f7dae6250

Open in pkg.svelte.dev: https://pkg.svelte.dev/repos/esrap/pr/188

Note

This PR is from a fork. A maintainer must approve approve each commit before it can be built and installed.

@changeset-bot

changeset-bot Bot commented Sep 20, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: e246144

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
esrap Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@Rich-Harris Rich-Harris left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you!

@Rich-Harris
Rich-Harris merged commit 4a0bb7f into sveltejs:main Sep 21, 2026
7 checks passed
trueadm added a commit to octanejs/octane that referenced this pull request Oct 29, 2026
…e .tsrx type view (#2284)

The virtual TSX that the editor and octane-tsc check rejected two shapes the
compiler accepts:

- `const first = <T,>(xs: T[]) => xs[0]` printed as `<T>(…)`, which TSX reads
  as a JSX element `T` (TS17008 plus a syntax-error cascade). esrap 2.3.2's
  TSX printer drops the trailing comma; backport sveltejs/esrap#188 (2.3.8) as
  a patch, since bundle-volar.mjs freezes this esrap into the published
  `octane/compiler/volar`.
- `return;`, `return null;` and an unowned `continue;` that end a directive
  arm early reported TSRX2005/TSRX2002/TSRX2007. The shared transform checks
  arms against TSRX's own rules, which have no exits. The type view now prints
  an exit `continue;` as `return;` from the function the arm becomes, so the
  rest of the arm is narrowed, and drops the shared reports at those exits.
  Value returns and directive breaks are still reported, as compiling rejects
  them.

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

Development

Successfully merging this pull request may close these issues.

2 participants