Skip to content

Commit

Permalink
refactor(transformer/class-properties): shortcut_static_class take …
Browse files Browse the repository at this point in the history
…`SymbolId` (#7531)

Pure refactor. `shortcut_static_class` method take `SymbolId` of class name, instead of the `&BoundIdentifier`. This change is in preparation for later PRs fixing class properties transform.
  • Loading branch information
overlookmotel committed Dec 3, 2024
1 parent ab1214d commit 367b6c8
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions crates/oxc_transformer/src/es2022/class_properties/private.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use oxc_ast::{ast::*, NONE};
use oxc_span::SPAN;
use oxc_syntax::{
reference::{ReferenceFlags, ReferenceId},
symbol::SymbolFlags,
symbol::{SymbolFlags, SymbolId},
};
use oxc_traverse::{ast_operations::get_var_name_from_node, BoundIdentifier, TraverseCtx};

Expand Down Expand Up @@ -59,7 +59,7 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> {

// If `object` is reference to class name, there's no need for the class brand assertion
if let Some(reference_id) =
Self::shortcut_static_class(is_declaration, class_binding, &object, ctx)
Self::shortcut_static_class(is_declaration, class_binding.symbol_id, &object, ctx)
{
// `_prop._`
ctx.symbols_mut().delete_resolved_reference(class_binding.symbol_id, reference_id);
Expand Down Expand Up @@ -89,18 +89,17 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> {
/// If can use shorter version, returns `ReferenceId` of the `IdentifierReference`.
//
// TODO(improve-on-babel): No reason not to use the short version for class expressions too.
// TODO: Take `SymbolId` instead of `class_binding: &BoundIdentifier<'a>`?
fn shortcut_static_class(
is_declaration: bool,
class_binding: &BoundIdentifier<'a>,
class_symbol_id: SymbolId,
object: &Expression<'a>,
ctx: &mut TraverseCtx<'a>,
) -> Option<ReferenceId> {
if is_declaration {
if let Expression::Identifier(ident) = object {
let reference_id = ident.reference_id();
if let Some(symbol_id) = ctx.symbols().get_reference(reference_id).symbol_id() {
if symbol_id == class_binding.symbol_id {
if symbol_id == class_symbol_id {
return Some(reference_id);
}
}
Expand Down Expand Up @@ -199,7 +198,9 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> {
// If `object` is reference to class name, there's no need for the class brand assertion
// TODO: Combine this check with `duplicate_object`. Both check if `object` is an identifier,
// and look up the `SymbolId`
if Self::shortcut_static_class(is_declaration, class_binding, &object, ctx).is_some() {
if Self::shortcut_static_class(is_declaration, class_binding.symbol_id, &object, ctx)
.is_some()
{
// `_prop._`
let callee =
Self::create_underscore_member_expression(prop_ident, field_expr.span, ctx);
Expand Down Expand Up @@ -320,8 +321,12 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> {
// Check if object (`object` in `object.#prop`) is a reference to class name
// TODO: Combine this check with `duplicate_object`. Both check if `object` is an identifier,
// and look up the `SymbolId`.
let object_reference_id =
Self::shortcut_static_class(is_declaration, &class_binding, &field_expr.object, ctx);
let object_reference_id = Self::shortcut_static_class(
is_declaration,
class_binding.symbol_id,
&field_expr.object,
ctx,
);

// If `object` is reference to class name, there's no need for the class brand assertion.
// `Class.#prop = value` -> `_prop._ = value`
Expand Down Expand Up @@ -642,7 +647,7 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> {
// TODO: Combine this check with `duplicate_object`. Both check if `object` is an identifier,
// and look up the `SymbolId`.
let object_reference_id =
Self::shortcut_static_class(is_declaration, &class_binding, &object, ctx);
Self::shortcut_static_class(is_declaration, class_binding.symbol_id, &object, ctx);

// `_assertClassBrand(Class, object, _prop)._` or `_prop._`
let (get_expr, object) = if let Some(reference_id) = object_reference_id {
Expand Down

0 comments on commit 367b6c8

Please sign in to comment.