@@ -426,13 +426,10 @@ let local_module_name =
426
426
427
427
(* Unpack requires core_type package for type inference;
428
428
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
+ ^ " __"
436
433
437
434
let expand_reverse (stru : Ast_structure.t ) (acc : Ast_structure.t ) :
438
435
Ast_structure. t =
@@ -466,14 +463,15 @@ let expand_reverse (stru : Ast_structure.t) (acc : Ast_structure.t) :
466
463
}
467
464
:: acc)
468
465
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
+ =
470
468
match stru with
471
469
| [] -> []
472
470
| item :: rest -> (
473
471
match item.pstr_desc with
474
472
| Pstr_extension (({txt = "bs.raw" | "raw" ; loc} , payload ), _attrs ) ->
475
473
Ast_exp_handle_external. handle_raw_structure loc payload
476
- :: structure_mapper self rest
474
+ :: structure_mapper ~await_context self rest
477
475
(* | Pstr_extension (({txt = "i"}, _),_)
478
476
->
479
477
structure_mapper self rest *)
@@ -493,7 +491,7 @@ let rec structure_mapper (self : mapper) (stru : Ast_structure.t) =
493
491
next
494
492
| PSig _ | PTyp _ | PPat _ ->
495
493
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)
497
495
in
498
496
aux [] stru
499
497
(* 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) =
502
500
as mb)
503
501
when Res_parsetree_viewer. hasAwaitAttribute pmod_attributes ->
504
502
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 *)
506
508
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
+ ]
512
520
in
513
- (* module __BeltList1__ = module type of Belt.List *)
514
521
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
529
546
530
547
let mapper : mapper =
531
548
{
@@ -536,7 +553,7 @@ let mapper : mapper =
536
553
signature_item = signature_item_mapper;
537
554
value_bindings = Ast_tuple_pattern_flatten. value_bindings_mapper;
538
555
structure_item = structure_item_mapper;
539
- structure = structure_mapper;
556
+ structure = structure_mapper ~await_context: ( ref ( Hashtbl. create 10 )) ;
540
557
(* Ad-hoc way to internalize stuff *)
541
558
label_declaration =
542
559
(fun self lbl ->
0 commit comments