Skip to content

feat(core): give every error a TypeScript or TSRX code (#843) - #888

Merged
leonidaz merged 3 commits into
mainfrom
feat/core-error-codes-843
Sep 27, 2026
Merged

leonidaz merged 3 commits into
mainfrom
feat/core-error-codes-843

Conversation

@leonidaz

@leonidaz leonidaz commented Sep 27, 2026 •

Copy link
Copy Markdown
Contributor

Closes #843.

Every error @tsrx/core reports now has a code:

  • A mistake TypeScript also reports gets TypeScript's code, so an editor shows the code TypeScript would.
  • A mistake only TSRX reports gets a TSRX code in four ranges: TSRX1xxx for markup, TSRX2xxx for template rules, TSRX3xxx for style blocks and CSS, and TSRX4xxx for everything else.

The specification's new appendix B lists them all: B.1 has the 52 TSRX codes with an example each, and B.2 has the 175 TypeScript codes with TypeScript's message for each.

What changes

Source Message Code before Code now
if (a) { '}' expected. none TS1005
let a = 1; let a = 2; Identifier 'a' has already been declared none TS2300
const a = <div></span>; Expected closing tag to match opening tag. … tsrx-mismatched-closing-tag TSRX1002
<div>@if (x) <b /></div> Expected `{` after JSX control-flow directive. none TSRX1008
@{ <a /> <b /> } A code block renders a single node; … none TSRX2011
<style apply={a} />, a not a style block 'a' is not a style block. … tsrx-style-apply-target TSRX3002
<style>p { color: red; </style> Expected } none TSRX3013
  • DIAGNOSTIC_CODES: every name keeps its name and gets the new value. JSX_EXPRESSION_VALUE is gone, since no error used it.
  • Message tables (get_error_code in diagnostics.js):
    • MESSAGE_CODES covers TSRX's own messages and core's TypeScript-worded ones.
    • PARSER_MESSAGE_CODES covers all 204 acorn and acorn-typescript messages. 140 of them map to TypeScript's code. The 6 that TypeScript accepts, such as a duplicate import attribute key, map to TSRX4003.
    • Every error goes through errors.js error() or the parser's raise, both of which set the code.
  • TSRX1008 removed: its two raise sites (Expected `for` after `@`. and Expected `@if`, `@for`, `@switch`, or `@try`.) could not be reached, because the parser reads a directive only after checking the word. The codes after it moved down by one. None of them had been released.
  • Loop messages: validateTsrxUnsupportedLoopStatement (used by Ripple) raised four messages with no code. They are now TSRX2015, the same mistake as TSRX `@for` currently supports `for...of` loops.
  • Solid: its two try errors pass their codes.

An import or export inside a block

acorn reports all of these with one message, 'import' and 'export' may only appear at the top level. TypeScript uses a separate error for each kind. Core now reports TypeScript's error, with the same code and position as TypeScript:

Inside a function Error
import a from 'a', import a = require('a'), export import a = b TS1232 An import declaration can only be used at the top level of a namespace or module.
export { f }, export * from 'a', export type { a } TS1233 An export declaration can only be used at the top level of a namespace or module.
export = f TS1231 An export assignment must be at the top level of a file or module declaration.
export as namespace A TS1316 Global module exports may only appear at top level.
export default 1 TS1258 A default export must be at the top level of a file or module declaration.
export namespace N {}, export declare module M {} TS1235 A namespace declaration is only allowed at the top level of a namespace or module.
export const a = 1, export default class {} TS1184 Modifiers cannot appear here.

Also, using and await using in a for…in head use TypeScript's messages, TS1493 and TS1494.

tsrx-prettier-plugin: rejects exactly what Prettier rejects

Important

This part changes what the formatter accepts. Please review it.

REJECTED_MISTAKES (16 message patterns) is now REJECTED_CODES, a set of codes. The old list used a different rule: refuse any input whose tree drops something, even where Prettier formats it. Several codes cover inputs on both sides of that rule, so a code set can't express it:

  • TS1206 covers a decorator on a function (the tree drops it) and one on a constructor (the tree keeps it).
  • TS1030 covers declare declare class (rejected before) and readonly readonly a (formatted before).
  • TS1184 covers public class in a block (rejected before) and export const in a block (formatted before).

So the set follows the 2026-09-27 direction to match Prettier exactly: it is the codes of the recorded mistakes that Prettier's typescript parser rejects. I ran every recorded mistake through Prettier to build it.

Formatted now as Prettier formats them (rejected before). Prettier's tree has no place for these either, so both leave them out:

Input Output (Prettier's and the plugin's)
function f(...a = []) {} function f(...a) {}
declare declare class A {} declare class A {}
function f() { public class A {} } function f() { class A {} }
declare import x from "m"; import x from "m";

Rejected now as Prettier rejects them (formatted before):

Input Error
class A { @dec constructor() {} } TS1206 Decorators can't be used with a constructor. …
function f(private a) {}, type F = (public x: number) => void TS2369 A parameter property is only allowed in a constructor implementation.
class A { constructor(public [a]: number[]) {} } TS1187 A parameter property may not be declared using a binding pattern.
import.source("x") TS17012 The only valid meta property for import is 'import.meta'

One exception: TS1039, an initializer in an ambient context, stays out of the set. Prettier rejects declare let x = 1 but formats declare class A { x = 1 }, and both have the same code. The plugin formats both, as it did before.

Downstream

The code values change, so Ripple, Octane and oxc need to know. I'll update Ripple-TS/ripple#1524, octanejs/octane#1337 and tsrx-org/oxc#117 with the final codes once this PR is reviewed. @tsrx/mcp maps the old tsrx-* codes a target might still report (Ripple today) to the new ones.

Side issues filed

Verification

  • Every error has a code: I logged every error without a code at error() and at parseModule's exit across the full suite (11,553 tests), and none was left. An earlier pass found acorn's recoverable errors, such as Private field '#x' must be declared in an enclosing class, thrown without a code; the second commit fixes that.
  • Exact entries before broad ones: Bugbot found three broad MESSAGE_CODES patterns catching acorn-typescript messages before their exact entries. The third commit removes them: an import attribute value now gets TS2858, the .mts/.cts as message TS7059, and the JSX hint TS1382 for > and TS1381 for }. A test checks them.
  • Each TSRX code: new tests check that a real input produces each code:
    • packages/tsrx/tests/utils/error-codes.test.js covers the parser and analysis codes, a set of TypeScript codes, and that every code is listed in the specification.
    • packages/tsrx-react/tests/error-codes.test.js covers the codes only a target's transform reports.
    • The Hono test now checks TSRX2026.
  • Codes nothing can produce: TSRX2017 and TSRX4002 come only from core functions Ripple calls; TSRX2018 and TSRX2019 only from a tree another parser builds; TSRX2020 and TSRX2025 only from a custom platform. The specification's examples say so.
  • Prettier parity: I compared 71 modifier and decorator inputs with Prettier's typescript parser. Four still differ, all tracked in [Bug]: tsrx-core: A modifier before a class static block throws, and a decorator before one is accepted #885 and [Bug]: tsrx-core: TS2369 isn't reported for a parameter property on a method, setter or constructor overload #886.
  • Checks: pnpm test (11,553 passed), pnpm typecheck and pnpm format pass.

🤖 Generated with Claude Code


Note

Medium Risk
Public diagnostic code values change across the ecosystem (editors, MCP, formatters), and prettier-plugin acceptance rules shift for some syntax errors; core/parser error mapping is broad and affects all compile paths.

Overview
Assigns a stable diagnostic code to every compile error: TypeScript TS#### codes when the mistake matches TypeScript (including acorn/acorn-typescript wording), and TSRX1xxx–TSRX4xxx for TSRX-only issues, replacing the old tsrx-* string codes. DIAGNOSTIC_CODES keeps the same keys with new values; JSX_EXPRESSION_VALUE is removed.

Core adds get_error_code via large MESSAGE_CODES / PARSER_MESSAGE_CODES tables, auto-attaches codes in error() and parser raise, maps nested import/export in blocks to TypeScript’s per-kind errors (not acorn’s generic top-level message), uses TS1493/TS1494 for using in for…in, and tags CSS parse failures as TSRX3013. Solid passes codes for its two template try errors.

@tsrx/mcp normalizes legacy tsrx-* codes from older compilers; docs, spec appendix B, and agent rules reference the new codes. tsrx-prettier-plugin switches from message-pattern rejection to a TypeScript code set aligned with what Prettier’s typescript parser rejects (behavior change for some modifier/decorator cases). New error-codes tests assert codes end-to-end and that every code appears in the specification.

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

leonidaz and others added 2 commits September 27, 2026 17:21
A mistake TypeScript also reports carries TypeScript's code; a mistake
only TSRX reports carries a TSRX1xxx (markup), TSRX2xxx (template rules),
TSRX3xxx (style/CSS) or TSRX4xxx (other) code. Every code is listed in the
specification's appendix B. An import or export inside a block is
reported with TypeScript's error for its kind. tsrx-prettier-plugin
rejects by code the mistakes Prettier's typescript parser rejects.

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

@cursor cursor Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

Bugbot Autofix prepared a fix for the issue found in the latest run.

  • ✅ Fixed: Message table assigns wrong TS codes
    • MESSAGE_CODES now maps import-attribute values to TS2858, matches only an exact Unexpected token for TS1012 so raw JSX > and } reach TS1382, and limits the .mts/.cts pattern to the trailing-comma form so an as assertion reaches TS7059.

