From 85a437322ec41adf615edac86e8e838464d1b838 Mon Sep 17 00:00:00 2001 From: OJ Kwon Date: Sun, 7 Nov 2021 22:25:57 -0800 Subject: [PATCH] fix(es/transforms/compat): apply regenerator transform for default expr --- .../compat/src/es2017/async_to_generator.rs | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/ecmascript/transforms/compat/src/es2017/async_to_generator.rs b/ecmascript/transforms/compat/src/es2017/async_to_generator.rs index c48523360711..af12dfa60351 100644 --- a/ecmascript/transforms/compat/src/es2017/async_to_generator.rs +++ b/ecmascript/transforms/compat/src/es2017/async_to_generator.rs @@ -362,6 +362,35 @@ impl Fold for Actual { } } + fn fold_module_item(&mut self, item: ModuleItem) -> ModuleItem { + match item { + // if fn is ExportDefaultDecl, fn is not FnDecl but FnExpr + ModuleItem::ModuleDecl(ModuleDecl::ExportDefaultDecl(export_default)) => { + if let DefaultDecl::Fn(expr) = export_default.decl { + let expr = if !expr.function.is_async { + expr + } else { + let function = self.fold_fn(expr.ident.clone(), expr.function, true); + FnExpr { + ident: expr.ident, + function, + } + }; + + ModuleItem::ModuleDecl(ModuleDecl::ExportDefaultDecl(ExportDefaultDecl { + decl: expr.into(), + ..export_default + })) + } else { + let item = + ModuleItem::ModuleDecl(ModuleDecl::ExportDefaultDecl(export_default)); + item.fold_children_with(self) + } + } + _ => item.fold_children_with(self), + } + } + fn fold_method_prop(&mut self, prop: MethodProp) -> MethodProp { let prop = prop.fold_children_with(self);