Repository navigation
fix(core): accept catch var redeclarations, type and namespace exports, quoted attribute keys, and import() trailing commas (#225, #253, #342, #554) - #602
Merged
Conversation
…s, quoted attribute keys, and import() trailing commas (#225, #253, #342, #554) - Enter a plain catch parameter's scope with acorn's SCOPE_SIMPLE_CATCH (32) instead of BIND_SIMPLE_CATCH (4, acorn's SCOPE_ASYNC), so Annex B lets `var` redeclare it. - Let checkLocalExport see acorn-typescript's `types` and `exportOnlyBindings`, which acorn-typescript's own check reads. - Override parseWithEntries to compare import attribute keys by value (UPSTREAM sveltejs/acorn-typescript#116). - Override parseDynamicImport with acorn's own grammar, which puts the options on `options` and takes trailing commas, and parse import.defer() with it (UPSTREAM sveltejs/acorn-typescript#110). Consumers read `options` only. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
leonidaz
added a commit
that referenced
this pull request
Sep 25, 2026
Resolves a conflict in upstream-parser-workarounds.test.js, where main (#602) and this branch both added tests after the sveltejs/acorn-typescript#121 block: both sets are kept, and the import takes main's parse_in_worker. Co-Authored-By: Claude Opus 5.5 <[email protected]>
leonidaz
added a commit
that referenced
this pull request
Sep 25, 2026
The parser now puts the options of import(source, options) on options (#602), so the printer drops its arguments[0] fallback. 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.
Four kinds of valid code that the parser rejected.
The bugs and the fixes
#225:
varcan't redeclare a catch parameter.catch (error) { var error = 2; }failed withIdentifier 'error' has already been declared. Annex B allows it for a parameter that is a plain name, and acorn, acorn-typescript, and TypeScript accept it. TSRX's ownparseTryStatement(both the ordinarycatchand the template@catch) entered the catch scope withBINDING_TYPES.BIND_SIMPLE_CATCH(4, a binding type), which acorn reads as the scope flagSCOPE_ASYNC, instead ofSCOPE_SIMPLE_CATCH(32). The fix passes acorn's scope flag. Theasyncflag on a catch scope had no effect, since acorn and TSRX only read it on function scopes.A destructured parameter still can't be redeclared (
catch ({ e }) { var e; }). That is an ECMAScript early error: acorn and V8 reject it, while TypeScript reports nothing. sveltejs/acorn-typescript#110 ("fix: let var redeclare a destructured catch parameter", 034b463ade) accepts it to match TypeScript. TSRX parses catch clauses itself, so that commit won't change TSRX when it's released. See the decision below.#253: named exports of namespaces, interfaces, and type aliases.
interface Props {} export type { Props };,type A = number; export { A };,namespace A {} export { A };, anddeclare function f(): void; export { f };failed withExport '…' is not defined. TSRX'scheckLocalExportoverride (for submodules) looked only in acorn'slexicalandvarlists. acorn-typescript's own check also reads itstypesandexportOnlyBindingslists. The override now reads all four, through the same per-scope cache. Names that no open scope declares are still reported, such as a type declared inside a namespace and exported from outside it.#342: import attributes with two quoted keys.
with { 'a': 'x', 'b': 'y' }failed withDuplicated key in attributes, andwith { type: 'a', 'type': 'b' }parsed. acorn-typescript'sparseWithEntriescompareskey.name, which a quoted key doesn't have. The override compares keys by value, as ECMAScript and acorn do (so'type'istypetoo). It keeps acorn-typescript's message and position.#554: a trailing comma in
import().import("./a.js",)andimport("./a.js", { with: … },)failed withUnexpected token. acorn-typescript replaces acorn 8.18'sparseDynamicImportwith an older one. The override calls acorn's own, which also puts the options onoptions, asimport.defer(), import types, and typescript-estree already do, and rejects a third argument. It had parsedimport(a, b, c)asimport(a, (b, c)).import.defer()now goes through the same method. That method was a copy of acorn's grammar (parseDeferredDynamicImport), so the copy is removed.Every consumer of
ImportExpressionnow readsoptionsonly:argumentsand thenoptions, so the new shape prints once.with_deferred_importsno longer falls back toarguments.segments.js: it visits onlysource, before and after. The options were unmapped in both shapes, which I filed as [Bug]: tsrx-core: The options ofimport()andimport.defer()get no editor mappings, so tsrx-tsc drops their errors #599.@tsrx/prettier-plugin: its fallback toargumentsis removed.getCallArgumentCountandisSimpleCallArgumentalready readoptions, and now see it on an ordinaryimport()too. Its layout of longimport()calls hasn't changed: the output is the same as before for the same inputs, and [Bug]: @tsrx/prettier-plugin: Formatter never breaks between the arguments ofimport(source, options)like Prettier #553 tracks the difference from Prettier.tsrx-prettier-plugin's adapter: itsarguments[0]→optionsmapping is dead code and is removed.argumentsfield is removed from core'sImportExpressiontype.Each fix is one method, or a flag in two places. No entries were added to
CHECKER_LEVEL_ERRORS.Overrides
parseWithEntries:// UPSTREAM(sveltejs/acorn-typescript#116), the key comparison of the open PR perf: single-pass ref classification and flat array-ref cleanups #116, which feat: remove lazy destructuring (RFC #106) #110 (82c3df5906) also has, with the same AST and message.parseDynamicImport:// UPSTREAM(sveltejs/acorn-typescript#110), "fix: defer dynamic import parsing to acorn" (b4fdd561aa). It deliberately doesn't mirror feat: remove lazy destructuring (RFC #106) #110's AST in full. feat: remove lazy destructuring (RFC #106) #110 also copiesoptionsontoargumentsfor compatibility, and esrap'sImportExpressionvisitor prints both, soimport("x", o)would print asimport("x", o, o). Walkers such as zimmerframe would also visit the options twice. The override produces acorn's shape. Its comment says to dropargumentsif the release still adds it.#225 and #253 are TSRX bugs, not upstream ones, so no upstream issues were filed.
Verification
Before (
origin/mainat 24386ca) and after, every input was parsed strict and with{ collect: true, errors: [], comments: [], preserveParens: true }:.tsrxfiles;That is 15,372 inputs. 49 changed, all intended: in each one the only difference is the new
ImportExpressionshape (arguments: [x]becomesoptions: x, andoptions: nullis added without options), checked by normalizing the old AST and comparing it with the new one. No thrown message or collected error changed.Tests:
argumentsshape now expectsoptions.@tsrx/prettier-pluginhas new tests forimport()with a trailing comma and for quoted attribute keys.tsrx-utils,tsrx-analyze,prettier-plugin,tsrx-prettier-plugin(the known-failures list is unchanged, and was refreshed after merging main),tsrx-react,tsrx-preact,tsrx-solid,tsrx-vue,tsrx-hono, andeslint-parser.pnpm typecheckandpnpm formatpass.tsrx-tscisn't linked after--ignore-scripts, so thevite-plugin-honostep ran throughpackages/typescript-plugin/dist/tsc.js.Findings
import()andimport.defer()get no editor mappings, so tsrx-tsc drops their errors #599: the options ofimport()andimport.defer()get no editor mappings, sotsrx-tscdrops errors inside them.declare namespaceordeclare modulebody rejects a function declared there #600: an export list in adeclare namespace/declare modulebody can't name a function declared there. acorn-typescript declares ambient functions only at the top level. feat: remove lazy destructuring (RFC #106) #110 (cc36101f3c) fixes acorn-typescript's own check, but TSRX replaces that check, so TSRX needs its own fix.declare globaldeclaration fails with "Export is not defined" #601: a named export of adeclare globaldeclaration is rejected. feat: remove lazy destructuring (RFC #106) #110 (f808499dd6) fixes it upstream, and with this PR TSRX picks that up once it's released.import(a, b, c)is now a syntax error in collect and loose mode too (Unexpected token), as in acorn. It used to parse loosely. TypeScript's parser accepts it and its checker reports TS1450.export { f }too).Decisions for you
catch ({ e }) { var e; }, which TypeScript accepts. It's collected in collect mode, as before. The alternative is to follow TypeScript and feat: remove lazy destructuring (RFC #106) #110. The compiled code would then fail to load in engines.argumentscopy. It may be worth a review comment on test: run the parser over the TypeScript compiler corpus sveltejs/acorn-typescript#110: an esrap consumer printing its AST would print dynamic import options twice.Closes #225
Closes #253
Closes #342
Closes #554
🤖 Generated with Claude Code
Note
Medium Risk
Core parser and AST shape changes for dynamic import affect printing and downstream tools; behavior is broad but covered by expanded tests.
Overview
Fixes four parser rejections of valid code and aligns
ImportExpressionwith acorn/typescript-estree.Catch scopes now enter with acorn’s
SCOPE_SIMPLE_CATCHflag sovarcan redeclare a plain-name catch parameter (Annex B); destructured catch parameters still error as before.Local export checks include acorn-typescript’s
typesandexportOnlyBindings, soexport { … }/export type { … }can name interfaces, type aliases, namespaces, and ambient functions.Import attributes use an overridden
parseWithEntriesthat deduplicates keys by value (quoted'type'vstype), allowing multiple quoted keys.Dynamic
import()/import.defer()delegate to acorn’sparseDynamicImport, enabling trailing commas, putting the second argument onoptions(notarguments), and treating a third argument as a syntax error. The prettier plugin, JSX transform printer, and tsrx-prettier parse adapter followoptionsonly and drop trailing commas when formatting.Types drop
ImportExpression.arguments; tests cover parse modes via workers and new formatter cases.Reviewed by Cursor Bugbot for commit bb3b8fc. Bugbot is set up for automated code reviews on this repo. Configure here.