-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
[7.x backport] fix: Rename switch discriminant references when body creates shadowing variable #17737
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[7.x backport] fix: Rename switch discriminant references when body creates shadowing variable #17737
Conversation
…reates shadowing variable
|
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/60708 |
There was a problem hiding this 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 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
Uh oh!
There was an error while loading. Please reload this page.
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: |
Backport of #17685