[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.
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: |
Backport of #17685