Commit 686d154
fix(es/transformer): Fix missing var declaration in nullish coalescing with spreads (#11377)
## Summary
Fixes #11375
The nullish coalescing operator (`??`) was missing the `var` declaration
for the temporary variable when used in functions with spread
parameters.
### Root Cause
The bug was in the `exit_stmt` function in the nullish coalescing
transformer. When exiting a statement, it was incorrectly restoring the
statement pointer:
- It popped the current statement from the stack
- Then set `stmt_ptr` to the popped value (the statement we just exited)
This meant `stmt_ptr` was pointing to the wrong statement when trying to
inject variable declarations for nested expressions.
### The Fix
Changed the `exit_stmt` function to correctly restore the parent
statement pointer by using `last().copied()` on the stack after popping.
### Before
```javascript
// Missing var declaration
(_props_children = props.children) !== null && _props_children !== void 0 ? _props_children : props.label
```
### After
```javascript
var _props_children;
(_props_children = props.children) !== null && _props_children !== void 0 ? _props_children : props.label
```
## Test plan
- ✅ Added test case for issue #11375
- ✅ All tests pass: `cargo test -p swc --test tsc` (4580 tests)
- ✅ All tests pass: `cargo test -p swc --test projects` (860 tests)
🤖 Generated with [Claude Code](https://claude.com/claude-code)
---------
Co-authored-by: Claude Sonnet 4.5 (1M context) <[email protected]>1 parent d05144c commit 686d154
File tree
5 files changed
+53
-2
lines changed- .changeset
- crates
- swc_ecma_transformer/src/es2020
- swc/tests/fixture/issues-11xxx/11375
- input
- output
5 files changed
+53
-2
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
Lines changed: 14 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
Lines changed: 12 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
Lines changed: 2 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
80 | 80 | | |
81 | 81 | | |
82 | 82 | | |
83 | | - | |
84 | | - | |
| 83 | + | |
| 84 | + | |
85 | 85 | | |
86 | 86 | | |
87 | 87 | | |
| |||
0 commit comments