Skip to content

Commit

Permalink
refactor(transformer/class-properties): use temp_var_name_base to g…
Browse files Browse the repository at this point in the history
…enerate temp var names for `super` transform
  • Loading branch information
overlookmotel committed Dec 18, 2024
1 parent fab198b commit 1784c5e
Showing 1 changed file with 24 additions and 12 deletions.
36 changes: 24 additions & 12 deletions crates/oxc_transformer/src/es2022/class_properties/supers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use oxc_allocator::{Box as ArenaBox, Vec as ArenaVec};
use oxc_ast::ast::*;
use oxc_span::SPAN;
use oxc_traverse::{BoundIdentifier, TraverseCtx};
use oxc_traverse::{ast_operations::get_var_name_from_node, TraverseCtx};

use crate::Helper;

Expand Down Expand Up @@ -352,15 +352,20 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> {
unreachable!()
};

let temp_binding =
self.ctx.var_declarations.create_uid_var_based_on_node(member.as_ref(), ctx);
let temp_var_name_base = get_var_name_from_node(member.as_ref());

let property = ctx.ast.expression_string_literal(
member.property.span,
member.property.name.clone(),
None,
);
*expr =
self.transform_super_update_expression_impl(&temp_binding, update_expr, property, ctx);

*expr = self.transform_super_update_expression_impl(
&temp_var_name_base,
update_expr,
property,
ctx,
);
}

/// Transform update expression (`++` or `--`) where argument is a computed member expression
Expand Down Expand Up @@ -412,11 +417,17 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> {
else {
unreachable!()
};
let temp_binding =
self.ctx.var_declarations.create_uid_var_based_on_node(member.as_ref(), ctx);

let temp_var_name_base = get_var_name_from_node(member.as_ref());

let property = ctx.ast.move_expression(&mut member.expression);
*expr =
self.transform_super_update_expression_impl(&temp_binding, update_expr, property, ctx);

*expr = self.transform_super_update_expression_impl(
&temp_var_name_base,
update_expr,
property,
ctx,
);
}

/// Transform update expression (`++` or `--`) where argument is a member expression with `super`.
Expand Down Expand Up @@ -454,7 +465,7 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> {
/// ```
fn transform_super_update_expression_impl(
&mut self,
temp_binding: &BoundIdentifier<'a>,
temp_var_name_base: &str,
mut update_expr: ArenaBox<'a, UpdateExpression<'a>>,
property: Expression<'a>,
ctx: &mut TraverseCtx<'a>,
Expand All @@ -466,7 +477,8 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> {
let get_call = self.create_super_prop_get(SPAN, property2, false, ctx);

// `_super$prop = _superPropGet(_Class, prop, _Class)`
let assignment = create_assignment(temp_binding, get_call, ctx);
let temp_binding = self.ctx.var_declarations.create_uid_var(temp_var_name_base, ctx);
let assignment = create_assignment(&temp_binding, get_call, ctx);

// `++_super$prop` / `_super$prop++` (reusing existing `UpdateExpression`)
let span = update_expr.span;
Expand All @@ -486,7 +498,7 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> {
} else {
// Source = `super.prop++` (postfix `++`)
// `_super$prop2 = _super$prop++`
let temp_binding2 = self.ctx.var_declarations.create_uid_var(&temp_binding.name, ctx);
let temp_binding2 = self.ctx.var_declarations.create_uid_var(temp_var_name_base, ctx);
let assignment2 = create_assignment(&temp_binding2, update_expr, ctx);

// `(_super$prop = _superPropGet(_Class, prop, _Class), _super$prop2 = _super$prop++, _super$prop)`
Expand Down

0 comments on commit 1784c5e

Please sign in to comment.