Skip to content

Commit

Permalink
Remove properties after class traversal
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel authored and Dunqing committed Dec 18, 2024
1 parent 14583bb commit 2cf9697
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 45 deletions.
83 changes: 43 additions & 40 deletions crates/oxc_transformer/src/es2022/class_properties/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 {
Expand All @@ -277,18 +245,21 @@ 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();
}

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!() };

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_transformer/src/es2022/class_properties/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
Expand Down

0 comments on commit 2cf9697

Please sign in to comment.