Skip to content

Commit

Permalink
handle delete chain expression within FormalParameter
Browse files Browse the repository at this point in the history
  • Loading branch information
Dunqing committed Nov 18, 2024
1 parent 3af9834 commit b895dce
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions crates/oxc_transformer/src/es2020/optional_chaining.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub struct OptionalChaining<'a, 'ctx> {
ctx: &'ctx TransformCtx<'a>,

// states
is_function_parameter: bool,
is_inside_function_parameter: bool,
temp_binding: Option<BoundIdentifier<'a>>,
context: Option<BoundIdentifier<'a>>,
check_mode: CheckMode,
Expand All @@ -31,7 +31,7 @@ impl<'a, 'ctx> OptionalChaining<'a, 'ctx> {
pub fn new(ctx: &'ctx TransformCtx<'a>) -> Self {
Self {
ctx,
is_function_parameter: false,
is_inside_function_parameter: false,
temp_binding: None,
context: None,
check_mode: CheckMode::Void0,
Expand All @@ -44,15 +44,15 @@ impl<'a, 'ctx> Traverse<'a> for OptionalChaining<'a, 'ctx> {
}

fn enter_formal_parameters(&mut self, _: &mut FormalParameters<'a>, _: &mut TraverseCtx<'a>) {
self.is_function_parameter = true;
self.is_inside_function_parameter = true;
}

fn exit_formal_parameters(
&mut self,
_node: &mut FormalParameters<'a>,
_ctx: &mut TraverseCtx<'a>,
) {
self.is_function_parameter = false;
self.is_inside_function_parameter = false;
}
}

Expand Down Expand Up @@ -258,7 +258,7 @@ impl<'a, 'ctx> OptionalChaining<'a, 'ctx> {
fn transform_chain_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
let (is_delete, chain_expr) = match expr {
Expression::ChainExpression(_) => {
if self.is_function_parameter {
if self.is_inside_function_parameter {
// To insert the temp binding in correct scope, we wrap the expression with an arrow function,
// during the chain expression transformation, the temp binding will be inserted to the arrow function's body
*expr = Self::wrap_arrow_function(expr, ctx);
Expand All @@ -272,6 +272,12 @@ impl<'a, 'ctx> OptionalChaining<'a, 'ctx> {
let Expression::ChainExpression(_) = unary_expr.argument else {
return;
};
if self.is_inside_function_parameter {
// To insert the temp binding in correct scope, we wrap the expression with an arrow function,
// during the chain expression transformation, the temp binding will be inserted to the arrow function's body
*expr = Self::wrap_arrow_function(expr, ctx);
return;
}
(true, ctx.ast.move_expression(&mut unary_expr.argument))
}
_ => return,
Expand Down

0 comments on commit b895dce

Please sign in to comment.