From 0a38eea95c6fe4240043eb444e4cba7c4f62dbdd Mon Sep 17 00:00:00 2001 From: overlookmotel <557937+overlookmotel@users.noreply.github.com> Date: Thu, 19 Dec 2024 02:24:58 +0000 Subject: [PATCH] refactor(transformer/class-properties): use `temp_var_name_base` to generate 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. --- .../src/es2022/class_properties/supers.rs | 36 ++++++++++++------- .../static-super-update-expression/output.js | 2 +- 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/crates/oxc_transformer/src/es2022/class_properties/supers.rs b/crates/oxc_transformer/src/es2022/class_properties/supers.rs index b1243f496766a..88f0fa6723fb2 100644 --- a/crates/oxc_transformer/src/es2022/class_properties/supers.rs +++ b/crates/oxc_transformer/src/es2022/class_properties/supers.rs @@ -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; @@ -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 @@ -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`. @@ -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>, @@ -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; @@ -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)` diff --git a/tasks/transform_conformance/tests/babel-plugin-transform-class-properties/test/fixtures/static-super-update-expression/output.js b/tasks/transform_conformance/tests/babel-plugin-transform-class-properties/test/fixtures/static-super-update-expression/output.js index 7bebd5629e7eb..af97750decada 100644 --- a/tasks/transform_conformance/tests/babel-plugin-transform-class-properties/test/fixtures/static-super-update-expression/output.js +++ b/tasks/transform_conformance/tests/babel-plugin-transform-class-properties/test/fixtures/static-super-update-expression/output.js @@ -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";