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`
  • Loading branch information
overlookmotel committed Nov 28, 2024
1 parent defaf4b commit 6d505f7
Showing 1 changed file with 23 additions and 12 deletions.
35 changes: 23 additions & 12 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 @@ -58,9 +58,12 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> {
let class_name_binding = class_name_binding.as_ref().unwrap();

// 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_name_binding, &object, ctx)
{
if let Some(reference_id) = Self::shortcut_static_class(
is_declaration,
class_name_binding.symbol_id,
&object,
ctx,
) {
// `_prop._`
ctx.symbols_mut()
.delete_resolved_reference(class_name_binding.symbol_id, reference_id);
Expand Down Expand Up @@ -90,18 +93,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_name_binding: &BoundIdentifier<'a>`?
fn shortcut_static_class(
is_declaration: bool,
class_name_binding: &BoundIdentifier<'a>,
class_name_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_name_binding.symbol_id {
if symbol_id == class_name_symbol_id {
return Some(reference_id);
}
}
Expand Down Expand Up @@ -200,8 +202,13 @@ 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_name_binding, &object, ctx)
.is_some()
if Self::shortcut_static_class(
is_declaration,
class_name_binding.symbol_id,
&object,
ctx,
)
.is_some()
{
// `_prop._`
let callee =
Expand Down Expand Up @@ -325,7 +332,7 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> {
// and look up the `SymbolId`.
let object_reference_id = Self::shortcut_static_class(
is_declaration,
&class_name_binding,
class_name_binding.symbol_id,
&field_expr.object,
ctx,
);
Expand Down Expand Up @@ -648,8 +655,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_name_binding, &object, ctx);
let object_reference_id = Self::shortcut_static_class(
is_declaration,
class_name_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 6d505f7

Please sign in to comment.