Create PR

Or push these changes by commenting:

@cursor push bb4102bd5c
Preview (bb4102bd5c)
diff --git a/packages/tsrx/src/diagnostics.js b/packages/tsrx/src/diagnostics.js
--- a/packages/tsrx/src/diagnostics.js
+++ b/packages/tsrx/src/diagnostics.js
@@ -276,7 +276,10 @@
 	['A rest parameter cannot have an initializer.', 'TS1048'],
 	['A rest parameter cannot be optional.', 'TS1047'],
 	['A binding pattern parameter cannot be optional in an implementation signature.', 'TS2463'],
-	[/^This syntax is reserved in files with the \.mts or \.cts extension\. /, 'TS7060'],
+	[
+		/^This syntax is reserved in files with the \.mts or \.cts extension\. Add a trailing comma/,
+		'TS7060',
+	],
 	["'readonly' type modifier is only permitted on array and tuple literal types.", 'TS1354'],
 	["'abstract' modifier can only appear on a class, method, or property declaration.", 'TS1242'],
 	[
@@ -301,10 +304,10 @@
 	['for-of loop variable declaration may not have an initializer', 'TS1190'],
 	['Missing catch or finally clause', 'TS1472'],
 	['Multiple default clauses', 'TS1113'],
-	['Only string is supported as an attribute value', 'TS1145'],
+	['Only string is supported as an attribute value', 'TS2858'],
 	['value should be either an expression or a quoted text', 'TS1145'],
 	['Unterminated JSX contents', 'TS17008'],
-	[/^Unexpected token\b/, 'TS1012'],
+	[/^Unexpected token$/, 'TS1012'],
 ];
 
 /**

You can send follow-ups to the cloud agent here.

Reviewed by Cursor Bugbot for commit 197811a. Configure here.

Comment thread packages/tsrx/src/diagnostics.js Resolved
…es (#843)

Broad patterns in MESSAGE_CODES caught acorn-typescript messages before
their exact entries: an import attribute value got TS1145 instead of
TS2858, the .mts/.cts `as` message TS7060 instead of TS7059, and the JSX
`>`/`}` hint TS1012 instead of TS1382/TS1381. Regenerate the RuleSync
outputs, which CI found out of date.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@leonidaz
leonidaz merged commit 21bb71e into main Sep 27, 2026
9 checks passed
@leonidaz
leonidaz deleted the feat/core-error-codes-843 branch September 27, 2026 21:49
thejackshelton added a commit to tsrx-org/yuku that referenced this pull request Sep 27, 2026
…gives them

Errors now carry @tsrx/core's codes (tsrx-org/tsrx#888): TSRX1001,
TSRX1004 and TSRX2014 replace tsrx-unclosed-tag,
tsrx-script-end-tag-in-body and tsrx-dynamic-tag-expression, and a
mistake TypeScript also reports gets TypeScript's code (TS2300, TS2304,
TS1005, ...), from one message table in core-compat.js that parse(),
analyze() and the site's wasm host share. A thrown unclosed tag and a
spread or empty dynamic tag carry their code, as core's now do.
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.

[Bug]: tsrx-core, tsrx-prettier-plugin: Most errors have no code: give TypeScript's mistakes their TS code and TSRX's own errors a TSRX code

1 participant