Skip to content

Commit

Permalink
refactor(transformer/class-properties): share replace_class_name_with…
Browse files Browse the repository at this point in the history
…_temp_var in class_properties
  • Loading branch information
Dunqing committed Dec 25, 2024
1 parent f5381c6 commit ee6fb67
Showing 1 changed file with 30 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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`.
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit ee6fb67

Please sign in to comment.