From 2cf96973b9a8752535a42f0c5ccb15be36676380 Mon Sep 17 00:00:00 2001 From: overlookmotel Date: Wed, 18 Dec 2024 01:44:02 +0000 Subject: [PATCH] Remove properties after class traversal --- .../src/es2022/class_properties/class.rs | 83 ++++++++++--------- .../es2022/class_properties/computed_key.rs | 1 - .../src/es2022/class_properties/mod.rs | 4 +- .../src/es2022/class_properties/prop_decl.rs | 1 - .../es2022/class_properties/static_block.rs | 1 - 5 files changed, 45 insertions(+), 45 deletions(-) diff --git a/crates/oxc_transformer/src/es2022/class_properties/class.rs b/crates/oxc_transformer/src/es2022/class_properties/class.rs index 5281ca764f5983..43062f1dd158b2 100644 --- a/crates/oxc_transformer/src/es2022/class_properties/class.rs +++ b/crates/oxc_transformer/src/es2022/class_properties/class.rs @@ -217,42 +217,6 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> { } } - /* - body.body.retain_mut(|element| { - match element { - ClassElement::PropertyDefinition(prop) => { - // self.convert_static_property(prop, ctx); - if !prop.r#static { - self.convert_instance_property(prop, &mut instance_inits, ctx); - return false; - } - } - ClassElement::StaticBlock(_block) => { - if self.transform_static_blocks { - self.convert_static_block(block, ctx); - return false; - } - } - ClassElement::MethodDefinition(method) => { - if method.kind == MethodDefinitionKind::Constructor { - if method.value.body.is_some() { - constructor_index = index_not_including_removed; - } - } else { - // self.substitute_temp_var_for_method_computed_key(method, ctx); - } - } - ClassElement::AccessorProperty(_) | ClassElement::TSIndexSignature(_) => { - // TODO: Need to handle these? - } - } - - index_not_including_removed += 1; - - true - }); - */ - // Insert instance initializers into constructor, or create constructor if there is none self.insert_instance_inits( body, @@ -267,7 +231,11 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> { /// Pop from private props stack. // `#[inline]` because this is function is so small #[inline] - pub(super) fn transform_class_on_exit(&mut self, class: &Class) { + pub(super) fn transform_class_on_exit( + &mut self, + class: &mut Class<'a>, + ctx: &mut TraverseCtx<'a>, + ) { // Ignore TS class declarations // TODO: Is this correct? if class.declare { @@ -277,10 +245,13 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> { // Deal with exit from class expression in `exit_expression` let class_details = self.current_class(); if !class_details.is_declaration { + // Handle class expressions in `exit_expression` return; } - // TODO + self.transform_class_on_exit_impl(class, ctx); + + // TODO: Insert statements before / after class self.classes_stack.pop(); } @@ -288,7 +259,7 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> { pub(super) fn transform_class_expression_on_exit( &mut self, expr: &mut Expression<'a>, - _ctx: &mut TraverseCtx<'a>, + ctx: &mut TraverseCtx<'a>, ) { let Expression::ClassExpression(class) = expr else { unreachable!() }; @@ -298,11 +269,43 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> { return; } - // TODO + self.transform_class_on_exit_impl(class, ctx); + + // TODO: Insert expressions before / after class self.classes_stack.pop(); } + fn transform_class_on_exit_impl(&mut self, class: &mut Class<'a>, ctx: &mut TraverseCtx<'a>) { + class.body.body.retain_mut(|element| { + match element { + ClassElement::PropertyDefinition(prop) => { + if prop.r#static { + self.convert_static_property(prop, ctx); + } else if prop.computed { + // TODO: Extract computed key + } + return false; + } + ClassElement::StaticBlock(block) => { + if self.transform_static_blocks { + self.convert_static_block(block, ctx); + return false; + } + } + ClassElement::MethodDefinition(method) => { + // TODO: This isn't right + self.substitute_temp_var_for_method_computed_key(method, ctx); + } + ClassElement::AccessorProperty(_) | ClassElement::TSIndexSignature(_) => { + // TODO: Need to handle these? + } + } + + true + }); + } + /// Transform class expression. // `#[inline]` so that compiler sees that `expr` is an `Expression::ClassExpression`. // Main guts of transform is broken out into `transform_class_expression_start` and diff --git a/crates/oxc_transformer/src/es2022/class_properties/computed_key.rs b/crates/oxc_transformer/src/es2022/class_properties/computed_key.rs index 82ae230236ffb8..cf16e5e998d978 100644 --- a/crates/oxc_transformer/src/es2022/class_properties/computed_key.rs +++ b/crates/oxc_transformer/src/es2022/class_properties/computed_key.rs @@ -11,7 +11,6 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> { /// Substitute temp var for method computed key. /// `class C { [x()]() {} }` -> `let _x; _x = x(); class C { [_x]() {} }` /// This transform is only required if class has properties or a static block. - #[expect(dead_code)] pub(super) fn substitute_temp_var_for_method_computed_key( &mut self, method: &mut MethodDefinition<'a>, diff --git a/crates/oxc_transformer/src/es2022/class_properties/mod.rs b/crates/oxc_transformer/src/es2022/class_properties/mod.rs index 29b8784ef948b3..f77d42c3b42655 100644 --- a/crates/oxc_transformer/src/es2022/class_properties/mod.rs +++ b/crates/oxc_transformer/src/es2022/class_properties/mod.rs @@ -348,8 +348,8 @@ impl<'a, 'ctx> Traverse<'a> for ClassProperties<'a, 'ctx> { // `#[inline]` because `transform_class_on_exit` is so small #[inline] - fn exit_class(&mut self, class: &mut Class<'a>, _ctx: &mut TraverseCtx<'a>) { - self.transform_class_on_exit(class); + fn exit_class(&mut self, class: &mut Class<'a>, ctx: &mut TraverseCtx<'a>) { + self.transform_class_on_exit(class, ctx); } #[inline] diff --git a/crates/oxc_transformer/src/es2022/class_properties/prop_decl.rs b/crates/oxc_transformer/src/es2022/class_properties/prop_decl.rs index 7bd5d89d6f8d7a..6c8049b2213bd9 100644 --- a/crates/oxc_transformer/src/es2022/class_properties/prop_decl.rs +++ b/crates/oxc_transformer/src/es2022/class_properties/prop_decl.rs @@ -83,7 +83,6 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> { impl<'a, 'ctx> ClassProperties<'a, 'ctx> { /// Convert static property to initialization expression. /// Property `static prop = 123;` -> Expression `C.prop = 123` or `_defineProperty(C, "prop", 123)`. - #[expect(dead_code)] pub(super) fn convert_static_property( &mut self, prop: &mut PropertyDefinition<'a>, diff --git a/crates/oxc_transformer/src/es2022/class_properties/static_block.rs b/crates/oxc_transformer/src/es2022/class_properties/static_block.rs index 0881e44a67f2a4..605454284bbb1c 100644 --- a/crates/oxc_transformer/src/es2022/class_properties/static_block.rs +++ b/crates/oxc_transformer/src/es2022/class_properties/static_block.rs @@ -17,7 +17,6 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> { /// TODO: Add tests for this if there aren't any already. /// Include tests for evaluation order inc that static block goes before class expression /// unless also static properties, or static block uses class name. - #[expect(dead_code)] pub(super) fn convert_static_block( &mut self, block: &mut StaticBlock<'a>,