From 231b8f094661b9967421b54cb389b18ea6ce7b58 Mon Sep 17 00:00:00 2001 From: Dunqing <29533304+Dunqing@users.noreply.github.com> Date: Fri, 21 Jun 2024 10:20:24 +0000 Subject: [PATCH] feat(isolated-declarations): support for export default function overloads (#3809) --- crates/oxc_isolated_declarations/src/lib.rs | 16 ++++++++++++++++ crates/oxc_isolated_declarations/tests/deno.rs | 15 +++++++-------- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/crates/oxc_isolated_declarations/src/lib.rs b/crates/oxc_isolated_declarations/src/lib.rs index 3b2c33e07d2fe..a9e4da97a3b44 100644 --- a/crates/oxc_isolated_declarations/src/lib.rs +++ b/crates/oxc_isolated_declarations/src/lib.rs @@ -328,6 +328,7 @@ impl<'a> IsolatedDeclarations<'a> { stmts: oxc_allocator::Vec<'a, Statement<'a>>, ) -> impl Iterator> + '_ { let mut last_function_name: Option> = None; + let mut is_export_default_function_overloads = false; stmts.into_iter().filter_map(move |stmt| match stmt { Statement::FunctionDeclaration(ref func) => { @@ -373,6 +374,21 @@ impl<'a> IsolatedDeclarations<'a> { Some(stmt) } } + Statement::ExportDefaultDeclaration(ref decl) => { + if let ExportDefaultDeclarationKind::FunctionDeclaration(ref func) = + decl.declaration + { + if is_export_default_function_overloads && func.body.is_some() { + is_export_default_function_overloads = false; + return None; + } + is_export_default_function_overloads = true; + Some(stmt) + } else { + is_export_default_function_overloads = false; + Some(stmt) + } + } _ => Some(stmt), }) } diff --git a/crates/oxc_isolated_declarations/tests/deno.rs b/crates/oxc_isolated_declarations/tests/deno.rs index ff5f692528836..e371e28f61d72 100644 --- a/crates/oxc_isolated_declarations/tests/deno.rs +++ b/crates/oxc_isolated_declarations/tests/deno.rs @@ -360,14 +360,13 @@ export function foo(a: any): number { "export default function(a: number, b: number): number {};", "export default function(a: number, b: number): number;", ); - // TODO export default function overloads - // transform_dts_test( - // "export default function(a: number, b: number): number; - // export default function(a: number, b: number): any { - // return foo - // };", - // "export default function(a: number, b: number): number;", - // ); + transform_dts_test( + "export default function(a: number, b: number): number; + export default function(a: number, b: number): any { + return foo + };", + "export default function(a: number, b: number): number;", + ); transform_dts_test( "export default class {foo = 2};", "export default class {\n foo: number;\n}",