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 <chain expression>` into the handler `transform_unary_expression`. Aim is to keep logic in one place and keep the main visitor as simple as possible.
This commit is contained in:
overlookmotel 2024-12-05 01:07:17 +00:00
parent 6c82589daf
commit 44fe854231
2 changed files with 19 additions and 5 deletions

View file

@ -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`"

View file

@ -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 <chain expression>` (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)
{