fix(turbopack): Collect declare global {} in VarDeclWithTsDeclareCollector#88568
fix(turbopack): Collect declare global {} in VarDeclWithTsDeclareCollector#88568mischnic merged 5 commits intovercel:canaryfrom
declare global {} in VarDeclWithTsDeclareCollector#88568Conversation
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>
|
Allow CI Workflow Run
Note: this should only be enabled once the PR is ready to go and can only be enabled by a maintainer |
There was a problem hiding this comment.
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
VarDeclWithTsDeclareCollectorto collect identifiers fromdeclare global {}anddeclare namespaceblocks - 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 Performance ReportMerging this PR will not alter performanceComparing Summary
Footnotes
|
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