[7.x backport] fix: Rename switch discriminant references when body creates shadowing variable#17737
Merged
nicolo-ribaudo merged 2 commits intobabel:7.xfrom Jan 28, 2026
Merged
Conversation
…reates shadowing variable
Collaborator
|
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/60708 |
There was a problem hiding this comment.
Pull request overview
This PR is a backport to the 7.x branch that fixes issue #17684, where switch statement discriminants were not properly renamed when the switch body introduced a shadowing variable during block scoping transformation. The fix ensures that identifiers in switch discriminants are correctly renamed when they reference variables that are shadowed within the switch statement's cases.
Changes:
- Added logic in the renamer to requeue switch discriminants for renaming when shadowing occurs
- Added comprehensive test coverage with 5 test cases covering different discriminant expressions (simple identifiers, member expressions, binary expressions, call expressions)
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 20 comments.
Show a summary per file
| File | Description |
|---|---|
| packages/babel-traverse/src/scope/lib/renamer.ts | Adds logic to requeue switch statement discriminants when a shadowing variable is introduced in the switch body, mirroring the existing pattern for method computed keys and decorators |
| packages/babel-plugin-transform-block-scoping/test/fixtures/regression/issue-17684/input.js | Input test file with 5 test cases covering various switch discriminant patterns with shadowing variables |
| packages/babel-plugin-transform-block-scoping/test/fixtures/regression/issue-17684/output.js | Expected output showing correctly renamed discriminant references |
| packages/babel-plugin-transform-block-scoping/test/fixtures/regression/issue-17684/options.json | Test configuration specifying the transform-block-scoping plugin |
| packages/babel-plugin-transform-block-scoping/test/fixtures/exec/switch-shadowing.js | Executable test file verifying runtime behavior with expect assertions |
💡 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/exec/switch-shadowing.js
Show resolved
Hide resolved
packages/babel-plugin-transform-block-scoping/test/fixtures/exec/switch-shadowing.js
Show resolved
Hide resolved
packages/babel-plugin-transform-block-scoping/test/fixtures/exec/switch-shadowing.js
Show resolved
Hide resolved
packages/babel-plugin-transform-block-scoping/test/fixtures/exec/switch-shadowing.js
Show resolved
Hide resolved
packages/babel-plugin-transform-block-scoping/test/fixtures/regression/issue-17684/output.js
Show resolved
Hide resolved
packages/babel-plugin-transform-block-scoping/test/fixtures/regression/issue-17684/input.js
Show resolved
Hide resolved
packages/babel-plugin-transform-block-scoping/test/fixtures/regression/issue-17684/input.js
Show resolved
Hide resolved
packages/babel-plugin-transform-block-scoping/test/fixtures/regression/issue-17684/output.js
Show resolved
Hide resolved
packages/babel-plugin-transform-block-scoping/test/fixtures/regression/issue-17684/input.js
Show resolved
Hide resolved
|
commit: |
liuxingbaoyu
approved these changes
Jan 28, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Backport of #17685