Skip to content

fix(core): parse with the latest ECMAScript version and upgrade acorn (#414) - #452

Merged
leonidaz merged 3 commits into
mainfrom
fix/parser-ecma-latest-414
Sep 25, 2026
Merged

leonidaz merged 3 commits into
mainfrom
fix/parser-ecma-latest-414

Conversation

@leonidaz

@leonidaz leonidaz commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

Closes #414.

Summary

  • createParser in @tsrx/core parses with acorn's ecmaVersion: 'latest' instead of 13 (ES2022). The parser now accepts a hashbang on the first line, using / await using declarations (with TypeScript annotations, and in for...of / for await...of heads), the regex v flag, and regex modifiers.
  • acorn goes from ^8.17.0 (locked 8.17.0) to ^8.18.0 (locked 8.18.0), and the lockfile's one remaining @sveltejs/[email protected] is deduped to 1.0.13. See acorn upgrade.
  • Acorn reports a hashbang to onComment as a Line comment at offset 0. Two consumers mishandled that comment, so this PR fixes them: compile output dropped the hashbang, and the Prettier plugin printed it as ///usr/bin/env node. See Hashbang.
  • The TSRX parseForAfterInitWithIndex override now rejects using in a for...in head, like acorn's own parseForAfterInit does. Before this PR the using branch in parseForStatement never ran, because acorn only recognizes using at ecmaVersion 17 and above. Once it ran, the override accepted for (using x in y).

Changes

File Change
packages/tsrx/src/parse/index.js ecmaVersion: 'latest'
packages/tsrx/types/parse.d.ts New Parse.ParseInputOptions for the static parse. It takes acorn's ecmaVersion values such as 'latest', while Parse.Options.ecmaVersion stays the normalized number that this.options holds
packages/tsrx/src/plugin.js for...in + using check, and an updated comment that still described TSRX as parsing at 13
packages/tsrx/src/comment-utils.js get_hashbang(source)
packages/tsrx/src/transform/jsx/helpers.js, transform/jsx/index.js tsx_with_ts_locations(…, hashbang) writes the hashbang as the first line of every target's build and type-only output, mapped to 1:0, and never prints the offset-0 comment as a preserved comment
packages/tsrx/src/vite/dep-scan.js The dep-scan prelude (import "react/jsx-runtime"; …) goes after a hashbang instead of before it
packages/prettier-plugin/src/index.js parse records the hashbang comment and printComment prints it as #!…
pnpm-workspace.yaml, pnpm-lock.yaml acorn catalog bump and @sveltejs/acorn-typescript dedupe
.changeset/parser-latest-ecma-version.md patch for @tsrx/core and @tsrx/prettier-plugin

Tests are added to the existing files:

  • tests/utils/parser.test.js covers each syntax without options, with the formatter's { collect: true, comments: [] }, and in loose mode. It also checks that a hashbang not at the start is still rejected and that using in for...in is rejected.
  • tests/shared/compile.js runs for React, Preact, Solid, Vue, and Hono. It checks hashbang preservation in build output with and without collect, using / await using / regex output, and the type-only virtual code: hashbang first, no TS parse diagnostics, mappings after the hashbang still exact.
  • tests/vite/dep-scan.test.js checks the prelude after a hashbang for both plugin kinds.
  • tests/utils/platform.test.js runs replacePlatformFlags on a module with a hashbang, using, and a v regex. The Turbopack plugin runs this over every non-foreign .js/.ts file when a platform is configured, and those files used to fail to parse.
  • prettier-plugin/src/index.test.js checks that hashbangs, using declarations, and regexes are kept. It also checks that a real ///usr/bin/env node comment stays a comment.

All 33 new tests fail with the source changes reverted.

Hashbang: what each consumer does with it

Before this PR every consumer failed at parse time (Unexpected character '!'). The table shows what each consumer did with ecmaVersion: 'latest' alone, and what it does now.

Consumer With 'latest' only Now
compile() for React, Preact, Solid, Vue, Hono (server and dom), with and without collect Dropped: build output prints no comments #!/usr/bin/env node is line 1, once, ahead of injected imports (vue-jsx-vapor, runtime helpers); source map maps it to 1:0
compile_to_volar_mappings() (type-only TSX used by the language server, typescript-plugin, tsrx-tsc) Dropped: only TS pragmas, JSDoc, and // @… survive Line 1, once; TS parses it; all mappings after it still land on the same text
Vite dep scan (createDepScanTransformPlugin / createDepScanLoadPlugin) Prelude imports printed before the hashbang, which is a syntax error in the scan pass Prelude goes after the hashbang
Build plugins' CSS imports (Vite, Rspack, Turbopack, Bun) Appended after the code, fine Unchanged
replacePlatformFlags (Turbopack platform loader) Kept (MagicString) Unchanged
packages/prettier-plugin Printed ///usr/bin/env node Printed as written. Blank line after, following comments, and hashbang-only files match Prettier's typescript output
ESLint via @tsrx/eslint-parser Never sees it: ESLint core replaces #! with // before calling any parser (languages/js/index.js) Unchanged

A hashbang is never turned into // …, moved, or duplicated in any of these outputs.

using in compile output

A component with using at module scope, in the component body, in @if / @for / @try / @switch case bodies and nested code blocks, plus a function with await using (multiple declarators), for (using x of y), and for await (await using x of y), compiles in all five targets, in build and type-only output, with every declaration printed unchanged by esrap (kind: 'using' / 'await using'). /[\p{L}--[a-z]]/v and /(?i:a)b/ print unchanged too. The generated code has no TypeScript parse diagnostics.

The exception is a using declaration in a template @for head, @for (using item of items). It compiles, but the loop variable becomes a plain callback parameter and the item is never disposed. That's a separate root cause in the @for lowering, filed as #453.

Other ecmaVersion settings

git grep ecmaVersion (excluding node_modules):

Where What it does Decision
packages/tsrx/src/parse/index.js The parser Changed to 'latest'
packages/tsrx/src/plugin.js (this.options.ecmaVersion >= 6/8/9) Lower-bound checks copied from acorn No change: always true now, as at 13
packages/tsrx/types/parse.d.ts Option types Added ParseInputOptions (above)
packages/eslint-plugin/src/index.ts (parserOptions.ecmaVersion: 'latest') Recommended config for .tsrx files Already 'latest'. @tsrx/eslint-parser ignores parserOptions.ecmaVersion and always calls parseModule, so it follows the core change automatically. ESLint's scope analysis uses languageOptions.ecmaVersion, default 'latest'. Linting a .tsrx file with using and a hashbang through Linter works
packages/eslint-plugin/tests/rules/*.test.ts (parserOptions.ecmaVersion: 2022) RuleTester options Left as is: the TSRX parser ignores them, and changing them has no effect

No other package parses TSRX or JS itself. The language server, typescript-plugin, and the target compilers all go through @tsrx/core's parseModule, and the TypeScript side already uses ScriptTarget.Latest / ESNext. The build plugins' esbuild/oxc targets are all esnext.

acorn upgrade

  • npm view acorn version gives 8.18.0, published 2026-07-28T06:47:49Z (npm view acorn time). pnpm-workspace.yaml has minimumReleaseAge: 1440 (one day), which 8.18.0 is well past, so no minimumReleaseAgeExclude entry is needed. pnpm install --frozen-lockfile reports "Lockfile passes supply-chain policies". 8.18.0 adds a startLocation option and rejects ** with an unparenthesized arrow function on its left (() => {} ** x). The AST comparison below found no other change.
  • @sveltejs/acorn-typescript 1.0.13 declares peerDependencies: { acorn: "^8.9.0" }, so it accepts 8.18.0. It only compares ecmaVersion against lower bounds (6 to 13), so 'latest' doesn't change its behavior.
  • The lockfile change is minimal. The catalog entry and @tsrx/core's importer move to [email protected] / @sveltejs/[email protected]([email protected]). A plain pnpm install also re-resolved espree and the external @tsrx/[email protected] from acorn 8.16.0 to 8.17.0; I kept those as they were. pnpm install --lockfile-only leaves the result unchanged.
  • The one older @sveltejs/acorn-typescript in the tree, 1.0.10([email protected]), is deduped to 1.0.13([email protected]). It came from the published @tsrx/[email protected] (via [email protected], a dependency of @tsrx/typescript-plugin and tsrx-website), which declares @sveltejs/acorn-typescript: ^1.0.10, so no override was needed. The tree now has one @sveltejs/acorn-typescript version with three peer variants (acorn 8.16.0 for @tsrx/[email protected], 8.17.0 for @tsrx/[email protected] via @tsrx/ripple / ripple / @ripple-ts/vite-plugin, 8.18.0 for the workspace). External packages keep their locked acorn: espree 11.2.0 and @tsrx/[email protected] on 8.16.0, @tsrx/[email protected] on 8.17.0. Their ranges allow 8.18, but moving them isn't needed here.
  • minimumReleaseAgeExclude still lists @sveltejs/[email protected] (published 2026-08-13), which it no longer needs. Left as is.

Verification

AST comparison. Only the new syntax changes. The corpus has 6,538 unique sources:

Each source is parsed plain, with { collect: true, comments: [] }, and in loose mode, for 19,614 parses. Each result (AST including comments and collected errors, or the error message) is hashed and compared. The baseline is origin/main's parser with acorn 8.17.0 and ecmaVersion: 13; this branch uses acorn 8.18.0 and 'latest'. 19,455 results are identical. The rest are the 26 distinct Prettier inputs behind #414's 28 cases, which go from errors to ASTs, and the new-syntax sources in the tests this PR adds. Acorn 8.17.0 vs 8.18.0 at ecmaVersion: 13 alone: identical.

#409's harness. The 28 cases #414 lists all pass when run through #409's plugin on top of this branch's @tsrx/core (outside #409's worktree, using its own harness logic, overrides, and snapshots).

Checks. pnpm test (118 files, 6,382 passed, 60 skipped; eslint-parser, typescript-plugin, and language-server built first), pnpm typecheck, pnpm format:check, and pnpm changeset:check all pass.

Note for #409

#409 (the new tsrx-prettier-plugin) lists 28 Prettier test cases under #414 in prettier-known-failures.json. They start passing once this merges, and a listed case that passes fails #409's run, so its known-failures list needs a refresh (UPDATE_KNOWN_FAILURES=1 pnpm test --project tsrx-prettier-plugin). This PR doesn't touch #409's files.

Related findings

🤖 Generated with Claude Code


Note

Medium Risk
Touches the core parser and all compile/format/scan output paths; behavior change is intentional but affects every .tsrx and plain JS/TS module parsed by the toolchain.

Overview
Parser and acorn now use ecmaVersion: 'latest' (was ES2022 / 13) with acorn 8.18.0, so modules can parse hashbangs, using / await using (including in for...of heads), and regex v flags and modifiers. The TSRX parser rejects using in for...in loops to match acorn.

Hashbang preservation is wired through the pipeline: get_hashbang drives compile/type-only output (tsx_with_ts_locations) as the first line without duplicating the offset-0 line comment; Vite dep-scan prepends runtime imports after the hashbang; the Prettier plugin marks and prints #!… instead of //….

Types add ParseInputOptions for acorn’s ecmaVersion input shape. Broad tests cover parsing, compile, dep-scan, platform flag pass, and Prettier formatting for the new syntax.

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

…#414)

Parse with acorn's `ecmaVersion: 'latest'` instead of 13 (ES2022), so the
parser accepts hashbangs, `using` / `await using` declarations, and the
regex `v` flag and modifiers, and upgrade acorn to 8.18.0.

Keep a hashbang as the first line of compiled and type-only output (esrap
would print the parser's hashbang comment as `//…`), put the Vite dep-scan
prelude after it, and print it as written in the Prettier plugin. Reject
`using` in `for...in` heads like acorn's own `parseForAfterInit`.

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 is ON. A cloud agent has been kicked off to fix the reported issue. You can view the agent here.

Reviewed by Cursor Bugbot for commit d587bf4. Configure here.

Comment thread packages/prettier-plugin/src/index.js Resolved
… it (#414)

Empty statements take no comments, so in `#!…\n;\nfoo();` the parser
attaches the hashbang to a later statement than the first, where
markHashbangComment didn't look, and the formatter printed it as
`///usr/bin/env node`. Find it by position instead: the `Line` comment at
offset 0 of a file that starts with `#!`, on whichever node holds it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@leonidaz
leonidaz merged commit bff5325 into main Sep 25, 2026
8 checks passed
@leonidaz
leonidaz deleted the fix/parser-ecma-latest-414 branch September 25, 2026 03:08
leonidaz added a commit that referenced this pull request Sep 25, 2026
…ixes on main

With #452 (latest ECMAScript version), #404 and #427 (statement boundaries)
on main, 29 cases the parser used to reject now parse and format like
Prettier, and so does a call whose type arguments start on the next line.
The fragment with a comment after `<` now parses but hits #467.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
domgan-oai pushed a commit that referenced this pull request Sep 25, 2026
…ested with Prettier's own tests (#409)

* feat(tsrx-prettier-plugin): add a Prettier plugin built on Prettier's own printer

A new, unpublished package that formats everything except TSRX syntax with
Prettier's `estree` printer (`prettier/plugins/estree`), instead of printing
every node itself like `@tsrx/prettier-plugin`:

- The parser reshapes the acorn-typescript AST into the typescript-estree
  shape Prettier expects, and hands Prettier the comments to attach.
- The printer prints `@{ … }`, `@if`, `@for`, `@switch`, `@try`, `{value}`
  shorthand props, and `<style>`/`<script>` bodies (as CSS/TypeScript), and
  delegates the rest. Directive heads are laid out like their statements.

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

* test(tsrx-prettier-plugin): run Prettier's own JavaScript, JSX, and TypeScript format tests

`scripts/import-prettier-tests.js` imports the cases of Prettier's format
tests (3.9.6) that apply to TSRX: Prettier's `typescript` parser prints the
snapshot's output, and the input is valid TSX in a strict-mode module. The
2044 imported cases run through the plugin: 1807 pass, and 251 known
failures (122 of them rejected by the TSRX parser) run as expected failures
until fixed. Overrides record where TSRX deliberately differs (`<T,>` arrow
type parameters, CommonJS `await(…)`).

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

* fix(tsrx-prettier-plugin): end statements where Prettier's parsers do

Prettier attaches comments using its parsers' `locEnd`, which ends a
statement where its content ends: before a semicolon that comments separate
from it (`__contentEnd`), at the keyword or label of `break`, `continue`, and
`debugger`, at the last declarator, and at the body of `if`, loops, and
labeled statements. Match it, so comments near those statements attach like
Prettier's instead of moving or going unprinted. 39 more Prettier cases pass.

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

* fix(tsrx-prettier-plugin): reshape the AST like Prettier's parser postprocess

Port the steps of Prettier's `parse/postprocess` that the printer relies on:
rebalance same-operator logical expressions (`a || (b || c)`), drop unions
and intersections of one type, join touching JSDoc comments, and make the
program span the whole text. Also rebuild the whitespace between JSX
children, which the TSRX parser leaves out of its text nodes and Prettier
reads to keep blank lines and choose line breaks. 32 more Prettier cases
pass.

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

* fix(tsrx-prettier-plugin): reshape accessors, import options, dotted namespaces, and export import

Match typescript-estree where the acorn-typescript AST differs:
`accessor` fields become `AccessorProperty`, dynamic `import()` options
move from `arguments` to `options`, `namespace A.B {}` is one declaration
named by a qualified name, and `export import a = A` is an export of the
import-equals declaration. The printer also sees the options as its own
`typescript` parser's, since it checks the parser name for TypeScript-only
rules such as keeping the quotes of class property keys. 41 more Prettier
cases pass.

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

* test(tsrx-prettier-plugin): leave out Prettier's sloppy-mode tests

A `.tsrx` file is a strict-mode module, so the importer now also leaves out
Prettier's `js/sloppy-mode` tests and `interface` used as a name, which the
TSRX parser rightly rejects. The rejection messages ("Unexpected token")
don't say strict mode, so the check uses the directory. The known-failures
check also no longer compares against the list while it is being rewritten.

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

* feat(tsrx-prettier-plugin): keep comments between JSX children in place

TSRX reads `//` and `/* */` between JSX children as comments, where TSX reads
text, and Prettier's JSX printer has no place for them. The adapter now
rebuilds each element's text children from the source and turns such a
comment into a `TSRXJSXComment` child. Elements with comment children are
laid out by a copy of Prettier's `printJsxElementInternal`/`printJsxChildren`
(`src/jsx.js`) in which a comment keeps its line: one on its own line stays
there, one after other content stays on that line, a line comment always ends
its line, and JSDoc-style block comments are re-indented. Every other element
is still printed by Prettier.

Comments are also given their source text: the core parser dedents a
multi-line block comment's value for its own printer, which moved the lines
of such comments. All 69 `.tsrx` files in the repository now format stably,
and 9 more Prettier cases pass (one, where TSX reads the text as text, is an
override).

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

* fix(tsrx-prettier-plugin): reshape decorators and heritage names, and reject recovered markup

- Decorators on a constructor parameter property belong to the
  `TSParameterProperty`, as in typescript-estree.
- `export default @d class {}` declares an anonymous class; only a
  parenthesized class stays a `ClassExpression`.
- `extends a.b.C` in `implements`/interface heritage is a member expression,
  not a qualified type name.
- The parser collects errors so that mistakes TypeScript reports only as
  diagnostics (a redeclared variable) still format. But after an unclosed or
  mismatched tag it guesses the markup's structure, and formatting that guess
  garbles the file, so those errors are now thrown like any parse error.

6 more Prettier cases pass. The two layout failures left are parser issues:
#439 (`static` before a line break) and TSRX's line-start tag rule.

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

* fix(tsrx-prettier-plugin): format only code scripts, with this plugin's parser

A `<script>` body is now formatted only when it holds JavaScript or
TypeScript (no `type`, `module`, or a JavaScript/TypeScript type, as in
Prettier's HTML printer). JSON, import maps, and other bodies are kept as
written, instead of being reformatted as code (`[1,2]` became `[1, 2];`,
which isn't JSON; #441 for `@tsrx/prettier-plugin`). Code bodies are
formatted with this plugin's `tsrx` parser instead of Prettier's
`typescript`, so `prettier/standalone` needs no other plugin; a test runs the
plugin there alone.

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

* test(tsrx-prettier-plugin): refresh Prettier's cases for the parser fixes on main

With #452 (latest ECMAScript version), #404 and #427 (statement boundaries)
on main, 29 cases the parser used to reject now parse and format like
Prettier, and so does a call whose type arguments start on the next line.
The fragment with a comment after `<` now parses but hits #467.

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

* test(tsrx-prettier-plugin): refresh Prettier's cases for #471

With #471 on main, the 6 cases behind #416 and #419 parse and format like
Prettier.

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

* feat(tsrx-prettier-plugin): give `@{ … }` values and directives JSX's parentheses

Like `@tsrx/prettier-plugin` on main, and as Prettier lays out a JSX element:
a directive, or a `@{ … }` block used as a value, is wrapped in `(` … `)`
where it is assigned, returned, thrown, or an arrow's body (a `@{ … }` arrow
or function body stays on its line), with its comments inside the
parentheses. While its parent prints, such a value presents itself as a JSX
element, so Prettier's JSX-specific layout applies too, such as keeping a
last-argument arrow on the call's line (`list.map((item) => (` … `))`).

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

* test(tsrx-prettier-plugin): refresh Prettier's cases for #495

With #495 on main, the parser reads JSX spread children, and Prettier's
`jsx/spread/child.js` case formats like Prettier.

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

* test(tsrx-prettier-plugin): skip Prettier's private field destructuring cases

Destructuring private fields (`const { #x: x } = this`) is a TC39 Stage 2
proposal, which TSRX will parse once it becomes part of JavaScript. Its 8
Prettier cases are skipped with that reason instead of listed as known
failures (#424).

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

---------

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: Parser rejects hashbangs, using declarations, and new regex syntax because it parses as ES2022

1 participant