Skip to content

Commit 686d154

Browse files
kdy1claude
andauthored
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

5 files changed

+53
-2
lines changed

.changeset/eight-chicken-sneeze.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
swc_core: patch
3+
swc_ecma_transformer: patch
4+
---
5+
6+
fix(es/transformer): Fix missing var declaration in nullish coalescing with spreads
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"jsc": {
3+
"parser": {
4+
"syntax": "typescript",
5+
"tsx": true
6+
},
7+
"target": "es5",
8+
"loose": false,
9+
"minify": {
10+
"compress": false,
11+
"mangle": false
12+
}
13+
},
14+
"module": {
15+
"type": "es6"
16+
},
17+
"minify": false,
18+
"isModule": true
19+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export default function DropdownNavbarItemDesktop({
2+
items,
3+
...props
4+
}) {
5+
return (
6+
<div>
7+
<NavbarNavLink
8+
onClick={props.to ? undefined : (e) => e.preventDefault()}
9+
>
10+
{props.children ?? props.label}
11+
</NavbarNavLink>
12+
</div>
13+
);
14+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { _ as _object_without_properties } from "@swc/helpers/_/_object_without_properties";
2+
export default function DropdownNavbarItemDesktop(_0) {
3+
var items = _0.items, props = _object_without_properties(_0, [
4+
"items"
5+
]);
6+
var _props_children;
7+
return React.createElement("div", null, React.createElement(NavbarNavLink, {
8+
onClick: props.to ? undefined : function(e) {
9+
return e.preventDefault();
10+
}
11+
}, (_props_children = props.children) !== null && _props_children !== void 0 ? _props_children : props.label));
12+
}

crates/swc_ecma_transformer/src/es2020/nullish_coalescing.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ impl VisitMutHook<TraverseCtx> for NullishCoalescingPass {
8080
}
8181

8282
fn exit_stmt(&mut self, _stmt: &mut Stmt, _ctx: &mut TraverseCtx) {
83-
self.stmt_ptr = None;
84-
self.stmt_ptr = self.stmt_ptr_stack.pop();
83+
self.stmt_ptr_stack.pop();
84+
self.stmt_ptr = self.stmt_ptr_stack.last().copied();
8585
}
8686
}
8787

0 commit comments

Comments
 (0)