From aa5e65ff3ccee94e9177ff0269dca493a0155125 Mon Sep 17 00:00:00 2001 From: overlookmotel <557937+overlookmotel@users.noreply.github.com> Date: Thu, 9 Jan 2025 03:17:41 +0000 Subject: [PATCH] refactor(transformer/private-methods): simplify finding parent statement of class expression (#8364) Pure refactor. Simplify code a little. --- .../src/es2022/class_properties/class.rs | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/crates/oxc_transformer/src/es2022/class_properties/class.rs b/crates/oxc_transformer/src/es2022/class_properties/class.rs index 9f8f20ec9..3e7bc1ed8 100644 --- a/crates/oxc_transformer/src/es2022/class_properties/class.rs +++ b/crates/oxc_transformer/src/es2022/class_properties/class.rs @@ -641,17 +641,18 @@ impl<'a> ClassProperties<'a, '_> { // Insert private methods if !self.insert_after_stmts.is_empty() { - // Find address of statement of class expression - let position = ctx - .ancestors() - .position(Ancestor::is_parent_of_statement) - .expect("Expression always inside a statement."); - // Position points to parent of statement, we need to find the statement itself, - // so `position - 1`. - let stmt_ancestor = ctx.ancestor(position - 1); + // Find `Address` of statement containing class expression + let mut stmt_address = Address::DUMMY; + for ancestor in ctx.ancestors() { + if ancestor.is_parent_of_statement() { + break; + } + stmt_address = ancestor.address(); + } + self.ctx .statement_injector - .insert_many_after(&stmt_ancestor, self.insert_after_stmts.drain(..)); + .insert_many_after(&stmt_address, self.insert_after_stmts.drain(..)); } // Insert computed key initializers