Skip to content

Commit

Permalink
refactor(transformer/class-properties): ResolvedPrivateProp type
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel committed Nov 28, 2024
1 parent c855c2d commit 2ea88cf
Showing 1 changed file with 25 additions and 8 deletions.
33 changes: 25 additions & 8 deletions crates/oxc_transformer/src/es2022/class_properties/private.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ use super::{
ClassProperties, PrivateProp,
};

/// Details of a private property resolved for a private field.
///
/// This is the return value of `lookup_private_property`.
struct ResolvedPrivateProp<'a, 'b> {
prop: &'b PrivateProp<'a>,
class_binding: &'b Option<BoundIdentifier<'a>>,
is_declaration: bool,
}

impl<'a, 'ctx> ClassProperties<'a, 'ctx> {
/// Transform private field expression.
///
Expand All @@ -44,9 +53,10 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> {
) -> Expression<'a> {
let prop_details = self.lookup_private_property(&field_expr.field);
// TODO: Should never be `None` - only because implementation is incomplete.
let Some((prop, class_binding, is_declaration)) = prop_details else {
let Some(prop_details) = prop_details else {
return Expression::PrivateFieldExpression(field_expr);
};
let ResolvedPrivateProp { prop, class_binding, is_declaration } = prop_details;
let prop_ident = prop.binding.create_read_expression(ctx);

// TODO: Move this to top of function once `lookup_private_property` does not return `Option`
Expand Down Expand Up @@ -182,7 +192,7 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> {
ctx: &mut TraverseCtx<'a>,
) -> Option<(Expression<'a>, Expression<'a>)> {
// TODO: Should never be `None` - only because implementation is incomplete.
let (prop, class_binding, is_declaration) =
let ResolvedPrivateProp { prop, class_binding, is_declaration } =
self.lookup_private_property(&field_expr.field)?;
let prop_ident = prop.binding.create_read_expression(ctx);

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

let prop_details = self.lookup_private_property(&field_expr.field);
// TODO: Should never be `None` - only because implementation is incomplete.
let Some((prop, class_binding, is_declaration)) = prop_details else { return };
let Some(prop_details) = prop_details else { return };
let ResolvedPrivateProp { prop, class_binding, is_declaration } = prop_details;

// Note: `transform_static_assignment_expression` and `transform_instance_assignment_expression`
// are marked `#[inline]`, so hopefully compiler will see these clones of `BoundIdentifier`s
Expand Down Expand Up @@ -613,7 +624,9 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> {

let prop_details = self.lookup_private_property(&field_expr.field);
// TODO: Should never be `None` - only because implementation is incomplete.
let Some((prop, class_binding, is_declaration)) = prop_details else { return };
let Some(prop_details) = prop_details else { return };
let ResolvedPrivateProp { prop, class_binding, is_declaration } = prop_details;

let prop_ident = prop.binding.create_read_expression(ctx);
let prop_ident2 = prop.binding.create_read_expression(ctx);

Expand Down Expand Up @@ -1032,15 +1045,19 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> {
}

/// Lookup details of private property referred to by `ident`.
fn lookup_private_property(
&self,
fn lookup_private_property<'b>(
&'b self,
ident: &PrivateIdentifier<'a>,
) -> Option<(&PrivateProp<'a>, &Option<BoundIdentifier<'a>>, /* is_declaration */ bool)> {
) -> Option<ResolvedPrivateProp<'a, 'b>> {
// Check for binding in closest class first, then enclosing classes
// TODO: Check there are tests for bindings in enclosing classes.
for private_props in self.private_props_stack.as_slice().iter().rev() {
if let Some(prop) = private_props.props.get(&ident.name) {
return Some((prop, &private_props.class_binding, private_props.is_declaration));
return Some(ResolvedPrivateProp {
prop,
class_binding: &private_props.class_binding,
is_declaration: private_props.is_declaration,
});
}
}
// TODO: This should be unreachable. Only returning `None` because implementation is incomplete.
Expand Down

0 comments on commit 2ea88cf

Please sign in to comment.