Skip to content

chore(sveltekit): replace recast + @babel/parser with acorn#19533

Open
roli-lpci wants to merge 1 commit intogetsentry:developfrom
roli-lpci:chore/sveltekit-replace-recast-with-acorn
Open

chore(sveltekit): replace recast + @babel/parser with acorn#19533
roli-lpci wants to merge 1 commit intogetsentry:developfrom
roli-lpci:chore/sveltekit-replace-recast-with-acorn

Conversation

@roli-lpci
Copy link

Replaces recast and @babel/parser with acorn and @sveltejs/acorn-typescript for AST parsing in the sveltekit auto-instrumentation plugin.

  • If you've added code that should be tested, please add tests.
  • Ensure your code lints and the test suite passes (yarn lint) & (yarn test).
  • Link an issue if there is one related to your pull request.

Ref #19447

What this does

The autoInstrument Vite plugin parses SvelteKit +page.ts / +layout.server.ts files to detect export const load or export function load declarations. It only reads the AST — never transforms or prints it — so recast's source-preserving round-trip feature is entirely unused.

This replaces the full recast + @babel/parser pipeline with acorn (Node.js's own parser) and @sveltejs/acorn-typescript (the actively maintained TypeScript plugin used by SvelteKit itself).

Changes

  • Delete recastTypescriptParser.ts — the 91-line Babel parser config with 20+ plugin declarations is no longer needed
  • Replace recast.parse() with acorn.Parser.extend(tsPlugin()).parse() in autoInstrument.ts
  • Fix AST node type: StringLiteral (Babel-specific) → Literal (ESTree standard) for string literal export detection
  • Fix AST structure: ast.program (recast's File wrapper) → direct ast (acorn returns Program)
  • Improve error handling: try/catch around parse (acorn throws SyntaxError) replaces dubious null check
  • Improve type safety: remove unsafe double-cast (specifier.exported as unknown as t.StringLiteral) in favor of proper discriminant narrowing

Dependency reduction

Removed: recast, ast-types, esprima, source-map, tslib, tiny-invariant, @babel/parser, @babel/types, @babel/helper-string-parser, @babel/helper-validator-identifier

Added: acorn (already in dep tree via Vite/Rollup), @sveltejs/acorn-typescript (already in dep tree via @sveltejs/kit)

Net effect: ~8 fewer transitive dependencies, zero new packages for end users.

Why @sveltejs/acorn-typescript?

The original acorn-typescript package by TyrealHu hasn't been updated since January 2024 and has known unpatched bugs. The Svelte team's fork (@sveltejs/acorn-typescript) is actively maintained and is already a transitive dependency of every SvelteKit project.

All 69 existing tests pass without modification.

Co-Authored-By: Claude Opus 4.6 noreply@anthropic.com

Replaces recast and @babel/parser with acorn and @sveltejs/acorn-typescript
for AST parsing in the auto-instrumentation plugin. Since the code only reads
the AST (never transforms or prints), the full recast pipeline is unnecessary.

Key changes:
- Delete recastTypescriptParser.ts (no longer needed)
- Use acorn Parser.extend(tsPlugin()) for TypeScript parsing
- Use @sveltejs/acorn-typescript (actively maintained) instead of TyrealHu's
  abandoned acorn-typescript
- Fix StringLiteral -> Literal node type check (ESTree compliance)
- Fix ast.program -> ast (acorn returns Program directly)
- Use try/catch for parse errors instead of null program check

Removes ~8 transitive dependencies (recast, ast-types, esprima, source-map,
tslib, tiny-invariant, @babel/parser, @babel/types).

Ref: getsentry#19447

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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.

1 participant