diff --git a/jscomp/frontend/bs_builtin_ppx.ml b/jscomp/frontend/bs_builtin_ppx.ml index 975bf5811a7..0e17ca23d3f 100644 --- a/jscomp/frontend/bs_builtin_ppx.ml +++ b/jscomp/frontend/bs_builtin_ppx.ml @@ -574,35 +574,39 @@ let rec structure_mapper ~await_context (self : mapper) (stru : Ast_structure.t) | Pstr_value (_, vbs) -> let item = self.structure_item self item in (* [ module __Belt_List__ = module type of Belt.List ] *) - let module_type_decls = - vbs - |> List.filter_map (fun ({pvb_expr} : Parsetree.value_binding) -> - match pvb_expr.pexp_desc with - | Pexp_letmodule - ( _, - ({pmod_desc = Pmod_ident {txt; loc}; pmod_attributes} as - me), - _ ) - when Res_parsetree_viewer.hasAwaitAttribute pmod_attributes - -> ( - let safe_module_type_name = local_module_type_name txt in - let has_local_module_name = - Hashtbl.find_opt !await_context safe_module_type_name - in + let rec spelunk_vbs acc vbs = + match vbs with + | [] -> acc + | ({pvb_expr} : Parsetree.value_binding) :: tl -> + let rec aux (expr : Parsetree.expression) = + match expr.pexp_desc with + | Pexp_letmodule + ( _, + ({pmod_desc = Pmod_ident {txt; loc}; pmod_attributes} as me), + expr ) + when Res_parsetree_viewer.hasAwaitAttribute pmod_attributes -> ( + let safe_module_type_name = local_module_type_name txt in + let has_local_module_name = + Hashtbl.find_opt !await_context safe_module_type_name + in - match has_local_module_name with - | Some _ -> None - | None -> - Hashtbl.add !await_context safe_module_type_name - safe_module_type_name; - Some - Ast_helper.( - Str.modtype ~loc - (Mtd.mk ~loc - {txt = safe_module_type_name; loc} - ~typ:(Mty.typeof_ ~loc me)))) - | _ -> None) + match has_local_module_name with + | Some _ -> aux expr + | None -> + Hashtbl.add !await_context safe_module_type_name + safe_module_type_name; + Ast_helper.( + Str.modtype ~loc + (Mtd.mk ~loc + {txt = safe_module_type_name; loc} + ~typ:(Mty.typeof_ ~loc me))) + :: aux expr) + | Pexp_fun (_, _, _, expr) -> aux expr + | _ -> acc + in + aux pvb_expr @ spelunk_vbs acc tl in + let module_type_decls = spelunk_vbs [] vbs in module_type_decls @ (item :: structure_mapper ~await_context self rest) | _ -> diff --git a/jscomp/test/Import.js b/jscomp/test/Import.js index 5f363128476..aac7b61eab8 100644 --- a/jscomp/test/Import.js +++ b/jscomp/test/Import.js @@ -101,6 +101,19 @@ async function f3(param) { ]; } +async function f4(param) { + return (await import("../../lib/js/belt_Array.js")).forEach; +} + +async function f5(param) { + var A = await import("../../lib/js/belt_Array.js"); + var O = await import("../../lib/js/belt_Option.js"); + return [ + A.forEach, + O.forEach + ]; +} + var each = M1.forEach; var M2; @@ -121,4 +134,6 @@ exports.f = f; exports.f1 = f1; exports.f2 = f2; exports.f3 = f3; +exports.f4 = f4; +exports.f5 = f5; /* Not a pure module */