Skip to content

Commit

Permalink
Remove private field visitors from StaticInitializerVisitor
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel committed Dec 19, 2024
1 parent 9353ffa commit 2b3c792
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 72 deletions.
2 changes: 0 additions & 2 deletions crates/oxc_transformer/src/es2022/class_properties/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,8 +277,6 @@ impl<'a, 'ctx> Traverse<'a> for ClassProperties<'a, 'ctx> {
// `#[inline]` because this is a hot path
#[inline]
fn enter_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
// IMPORTANT: If add any other visitors here to handle private fields,
// also need to add them to visitor in `static_prop.rs`.
match expr {
// `object.#prop`
Expression::PrivateFieldExpression(_) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1439,8 +1439,7 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> {
}
}

// Note: This is also called by visitor in `static_prop.rs`
pub(super) fn transform_unary_expression_impl(
fn transform_unary_expression_impl(
&mut self,
expr: &mut Expression<'a>,
ctx: &mut TraverseCtx<'a>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use super::ClassProperties;
impl<'a, 'ctx> ClassProperties<'a, 'ctx> {
/// Transform static property initializer.
///
/// Replace `this`, and references to class name, with temp var for class. Transform private fields.
/// Replace `this`, and references to class name, with temp var for class.
/// See below for full details of transforms.
pub(super) fn transform_static_initializer(
&mut self,
Expand Down Expand Up @@ -126,30 +126,6 @@ impl<'a, 'ctx, 'v> StaticInitializerVisitor<'a, 'ctx, 'v> {
}

impl<'a, 'ctx, 'v> VisitMut<'a> for StaticInitializerVisitor<'a, 'ctx, 'v> {
// TODO: Also need to call class visitors so private props stack is in correct state.
// Otherwise, in this example, `#x` in `getInnerX` is resolved incorrectly
// and `getInnerX()` will return 1 instead of 2.
// We have to visit the inner class now rather than later after exiting outer class so that
// `#y` in `getOuterY` resolves correctly too.
// ```js
// class Outer {
// #x = 1;
// #y = 1;
// static inner = class Inner {
// #x = 2;
// getInnerX() {
// return this.#x; // Should equal 2
// }
// getOuterY() {
// return this.#y; // Should equal 1
// }
// };
// }
// ```
//
// Need to save all per-class state (`insert_before` etc), and restore it again after.
// Using a stack would be overkill because nested classes in static blocks will be rare.

#[inline]
fn visit_expression(&mut self, expr: &mut Expression<'a>) {
match expr {
Expand All @@ -159,23 +135,14 @@ impl<'a, 'ctx, 'v> VisitMut<'a> for StaticInitializerVisitor<'a, 'ctx, 'v> {
self.replace_this_with_temp_var(expr, span);
return;
}
// `delete this` / `delete object?.#prop.xyz`
// `delete this`
Expression::UnaryExpression(unary_expr) => {
if unary_expr.operator == UnaryOperator::Delete {
match &unary_expr.argument {
Expression::ThisExpression(_) => {
let span = unary_expr.span;
self.replace_delete_this_with_true(expr, span);
return;
}
Expression::ChainExpression(_) => {
// Call directly into `transform_unary_expression_impl` rather than
// main entry point `transform_unary_expression`. We already checked that
// `expr` is `delete <chain expression>`, so can avoid checking that again.
self.class_properties.transform_unary_expression_impl(expr, self.ctx);
}
_ => {}
}
if unary_expr.operator == UnaryOperator::Delete
&& matches!(&unary_expr.argument, Expression::ThisExpression(_))
{
let span = unary_expr.span;
self.replace_delete_this_with_true(expr, span);
return;
}
}
// `super.prop`
Expand All @@ -186,41 +153,17 @@ impl<'a, 'ctx, 'v> VisitMut<'a> for StaticInitializerVisitor<'a, 'ctx, 'v> {
Expression::ComputedMemberExpression(_) => {
self.transform_computed_member_expression_if_super(expr);
}
// `object.#prop`
Expression::PrivateFieldExpression(_) => {
self.class_properties.transform_private_field_expression(expr, self.ctx);
}
// `super.prop()` or `object.#prop()`
// `super.prop()`
Expression::CallExpression(call_expr) => {
self.transform_call_expression_if_super_member_expression(call_expr);
self.class_properties.transform_call_expression(expr, self.ctx);
}
// `super.prop = value`, `super.prop += value`, `super.prop ??= value` or
// `object.#prop = value`, `object.#prop += value`, `object.#prop ??= value` etc
// `super.prop = value`, `super.prop += value`, `super.prop ??= value`
Expression::AssignmentExpression(_) => {
self.transform_assignment_expression_if_super_member_assignment_target(expr);
// Check again if it's an assignment expression, because it could have been transformed
// to other expression.
if matches!(expr, Expression::AssignmentExpression(_)) {
self.class_properties.transform_assignment_expression(expr, self.ctx);
}
}
// `object.#prop++`, `--object.#prop`
// `super.prop++`, `--super.prop`
Expression::UpdateExpression(_) => {
self.transform_update_expression_if_super_member_assignment_target(expr);
// Check again if it's an update expression, because it could have been transformed
// to other expression.
if matches!(expr, Expression::UpdateExpression(_)) {
self.class_properties.transform_update_expression(expr, self.ctx);
}
}
// `object?.#prop`
Expression::ChainExpression(_) => {
self.class_properties.transform_chain_expression(expr, self.ctx);
}
// "object.#prop`xyz`"
Expression::TaggedTemplateExpression(_) => {
self.class_properties.transform_tagged_template_expression(expr, self.ctx);
}
_ => {}
}
Expand Down

0 comments on commit 2b3c792

Please sign in to comment.