Skip to content

Commit

Permalink
refactor(transformer/exponentiation-operator): use built-in ctx.clone…
Browse files Browse the repository at this point in the history
…_identifier_reference
  • Loading branch information
Dunqing authored and overlookmotel committed Aug 22, 2024
1 parent f2846f7 commit 09bdc81
Showing 1 changed file with 12 additions and 21 deletions.
33 changes: 12 additions & 21 deletions crates/oxc_transformer/src/es2016/exponentiation_operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,21 +127,11 @@ impl<'a> Traverse<'a> for ExponentiationOperator<'a> {
}

impl<'a> ExponentiationOperator<'a> {
fn clone_identifier_reference(
ident: &IdentifierReference<'a>,
ctx: &mut TraverseCtx<'a>,
) -> IdentifierReference<'a> {
let reference = ctx.symbols().get_reference(ident.reference_id.get().unwrap());
let symbol_id = reference.symbol_id();
let flags = reference.flags();
ctx.create_reference_id(ident.span, ident.name.clone(), symbol_id, flags)
}

fn clone_expression(expr: &Expression<'a>, ctx: &mut TraverseCtx<'a>) -> Expression<'a> {
match expr {
Expression::Identifier(ident) => ctx
.ast
.expression_from_identifier_reference(Self::clone_identifier_reference(ident, ctx)),
Expression::Identifier(ident) => ctx.ast.expression_from_identifier_reference(
ctx.clone_identifier_reference(ident, ReferenceFlags::Read),
),
_ => expr.clone_in(ctx.ast.allocator),
}
}
Expand Down Expand Up @@ -184,9 +174,10 @@ impl<'a> ExponentiationOperator<'a> {
let obj = self.get_obj_ref(node, nodes, ctx)?;
let (reference, uid) = match node {
SimpleAssignmentTarget::AssignmentTargetIdentifier(ident) => {
let reference = AssignmentTarget::AssignmentTargetIdentifier(
ctx.ast.alloc(Self::clone_identifier_reference(ident.as_ref(), ctx)),
);
let reference =
AssignmentTarget::AssignmentTargetIdentifier(ctx.ast.alloc(
ctx.clone_identifier_reference(ident.as_ref(), ReferenceFlags::Write),
));
(reference, obj)
}
match_member_expression!(SimpleAssignmentTarget) => {
Expand Down Expand Up @@ -250,13 +241,13 @@ impl<'a> ExponentiationOperator<'a> {
// that evaluating it multiple times won't trigger a getter
// or something else
return Some(ctx.ast.expression_from_identifier_reference(
Self::clone_identifier_reference(ident, ctx),
ctx.clone_identifier_reference(ident, ReferenceFlags::Write),
));
}
// could possibly trigger a getter so we need to only evaluate it once
ctx.ast.expression_from_identifier_reference(Self::clone_identifier_reference(
ident, ctx,
))
ctx.ast.expression_from_identifier_reference(
ctx.clone_identifier_reference(ident, ReferenceFlags::Read),
)
}
match_member_expression!(SimpleAssignmentTarget) => {
let expr = match node {
Expand Down Expand Up @@ -349,7 +340,7 @@ impl<'a> ExponentiationOperator<'a> {
// let ident = self.create_new_var_with_expression(&expr);
// Add new reference `_name = name` to nodes
let left = ctx.ast.simple_assignment_target_from_identifier_reference(
Self::clone_identifier_reference(&ident, ctx),
ctx.clone_identifier_reference(&ident, ReferenceFlags::Write),
);
let op = AssignmentOperator::Assign;
nodes.push(ctx.ast.expression_assignment(SPAN, op, AssignmentTarget::from(left), expr));
Expand Down

0 comments on commit 09bdc81

Please sign in to comment.