From fee4053fa6532345655e475f61e34f06e293eb1d Mon Sep 17 00:00:00 2001 From: Dunqing Date: Wed, 25 Dec 2024 16:50:50 +0800 Subject: [PATCH] refactor(transformer/class-properties): share replace_class_name_with_temp_var in class_properties --- .../static_block_and_prop_init.rs | 56 ++++++++++--------- 1 file changed, 30 insertions(+), 26 deletions(-) diff --git a/crates/oxc_transformer/src/es2022/class_properties/static_block_and_prop_init.rs b/crates/oxc_transformer/src/es2022/class_properties/static_block_and_prop_init.rs index 32b145383a33b..89c353e672b14 100644 --- a/crates/oxc_transformer/src/es2022/class_properties/static_block_and_prop_init.rs +++ b/crates/oxc_transformer/src/es2022/class_properties/static_block_and_prop_init.rs @@ -107,6 +107,35 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> { ctx.ast.move_expression(expr) } + + /// Replace reference to class name with reference to temp var for class. + fn replace_class_name_with_temp_var( + &mut self, + ident: &mut IdentifierReference<'a>, + ctx: &mut TraverseCtx<'a>, + ) { + // Check identifier is reference to class name + let class_details = self.current_class_mut(); + let class_name_symbol_id = class_details.bindings.name_symbol_id(); + let Some(class_name_symbol_id) = class_name_symbol_id else { return }; + + let reference_id = ident.reference_id(); + let reference = ctx.symbols().get_reference(reference_id); + let Some(symbol_id) = reference.symbol_id() else { return }; + + if symbol_id != class_name_symbol_id { + return; + } + + // Identifier is reference to class name. Rename it. + let temp_binding = class_details.bindings.get_or_init_static_binding(ctx); + ident.name = temp_binding.name.clone(); + + let symbols = ctx.symbols_mut(); + symbols.get_reference_mut(reference_id).set_symbol_id(temp_binding.symbol_id); + symbols.delete_resolved_reference(symbol_id, reference_id); + symbols.add_resolved_reference(temp_binding.symbol_id, reference_id); + } } /// Visitor to transform: @@ -268,7 +297,7 @@ impl<'a, 'ctx, 'v> VisitMut<'a> for StaticVisitor<'a, 'ctx, 'v> { /// Transform reference to class name to temp var fn visit_identifier_reference(&mut self, ident: &mut IdentifierReference<'a>) { - self.replace_class_name_with_temp_var(ident); + self.super_converter.class_properties.replace_class_name_with_temp_var(ident, self.ctx); } /// Convert scope to sloppy mode if `self.make_sloppy_mode == true`. @@ -498,31 +527,6 @@ impl<'a, 'ctx, 'v> StaticVisitor<'a, 'ctx, 'v> { } } - /// Replace reference to class name with reference to temp var for class. - fn replace_class_name_with_temp_var(&mut self, ident: &mut IdentifierReference<'a>) { - // Check identifier is reference to class name - let class_details = self.super_converter.class_properties.current_class_mut(); - let class_name_symbol_id = class_details.bindings.name_symbol_id(); - let Some(class_name_symbol_id) = class_name_symbol_id else { return }; - - let reference_id = ident.reference_id(); - let reference = self.ctx.symbols().get_reference(reference_id); - let Some(symbol_id) = reference.symbol_id() else { return }; - - if symbol_id != class_name_symbol_id { - return; - } - - // Identifier is reference to class name. Rename it. - let temp_binding = class_details.bindings.get_or_init_static_binding(self.ctx); - ident.name = temp_binding.name.clone(); - - let symbols = self.ctx.symbols_mut(); - symbols.get_reference_mut(reference_id).set_symbol_id(temp_binding.symbol_id); - symbols.delete_resolved_reference(symbol_id, reference_id); - symbols.add_resolved_reference(temp_binding.symbol_id, reference_id); - } - /// Replace `delete this` with `true`. fn replace_delete_this_with_true(&self, expr: &mut Expression<'a>, span: Span) { if self.this_depth == 0 {