Skip to content

fix(core): read static before a line break as a modifier, a generic call signature first in an interface, a class named after a contextual keyword, and assert after an import (#439, #420, #421, #423) - #551

Merged
leonidaz merged 2 commits into
mainfrom
fix/core-upstream-parser-439
Sep 25, 2026

Conversation

@leonidaz

@leonidaz leonidaz commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

Fixes four parser bugs that come from @sveltejs/acorn-typescript 1.0.13. This is Phase 1, item 1.1 of #511, the plan for tsrx-prettier-plugin. Each one is worked around by overriding the responsible acorn-typescript method in TSRXPlugin (packages/tsrx/src/plugin.js), marked // UPSTREAM(sveltejs/acorn-typescript#<n>): remove once a release includes the fix (decision 13 in #511).

The bugs

Overrides

Override in plugin.js tsrx issue Upstream Why it's narrow
tsParseModifier and tsParseModifiers #439 sveltejs/acorn-typescript#119 (filed) Only a plain static token that the caller allows goes through the new path. Any other token or modifier calls super, so the same-line rule for readonly, public, declare, accessor, override, and the rest doesn't change. The new path is TypeScript's rule: canFollowModifier() without the line check, and never a second static (TypeScript's hasSeenStaticModifier, Babel's too). tsParseModifiers only records the node it reads modifiers for, so tsParseModifier can see whether static has already been read.
tsParseInterfaceBody #420 sveltejs/acorn-typescript#120 (filed) The same statements as upstream, except that { is now consumed inside tsInType. The token after the closing } is still read outside the type, as upstream does (decorators and elements after an interface).
parseClassId #421 sveltejs/acorn-typescript#110 (open PR by JakeChampion, commit "fix: accept TypeScript contextual keywords as class names") Only when the token is one of acorn-typescript's contextual-keyword token types does the override change it to tt.name and then call super. Everything else stays with acorn-typescript: the implements check for anonymous classes, anonymous export default class, and type parameters. There was no parseClassId override before. The AST matches sveltejs/acorn-typescript#110's: a stand-in parser with this override and a build of that PR give identical output, errors included, for 168 forms (14 words × 12 positions).
parseMaybeImportAttributes #423 sveltejs/acorn-typescript#121 (filed) It returns early only for an assert token after a line break. with keeps working after a line break, as the standard grammar and acorn allow.

Before filing, I checked upstream issues and PRs. sveltejs/acorn-typescript#110 fixes only #421, so there's no new issue for that one, and the marker points at that PR. Nothing upstream covered the other three, so I filed sveltejs/acorn-typescript#119, sveltejs/acorn-typescript#120, and sveltejs/acorn-typescript#121, each with a repro that uses only acorn and @sveltejs/acorn-typescript, and a fix direction. I checked each fix direction with a subclass of plain acorn-typescript. sveltejs/acorn-typescript#113 (#194 here) changes the same upstream parseClassId, and doesn't conflict with this override. export default class implements I {} still fails here, as before, until a release includes sveltejs/acorn-typescript#113.

Behavior changes to review

All of them match TypeScript:

  • interface I { / static / x: number / } is now rejected, the way static x: number on one line always was. TypeScript also reads static there as a modifier on x and reports TS1070. Before, this parsed as two members, static and x. The message is the garbled one in [Bug]: tsrx-core: Parser error for a misplaced modifier shows a JavaScript function instead of a message #550, same as the one-line form.
  • import "x" followed by assert { type: "json" } on the next line is now an error. TypeScript reports one too. Before, it was read as import assertions.
  • class C { static static a() {} } still fails, now with Unexpected token (TypeScript: ';' expected) instead of Duplicate modifier: 'static'.

Verification

AST comparison. I parsed every input on main (d1f89bdd) and on this branch with the formatter's options (collect: true, errors: [], comments: [], preserveParens: true), then compared the full ASTs, comments, and collected or thrown errors:

  • all 58 distinct tracked .tsrx files (69 paths)
  • 6,578 string and template literals from the repository's 151 test files
  • 1,548 distinct inputs behind the 2,040 imported Prettier cases (readSnapshotEntries / readFormatCase)

Out of 8,184 inputs, 6 changed, all intended and all Prettier inputs:

Prettier case Before After
js/classes/multiple-static.js 3 instance fields named static and instance method a static field static and static method a, as in TypeScript and acorn
js/import-assertions/not-import-assertions.js Unexpected token (2:7) import, then a call to assert
typescript/comments/type-parameters.ts Unexpected token (7:1) parses: call signature first in interface Foo
typescript/custom/typeParameters/callAndConstructSignatureLong.ts Unexpected token (2:4) parses: call signature and construct signature
typescript/compiler/globalIsContextualKeyword.ts Unexpected token (5:10) parses: class global {}
typescript/conformance/.../classAbstractAsIdentifier.ts Unexpected token (1:6) parses: class abstract {}

Compile output. For #439, @tsrx/core's JSX transform now emits static count = 0; and static create() {…}. Before, it emitted static;, count = 0;, static;, and an instance create(). There's a test for it.

Tests. The new packages/tsrx/tests/utils/upstream-parser-workarounds.test.js has 20 tests, one describe per override:

Without the fix, 15 of the 20 fail. The other 5 guard behavior that worked before.

  • pnpm vitest run --maxWorkers=4: 121 files, 8,931 passed, 69 expected fail, 70 skipped. The affected projects (tsrx-utils, tsrx-analyze, tsrx-vite, prettier-plugin, tsrx-prettier-plugin) pass again after updating to main.
  • pnpm typecheck: every step passes. vite-plugin-hono's typecheck:tests ran through packages/typescript-plugin/dist/tsc.js, since tsrx-tsc wasn't linked in the new worktree.
  • pnpm format:check and pnpm changeset:check pass.
  • @tsrx/prettier-plugin formats the six newly parsed Prettier inputs exactly as Prettier does.

Known failures. UPDATE_KNOWN_FAILURES=1 took out exactly the 6 cases above, from 75 to 69. A normal run then passes. I didn't re-run the Prettier test import.

A patch changeset for @tsrx/core is included.

Findings

Closes #439
Closes #420
Closes #421
Closes #423

🤖 Generated with Claude Code


Note

Medium Risk
Changes core parser behavior for classes, interfaces, and import syntax, but each override is narrow, TS-aligned, and covered by new tests; main risk is subtle edge cases until upstream absorbs the fixes.

Overview
Adds four temporary overrides in TSRXPlugin (plugin.js) so parsing matches TypeScript until upstream fixes land—each tagged UPSTREAM(sveltejs/acorn-typescript#…).

static after a line break is treated as a modifier (fixes a miscompile where static / count = 0 became an instance field named static). Other modifiers still require same-line continuation; double-static and interface static behavior align with TS.

Interface bodies parse inside tsInType so a generic call signature as the first member (interface I { <T>(…)… }) no longer breaks under JSX.

Class names can use contextual keywords (class global {}, etc.) by normalizing those tokens to tt.name before parseClassId.

Import/export assert only starts import assertions on the same line; assert(…) on the next line is a separate statement (and assert { … } after a break is an error).

Adds upstream-parser-workarounds.test.js, extends parse.d.ts, drops six Prettier known failures that now format correctly, and a patch changeset for @tsrx/core.

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

… call signature first in an interface, a class named after a contextual keyword, and `assert` after an import (#439, #420, #421, #423)

Four parser bugs caused upstream in @sveltejs/acorn-typescript 1.0.13, each
worked around by overriding the responsible method in TSRXPlugin, marked
`UPSTREAM(sveltejs/acorn-typescript#<n>)`:

- #439: `static` followed by a line break was read as a field named
  `static`, making the next member an instance member (a miscompile).
  `tsParseModifier` now takes `static` as a modifier without the same-line
  rule, as TypeScript's `nextTokenCanFollowModifier` does, and never takes a
  second `static` (TypeScript's `hasSeenStaticModifier`); `tsParseModifiers`
  records the node for that. Other modifiers keep the same-line rule.
  Upstream: sveltejs/acorn-typescript#119.
- #420: `tsParseInterfaceBody` reads the token after `{` inside the type, so
  a generic call signature as the first member no longer reads as a tag.
  Upstream: sveltejs/acorn-typescript#120.
- #421: `parseClassId` reads a TypeScript contextual keyword (`global`,
  `abstract`, `type`, ...) as a class name, with the same AST as the fix in
  sveltejs/acorn-typescript#110; everything else stays with acorn-typescript.
- #423: `parseMaybeImportAttributes` takes `assert` only on the line the
  import or export ends on, as TypeScript does.
  Upstream: sveltejs/acorn-typescript#121.

The six Prettier cases these fixed now pass and leave the known failures.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@leonidaz
leonidaz merged commit 4a5d385 into main Sep 25, 2026
8 checks passed
@leonidaz
leonidaz deleted the fix/core-upstream-parser-439 branch September 25, 2026 17:11
@github-actions github-actions Bot mentioned this pull request Sep 25, 2026
leonidaz added a commit that referenced this pull request Sep 25, 2026
Regenerated prettier-known-failures.json: the 7 cases #511 skips remain.

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