fix: Rename switch discriminant references when body creates shadowing variable#17685
fix: Rename switch discriminant references when body creates shadowing variable#17685JLHwung merged 4 commits intobabel:mainfrom
Conversation
magic-akari
commented
Dec 25, 2025
| 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 |
|
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/60546 |
There was a problem hiding this comment.
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
SwitchStatementvisitor 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.
packages/babel-plugin-transform-block-scoping/test/fixtures/exec/switch-shadowing.js
Outdated
Show resolved
Hide resolved
packages/babel-plugin-transform-block-scoping/test/fixtures/regression/issue-17684/input.js
Show resolved
Hide resolved
|
commit: |
|
Thanks! |
Done |
|
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! |