Skip to content

fix(turbopack): Collect declare global {} in VarDeclWithTsDeclareCollector#88568

Merged
mischnic merged 5 commits intovercel:canaryfrom
kdy1:kdy1/typescript
Jan 15, 2026
Merged

fix(turbopack): Collect declare global {} in VarDeclWithTsDeclareCollector#88568
mischnic merged 5 commits intovercel:canaryfrom
kdy1:kdy1/typescript

Conversation

@kdy1
Copy link
Contributor

@kdy1 kdy1 commented Jan 15, 2026

What?

Fix the issue described in swc-project/swc#11448.

Why?

The behavior described in the SWC issue is a kind of intended behavior.

How?

cc @mischnic

kdy1 and others added 2 commits January 15, 2026 12:38
This extends `VarDeclWithTsDeclareCollector` to also collect identifiers
from `declare global {}` and similar TypeScript module declarations.

Previously, only `declare const/let/var` were collected, but
`declare global {}` was missed. This caused the `global` identifier
to remain marked as `top_level_mark` instead of being treated as
a free variable after TypeScript transformation removes it.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Add unit tests to verify the collector handles:
- `declare const` statements
- `declare global {}` blocks
- `declare namespace` blocks
- Multiple declare statements together
- Non-declare statements (should be ignored)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@nextjs-bot nextjs-bot added the Turbopack Related to Turbopack with Next.js. label Jan 15, 2026
@nextjs-bot
Copy link
Collaborator

Allow CI Workflow Run

  • approve CI run for commit: d32584f

Note: this should only be enabled once the PR is ready to go and can only be enabled by a maintainer

@mischnic mischnic marked this pull request as ready for review January 15, 2026 14:11
Copilot AI review requested due to automatic review settings January 15, 2026 14:12
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes an issue where declare global {} blocks in TypeScript files were not being properly handled by the VarDeclWithTsDeclareCollector. The fix ensures that the global identifier from declare global {} declarations is collected and treated as a free variable, allowing proper code generation and global variable access.

Changes:

  • Extended VarDeclWithTsDeclareCollector to collect identifiers from declare global {} and declare namespace blocks
  • Added comprehensive unit tests for the new functionality
  • Added integration tests to verify correct behavior in real-world scenarios

Reviewed changes

Copilot reviewed 3 out of 4 changed files in this pull request and generated no comments.

File Description
turbopack/crates/turbopack-ecmascript/src/parse.rs Added TsModuleDecl and TsModuleName imports, implemented visit_ts_module_decl method to collect module/namespace identifiers with declare modifier, and added comprehensive unit tests
turbopack/crates/turbopack-tests/tests/execution/turbopack/code-gen/global-rewrite/input/index.js Added test case for basic global variable rewriting
turbopack/crates/turbopack-tests/tests/execution/turbopack/code-gen/global-rewrite-declare/input/index.js Added integration test to verify declare global works correctly with global variable access
turbopack/crates/turbopack-tests/tests/execution/turbopack/code-gen/global-rewrite-declare/input/foo.ts Added TypeScript file with declare global block to test the fix

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@codspeed-hq
Copy link

codspeed-hq bot commented Jan 15, 2026

CodSpeed Performance Report

Merging this PR will not alter performance

Comparing kdy1:kdy1/typescript (c1adf60) with canary (f5c8b6a)

Summary

✅ 17 untouched benchmarks
⏩ 3 skipped benchmarks1

Footnotes

  1. 3 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

Copy link
Member

@mischnic mischnic left a comment

Choose a reason for hiding this comment

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

Thank you!

@mischnic mischnic merged commit 9dbe6ba into vercel:canary Jan 15, 2026
154 checks passed
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 30, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

locked Turbopack Related to Turbopack with Next.js.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants