Skip to content

Commit

Permalink
u
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Nov 21, 2024
1 parent a47d829 commit 5c765b0
Showing 1 changed file with 40 additions and 16 deletions.
56 changes: 40 additions & 16 deletions crates/oxc_transformer/src/es2018/object_rest_spread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,21 @@ impl<'a, 'ctx> Traverse<'a> for ObjectRestSpread<'a, 'ctx> {
// `({ x, ..y })`.
// `({ x, ..y } = foo)`.
// `([{ x, ..y }] = foo)`.
#[inline]
fn enter_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
Self::transform_object_expression(self.options, expr, self.ctx, ctx);
self.transform_assignment_expression(expr, ctx);
match expr {
Expression::ObjectExpression(_) => {
Self::transform_object_expression(self.options, expr, self.ctx, ctx);
}
Expression::AssignmentExpression(_) => {
self.transform_assignment_expression(expr, ctx);
}
_ => {}
}
}

// `(...x) => {}`.
#[inline]
fn enter_arrow_function_expression(
&mut self,
arrow: &mut ArrowFunctionExpression<'a>,
Expand All @@ -114,13 +123,15 @@ impl<'a, 'ctx> Traverse<'a> for ObjectRestSpread<'a, 'ctx> {
}

// `function foo({...x}) {}`.
#[inline]
fn enter_function(&mut self, func: &mut Function<'a>, ctx: &mut TraverseCtx<'a>) {
Self::transform_function(func, ctx);
}

// `let { x, ..y } = foo`.
// `let [{ x, ..y }] = foo`
// Includes `for (var {...x} = 1;;);`
#[inline]
fn enter_variable_declaration(
&mut self,
decl: &mut VariableDeclaration<'a>,
Expand All @@ -130,22 +141,43 @@ impl<'a, 'ctx> Traverse<'a> for ObjectRestSpread<'a, 'ctx> {
}

// Transform `try {} catch (...x) {}`.
#[inline]
fn enter_catch_clause(&mut self, clause: &mut CatchClause<'a>, ctx: &mut TraverseCtx<'a>) {
Self::transform_catch_clause(clause, ctx);
if clause.param.is_some() {
Self::transform_catch_clause(clause, ctx);
}
}

// `for ({...x} in []);` `for ({...x} of []);`
// `for ([{...x}] in []);` `for ([{...x}] of []);`
#[inline]
fn enter_for_in_statement(&mut self, stmt: &mut ForInStatement<'a>, ctx: &mut TraverseCtx<'a>) {
let scope_id = stmt.scope_id();
Self::transform_for_statement_left(scope_id, &mut stmt.left, &mut stmt.body, ctx);
match &mut stmt.left {
ForStatementLeft::VariableDeclaration(decl) => {
let body = &mut stmt.body;
Self::transform_variable_declaration_for_x_statement(decl, body, scope_id, ctx);
}
_ => {
Self::transform_for_statement_left(scope_id, &mut stmt.left, &mut stmt.body, ctx);
}
}
}

// `for ({...x} in []);` `for ({...x} of []);`
// `for ([{...x}] in []);` `for ([{...x}] of []);`
#[inline]
fn enter_for_of_statement(&mut self, stmt: &mut ForOfStatement<'a>, ctx: &mut TraverseCtx<'a>) {
let scope_id = stmt.scope_id();
Self::transform_for_statement_left(scope_id, &mut stmt.left, &mut stmt.body, ctx);
match &mut stmt.left {
ForStatementLeft::VariableDeclaration(decl) => {
let body = &mut stmt.body;
Self::transform_variable_declaration_for_x_statement(decl, body, scope_id, ctx);
}
_ => {
Self::transform_for_statement_left(scope_id, &mut stmt.left, &mut stmt.body, ctx);
}
}
}
}

Expand All @@ -157,9 +189,7 @@ impl<'a, 'ctx> ObjectRestSpread<'a, 'ctx> {
expr: &mut Expression<'a>,
ctx: &mut TraverseCtx<'a>,
) {
let Expression::AssignmentExpression(assign_expr) = expr else {
return;
};
let Expression::AssignmentExpression(assign_expr) = expr else { unreachable!() };
// Allow `{...x} = {}` and `[{...x}] = []`.
if !Self::has_nested_target_rest(&assign_expr.left) {
return;
Expand Down Expand Up @@ -455,9 +485,7 @@ impl<'a, 'ctx> ObjectRestSpread<'a, 'ctx> {
transform_ctx: &'ctx TransformCtx<'a>,
ctx: &mut TraverseCtx<'a>,
) {
let Expression::ObjectExpression(obj_expr) = expr else {
return;
};
let Expression::ObjectExpression(obj_expr) = expr else { unreachable!() };

if obj_expr.properties.iter().all(|prop| !prop.is_spread()) {
return;
Expand Down Expand Up @@ -551,7 +579,7 @@ impl<'a, 'ctx> ObjectRestSpread<'a, 'ctx> {

// Transform `try {} catch (...x) {}`.
fn transform_catch_clause(clause: &mut CatchClause<'a>, ctx: &mut TraverseCtx<'a>) {
let Some(param) = &mut clause.param else { return };
let Some(param) = &mut clause.param else { unreachable!() };
if Self::has_nested_object_rest(&param.pattern) {
let scope_id = clause.body.scope_id();
// Remove `SymbolFlags::CatchVariable`.
Expand Down Expand Up @@ -617,10 +645,6 @@ impl<'a, 'ctx> ObjectRestSpread<'a, 'ctx> {
body: &mut Statement<'a>,
ctx: &mut TraverseCtx<'a>,
) {
if let ForStatementLeft::VariableDeclaration(decl) = left {
Self::transform_variable_declaration_for_x_statement(decl, body, scope_id, ctx);
return;
}
if !Self::has_nested_target_rest(left.to_assignment_target()) {
return;
}
Expand Down

0 comments on commit 5c765b0

Please sign in to comment.