-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
fix(es/transformer): Fix missing var declaration in nullish coalescing with spreads #11377
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
Conversation
…g with spreads The exit_stmt function was incorrectly restoring the statement pointer when exiting a statement. It was popping the current statement and then setting stmt_ptr to the popped value (the statement we just exited), instead of restoring the parent statement pointer. This caused the variable declaration for the temporary variable used in nullish coalescing to not be injected when the operator was used inside nested expressions. Fixed #11375 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 (1M context) <[email protected]>
🦋 Changeset detectedLatest commit: e9b9097 The changes in this PR will be included in the next version bump. Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
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 fixes a bug where the nullish coalescing operator (??) was missing the var declaration for temporary variables when used in functions with spread parameters. The root cause was incorrect statement pointer restoration in the exit_stmt function.
- Fixed the
exit_stmtfunction to properly restore the parent statement pointer from the stack - Added a comprehensive test case demonstrating the fix with a real-world example using spread parameters and nullish coalescing
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| crates/swc_ecma_transformer/src/es2020/nullish_coalescing.rs | Fixed exit_stmt to correctly restore parent statement pointer by using last().copied() after popping, instead of setting it to the popped value |
| crates/swc/tests/fixture/issues-11xxx/11375/input/index.tsx | Added test input with function using spread parameters and nullish coalescing operator |
| crates/swc/tests/fixture/issues-11xxx/11375/input/.swcrc | Added test configuration targeting ES5 with TypeScript and JSX support |
| crates/swc/tests/fixture/issues-11xxx/11375/output/index.tsx | Added expected output showing correct var declaration for the temporary variable |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Code ReviewSummaryThis PR fixes a critical bug in the nullish coalescing transformer where temporary variable declarations were missing when the operator was used inside functions with spread parameters. The fix correctly restores the statement pointer stack management. ✅ Code Quality: ExcellentThe Fix (lines 82-85 in fn exit_stmt(&mut self, _stmt: &mut Stmt, _ctx: &mut TraverseCtx) {
self.stmt_ptr_stack.pop();
self.stmt_ptr = self.stmt_ptr_stack.last().copied();
}Why this is correct:
Pattern Consistency:
This consistency is excellent and reduces the risk of similar bugs. ✅ Bug Fix ValidationRoot Cause Analysis: Accurate and well-explained in the PR description. The issue occurs when:
Test Coverage:
✅ Performance ConsiderationsNo performance concerns:
✅ Security ConcernsNo security issues:
✅ Best Practices & ConventionsFollows all CLAUDE.md rules:
Minor observations:
🎯 Recommendations1. Consider additional edge case tests (optional):
Example: function foo({...props}) {
return props.a ?? props.b ?? props.c;
}2. Documentation improvement (optional): fn exit_stmt(&mut self, _stmt: &mut Stmt, _ctx: &mut TraverseCtx) {
// Pop current statement and restore parent statement pointer
// so variable declarations are inserted at the correct nesting level
self.stmt_ptr_stack.pop();
self.stmt_ptr = self.stmt_ptr_stack.last().copied();
}However, given that this pattern is used consistently across multiple transformers, perhaps a comment in just one location (or in shared documentation) would be sufficient. 🚀 Verdict: LGTM ✅This is a high-quality bug fix that:
The PR is ready to merge. Review completed by Claude Code |
Code ReviewSummaryThis PR successfully fixes issue #11375 where the nullish coalescing operator () was missing the declaration for temporary variables when used in functions with spread parameters. The fix is correct, minimal, and follows established patterns in the codebase. ✅ Code Quality & Best PracticesThe Fix (crates/swc_ecma_transformer/src/es2020/nullish_coalescing.rs:82-85) The bug was in the // BEFORE (incorrect)
self.stmt_ptr = None; // Reset to None
self.stmt_ptr = self.stmt_ptr_stack.pop(); // Set to the just-exited statementThis caused The fix correctly restores the parent statement pointer: // AFTER (correct)
self.stmt_ptr_stack.pop(); // Remove current statement
self.stmt_ptr = self.stmt_ptr_stack.last().copied(); // Set to parent statementPattern Consistency: This implementation matches the exact pattern used in other transformers:
This consistency is excellent for maintainability. ✅ Test CoverageThe test case added is appropriate and comprehensive:
The test directly addresses the reported bug and verifies the fix works correctly. ✅ Performance ConsiderationsNo performance impact: The fix only changes the order of operations:
The change from
✅ Security ConcernsNo security issues identified:
✅ Correctness & Edge CasesRoot Cause Analysis: The bug only manifested when:
The fix correctly handles this by ensuring Potential Edge Cases (all appear handled correctly):
📝 Minor Observations
✅ Recommendation: APPROVEThis is a high-quality fix that:
The fix aligns perfectly with the repository's conventions for writing performant, well-documented code. Reviewed with Claude Code 🤖 |
Binary Sizes
Commit: f508b20 |
CodSpeed Performance ReportMerging #11377 will not alter performanceComparing Summary
Footnotes |
Summary
Fixes #11375
The nullish coalescing operator (
??) was missing thevardeclaration for the temporary variable when used in functions with spread parameters.Root Cause
The bug was in the
exit_stmtfunction in the nullish coalescing transformer. When exiting a statement, it was incorrectly restoring the statement pointer:stmt_ptrto the popped value (the statement we just exited)This meant
stmt_ptrwas pointing to the wrong statement when trying to inject variable declarations for nested expressions.The Fix
Changed the
exit_stmtfunction to correctly restore the parent statement pointer by usinglast().copied()on the stack after popping.Before
After
Test plan
vardeclaration when using spreads #11375cargo test -p swc --test tsc(4580 tests)cargo test -p swc --test projects(860 tests)🤖 Generated with Claude Code