Skip to content

Commit

Permalink
w
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Nov 13, 2024
1 parent 629dcba commit c1e07ab
Showing 1 changed file with 30 additions and 14 deletions.
44 changes: 30 additions & 14 deletions crates/oxc_transformer/src/es2018/object_rest_spread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use oxc_allocator::{CloneIn, Vec as ArenaVec};
use oxc_ast::{ast::*, NONE};
use oxc_diagnostics::OxcDiagnostic;
use oxc_semantic::{ReferenceFlags, SymbolFlags, SymbolId};
use oxc_span::SPAN;
use oxc_span::{GetSpan, SPAN};
use oxc_traverse::{Traverse, TraverseCtx};

use crate::{common::helper_loader::Helper, TransformCtx};
Expand Down Expand Up @@ -271,6 +271,32 @@ impl<'a, 'ctx> ObjectRestSpread<'a, 'ctx> {

// Insert rest access.
for datum in data {
let mut arg = if let Some(bound_identifier) = &temp_bound_identifier {
bound_identifier.create_read_expression(ctx)
} else {
// FIXME: remove clone
init.clone_in(ctx.ast.allocator)
};

for property_key in datum.path.into_iter().rev() {
let span = property_key.span();
match property_key {
PropertyKey::StaticIdentifier(ident) => {
let property = ctx.ast.identifier_name(ident.span, ident.name.clone());
arg = Expression::StaticMemberExpression(
ctx.ast.alloc_static_member_expression(span, arg, property, false),
);
}
PropertyKey::PrivateIdentifier(_) => continue,
_ => {
let expr = property_key.into_expression();
arg = Expression::ComputedMemberExpression(
ctx.ast.alloc_computed_member_expression(span, arg, expr, false),
)
}
}
}

let rhs = if datum.has_no_properties {
// `let { ...a } = z` -> `_extends({}, (_objectDestructuringEmpty(z), z));`
// The `ObjectDestructuringEmpty` function throws a type error when destructuring null.
Expand All @@ -289,28 +315,18 @@ impl<'a, 'ctx> ObjectRestSpread<'a, 'ctx> {
let mut sequence = ctx.ast.vec();
sequence.push(self.ctx.helper_call_expr(
Helper::ObjectDestructuringEmpty,
// FIXME: create a new reference instead of clone.
ctx.ast.vec(),
// ctx.ast.vec1(Argument::from(rhs.clone_in(ctx.ast.allocator))),
ctx.ast.vec1(Argument::from(arg.clone_in(ctx.ast.allocator))),
ctx,
));
// sequence.push(rhs);
sequence.push(arg);
sequence
},
)));
self.ctx.helper_call_expr(Helper::Extends, arguments, ctx)
} else {
// / `let { a, b, ...c } = z` -> _objectWithoutProperties(_z, ["a", "b"]);
// / `_objectWithoutProperties(_z, ["a", "b"])`
let mut arguments = ctx.ast.vec();
// Add `_z`.
let arg = if let Some(bound_identifier) = &temp_bound_identifier {
bound_identifier.create_read_expression(ctx)
} else {
// FIXME: remove clone
init.clone_in(ctx.ast.allocator)
};
arguments.push(Argument::from(arg));
let mut arguments = ctx.ast.vec1(Argument::from(arg));

let mut key_expression = ctx.ast.expression_array(SPAN, datum.keys, None);

Expand Down

0 comments on commit c1e07ab

Please sign in to comment.