Skip to content

Commit

Permalink
refactor(traverse)!: rename TraverseCtx methods for creating `Ident…
Browse files Browse the repository at this point in the history
…ifierReference`s (#7300)

`create_reference_id` was a confusing name, as it doesn't return a `ReferenceId`. Rename these methods to more clearly represent what they do.
  • Loading branch information
overlookmotel committed Nov 15, 2024
1 parent a0766e6 commit 1cbc624
Show file tree
Hide file tree
Showing 12 changed files with 67 additions and 54 deletions.
8 changes: 6 additions & 2 deletions crates/oxc_transformer/src/common/helper_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,8 +277,12 @@ impl<'a> HelperLoaderStore<'a> {
static HELPER_VAR: &str = "babelHelpers";

let symbol_id = ctx.scopes().find_binding(ctx.current_scope_id(), HELPER_VAR);
let ident =
ctx.create_reference_id(SPAN, Atom::from(HELPER_VAR), symbol_id, ReferenceFlags::Read);
let ident = ctx.create_ident_reference(
SPAN,
Atom::from(HELPER_VAR),
symbol_id,
ReferenceFlags::Read,
);
let object = Expression::Identifier(ctx.alloc(ident));
let property = ctx.ast.identifier_name(SPAN, Atom::from(helper.name()));
Expression::from(ctx.ast.member_expression_static(SPAN, object, property, false))
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_transformer/src/common/module_imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ impl<'a> ModuleImportsStore<'a> {
ctx: &mut TraverseCtx<'a>,
) -> Statement<'a> {
let var_kind = VariableDeclarationKind::Var;
let ident = ctx.create_reference_id(
let ident = ctx.create_ident_reference(
SPAN,
Atom::from("require"),
require_symbol_id,
Expand Down
69 changes: 37 additions & 32 deletions crates/oxc_transformer/src/es2016/exponentiation_operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,31 +155,30 @@ impl<'a, 'ctx> ExponentiationOperator<'a, 'ctx> {

// Make sure side-effects of evaluating `left` only happen once
let reference = ctx.scoping.symbols_mut().get_reference_mut(ident.reference_id());
let pow_left =
if let Some(symbol_id) = reference.symbol_id() {
// This variable is declared in scope so evaluating it multiple times can't trigger a getter.
// No need for a temp var.
// `left **= right` is being transformed to `left = Math.pow(left, right)`,
// so if `left` is no longer being read from, update its `ReferenceFlags`.
if matches!(ctx.ancestry.parent(), Ancestor::ExpressionStatementExpression(_)) {
*reference.flags_mut() = ReferenceFlags::Write;
}
let pow_left = if let Some(symbol_id) = reference.symbol_id() {
// This variable is declared in scope so evaluating it multiple times can't trigger a getter.
// No need for a temp var.
// `left **= right` is being transformed to `left = Math.pow(left, right)`,
// so if `left` is no longer being read from, update its `ReferenceFlags`.
if matches!(ctx.ancestry.parent(), Ancestor::ExpressionStatementExpression(_)) {
*reference.flags_mut() = ReferenceFlags::Write;
}

Expression::Identifier(ctx.ast.alloc(ctx.create_bound_reference_id(
SPAN,
ident.name.clone(),
symbol_id,
ReferenceFlags::Read,
)))
} else {
// Unbound reference. Could possibly trigger a getter so we need to only evaluate it once.
// Assign to a temp var.
let reference = Expression::Identifier(ctx.ast.alloc(
ctx.create_unbound_reference_id(SPAN, ident.name.clone(), ReferenceFlags::Read),
));
let binding = self.create_temp_var(reference, &mut temp_var_inits, ctx);
binding.create_read_expression(ctx)
};
Expression::Identifier(ctx.ast.alloc(ctx.create_bound_ident_reference(
SPAN,
ident.name.clone(),
symbol_id,
ReferenceFlags::Read,
)))
} else {
// Unbound reference. Could possibly trigger a getter so we need to only evaluate it once.
// Assign to a temp var.
let reference = Expression::Identifier(ctx.ast.alloc(
ctx.create_unbound_ident_reference(SPAN, ident.name.clone(), ReferenceFlags::Read),
));
let binding = self.create_temp_var(reference, &mut temp_var_inits, ctx);
binding.create_read_expression(ctx)
};

(pow_left, temp_var_inits)
}
Expand Down Expand Up @@ -496,12 +495,14 @@ impl<'a, 'ctx> ExponentiationOperator<'a, 'ctx> {
if let Some(symbol_id) = symbol_id {
// This variable is declared in scope so evaluating it multiple times can't trigger a getter.
// No need for a temp var.
return Expression::Identifier(ctx.ast.alloc(ctx.create_bound_reference_id(
SPAN,
ident.name.clone(),
symbol_id,
ReferenceFlags::Read,
)));
return Expression::Identifier(ctx.ast.alloc(
ctx.create_bound_ident_reference(
SPAN,
ident.name.clone(),
symbol_id,
ReferenceFlags::Read,
),
));
}
// Unbound reference. Could possibly trigger a getter so we need to only evaluate it once.
// Assign to a temp var.
Expand Down Expand Up @@ -547,8 +548,12 @@ impl<'a, 'ctx> ExponentiationOperator<'a, 'ctx> {
ctx: &mut TraverseCtx<'a>,
) -> Expression<'a> {
let math_symbol_id = ctx.scopes().find_binding(ctx.current_scope_id(), "Math");
let ident_math =
ctx.create_reference_id(SPAN, Atom::from("Math"), math_symbol_id, ReferenceFlags::Read);
let ident_math = ctx.create_ident_reference(
SPAN,
Atom::from("Math"),
math_symbol_id,
ReferenceFlags::Read,
);
let object = Expression::Identifier(ctx.alloc(ident_math));
let property = ctx.ast.identifier_name(SPAN, "pow");
let callee =
Expand Down
10 changes: 7 additions & 3 deletions crates/oxc_transformer/src/es2017/async_to_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ impl<'a, 'ctx> AsyncGeneratorExecutor<'a, 'ctx> {
let id = caller_function.id.as_ref().unwrap();
// If the function has an id, then we need to return the id.
// `function foo() { ... }` -> `function foo() {} return foo;`
let reference = ctx.create_bound_reference_id(
let reference = ctx.create_bound_ident_reference(
SPAN,
id.name.clone(),
id.symbol_id(),
Expand Down Expand Up @@ -597,8 +597,12 @@ impl<'a, 'ctx> AsyncGeneratorExecutor<'a, 'ctx> {
ctx: &mut TraverseCtx<'a>,
) -> Statement<'a> {
let symbol_id = ctx.scopes().find_binding(ctx.current_scope_id(), "arguments");
let arguments_ident =
ctx.create_reference_id(SPAN, Atom::from("arguments"), symbol_id, ReferenceFlags::Read);
let arguments_ident = ctx.create_ident_reference(
SPAN,
Atom::from("arguments"),
symbol_id,
ReferenceFlags::Read,
);
let arguments_ident = Argument::Identifier(ctx.alloc(arguments_ident));

// (this, arguments)
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_transformer/src/es2018/object_rest_spread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ impl<'a, 'ctx> ObjectRestSpread<'a, 'ctx> {

fn object_assign(symbol_id: Option<SymbolId>, ctx: &mut TraverseCtx<'a>) -> Expression<'a> {
let ident =
ctx.create_reference_id(SPAN, Atom::from("Object"), symbol_id, ReferenceFlags::Read);
ctx.create_ident_reference(SPAN, Atom::from("Object"), symbol_id, ReferenceFlags::Read);
let object = Expression::Identifier(ctx.alloc(ident));
let property = ctx.ast.identifier_name(SPAN, Atom::from("assign"));
Expression::from(ctx.ast.member_expression_static(SPAN, object, property, false))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ impl<'a, 'ctx> LogicalAssignmentOperators<'a, 'ctx> {
let symbol_id = reference.symbol_id();
let left_expr = Expression::Identifier(ctx.alloc(ident.clone()));

let ident = ctx.create_reference_id(
let ident = ctx.create_ident_reference(
SPAN,
ident.name.clone(),
symbol_id,
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_transformer/src/jsx/refresh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ impl<'a, 'ctx> Traverse<'a> for ReactRefresh<'a, 'ctx> {
binding_name.as_str(),
)
.map(|symbol_id| {
let ident = ctx.create_bound_reference_id(
let ident = ctx.create_bound_ident_reference(
SPAN,
binding_name,
symbol_id,
Expand Down Expand Up @@ -496,7 +496,7 @@ impl<'a, 'ctx> ReactRefresh<'a, 'ctx> {
ctx: &mut TraverseCtx<'a>,
) -> Statement<'a> {
let left = self.create_registration(id.name.clone(), ReferenceFlags::Write, ctx);
let right = ctx.create_bound_reference_id(
let right = ctx.create_bound_ident_reference(
SPAN,
id.name.clone(),
id.symbol_id(),
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_transformer/src/regexp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ impl<'a, 'ctx> Traverse<'a> for RegExp<'a, 'ctx> {

let callee = {
let symbol_id = ctx.scopes().find_binding(ctx.current_scope_id(), "RegExp");
let ident = ctx.create_reference_id(
let ident = ctx.create_ident_reference(
SPAN,
Atom::from("RegExp"),
symbol_id,
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_transformer/src/typescript/enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ impl<'a> TypeScriptEnum<'a> {
} else {
// }(Foo || {});
let op = LogicalOperator::Or;
let left = ctx.create_bound_reference_id(
let left = ctx.create_bound_ident_reference(
decl.id.span,
enum_name.clone(),
var_symbol_id,
Expand All @@ -138,7 +138,7 @@ impl<'a> TypeScriptEnum<'a> {

if is_already_declared {
let op = AssignmentOperator::Assign;
let left = ctx.create_bound_reference_id(
let left = ctx.create_bound_ident_reference(
decl.id.span,
enum_name.clone(),
var_symbol_id,
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_traverse/src/context/bound_identifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ impl<'a> BoundIdentifier<'a> {
flags: ReferenceFlags,
ctx: &mut TraverseCtx<'a>,
) -> IdentifierReference<'a> {
ctx.create_bound_reference_id(span, self.name.clone(), self.symbol_id, flags)
ctx.create_bound_ident_reference(span, self.name.clone(), self.symbol_id, flags)
}

/// Create `Expression::Identifier` referencing this binding, with specified `Span` and `ReferenceFlags`
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_traverse/src/context/maybe_bound_identifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ impl<'a> MaybeBoundIdentifier<'a> {
flags: ReferenceFlags,
ctx: &mut TraverseCtx<'a>,
) -> IdentifierReference<'a> {
ctx.create_reference_id(span, self.name.clone(), self.symbol_id, flags)
ctx.create_ident_reference(span, self.name.clone(), self.symbol_id, flags)
}

/// Create `Expression::Identifier` referencing this binding, with specified `Span` and `ReferenceFlags`
Expand Down
14 changes: 7 additions & 7 deletions crates/oxc_traverse/src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ impl<'a> TraverseCtx<'a> {
}

/// Create an `IdentifierReference` bound to a `SymbolId`.
pub fn create_bound_reference_id(
pub fn create_bound_ident_reference(
&mut self,
span: Span,
name: Atom<'a>,
Expand All @@ -458,7 +458,7 @@ impl<'a> TraverseCtx<'a> {
}

/// Create an unbound `IdentifierReference`.
pub fn create_unbound_reference_id(
pub fn create_unbound_ident_reference(
&mut self,
span: Span,
name: Atom<'a>,
Expand Down Expand Up @@ -486,19 +486,19 @@ impl<'a> TraverseCtx<'a> {

/// Create an `IdentifierReference` optionally bound to a `SymbolId`.
///
/// If you know if there's a `SymbolId` or not, prefer `TraverseCtx::create_bound_reference_id`
/// or `TraverseCtx::create_unbound_reference_id`.
pub fn create_reference_id(
/// If you know if there's a `SymbolId` or not, prefer `TraverseCtx::create_bound_ident_reference`
/// or `TraverseCtx::create_unbound_ident_reference`.
pub fn create_ident_reference(
&mut self,
span: Span,
name: Atom<'a>,
symbol_id: Option<SymbolId>,
flags: ReferenceFlags,
) -> IdentifierReference<'a> {
if let Some(symbol_id) = symbol_id {
self.create_bound_reference_id(span, name, symbol_id, flags)
self.create_bound_ident_reference(span, name, symbol_id, flags)
} else {
self.create_unbound_reference_id(span, name, flags)
self.create_unbound_ident_reference(span, name, flags)
}
}

Expand Down

0 comments on commit 1cbc624

Please sign in to comment.