Skip to content

Commit

Permalink
transform module import
Browse files Browse the repository at this point in the history
  • Loading branch information
mununki committed Oct 10, 2023
1 parent 8358392 commit 5ff1558
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 27 deletions.
58 changes: 31 additions & 27 deletions jscomp/frontend/bs_builtin_ppx.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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)
| _ ->
Expand Down
15 changes: 15 additions & 0 deletions jscomp/test/Import.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5ff1558

Please sign in to comment.