-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
feat(es/transformer): Implement object rest spread transform #11356
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
Implements ES2018 object rest spread transformation, including: - Object rest patterns in destructuring - Object spread expressions in object literals - Support for nested patterns and complex cases - Integration with assumptions (no_symbol, set_property, pure_getters) All tests passing: - cargo test -p swc_ecma_compat_es2018 ✓ - cargo test -p swc_ecma_transforms_compat ✓ (72 passed) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
|
Pull Request Review: ES2018 Object Rest Spread TransformationSummaryThis PR implements ES2018 object rest/spread transformation in the new Code Quality & Architecture ✅Strengths:
Design Patterns:
Potential Issues & Concerns1.
|
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 implements ES2018 object rest/spread transformation in the new swc_ecma_transformer architecture, migrating functionality from the older swc_ecma_compat_es2018 package. The implementation follows the esbuild lowering algorithm and handles complex destructuring scenarios including nested patterns, computed property keys, and various statement contexts.
Key changes include:
- New
ObjectRestandObjectSpreadtransformers with comprehensive pattern handling - Integration with the assumptions system for configuration (
object_rest_no_symbols,set_spread_properties,pure_getters) - Removal of old implementation files and creation of a compatibility adapter that delegates to the new architecture
Reviewed changes
Copilot reviewed 10 out of 11 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
crates/swc_ecma_transformer/src/es2018/object_rest_spread.rs |
Core implementation of object rest/spread transformation with 1200+ lines of pattern lowering logic |
crates/swc_ecma_transformer/src/es2018/mod.rs |
Integration layer that wires up the transformers with configuration from assumptions |
crates/swc_ecma_transformer/src/lib.rs |
Updates hook chain to pass assumptions to ES2018 transformers |
crates/swc_ecma_transformer/src/hook_utils.rs |
Adds boilerplate hook methods for various statement types needed by the transformer |
crates/swc_ecma_preset_env/src/lib.rs |
Updates to enable object rest/spread via new transformer instead of old pass-based approach |
crates/swc_ecma_compat_es2018/src/object_rest_spread.rs |
Replaces implementation with thin adapter that creates Options and delegates to new transformer |
crates/swc_ecma_compat_es2018/src/object_rest.rs |
Deleted - functionality moved to transformer package |
crates/swc_ecma_compat_es2018/src/object_spread.rs |
Deleted - functionality moved to transformer package |
crates/swc_ecma_compat_es2018/src/lib.rs |
Removes deleted module references |
crates/swc_ecma_compat_es2018/Cargo.toml |
Adds dependency on swc_ecma_transformer |
Cargo.lock |
Updates lockfile with new dependency |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if !should_work::<PatternRestVisitor, _>(&var_decl.decls[0].name) { | ||
| return; | ||
| } | ||
|
|
||
| let ref_ident = private_ident!("_ref"); | ||
| let pat = var_decl.decls[0].name.take(); | ||
| var_decl.decls[0].name = ref_ident.clone().into(); |
Copilot
AI
Dec 8, 2025
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.
Potential panic: accessing var_decl.decls[0] without checking if the array is empty. While for-in/for-of statements typically require at least one declarator, defensive programming would suggest checking is_empty() or using get(0) to avoid panics in edge cases or malformed AST inputs.
| right: init, | ||
| } | ||
| .into(), | ||
| )); |
Copilot
AI
Dec 8, 2025
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.
The try_into() conversion can fail silently here. When a pattern cannot be converted to an AssignTarget, the assignment is dropped without any error or warning. This could lead to incorrect transformations where assignments are silently skipped.
Consider either:
- Logging or handling the error case
- Using
unwrap_or_elsewith a fallback (as done inpat_to_assign_target_paton line 1094) - Documenting why silent failures are acceptable in this context
| )); | |
| )); | |
| } else { | |
| eprintln!( | |
| "Warning: Failed to convert pattern to AssignTarget in ExprOutput::assign at {:?}", | |
| pat.span() | |
| ); |
| /// -> | ||
| /// ```JavaScript | ||
| /// function foo(a, _1, _2, ..._3) { | ||
| /// let [{ b, ...c}, d = b, e] = [_1, _2, _3]; |
Copilot
AI
Dec 8, 2025
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.
The documentation comment shows an incorrect transformation. The example states:
function foo(a, { b, ...c}, d = b, ...e) {}transforms to:
function foo(a, _1, _2, ..._3) {
let [{ b, ...c}, d = b, e] = [_1, _2, _3];
}However, the pattern destructuring for the rest parameter ...e should be ...e not just e in the array pattern. The correct output should be:
let [{ b, ...c}, d = b, ...e] = [_1, _2, _3];or without the array wrapper since rest parameters cannot be in array destructuring on the left side.
| /// let [{ b, ...c}, d = b, e] = [_1, _2, _3]; | |
| /// let [{ b, ...c}, d = b, ...e] = [_1, _2, _3]; |
Binary Sizes
Commit: 0449f16 |
CodSpeed Performance ReportMerging #11356 will degrade performances by 11.66%Comparing Summary
Benchmarks breakdown
Footnotes |
Summary
Implements ES2018 object rest spread transformation in the new
swc_ecma_transformerarchitecture.Changes
ObjectResttransformer for rest patterns in destructuringObjectSpreadtransformer for spread expressions in object literalsAssumptionsfor configuration options:object_rest_no_symbols: Use loose mode without symbol handlingset_spread_properties: UseObject.assign-like semanticspure_getters: Assume getters have no side effectsImplementation Details
The implementation follows the esbuild lowering algorithm and handles:
Test Plan
✅ All tests passing:
cargo test -p swc_ecma_compat_es2018(0 tests in this package)cargo test -p swc_ecma_transforms_compat --test es2018_object_rest_spread(72 passed, 6 ignored)🤖 Generated with Claude Code