Skip to content

fix(core): accept catch var redeclarations, type and namespace exports, quoted attribute keys, and import() trailing commas (#225, #253, #342, #554) - #602

Merged
leonidaz merged 3 commits into
mainfrom
fix/core-valid-code-rejected
Sep 25, 2026

Conversation

@leonidaz

@leonidaz leonidaz commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

Four kinds of valid code that the parser rejected.

The bugs and the fixes

#225: var can't redeclare a catch parameter. catch (error) { var error = 2; } failed with Identifier '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 own parseTryStatement (both the ordinary catch and the template @catch) entered the catch scope with BINDING_TYPES.BIND_SIMPLE_CATCH (4, a binding type), which acorn reads as the scope flag SCOPE_ASYNC, instead of SCOPE_SIMPLE_CATCH (32). The fix passes acorn's scope flag. The async flag 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 };, and declare function f(): void; export { f }; failed with Export '…' is not defined. TSRX's checkLocalExport override (for submodules) looked only in acorn's lexical and var lists. acorn-typescript's own check also reads its types and exportOnlyBindings lists. 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 with Duplicated key in attributes, and with { type: 'a', 'type': 'b' } parsed. acorn-typescript's parseWithEntries compares key.name, which a quoted key doesn't have. The override compares keys by value, as ECMAScript and acorn do (so 'type' is type too). It keeps acorn-typescript's message and position.

#554: a trailing comma in import(). import("./a.js",) and import("./a.js", { with: … },) failed with Unexpected token. acorn-typescript replaces acorn 8.18's parseDynamicImport with an older one. The override calls acorn's own, which also puts the options on options, as import.defer(), import types, and typescript-estree already do, and rejects a third argument. It had parsed import(a, b, c) as import(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 ImportExpression now reads options only:

Each fix is one method, or a flag in two places. No entries were added to CHECKER_LEVEL_ERRORS.

Overrides

#225 and #253 are TSRX bugs, not upstream ones, so no upstream issues were filed.

Verification

Before (origin/main at 24386ca) and after, every input was parsed strict and with { collect: true, errors: [], comments: [], preserveParens: true }:

  • the 58 unique tracked .tsrx files;
  • 12,197 unique string and template literals from 143 test files;
  • 1,544 unique Prettier snapshot inputs and 1,573 unique outputs.

That is 15,372 inputs. 49 changed, all intended: in each one the only difference is the new ImportExpression shape (arguments: [x] becomes options: x, and options: null is added without options), checked by normalizing the old AST and comparing it with the new one. No thrown message or collected error changed.

Tests:

  • New parser tests use the worker helpers, in strict, collect, and loose mode. They fail without the fix. The test that pinned the old arguments shape now expects options.
  • A new transform test checks that esrap prints the options once. @tsrx/prettier-plugin has new tests for import() with a trailing comma and for quoted attribute keys.
  • These projects pass: 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, and eslint-parser.
  • pnpm typecheck and pnpm format pass. tsrx-tsc isn't linked after --ignore-scripts, so the vite-plugin-hono step ran through packages/typescript-plugin/dist/tsc.js.

Findings

Decisions for you

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 ImportExpression with acorn/typescript-estree.

Catch scopes now enter with acorn’s SCOPE_SIMPLE_CATCH flag so var can redeclare a plain-name catch parameter (Annex B); destructured catch parameters still error as before.

Local export checks include acorn-typescript’s types and exportOnlyBindings, so export { … } / export type { … } can name interfaces, type aliases, namespaces, and ambient functions.

Import attributes use an overridden parseWithEntries that deduplicates keys by value (quoted 'type' vs type), allowing multiple quoted keys.

Dynamic import() / import.defer() delegate to acorn’s parseDynamicImport, enabling trailing commas, putting the second argument on options (not arguments), and treating a third argument as a syntax error. The prettier plugin, JSX transform printer, and tsrx-prettier parse adapter follow options only 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.

leonidaz and others added 3 commits September 25, 2026 15:01
…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]>
@leonidaz
leonidaz merged commit b3f3b03 into main Sep 25, 2026
8 checks passed
@leonidaz
leonidaz deleted the fix/core-valid-code-rejected branch September 25, 2026 19:27
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant