From 44fe85423133e20a06a68722c7d1e53e4d80ddbf Mon Sep 17 00:00:00 2001 From: overlookmotel <557937+overlookmotel@users.noreply.github.com> Date: Thu, 5 Dec 2024 01:07:17 +0000 Subject: [PATCH] refactor(transformer/class-properties): move logic for handling `delete` of chain expression into `transform_unary_expression` (#7655) Follow-on after #7575. Pure refactor. Move all logic for transforming `delete ` into the handler `transform_unary_expression`. Aim is to keep logic in one place and keep the main visitor as simple as possible. --- .../src/es2022/class_properties/mod.rs | 5 +---- .../src/es2022/class_properties/private.rs | 19 ++++++++++++++++++- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/crates/oxc_transformer/src/es2022/class_properties/mod.rs b/crates/oxc_transformer/src/es2022/class_properties/mod.rs index 3f4909a3a3f58..3e3385e12f3fc 100644 --- a/crates/oxc_transformer/src/es2022/class_properties/mod.rs +++ b/crates/oxc_transformer/src/es2022/class_properties/mod.rs @@ -279,10 +279,7 @@ impl<'a, 'ctx> Traverse<'a> for ClassProperties<'a, 'ctx> { self.transform_chain_expression(expr, ctx); } // `delete object?.#prop.xyz` - Expression::UnaryExpression(unary) - if unary.operator == UnaryOperator::Delete - && matches!(unary.argument, Expression::ChainExpression(_)) => - { + Expression::UnaryExpression(_) => { self.transform_unary_expression(expr, ctx); } // "object.#prop`xyz`" diff --git a/crates/oxc_transformer/src/es2022/class_properties/private.rs b/crates/oxc_transformer/src/es2022/class_properties/private.rs index 170dd33b49ac7..6cc87e24f0a0a 100644 --- a/crates/oxc_transformer/src/es2022/class_properties/private.rs +++ b/crates/oxc_transformer/src/es2022/class_properties/private.rs @@ -1357,7 +1357,9 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> { /// * `delete object?.#prop?.xyz;` /// -> `delete (object === null || object === void 0 ? void 0 : _assertClassBrand(Foo, object, _prop)._)?.xyz;` // - // `#[inline]` so that compiler sees that `expr` is an `Expression::UnaryExpression`. + // `#[inline]` so that compiler sees that `expr` is an `Expression::UnaryExpression`, + // and make bailing out if is not `delete ` (it rarely will be) a fast path without + // cost of a function call. #[inline] pub(super) fn transform_unary_expression( &mut self, @@ -1365,6 +1367,21 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> { ctx: &mut TraverseCtx<'a>, ) { let Expression::UnaryExpression(unary_expr) = expr else { unreachable!() }; + + if unary_expr.operator == UnaryOperator::Delete + && matches!(unary_expr.argument, Expression::ChainExpression(_)) + { + self.transform_unary_expression_impl(expr, ctx); + } + } + + fn transform_unary_expression_impl( + &mut self, + expr: &mut Expression<'a>, + ctx: &mut TraverseCtx<'a>, + ) { + let Expression::UnaryExpression(unary_expr) = expr else { unreachable!() }; + if let Some((result, chain_expr)) = self.transform_chain_expression_impl(&mut unary_expr.argument, ctx) {