Skip to content

fix(core): map primitive type keywords so return-type diagnostics report (#210) - #217

Merged
leonidaz merged 1 commit into
mainfrom
fix/primitive-return-type-mapping-210
Sep 23, 2026
Merged

leonidaz merged 1 commit into
mainfrom
fix/primitive-return-type-mapping-210

Conversation

@leonidaz

@leonidaz leonidaz commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #210

Problem

tsrx-tsc exited 0 for export class Model { value(): number {} }, which tsc rejects with TS2355. TypeScript reports missing-return errors on the return type: TS2355, TS2366 (no ending return), TS2534 (never with a reachable end), and TS1064 (async return type not Promise). The source-mapping walker in segments.js returned early for primitive type keywords without adding a mapping. Volar drops any diagnostic it cannot map back to the source.

The problem was not limited to class methods. function value(): number {} and function (): number {} were silent too, for every primitive keyword.

A second bug hid part of this. When a file contains no function or async keyword at all, the lexer's collected keyword list is empty. The walker treated an empty list as "tokens not collected" and fell back to arithmetic that assumes a function starts with function. For a method value, which starts at (, that mapped the 8 characters (): numb as a function keyword. That stray span is the [?,35) mapping the issue found. It also made value(): this {} report, but only in files with no function keyword.

Fix

  • Map each primitive type keyword (number, string, boolean, this, void, never, and the rest) as a token, as identifiers are. esrap writes these keywords with their node's location, so the lookup is exact.
  • An empty collected keyword list now means there is no keyword. The arithmetic fallback, used only by callers that collect no tokens, also checks that the source actually has the keyword at the offset.

Verification

I compared tsrx-tsc (Preact) with tsc on the same code as .ts, over 24 cases. They cover primitive, union, generic, template-literal, and composite return types on class methods, functions, and function expressions, plus annotation and argument controls.

cases matching tsc
origin/main 9/24
this PR 15/24

The issue's repro now reports App.tsrx(1,31): error TS2355 and exits 2, the same as tsc.

The 9 remaining cases are composite return types: number[], [number], { a: number }, keyof Model, () => void, (string), Model["value"], and typeof globalThis. esrap records no location at their boundaries, so tsrx has nothing to map them from. On main, keyof Model and typeof globalThis reported through the stray span, but only in files with no function keyword. Add export function f() {} and main misses both. They now fail the same way as the other composite types.

Tests

  • Shared source-mapping test, run for every target: each of 13 primitive keyword return types maps to the same generated keyword, and no mapping starts at a method's (. The file has no function keyword, so both bugs are exercised. Both assertions fail without the fix.
  • tsrx-tsc CLI test: TS2355 on value(): number {} at the number column. Fails without the fix.
  • pnpm test, pnpm typecheck, pnpm format:check, and pnpm changeset:check pass. In one full run, the first spawn in tsc.test.js hit the 5s timeout. That file passes on its own, as seen before in fix(core): map private names so their diagnostics report (#203) #207.

Follow-up

#216 tracks replacing the keyword tokens with a verify-only mapping over the whole return type once tsrx is on an esrap release with sveltejs/esrap#198 (open). With #198 linked in and that mapping prototyped, all 24 cases match tsc, with or without the keyword tokens. esrap main (with sveltejs/esrap#192 and #199) changes nothing here, and sveltejs/esrap#183 reaches 21/24 with the prototype. The code comment links to #216.

🤖 Generated with Claude Code


Note

Medium Risk
Changes source-map/diagnostic anchoring in the core transform walker, which affects editor and CLI type-check reporting across all .tsrx targets; behavior is well-covered by new tests but touches a sensitive path.

Overview
Fixes missing-return diagnostics (e.g. TS2355 on value(): number {}) in .tsrx files by emitting Volar source mappings for primitive type keywords (number, string, void, this, etc.) instead of skipping them in the mapping walker.

Also fixes false function keyword mappings on class methods when the file has no function/async tokens: an empty lexer keyword list is treated as “no keywords,” and the arithmetic fallback only maps when the source actually starts with the keyword at the computed offset.

Adds shared mapping tests for all primitive return-type keywords (including the no-function-in-file case) and a tsrx-tsc CLI test asserting TS2355 at the number column. Composite return types (e.g. number[]) remain unmapped until esrap boundary locations land (#216).

Reviewed by Cursor Bugbot for commit 4306581. Bugbot is set up for automated code reviews on this repo. Configure here.

…ort (#210)

`tsrx-tsc` exited 0 for `export class Model { value(): number {} }`,
which TypeScript rejects with TS2355. TypeScript reports missing-return
errors (TS2355, TS2366, TS2534, TS1064) on the return type, but the
source-mapping walker in segments.js returned early for primitive type
keywords without adding a mapping. Volar drops any diagnostic it cannot
map back to the source.

Map each primitive type keyword (`number`, `string`, `this`, and the
rest) as a token, as identifiers are. esrap writes these keywords with
their node's location.

The walker also mapped a method's value as a `function` keyword when the
file had no `function` or `async` keyword at all: an empty collected
token list fell through to arithmetic meant for callers that collect no
tokens, which mapped the 8 characters after the method's `(`. That stray
span is how `value(): this {}` reported before. An empty list now means
there is no keyword, and the arithmetic fallback checks the source text.

Composite return types (`number[]`, `{ a: number }`, `keyof T`) still
lose these diagnostics because esrap records no location at their
boundaries. #216 tracks replacing the keyword tokens with a whole return
type mapping once esrap ships sveltejs/esrap#198.

Fixes #210

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@vercel

vercel Bot commented Sep 23, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
tsrx-website Ready Ready Preview Sep 23, 2026 11:55pm UTC
tsrx-website-mcp Ready Ready Preview Sep 23, 2026 11:55pm UTC

Request Review

@leonidaz
leonidaz merged commit c426225 into main Sep 23, 2026
11 checks passed
@leonidaz
leonidaz deleted the fix/primitive-return-type-mapping-210 branch September 23, 2026 23:58
@github-actions github-actions Bot mentioned this pull request Sep 23, 2026

This branch was successfully deployed

2 active deployments
Preview – tsrx-website — 4306581a Deployed Sep 23, 2026 by vercel[bot]
Preview – tsrx-website-mcp — 4306581a Deployed Sep 23, 2026 by vercel[bot]
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.

[Bug]: tsrx-tsc silently accepts class methods missing a declared numeric return value

1 participant