Skip to content

fix: Rename switch discriminant references when body creates shadowing variable#17685

Merged
JLHwung merged 4 commits intobabel:mainfrom
magic-akari:fix/issue-17684
Dec 26, 2025
Merged

fix: Rename switch discriminant references when body creates shadowing variable#17685
JLHwung merged 4 commits intobabel:mainfrom
magic-akari:fix/issue-17684

Conversation

@magic-akari
Copy link
Contributor

Q                       A
Fixed Issues? Fixes #17684
Patch: Bug Fix? 👍
Major: Breaking Change?
Minor: New Feature?
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes?
License MIT

Copilot AI review requested due to automatic review settings December 25, 2025 07:21
@babel-bot
Copy link
Collaborator

babel-bot commented Dec 25, 2025

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/60546

Copy link

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 a bug where switch statement discriminants were not being renamed when the switch body creates a shadowing variable. The issue occurred during block scoping transformation when a variable used in a switch discriminant was shadowed by a declaration inside the switch cases.

Key Changes:

  • Added a SwitchStatement visitor to the renamer that explicitly renames simple identifier discriminants before scope traversal is skipped
  • Added comprehensive test coverage including both expected output verification and runtime execution tests

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
packages/babel-traverse/src/scope/lib/renamer.ts Adds SwitchStatement visitor to handle discriminant renaming when switch body contains shadowing variables
packages/babel-plugin-transform-block-scoping/test/fixtures/regression/issue-17684/input.js Test input demonstrating the bug with variable shadowing in switch statements
packages/babel-plugin-transform-block-scoping/test/fixtures/regression/issue-17684/output.js Expected transformation output showing correctly renamed discriminant
packages/babel-plugin-transform-block-scoping/test/fixtures/regression/issue-17684/options.json Test configuration specifying transform-block-scoping plugin
packages/babel-plugin-transform-block-scoping/test/fixtures/exec/switch-shadowing.js Runtime execution test verifying correct behavior of the transformation

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

@pkg-pr-new
Copy link

pkg-pr-new bot commented Dec 25, 2025

Open in StackBlitz

commit: b7ab129

@liuxingbaoyu
Copy link
Member

Thanks!
It seems we need to fix it elsewhere.
For example, switch (fn(e)).

@magic-akari magic-akari marked this pull request as draft December 25, 2025 07:45
@magic-akari magic-akari marked this pull request as ready for review December 25, 2025 11:42
@magic-akari
Copy link
Contributor Author

Thanks! It seems we need to fix it elsewhere. For example, switch (fn(e)).

Done

@JLHwung JLHwung merged commit 76a9178 into babel:main Dec 26, 2025
54 checks passed
@magic-akari magic-akari deleted the fix/issue-17684 branch December 28, 2025 16:36
@nicolo-ribaudo
Copy link
Member

nicolo-ribaudo commented Jan 12, 2026

Does this PR need to be included in Babel 7? If so, @magic-akari would you mind opening a PR cherry-picking the commit (76a9178) to the 7.x branch? Thank you!

@nicolo-ribaudo nicolo-ribaudo added PR: Bug Fix (next major) 🐛 A type of pull request used for our changelog categories for next major release and removed 7.x: needs backport labels Jan 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

PR: Bug Fix (next major) 🐛 A type of pull request used for our changelog categories for next major release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Issues encountered when converting babel to ES5 syntax

6 participants