Skip to content

refactor(core): define each error message once, with its code, and raise and test by code - #950

Merged
leonidaz merged 4 commits into
mainfrom
chore/core-diagnostic-entries-947
Sep 29, 2026
Merged

leonidaz merged 4 commits into
mainfrom
chore/core-diagnostic-entries-947

Conversation

@leonidaz

@leonidaz leonidaz commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

Closes #947.

Each error message TSRX raises is now written once, in diagnostics.js, together with its code. The places that raise an error use that entry, and tests check the code and the position instead of the message text. No message or code changes.

For example, TypeScript's TS1048:

// Before: the text in plugin.js, again in diagnostics.js, and in 22 tests
const REST_PARAMETER_INITIALIZER = 'A rest parameter cannot have an initializer.';
this.raise(position, REST_PARAMETER_INITIALIZER); // code looked up from the text

// diagnostics.js
['A rest parameter cannot have an initializer.', 'TS1048'],

// parser.test.js
errors: [['A rest parameter cannot have an initializer.', 'a =']],
throws: 'A rest parameter cannot have an initializer. (1:14)',
// After: diagnostics.js
REST_PARAMETER_INITIALIZER: { code: 'TS1048', message: 'A rest parameter cannot have an initializer.' },

// plugin.js
this.raise(position, TS_ERRORS.REST_PARAMETER_INITIALIZER);

// parser.test.js
errors: [[TS_ERRORS.REST_PARAMETER_INITIALIZER, 'a =']],
throws: [TS_ERRORS.REST_PARAMETER_INITIALIZER, '1:14'],

A message that includes a value is a function of that value, and the function has the code too:

TS_ERRORS.MODIFIER_MUST_PRECEDE('export', 'abstract');
// { code: 'TS1029', message: "'export' modifier must precede 'abstract' modifier." }
TS_ERRORS.MODIFIER_MUST_PRECEDE.code; // 'TS1029'

What changes

Each point below follows the proposal in #947.

  1. Entries in diagnostics.js. TSRX_ERRORS holds the errors with TSRX's own codes. TS_ERRORS holds the mistakes TypeScript also reports, with TypeScript's codes. Messages in acorn's or acorn-typescript's wording that TSRX raises itself, where it parses in their place, are entries too (for example UNEXPECTED_TOKEN or MISSING_CATCH_OR_FINALLY).
  2. Raise sites use the entries. This covers the parser, but also the analysis, the transform, the scope checks and the Solid, Vue and Hono targets, because point 4 removes the lookup they relied on. Each place raises an entry, or passes a code next to a target's own message. error() also takes an entry. plugin.js no longer defines its own message strings. The copies in transform/jsx/index.js and tsrx-solid are gone as well.
  3. CHECKER_LEVEL_ERRORS is a Set of entries, not of messages and regular expressions. It holds the TSRX entries, plus UPSTREAM_ERRORS for the acorn and acorn-typescript messages the parser needs to recognize (a redeclaration, Duplicate modifier: …, and so on).
  4. Only acorn's and acorn-typescript's own messages are still looked up. Each lookup row has a comment naming the upstream functions that raise it (// acorn: parseBreakContinueStatement). A row for a message TSRX also writes points to its entry instead of repeating the text. MESSAGE_CODES and get_error_code are gone.
  5. Tests check codes and positions. Tables pair an entry with the source text at its position ([TS_ERRORS.X, 'a =']). A thrown error is checked by line:column or offset. Collected errors are compared by code. Helpers are in tests/shared/errors.js, and the worker parse harness now returns codes. A new test in error-codes.test.js checks the wording of every TS_ERRORS entry against TypeScript 5.9's diagnostic message for its code; about 20 entries are listed there as using acorn's, acorn-typescript's or TSRX's own wording. Another test checks that every TSRX_ERRORS entry has a TSRX code. After this, rewording a message means changing only diagnostics.js.

The formatter no longer needs MODIFIER_BEFORE_EXPORT. A modifier placed before export is now recognized as TS1029 raised at the export keyword.

On the question #947 left open: @tsrx/core now exports TS_ERRORS and TSRX_ERRORS, because the target packages in this repo raise with them. UPSTREAM_ERRORS is not exported. The existing TSRX_*_ERROR message exports stay, as the entries' messages. @tsrx/mcp uses them to match errors from compilers that report no code, so it is unchanged.

Beyond the files the issue lists, these test files checked TSRX's message text too and were converted: closing-brace, tokenizer-context and export-scope-cache, the formatter's tsrx-migrated tests, one language-server test, and one test each in tsrx-react and tsrx-vue. The style syntax fixture now gives code: 'TSRX2011' instead of the message. oxc-tsrx ports that table by hand into its own test, so nothing there reads the field.

Checking that nothing changed

  • Every code the tests produce. With temporary logging in error() and in the parser's throw path, I ran tsrx-utils, tsrx-analyze, prettier-plugin, the React, Solid, Vue, Hono and Preact targets, and tsrx-mcp, before and after the source change. Both runs produced the same 272 distinct (message, code) pairs.
  • The upstream lookup. I compared main's lookup with the new one on a message built from each row's upstream wording (230 messages). There are 5 differences, and none can reach an editor or a build:
    • Three acorn-typescript rows were removed, because acorn-typescript 1.0.13 defines those messages but never raises them: GenericsEndWithComma, LetInLexicalBinding, and 'abstract' modifier can only appear on …. TSRX still raises that last message itself, with its entry.
    • Binding let in strict mode and Assigning to let in strict mode now look up to TS2480. Only the first of acorn's errors at a position is recorded, and that one is always The keyword 'let' is reserved, so these two are never reported.
  • The core, Solid, Vue, Hono, React, formatter, language-server and MCP packages type check. pnpm changeset:check passes, and the changed files are formatted.

Downstream

  • Ripple. Ripple's `await` is not allowed inside components. … error calls core's error() without a code, so today it gets TSRX2021 only because core looks the code up from the text. Once Ripple upgrades to a core release that includes this PR, that error has no code until Ripple passes DIAGNOSTIC_CODES.TARGET_AWAIT_UNSUPPORTED. I'll note this on [Bug]: @tsrx/core's error codes are final: new codes for its errors, and Ripple's own errors have none Ripple-TS/ripple#1529, which already covers Ripple giving its own errors codes.
  • Octane. None of the messages Octane's compiler raises matched core's lookup, so nothing changes there.

Findings

How the comparisons were run

The logging was one line in errors.js and one in #throwError, and was removed before committing:

if (process.env.TSRX_ERROR_LOG) appendFileSync(process.env.TSRX_ERROR_LOG, JSON.stringify([message, code]) + '\n');
for p in tsrx-utils tsrx-analyze prettier-plugin tsrx-react tsrx-solid tsrx-vue tsrx-hono tsrx-preact tsrx-mcp; do
  TSRX_ERROR_LOG=before.log pnpm vitest run --project $p
done
sort -u before.log > before.sorted   # and the same for after
diff before.sorted after.sorted      # no output

For the lookup, a script imported main's diagnostics.js and this branch's, built a message from each // acorn: … or // acorn-typescript: … template in main's table (plus the messages cut short in those comments), and compared get_error_code(message) with get_upstream_error(message)?.code.

🤖 Generated with Claude Code


Note

Medium Risk
Touches error reporting across parser, transform, and multiple packages; behavior is intended to be unchanged but any missed raise site could lose a diagnostic code.

Overview
Centralizes TSRX compile errors in @tsrx/core as TSRX_ERRORS (TSRX codes) and TS_ERRORS (TypeScript-aligned codes), each { code, message } or a parameterized factory. error() now accepts these entries directly; raise sites across the parser, analysis, transform, scope, and Solid/Vue/Hono targets pass entries (or explicit codes) instead of duplicating strings and relying on message→code lookup.

diagnostics.js replaces the old MESSAGE_CODES / get_error_code tables with structured entries, UPSTREAM_ERRORS, and get_upstream_error / upstream_diagnostic for acorn/acorn-typescript messages only. Legacy TSRX_*_ERROR exports remain as aliases to entry messages for backward compatibility.

Tests and tooling assert codes and positions (shared error_with / expect_reported helpers) rather than full message text; the Prettier plugin detects modifier-before-export via TS1029 + source position instead of a message regex.

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

leonidaz and others added 4 commits September 28, 2026 19:29
…ise by entry

`diagnostics.js` now defines every message TSRX raises once, with its code:
`TSRX_ERRORS` for TSRX's own codes and `TS_ERRORS` for mistakes TypeScript
also reports. A message that takes values is a function of them. The parser,
the analysis, the transform and the Solid, Vue and Hono targets raise these
entries, or pass their code with a target's own message, instead of writing
the message out and looking its code up.

Only acorn's and acorn-typescript's own messages keep a lookup. Each row
names the upstream functions that raise it, and a row for a message TSRX
also writes points at its entry. `CHECKER_LEVEL_ERRORS` is a set of entries
and lookup rows instead of messages and regular expressions, and the
formatter finds a modifier before `export` by its position.

The messages and codes don't change. `@tsrx/core` exports `TS_ERRORS` and
`TSRX_ERRORS`; its `TSRX_*_ERROR` message exports are now the entries'
messages.

Refs #947

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… message exports

TypeScript's TS1079 takes the modifier (`A '{0}' modifier cannot be used
with an import declaration.`), so its entry does too. The message is the
same. The `TSRX_*_ERROR` exports stay, for matching an error that has no
code, as `@tsrx/mcp` does for a compiler that reports none.

Refs #947

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The tests name each error by its entry in `diagnostics.js`, or by its code
for an acorn or acorn-typescript message that has none, and check its code
and position. The worker parse harness gives codes, and the helpers in
`tests/shared/errors.js` match a thrown error or a worker outcome by them.
The style syntax fixture, which the Rust parser ports, gives plain codes.

A test checks each `TS_ERRORS` entry's wording against TypeScript's
diagnostic for its code, except those in acorn's, acorn-typescript's or
TSRX's own wording, and another that every `TSRX_ERRORS` entry has a TSRX
code. So rewording a message changes `diagnostics.js` and no other test.

Refs #947

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@leonidaz
leonidaz merged commit 9cfadf4 into main Sep 29, 2026
8 checks passed
@leonidaz
leonidaz deleted the chore/core-diagnostic-entries-947 branch September 29, 2026 00:11
jonkwheeler added a commit that referenced this pull request Nov 12, 2026
… channel (#1294)

* feat: carry diagnostic severity and codes through collected-error channel

CompileError gains an optional severity ('error' | 'warning') so consumer
compilers can publish warning-severity diagnostics; error() accepts it as a
trailing parameter. tsrx-tsc now prints collected diagnostics in tsc
convention — [tsrx-tsc] file(line,col): <error|warning> <code>: message —
instead of dropping the code. The language server's compile-diagnostic plugin
maps severity 'warning' to real editor warnings.

Re-cut from feat/diagnostic-code-propagation (slice T-A, tracked in
octanejs/octane#1204; the fatal-stub emit, exit-path, and class-map emit pins
land in a follow-up).

Generated with [Devin](https://devin.ai)

* fix(typescript-plugin): print a parse error's position in tsrx-tsc

A parse error acorn throws gives its position as `loc` itself
(`{ line, column }`), not as a range with `start`, so tsrx-tsc printed it
without one: `[tsrx-tsc] App.tsrx: error TS1005: '}' expected. (3:5)`.
Since #888 that error carries TypeScript's code, so with its position
the line now reads `App.tsrx(3,6): error TS1005: ...`, as tsc prints it.

The content-mapper compatibility matrix described the classic CLI's line
for a fatal error as free text without a code; it now gives the code and
position.

* test: pin severity and tsrx-tsc lines against the compiler's own error entries

`error()` takes an error entry with its code since #950; a test checks a
warning severity passes through with an entry's code. A test compiles two
rest elements with an initializer with the React compiler and checks that
tsrx-tsc prints each one at its own position with TS1186 (#957), where it
printed one line without a position or code.

* fix(core): never throw a warning-severity diagnostic

`error()` throws when it has no `errors` array, which is every compile
that doesn't collect, such as a Vite build. So a consumer compiler or a
core check reporting `error(..., code, 'warning')` the usual way, with
`collect ? errors : undefined`, failed the build on a warning. The
thrown error was also fatal: the language server showed it as an error
coded `tsrx-compile-error`.

A warning is now only ever recorded. Without `errors`, `error()` drops it.

* fix(typescript-plugin): place a pos-only diagnostic in tsrx-tsc

`tsrx-tsc` read a diagnostic's position from `loc` only. A consumer
compiler may give only `pos` and `end`, as the language server allows,
since it places a diagnostic by `pos`. So a warning the editor showed at
its place printed without one: `[tsrx-tsc] App.tsrx: warning ...`.

Without `loc`, the position now comes from `pos` in the compiled
source, with lines broken where tsc breaks them.

* fix(typescript-plugin): print a thrown diagnostic as an error in tsrx-tsc

A thrown error stops the compile, and the language server shows it as an
error whatever its `severity`. `tsrx-tsc` printed one marked
`severity: 'warning'` as `warning`, for a file that failed to compile.
It now prints a fatal error as `error`, as the language server does.

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
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.

tsrx-core: Define each error message once, with its code, and raise and test by code

1 participant