Skip to content

ref(sveltekit): Replace recast + @babel/parser with acorn#19533

Merged
Lms24 merged 5 commits intogetsentry:developfrom
roli-lpci:chore/sveltekit-replace-recast-with-acorn
Mar 20, 2026
Merged

ref(sveltekit): Replace recast + @babel/parser with acorn#19533
Lms24 merged 5 commits intogetsentry:developfrom
roli-lpci:chore/sveltekit-replace-recast-with-acorn

Conversation

@roli-lpci
Copy link
Contributor

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

Copy link
Member

@JPeer264 JPeer264 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for these changes, they look good. I actually want to see if the tests are going through and want to wait for a review from @Lms24 before we merge it.

@roli-lpci roli-lpci force-pushed the chore/sveltekit-replace-recast-with-acorn branch from fa80627 to d9cb391 Compare February 26, 2026 18:23
@roli-lpci roli-lpci force-pushed the chore/sveltekit-replace-recast-with-acorn branch from d9cb391 to f54035e Compare March 1, 2026 04:11
@Lms24 Lms24 changed the title chore(sveltekit): replace recast + @babel/parser with acorn ref(sveltekit): Replace recast + @babel/parser with acorn Mar 2, 2026
Copy link
Member

@Lms24 Lms24 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, thanks for taking this on! This looks good to me! Once we drop support for SvelteKit <2.31.0, we can even get rid of these deps entirely but for now, the reduction from 8 to 2 deps already is a win.

heads-up: I adjusted the PR title to better reflect the type of change

@Lms24 Lms24 enabled auto-merge (squash) March 2, 2026 14:18
auto-merge was automatically disabled March 14, 2026 04:25

Head branch was pushed to by a user without write access

@roli-lpci roli-lpci force-pushed the chore/sveltekit-replace-recast-with-acorn branch from 470a2a7 to 2299bb2 Compare March 14, 2026 04:25
roli-lpci and others added 3 commits March 19, 2026 11:19
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>
- Fix import sort order in autoInstrument.ts (scoped package before unscoped)
- Deduplicate @sveltejs/acorn-typescript entries in yarn.lock

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@JPeer264 JPeer264 force-pushed the chore/sveltekit-replace-recast-with-acorn branch from 2299bb2 to 87f80a4 Compare March 19, 2026 10:20
@JPeer264
Copy link
Member

I just rebased the branch and deduplicated yarn.lock. Hopefully this can be merged then right after

@github-actions
Copy link
Contributor

github-actions bot commented Mar 19, 2026

Semver Impact of This PR

🟢 Patch (bug fixes)

📋 Changelog Preview

This is how your changes will appear in the changelog.
Entries from this PR are highlighted with a left border (blockquote style).


New Features ✨

Deps

  • Bump mongodb-memory-server-global from 10.1.4 to 11.0.1 by dependabot in #19888
  • Bump stacktrace-parser from 0.1.10 to 0.1.11 by dependabot in #19887

Bug Fixes 🐛

  • (cloudflare) Forward ctx argument to Workflow.do user callback by Lms24 in #19891
  • (core) Return same value from startSpan as callback returns by s1gr1d in #19300
  • (deps) Bump socket.io-parser to 4.2.6 to fix CVE-2026-33151 by chargome in #19880
  • (nestjs) Add node to nest metadata by chargome in #19875
  • (serverless) Add node to metadata by nicohrubec in #19878

Internal Changes 🔧

  • (astro) Re-enable server island tracing e2e test in Astro 6 by Lms24 in #19872
  • (lint) Resolve oxlint warnings by isaacs in #19893
  • (node-integration-tests) Remove unnecessary file-type dependency by Lms24 in #19824
  • (sveltekit) Replace recast + @babel/parser with acorn by roli-lpci in #19533

🤖 This preview updates automatically when you update the PR.

@Lms24
Copy link
Member

Lms24 commented Mar 20, 2026

rebased once more after the last release. Gonna merge this afterwards!

Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Co-authored-by: Lukas Stracke <lukas.stracke@sentry.io>
@Lms24 Lms24 enabled auto-merge (squash) March 20, 2026 11:11
@Lms24 Lms24 merged commit c1a7b22 into getsentry:develop Mar 20, 2026
425 of 429 checks passed
Lms24 added a commit that referenced this pull request Mar 20, 2026
This PR adds the external contributor to the CHANGELOG.md file, so that
they are credited for their contribution. See #19533

Co-authored-by: Lms24 <8420481+Lms24@users.noreply.github.com>
s1gr1d pushed a commit that referenced this pull request Mar 20, 2026
This PR adds the external contributor to the CHANGELOG.md file, so that
they are credited for their contribution. See #19533

Co-authored-by: Lms24 <8420481+Lms24@users.noreply.github.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.

3 participants