mirror of
https://github.com/danbulant/oxc
synced 2026-05-25 12:51:57 +00:00
refactor(traverse)!: rename TraverseCtx methods for creating IdentifierReferences (#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.
This commit is contained in:
parent
a0766e6a7f
commit
1cbc624a8b
12 changed files with 67 additions and 54 deletions
|
|
@ -277,8 +277,12 @@ impl<'a> HelperLoaderStore<'a> {
|
||||||
static HELPER_VAR: &str = "babelHelpers";
|
static HELPER_VAR: &str = "babelHelpers";
|
||||||
|
|
||||||
let symbol_id = ctx.scopes().find_binding(ctx.current_scope_id(), HELPER_VAR);
|
let symbol_id = ctx.scopes().find_binding(ctx.current_scope_id(), HELPER_VAR);
|
||||||
let ident =
|
let ident = ctx.create_ident_reference(
|
||||||
ctx.create_reference_id(SPAN, Atom::from(HELPER_VAR), symbol_id, ReferenceFlags::Read);
|
SPAN,
|
||||||
|
Atom::from(HELPER_VAR),
|
||||||
|
symbol_id,
|
||||||
|
ReferenceFlags::Read,
|
||||||
|
);
|
||||||
let object = Expression::Identifier(ctx.alloc(ident));
|
let object = Expression::Identifier(ctx.alloc(ident));
|
||||||
let property = ctx.ast.identifier_name(SPAN, Atom::from(helper.name()));
|
let property = ctx.ast.identifier_name(SPAN, Atom::from(helper.name()));
|
||||||
Expression::from(ctx.ast.member_expression_static(SPAN, object, property, false))
|
Expression::from(ctx.ast.member_expression_static(SPAN, object, property, false))
|
||||||
|
|
|
||||||
|
|
@ -227,7 +227,7 @@ impl<'a> ModuleImportsStore<'a> {
|
||||||
ctx: &mut TraverseCtx<'a>,
|
ctx: &mut TraverseCtx<'a>,
|
||||||
) -> Statement<'a> {
|
) -> Statement<'a> {
|
||||||
let var_kind = VariableDeclarationKind::Var;
|
let var_kind = VariableDeclarationKind::Var;
|
||||||
let ident = ctx.create_reference_id(
|
let ident = ctx.create_ident_reference(
|
||||||
SPAN,
|
SPAN,
|
||||||
Atom::from("require"),
|
Atom::from("require"),
|
||||||
require_symbol_id,
|
require_symbol_id,
|
||||||
|
|
|
||||||
|
|
@ -155,31 +155,30 @@ impl<'a, 'ctx> ExponentiationOperator<'a, 'ctx> {
|
||||||
|
|
||||||
// Make sure side-effects of evaluating `left` only happen once
|
// Make sure side-effects of evaluating `left` only happen once
|
||||||
let reference = ctx.scoping.symbols_mut().get_reference_mut(ident.reference_id());
|
let reference = ctx.scoping.symbols_mut().get_reference_mut(ident.reference_id());
|
||||||
let pow_left =
|
let pow_left = if let Some(symbol_id) = reference.symbol_id() {
|
||||||
if let Some(symbol_id) = reference.symbol_id() {
|
// This variable is declared in scope so evaluating it multiple times can't trigger a getter.
|
||||||
// This variable is declared in scope so evaluating it multiple times can't trigger a getter.
|
// No need for a temp var.
|
||||||
// No need for a temp var.
|
// `left **= right` is being transformed to `left = Math.pow(left, right)`,
|
||||||
// `left **= right` is being transformed to `left = Math.pow(left, right)`,
|
// so if `left` is no longer being read from, update its `ReferenceFlags`.
|
||||||
// so if `left` is no longer being read from, update its `ReferenceFlags`.
|
if matches!(ctx.ancestry.parent(), Ancestor::ExpressionStatementExpression(_)) {
|
||||||
if matches!(ctx.ancestry.parent(), Ancestor::ExpressionStatementExpression(_)) {
|
*reference.flags_mut() = ReferenceFlags::Write;
|
||||||
*reference.flags_mut() = ReferenceFlags::Write;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
Expression::Identifier(ctx.ast.alloc(ctx.create_bound_reference_id(
|
Expression::Identifier(ctx.ast.alloc(ctx.create_bound_ident_reference(
|
||||||
SPAN,
|
SPAN,
|
||||||
ident.name.clone(),
|
ident.name.clone(),
|
||||||
symbol_id,
|
symbol_id,
|
||||||
ReferenceFlags::Read,
|
ReferenceFlags::Read,
|
||||||
)))
|
)))
|
||||||
} else {
|
} else {
|
||||||
// Unbound reference. Could possibly trigger a getter so we need to only evaluate it once.
|
// Unbound reference. Could possibly trigger a getter so we need to only evaluate it once.
|
||||||
// Assign to a temp var.
|
// Assign to a temp var.
|
||||||
let reference = Expression::Identifier(ctx.ast.alloc(
|
let reference = Expression::Identifier(ctx.ast.alloc(
|
||||||
ctx.create_unbound_reference_id(SPAN, ident.name.clone(), ReferenceFlags::Read),
|
ctx.create_unbound_ident_reference(SPAN, ident.name.clone(), ReferenceFlags::Read),
|
||||||
));
|
));
|
||||||
let binding = self.create_temp_var(reference, &mut temp_var_inits, ctx);
|
let binding = self.create_temp_var(reference, &mut temp_var_inits, ctx);
|
||||||
binding.create_read_expression(ctx)
|
binding.create_read_expression(ctx)
|
||||||
};
|
};
|
||||||
|
|
||||||
(pow_left, temp_var_inits)
|
(pow_left, temp_var_inits)
|
||||||
}
|
}
|
||||||
|
|
@ -496,12 +495,14 @@ impl<'a, 'ctx> ExponentiationOperator<'a, 'ctx> {
|
||||||
if let Some(symbol_id) = symbol_id {
|
if let Some(symbol_id) = symbol_id {
|
||||||
// This variable is declared in scope so evaluating it multiple times can't trigger a getter.
|
// This variable is declared in scope so evaluating it multiple times can't trigger a getter.
|
||||||
// No need for a temp var.
|
// No need for a temp var.
|
||||||
return Expression::Identifier(ctx.ast.alloc(ctx.create_bound_reference_id(
|
return Expression::Identifier(ctx.ast.alloc(
|
||||||
SPAN,
|
ctx.create_bound_ident_reference(
|
||||||
ident.name.clone(),
|
SPAN,
|
||||||
symbol_id,
|
ident.name.clone(),
|
||||||
ReferenceFlags::Read,
|
symbol_id,
|
||||||
)));
|
ReferenceFlags::Read,
|
||||||
|
),
|
||||||
|
));
|
||||||
}
|
}
|
||||||
// Unbound reference. Could possibly trigger a getter so we need to only evaluate it once.
|
// Unbound reference. Could possibly trigger a getter so we need to only evaluate it once.
|
||||||
// Assign to a temp var.
|
// Assign to a temp var.
|
||||||
|
|
@ -547,8 +548,12 @@ impl<'a, 'ctx> ExponentiationOperator<'a, 'ctx> {
|
||||||
ctx: &mut TraverseCtx<'a>,
|
ctx: &mut TraverseCtx<'a>,
|
||||||
) -> Expression<'a> {
|
) -> Expression<'a> {
|
||||||
let math_symbol_id = ctx.scopes().find_binding(ctx.current_scope_id(), "Math");
|
let math_symbol_id = ctx.scopes().find_binding(ctx.current_scope_id(), "Math");
|
||||||
let ident_math =
|
let ident_math = ctx.create_ident_reference(
|
||||||
ctx.create_reference_id(SPAN, Atom::from("Math"), math_symbol_id, ReferenceFlags::Read);
|
SPAN,
|
||||||
|
Atom::from("Math"),
|
||||||
|
math_symbol_id,
|
||||||
|
ReferenceFlags::Read,
|
||||||
|
);
|
||||||
let object = Expression::Identifier(ctx.alloc(ident_math));
|
let object = Expression::Identifier(ctx.alloc(ident_math));
|
||||||
let property = ctx.ast.identifier_name(SPAN, "pow");
|
let property = ctx.ast.identifier_name(SPAN, "pow");
|
||||||
let callee =
|
let callee =
|
||||||
|
|
|
||||||
|
|
@ -310,7 +310,7 @@ impl<'a, 'ctx> AsyncGeneratorExecutor<'a, 'ctx> {
|
||||||
let id = caller_function.id.as_ref().unwrap();
|
let id = caller_function.id.as_ref().unwrap();
|
||||||
// If the function has an id, then we need to return the id.
|
// If the function has an id, then we need to return the id.
|
||||||
// `function foo() { ... }` -> `function foo() {} return foo;`
|
// `function foo() { ... }` -> `function foo() {} return foo;`
|
||||||
let reference = ctx.create_bound_reference_id(
|
let reference = ctx.create_bound_ident_reference(
|
||||||
SPAN,
|
SPAN,
|
||||||
id.name.clone(),
|
id.name.clone(),
|
||||||
id.symbol_id(),
|
id.symbol_id(),
|
||||||
|
|
@ -597,8 +597,12 @@ impl<'a, 'ctx> AsyncGeneratorExecutor<'a, 'ctx> {
|
||||||
ctx: &mut TraverseCtx<'a>,
|
ctx: &mut TraverseCtx<'a>,
|
||||||
) -> Statement<'a> {
|
) -> Statement<'a> {
|
||||||
let symbol_id = ctx.scopes().find_binding(ctx.current_scope_id(), "arguments");
|
let symbol_id = ctx.scopes().find_binding(ctx.current_scope_id(), "arguments");
|
||||||
let arguments_ident =
|
let arguments_ident = ctx.create_ident_reference(
|
||||||
ctx.create_reference_id(SPAN, Atom::from("arguments"), symbol_id, ReferenceFlags::Read);
|
SPAN,
|
||||||
|
Atom::from("arguments"),
|
||||||
|
symbol_id,
|
||||||
|
ReferenceFlags::Read,
|
||||||
|
);
|
||||||
let arguments_ident = Argument::Identifier(ctx.alloc(arguments_ident));
|
let arguments_ident = Argument::Identifier(ctx.alloc(arguments_ident));
|
||||||
|
|
||||||
// (this, arguments)
|
// (this, arguments)
|
||||||
|
|
|
||||||
|
|
@ -142,7 +142,7 @@ impl<'a, 'ctx> ObjectRestSpread<'a, 'ctx> {
|
||||||
|
|
||||||
fn object_assign(symbol_id: Option<SymbolId>, ctx: &mut TraverseCtx<'a>) -> Expression<'a> {
|
fn object_assign(symbol_id: Option<SymbolId>, ctx: &mut TraverseCtx<'a>) -> Expression<'a> {
|
||||||
let ident =
|
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 object = Expression::Identifier(ctx.alloc(ident));
|
||||||
let property = ctx.ast.identifier_name(SPAN, Atom::from("assign"));
|
let property = ctx.ast.identifier_name(SPAN, Atom::from("assign"));
|
||||||
Expression::from(ctx.ast.member_expression_static(SPAN, object, property, false))
|
Expression::from(ctx.ast.member_expression_static(SPAN, object, property, false))
|
||||||
|
|
|
||||||
|
|
@ -134,7 +134,7 @@ impl<'a, 'ctx> LogicalAssignmentOperators<'a, 'ctx> {
|
||||||
let symbol_id = reference.symbol_id();
|
let symbol_id = reference.symbol_id();
|
||||||
let left_expr = Expression::Identifier(ctx.alloc(ident.clone()));
|
let left_expr = Expression::Identifier(ctx.alloc(ident.clone()));
|
||||||
|
|
||||||
let ident = ctx.create_reference_id(
|
let ident = ctx.create_ident_reference(
|
||||||
SPAN,
|
SPAN,
|
||||||
ident.name.clone(),
|
ident.name.clone(),
|
||||||
symbol_id,
|
symbol_id,
|
||||||
|
|
|
||||||
|
|
@ -336,7 +336,7 @@ impl<'a, 'ctx> Traverse<'a> for ReactRefresh<'a, 'ctx> {
|
||||||
binding_name.as_str(),
|
binding_name.as_str(),
|
||||||
)
|
)
|
||||||
.map(|symbol_id| {
|
.map(|symbol_id| {
|
||||||
let ident = ctx.create_bound_reference_id(
|
let ident = ctx.create_bound_ident_reference(
|
||||||
SPAN,
|
SPAN,
|
||||||
binding_name,
|
binding_name,
|
||||||
symbol_id,
|
symbol_id,
|
||||||
|
|
@ -496,7 +496,7 @@ impl<'a, 'ctx> ReactRefresh<'a, 'ctx> {
|
||||||
ctx: &mut TraverseCtx<'a>,
|
ctx: &mut TraverseCtx<'a>,
|
||||||
) -> Statement<'a> {
|
) -> Statement<'a> {
|
||||||
let left = self.create_registration(id.name.clone(), ReferenceFlags::Write, ctx);
|
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,
|
SPAN,
|
||||||
id.name.clone(),
|
id.name.clone(),
|
||||||
id.symbol_id(),
|
id.symbol_id(),
|
||||||
|
|
|
||||||
|
|
@ -180,7 +180,7 @@ impl<'a, 'ctx> Traverse<'a> for RegExp<'a, 'ctx> {
|
||||||
|
|
||||||
let callee = {
|
let callee = {
|
||||||
let symbol_id = ctx.scopes().find_binding(ctx.current_scope_id(), "RegExp");
|
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,
|
SPAN,
|
||||||
Atom::from("RegExp"),
|
Atom::from("RegExp"),
|
||||||
symbol_id,
|
symbol_id,
|
||||||
|
|
|
||||||
|
|
@ -122,7 +122,7 @@ impl<'a> TypeScriptEnum<'a> {
|
||||||
} else {
|
} else {
|
||||||
// }(Foo || {});
|
// }(Foo || {});
|
||||||
let op = LogicalOperator::Or;
|
let op = LogicalOperator::Or;
|
||||||
let left = ctx.create_bound_reference_id(
|
let left = ctx.create_bound_ident_reference(
|
||||||
decl.id.span,
|
decl.id.span,
|
||||||
enum_name.clone(),
|
enum_name.clone(),
|
||||||
var_symbol_id,
|
var_symbol_id,
|
||||||
|
|
@ -138,7 +138,7 @@ impl<'a> TypeScriptEnum<'a> {
|
||||||
|
|
||||||
if is_already_declared {
|
if is_already_declared {
|
||||||
let op = AssignmentOperator::Assign;
|
let op = AssignmentOperator::Assign;
|
||||||
let left = ctx.create_bound_reference_id(
|
let left = ctx.create_bound_ident_reference(
|
||||||
decl.id.span,
|
decl.id.span,
|
||||||
enum_name.clone(),
|
enum_name.clone(),
|
||||||
var_symbol_id,
|
var_symbol_id,
|
||||||
|
|
|
||||||
|
|
@ -236,7 +236,7 @@ impl<'a> BoundIdentifier<'a> {
|
||||||
flags: ReferenceFlags,
|
flags: ReferenceFlags,
|
||||||
ctx: &mut TraverseCtx<'a>,
|
ctx: &mut TraverseCtx<'a>,
|
||||||
) -> IdentifierReference<'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`
|
/// Create `Expression::Identifier` referencing this binding, with specified `Span` and `ReferenceFlags`
|
||||||
|
|
|
||||||
|
|
@ -218,7 +218,7 @@ impl<'a> MaybeBoundIdentifier<'a> {
|
||||||
flags: ReferenceFlags,
|
flags: ReferenceFlags,
|
||||||
ctx: &mut TraverseCtx<'a>,
|
ctx: &mut TraverseCtx<'a>,
|
||||||
) -> IdentifierReference<'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`
|
/// Create `Expression::Identifier` referencing this binding, with specified `Span` and `ReferenceFlags`
|
||||||
|
|
|
||||||
|
|
@ -434,7 +434,7 @@ impl<'a> TraverseCtx<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create an `IdentifierReference` bound to a `SymbolId`.
|
/// Create an `IdentifierReference` bound to a `SymbolId`.
|
||||||
pub fn create_bound_reference_id(
|
pub fn create_bound_ident_reference(
|
||||||
&mut self,
|
&mut self,
|
||||||
span: Span,
|
span: Span,
|
||||||
name: Atom<'a>,
|
name: Atom<'a>,
|
||||||
|
|
@ -458,7 +458,7 @@ impl<'a> TraverseCtx<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create an unbound `IdentifierReference`.
|
/// Create an unbound `IdentifierReference`.
|
||||||
pub fn create_unbound_reference_id(
|
pub fn create_unbound_ident_reference(
|
||||||
&mut self,
|
&mut self,
|
||||||
span: Span,
|
span: Span,
|
||||||
name: Atom<'a>,
|
name: Atom<'a>,
|
||||||
|
|
@ -486,9 +486,9 @@ impl<'a> TraverseCtx<'a> {
|
||||||
|
|
||||||
/// Create an `IdentifierReference` optionally bound to a `SymbolId`.
|
/// Create an `IdentifierReference` optionally bound to a `SymbolId`.
|
||||||
///
|
///
|
||||||
/// If you know if there's a `SymbolId` or not, prefer `TraverseCtx::create_bound_reference_id`
|
/// If you know if there's a `SymbolId` or not, prefer `TraverseCtx::create_bound_ident_reference`
|
||||||
/// or `TraverseCtx::create_unbound_reference_id`.
|
/// or `TraverseCtx::create_unbound_ident_reference`.
|
||||||
pub fn create_reference_id(
|
pub fn create_ident_reference(
|
||||||
&mut self,
|
&mut self,
|
||||||
span: Span,
|
span: Span,
|
||||||
name: Atom<'a>,
|
name: Atom<'a>,
|
||||||
|
|
@ -496,9 +496,9 @@ impl<'a> TraverseCtx<'a> {
|
||||||
flags: ReferenceFlags,
|
flags: ReferenceFlags,
|
||||||
) -> IdentifierReference<'a> {
|
) -> IdentifierReference<'a> {
|
||||||
if let Some(symbol_id) = symbol_id {
|
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 {
|
} else {
|
||||||
self.create_unbound_reference_id(span, name, flags)
|
self.create_unbound_ident_reference(span, name, flags)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue