From 2cdb34f6dfca3c301529f905966288e76ec0b41c Mon Sep 17 00:00:00 2001 From: Dunqing <29533304+Dunqing@users.noreply.github.com> Date: Fri, 21 Jun 2024 14:37:10 +0000 Subject: [PATCH] feat(isolated-declarations): support for class function overloads (#3811) --- crates/oxc_isolated_declarations/src/class.rs | 61 ++++++++++++------- .../oxc_isolated_declarations/tests/deno.rs | 1 - 2 files changed, 39 insertions(+), 23 deletions(-) diff --git a/crates/oxc_isolated_declarations/src/class.rs b/crates/oxc_isolated_declarations/src/class.rs index fd49597b6ab2c..1064cdb1086ba 100644 --- a/crates/oxc_isolated_declarations/src/class.rs +++ b/crates/oxc_isolated_declarations/src/class.rs @@ -247,33 +247,15 @@ impl<'a> IsolatedDeclarations<'a> { elements } - pub fn transform_class( + /// Infer get accessor return type from set accessor + /// Infer set accessor parameter type from get accessor return type + fn collect_inferred_accessor_types( &self, decl: &Class<'a>, - modifiers: Option>, - ) -> Option>> { - if decl.is_declare() { - return None; - } - - if let Some(super_class) = &decl.super_class { - let is_not_allowed = match super_class { - Expression::Identifier(_) => false, - Expression::StaticMemberExpression(expr) => { - !expr.get_first_object().is_identifier_reference() - } - _ => true, - }; - if is_not_allowed { - self.error(extends_clause_expression(super_class.span())); - } - } - + ) -> FxHashMap>> { let mut inferred_accessor_types: FxHashMap, Box<'a, TSTypeAnnotation<'a>>> = FxHashMap::default(); - // Infer get accessor return type from set accessor - // Infer set accessor parameter type from get accessor for element in &decl.body.body { if let ClassElement::MethodDefinition(method) = element { if method.key.is_private_identifier() @@ -318,12 +300,45 @@ impl<'a> IsolatedDeclarations<'a> { } } + inferred_accessor_types + } + + pub fn transform_class( + &self, + decl: &Class<'a>, + modifiers: Option>, + ) -> Option>> { + if decl.is_declare() { + return None; + } + + if let Some(super_class) = &decl.super_class { + let is_not_allowed = match super_class { + Expression::Identifier(_) => false, + Expression::StaticMemberExpression(expr) => { + !expr.get_first_object().is_identifier_reference() + } + _ => true, + }; + if is_not_allowed { + self.error(extends_clause_expression(super_class.span())); + } + } + let mut has_private_key = false; let mut elements = self.ast.new_vec(); + let mut is_function_overloads = false; for element in &decl.body.body { match element { ClassElement::StaticBlock(_) => {} ClassElement::MethodDefinition(ref method) => { + if method.value.body.is_none() { + is_function_overloads = true; + } else if is_function_overloads { + // Skip implementation of function overloads + is_function_overloads = false; + continue; + } if method.key.is_private_identifier() { has_private_key = true; continue; @@ -335,6 +350,8 @@ impl<'a> IsolatedDeclarations<'a> { elements.push(self.transform_private_modifier_method(method)); continue; } + + let inferred_accessor_types = self.collect_inferred_accessor_types(decl); let function = &method.value; let params = if method.kind.is_set() { method.key.static_name().map_or_else( diff --git a/crates/oxc_isolated_declarations/tests/deno.rs b/crates/oxc_isolated_declarations/tests/deno.rs index f471f6e03f64d..fcfea244053eb 100644 --- a/crates/oxc_isolated_declarations/tests/deno.rs +++ b/crates/oxc_isolated_declarations/tests/deno.rs @@ -103,7 +103,6 @@ export function foo(a: any): number { } #[test] - #[ignore = "Doesn't support class function overloads"] fn dts_class_decl_overloads_test() { transform_dts_test( "export class Foo {