Skip to content

Commit b7a2037

Browse files
committed
using table instead number for safe module type name
1 parent 6b00e3e commit b7a2037

File tree

3 files changed

+107
-33
lines changed

3 files changed

+107
-33
lines changed

jscomp/frontend/bs_builtin_ppx.ml

Lines changed: 49 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -426,13 +426,10 @@ let local_module_name =
426426

427427
(* Unpack requires core_type package for type inference;
428428
use module type bindings and a function to create safe local names instead. *)
429-
let local_module_type_name =
430-
let v = ref 0 in
431-
fun ({txt} : Longident.t Location.loc) ->
432-
incr v;
433-
"__"
434-
^ (Longident.flatten txt |> List.fold_left (fun ll l -> ll ^ l) "")
435-
^ string_of_int !v ^ "__"
429+
let local_module_type_name txt =
430+
"_"
431+
^ (Longident.flatten txt |> List.fold_left (fun ll l -> ll ^ "_" ^ l) "")
432+
^ "__"
436433

437434
let expand_reverse (stru : Ast_structure.t) (acc : Ast_structure.t) :
438435
Ast_structure.t =
@@ -466,14 +463,15 @@ let expand_reverse (stru : Ast_structure.t) (acc : Ast_structure.t) :
466463
}
467464
:: acc)
468465

469-
let rec structure_mapper (self : mapper) (stru : Ast_structure.t) =
466+
let rec structure_mapper ~await_context (self : mapper) (stru : Ast_structure.t)
467+
=
470468
match stru with
471469
| [] -> []
472470
| item :: rest -> (
473471
match item.pstr_desc with
474472
| Pstr_extension (({txt = "bs.raw" | "raw"; loc}, payload), _attrs) ->
475473
Ast_exp_handle_external.handle_raw_structure loc payload
476-
:: structure_mapper self rest
474+
:: structure_mapper ~await_context self rest
477475
(* | Pstr_extension (({txt = "i"}, _),_)
478476
->
479477
structure_mapper self rest *)
@@ -493,7 +491,7 @@ let rec structure_mapper (self : mapper) (stru : Ast_structure.t) =
493491
next
494492
| PSig _ | PTyp _ | PPat _ ->
495493
Location.raise_errorf ~loc "private extension is not support")
496-
| _ -> expand_reverse acc (structure_mapper self rest)
494+
| _ -> expand_reverse acc (structure_mapper ~await_context self rest)
497495
in
498496
aux [] stru
499497
(* Dynamic import of module transformation: module M = @res.await Belt.List *)
@@ -502,30 +500,49 @@ let rec structure_mapper (self : mapper) (stru : Ast_structure.t) =
502500
as mb)
503501
when Res_parsetree_viewer.hasAwaitAttribute pmod_attributes ->
504502
let item = self.structure_item self item in
505-
let safe_module_type_name = local_module_type_name {txt; loc} in
503+
let safe_module_type_name = local_module_type_name txt in
504+
let has_local_module_name =
505+
Hashtbl.find_opt !await_context safe_module_type_name
506+
in
507+
(* module __Belt_List__ = module type of Belt.List *)
506508
let module_type_decl =
507-
let open Ast_helper in
508-
Str.modtype ~loc
509-
(Mtd.mk ~loc
510-
{txt = safe_module_type_name; loc}
511-
~typ:(Mty.typeof_ ~loc me))
509+
match has_local_module_name with
510+
| Some _ -> []
511+
| None ->
512+
let open Ast_helper in
513+
Hashtbl.add !await_context safe_module_type_name safe_module_type_name;
514+
[
515+
Str.modtype ~loc
516+
(Mtd.mk ~loc
517+
{txt = safe_module_type_name; loc}
518+
~typ:(Mty.typeof_ ~loc me));
519+
]
512520
in
513-
(* module __BeltList1__ = module type of Belt.List *)
514521
module_type_decl
515-
:: {
516-
item with
517-
pstr_desc =
518-
Pstr_module
519-
{
520-
mb with
521-
pmb_expr =
522-
Ast_await.create_await_module_expression
523-
~module_type_name:safe_module_type_name mb.pmb_expr;
524-
};
525-
}
526-
(* module M = @res.await Belt.List *)
527-
:: structure_mapper self rest
528-
| _ -> self.structure_item self item :: structure_mapper self rest)
522+
@ (* module M = @res.await Belt.List *)
523+
{
524+
item with
525+
pstr_desc =
526+
Pstr_module
527+
{
528+
mb with
529+
pmb_expr =
530+
Ast_await.create_await_module_expression
531+
~module_type_name:safe_module_type_name mb.pmb_expr;
532+
};
533+
}
534+
:: structure_mapper ~await_context self rest
535+
| _ ->
536+
self.structure_item self item :: structure_mapper ~await_context self rest
537+
)
538+
539+
let structure_mapper ~await_context (self : mapper) (stru : Ast_structure.t) =
540+
let await_saved = !await_context in
541+
let result =
542+
structure_mapper ~await_context:(ref (Hashtbl.create 10)) self stru
543+
in
544+
await_context := await_saved;
545+
result
529546

530547
let mapper : mapper =
531548
{
@@ -536,7 +553,7 @@ let mapper : mapper =
536553
signature_item = signature_item_mapper;
537554
value_bindings = Ast_tuple_pattern_flatten.value_bindings_mapper;
538555
structure_item = structure_item_mapper;
539-
structure = structure_mapper;
556+
structure = structure_mapper ~await_context:(ref (Hashtbl.create 10));
540557
(* Ad-hoc way to internalize stuff *)
541558
label_declaration =
542559
(fun self lbl ->

jscomp/test/Import.js

Lines changed: 36 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

jscomp/test/Import.res

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,25 @@ let beltAsModule = await Js.import(module(Belt.List: BeltList))
1515
// module M = unpack(@res.await Js.import(module(Belt.List: BeltList0)))
1616
module M = await Belt.List
1717
let each = M.forEach
18+
19+
module N = {
20+
module N0 = await Belt.List
21+
let each = N0.forEach
22+
23+
module N1 = {
24+
module O = await Belt.List
25+
let each = O.forEach
26+
}
27+
28+
module N2 = await Belt.List
29+
let each = N2.forEach
30+
}
31+
32+
module M0 = await Belt.List
33+
let each = M0.forEach
34+
35+
module M1 = await Belt.List
36+
let each = M1.forEach
37+
38+
module M2 = N.N1.O
39+
let each2 = M2.forEach

0 commit comments

Comments
 (0)