Skip to content

Commit

Permalink
fix(transformer/class-properties): transform delete chain expressio…
Browse files Browse the repository at this point in the history
…n in static prop initializers
  • Loading branch information
overlookmotel committed Dec 4, 2024
1 parent 2080da3 commit 9776930
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
3 changes: 2 additions & 1 deletion crates/oxc_transformer/src/es2022/class_properties/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,8 @@ 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>) {
// Note: `delete this.#prop` is an early syntax error, so no need to handle transforming it
// IMPORTANT: If add any other visitors here to handle private fields,
// also need to add them to visitor in `static_prop.rs`.
match expr {
// `class {}`
Expression::ClassExpression(_) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1360,12 +1360,15 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> {
}
}

fn transform_unary_expression_impl(
// Note: This is also called by visitor in `static_prop.rs`
pub(super) fn transform_unary_expression_impl(
&mut self,
expr: &mut Expression<'a>,
ctx: &mut TraverseCtx<'a>,
) {
let Expression::UnaryExpression(unary_expr) = expr else { unreachable!() };
debug_assert!(unary_expr.operator == UnaryOperator::Delete);
debug_assert!(matches!(unary_expr.argument, Expression::ChainExpression(_)));

if let Some((result, chain_expr)) =
self.transform_chain_expression_impl(&mut unary_expr.argument, ctx)
Expand Down
23 changes: 16 additions & 7 deletions crates/oxc_transformer/src/es2022/class_properties/static_prop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,23 @@ impl<'a, 'ctx, 'v> VisitMut<'a> for StaticInitializerVisitor<'a, 'ctx, 'v> {
self.replace_this_with_temp_var(expr, span);
return;
}
// `delete this`
// `delete this` / `delete object?.#prop.xyz`
Expression::UnaryExpression(unary_expr) => {
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;
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);
}
_ => {}
}
}
}
// `object.#prop`
Expand Down

0 comments on commit 9776930

Please sign in to comment.