Skip to content

fix(es/react-compiler): Scope ClassStaticBlock and TsModuleBlock as var boundaries#11943

Merged
kdy1 merged 3 commits into
swc-project:mainfrom
magic-akari:fix/react-compiler-scope
Jun 18, 2026
Merged

fix(es/react-compiler): Scope ClassStaticBlock and TsModuleBlock as var boundaries#11943
kdy1 merged 3 commits into
swc-project:mainfrom
magic-akari:fix/react-compiler-scope

Conversation

@magic-akari

@magic-akari magic-akari commented Jun 16, 2026

Copy link
Copy Markdown
Member

Description:

This PR fixes scope analysis edge cases in the React Compiler where var declarations and TypeScript constructs were incorrectly hoisted or leaked across scope boundaries.

Design Notes:

declare and global are TypeScript type-system annotations that don't affect runtime emission, yet they still create declarations that downstream code can reference. The analyzer must walk their bodies normally and create bindings accordingly, rather than skipping traversal.

Changes:

  • Class static blocks as var boundaries
    var declarations inside static { } blocks are now scoped to the block itself, matching ECMAScript semantics, instead of leaking into the enclosing function.

  • TypeScript namespace / module scopes
    namespace and module bodies now create proper TsModuleBlock scopes. Their members are no longer incorrectly hoisted to the Program scope.

  • Consistent binding for declare enum
    declare enum is now registered as a local binding so references resolve correctly.

  • Refactoring
    Renamed enclosing_function_scope()enclosing_var_scope() and updated ScopeFlags definitions to make scope-kind checks clearer.

Known Limitations:

TypeScript namespace merging is not yet supported. The scope collector treats each namespace/module declaration as an isolated lexical scope. Exported members in one block are not visible to later merged blocks, and they are modeled as plain local bindings. This is a similar issue described in #11607.

BREAKING CHANGE:

Related issue (if exists):

@codspeed-hq

codspeed-hq Bot commented Jun 16, 2026

Copy link
Copy Markdown

Merging this PR will degrade performance by 2.13%

❌ 1 regressed benchmark
✅ 188 untouched benchmarks
⏩ 120 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Mode Benchmark BASE HEAD Efficiency
Simulation es/minifier/libs/terser 320.7 ms 327.7 ms -2.13%

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing magic-akari:fix/react-compiler-scope (21230c1) with main (dcc0f2d)2

Open in CodSpeed

Footnotes

  1. 120 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.

  2. No successful run was found on main (bef16f3) during the generation of this report, so dcc0f2d was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

@magic-akari
magic-akari marked this pull request as ready for review June 17, 2026 00:14
@magic-akari
magic-akari requested a review from a team as a code owner June 17, 2026 00:14

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: accc1ad2e7

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread crates/swc_ecma_react_compiler/src/convert_scope.rs Outdated
Comment thread crates/swc_ecma_react_compiler/src/convert_scope.rs Outdated
Comment thread crates/swc_ecma_react_compiler/src/convert_scope.rs Outdated
@magic-akari
magic-akari marked this pull request as draft June 17, 2026 01:29
@changeset-bot

changeset-bot Bot commented Jun 17, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 21230c1

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@magic-akari
magic-akari marked this pull request as ready for review June 17, 2026 08:04

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 1b866bfd1a

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread crates/swc_ecma_react_compiler/src/convert_scope.rs
Comment thread crates/swc_ecma_react_compiler/src/convert_scope.rs
Comment thread crates/swc_ecma_react_compiler/src/convert_scope.rs
@magic-akari
magic-akari marked this pull request as draft June 17, 2026 08:34
@magic-akari
magic-akari marked this pull request as ready for review June 17, 2026 13:29
@kdy1
kdy1 requested a review from a team as a code owner June 18, 2026 04:12
@kdy1
kdy1 enabled auto-merge (squash) June 18, 2026 04:15
@kdy1
kdy1 merged commit 1ee74a0 into swc-project:main Jun 18, 2026
57 checks passed
@github-actions github-actions Bot added this to the Planned milestone Jun 18, 2026
@magic-akari
magic-akari deleted the fix/react-compiler-scope branch June 18, 2026 23:27
@kdy1 kdy1 modified the milestones: Planned, v1.15.43 Jun 23, 2026
@swc-project swc-project locked as resolved and limited conversation to collaborators Jul 23, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants