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 (#8004)

Follow-on after #7997.

Generate "base name" for temp var using `temp_var_name_base` and then create the 2 temp bindings from it.

This is a bit more efficient than creating 2nd temp binding from name of the first temp binding, because the first binding's name has `_` added to start, and may have digits on the end, which have to be trimmed off again. Whereas the "base name" is ready to go.

Incidentally, changing the timing of when temp bindings are created also aligns output with Babel.
  • Loading branch information
overlookmotel committed Dec 19, 2024
1 parent 63a95e4 commit 0a38eea
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var _super$A, _super$A2, _super$A3, _super$A4, _super$A5, _super$A6, _super$bound, _super$bound2, _super$bound3, _super$bound4, _super$bound5, _super$bound6, _super$unbound, _unbound, _super$unbound2, _super$unbound3, _unbound2, _super$unbound4, _super$unbound5, _unbound3, _super$unbound6, _unbound4, _Outer;
var _super$A, _super$A2, _super$A3, _super$A4, _super$A5, _super$A6, _super$bound, _super$bound2, _super$bound3, _super$bound4, _super$bound5, _super$bound6, _unbound, _super$unbound, _super$unbound2, _unbound2, _super$unbound3, _super$unbound4, _unbound3, _super$unbound5, _unbound4, _super$unbound6, _Outer;

let bound = "A";

Expand Down

0 comments on commit 0a38eea

Please sign in to comment.