Repository navigation
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
Conversation
… 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]>
Merged
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]>
This was referenced Sep 25, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes four parser bugs that come from
@sveltejs/acorn-typescript1.0.13. This is Phase 1, item 1.1 of #511, the plan fortsrx-prettier-plugin. Each one is worked around by overriding the responsible acorn-typescript method inTSRXPlugin(packages/tsrx/src/plugin.js), marked// UPSTREAM(sveltejs/acorn-typescript#<n>): remove once a release includes the fix(decision 13 in #511).The bugs
staticbefore a line break as a field name, making static members instance members #439 (a miscompile). Astaticfollowed by a line break was read as a field namedstatic, and the member after it became an instance member.static/count = 0;compiled tostatic;andcount = 0;, soC.countno longer existed. TypeScript'snextTokenCanFollowModifierchecksstaticwithcanFollowModifier()only, with no same-line rule, and acorn doesn't needstaticand the next token on one line either. acorn-typescript applies the same-line rule to every modifier.interface I { <T>(x: T): T }) failed withUnexpected token.tsParseInterfaceBodyreads the token after{before it enters the type, so the<became a tag start.class global {},class abstract {}) #421. A class named after a TypeScript contextual keyword (class global {},class abstract {},class type {}…) failed. acorn-typescript gives those words their own token types, and acorn'sparseClassIdonly takestt.name.assert (…)on the line after an import as import assertions #423.import "x"thenassert (…)on the next line (no semicolon) was read as import assertions and failed. TypeScript readsassertattributes only when there's no line break before them.Overrides
plugin.jstsParseModifierandtsParseModifiersstatictoken that the caller allows goes through the new path. Any other token or modifier callssuper, so the same-line rule forreadonly,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 secondstatic(TypeScript'shasSeenStaticModifier, Babel's too).tsParseModifiersonly records the node it reads modifiers for, sotsParseModifiercan see whetherstatichas already been read.tsParseInterfaceBody{is now consumed insidetsInType. The token after the closing}is still read outside the type, as upstream does (decorators and elements after an interface).parseClassIdtt.nameand then callsuper. Everything else stays with acorn-typescript: theimplementscheck for anonymous classes, anonymousexport default class, and type parameters. There was noparseClassIdoverride 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).parseMaybeImportAttributesasserttoken after a line break.withkeeps 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
acornand@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 upstreamparseClassId, 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 waystatic x: numberon one line always was. TypeScript also readsstaticthere as a modifier onxand reports TS1070. Before, this parsed as two members,staticandx. 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 byassert { 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 withUnexpected token(TypeScript:';' expected) instead ofDuplicate 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:.tsrxfiles (69 paths)readSnapshotEntries/readFormatCase)Out of 8,184 inputs, 6 changed, all intended and all Prettier inputs:
js/classes/multiple-static.jsstaticand instance methodastaticand static methoda, as in TypeScript and acornjs/import-assertions/not-import-assertions.jsUnexpected token (2:7)asserttypescript/comments/type-parameters.tsUnexpected token (7:1)interface Footypescript/custom/typeParameters/callAndConstructSignatureLong.tsUnexpected token (2:4)typescript/compiler/globalIsContextualKeyword.tsUnexpected token (5:10)class global {}typescript/conformance/.../classAbstractAsIdentifier.tsUnexpected token (1:6)class abstract {}Compile output. For #439,
@tsrx/core's JSX transform now emitsstatic count = 0;andstatic create() {…}. Before, it emittedstatic;,count = 0;,static;, and an instancecreate(). There's a test for it.Tests. The new
packages/tsrx/tests/utils/upstream-parser-workarounds.test.jshas 20 tests, onedescribeper override:staticbefore a line break as a field name, making static members instance members #439,staticbefore a line break and then a name,[,*,#x, a string, a number,get,static(),(,=,;,},?:,:, a static block, two and threestatics, the other modifiers keeping their rule, and the compile outputclass global {},class abstract {}) #421, reserved words (interface,enum,implements) still rejected and anonymous classes still anonymousassert (…)on the line after an import as import assertions #423, the;-terminated and same-lineassert { … }forms,withafter a line break, and a comment beforeassertWithout 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 tomain.pnpm typecheck: every step passes.vite-plugin-hono'stypecheck:testsran throughpackages/typescript-plugin/dist/tsc.js, sincetsrx-tscwasn't linked in the new worktree.pnpm format:checkandpnpm changeset:checkpass.@tsrx/prettier-pluginformats the six newly parsed Prettier inputs exactly as Prettier does.Known failures.
UPDATE_KNOWN_FAILURES=1took 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
patchchangeset for@tsrx/coreis included.Findings
privateon an interface member orinon a function's type parameter, fails with the source of a JavaScript arrow function as its message, at the token after the modifier. The cause is upstream:tsParseModifiersraises the error template without calling it. The root cause is separate from these four. It overlaps with [Bug]: tsrx-core: Parser throws on mistakes that TypeScript reports only as type-check diagnostics, even withcollect: true#415, since TypeScript reports these errors from its checker. No upstream issue yet.tsParseModifier/tsParseModifiers([Bug]: tsrx-core: Parser readsstaticbefore a line break as a field name, making static members instance members #439,staticfollowed by a line break is read as a field name, and the next member losesstaticsveltejs/acorn-typescript#119),tsParseInterfaceBody([Bug]: tsrx-core: Parser rejects a generic call signature as the first member of an interface #420, Generic call signature as the first member of an interface fails to parse withjsxsveltejs/acorn-typescript#120),parseClassId([Bug]: tsrx-core: Parser rejects classes named after TypeScript contextual keywords (class global {},class abstract {}) #421, test: run the parser over the TypeScript compiler corpus sveltejs/acorn-typescript#110), andparseMaybeImportAttributes([Bug]: tsrx-core: Parser readsassert (…)on the line after an import as import assertions #423,asserton the line after an import is read as import assertions sveltejs/acorn-typescript#121).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 taggedUPSTREAM(sveltejs/acorn-typescript#…).staticafter a line break is treated as a modifier (fixes a miscompile wherestatic/count = 0became an instance field namedstatic). Other modifiers still require same-line continuation; double-staticand interfacestaticbehavior align with TS.Interface bodies parse inside
tsInTypeso 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 tott.namebeforeparseClassId.Import/export
assertonly starts import assertions on the same line;assert(…)on the next line is a separate statement (andassert { … }after a break is an error).Adds
upstream-parser-workarounds.test.js, extendsparse.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.