@@ -336,6 +336,33 @@ type frozen_kind =
336
336
337
337
let ignore2 _ _ = ()
338
338
339
+ let loc_of_binding = function
340
+ | LocalBinding node ->
341
+ (match Local_defs. value node with
342
+ | VarBinding { id_loc; _ }
343
+ | LetConstBinding { id_loc; _ }
344
+ | ConstRefBinding { id_loc; _ }
345
+ | ConstFunBinding { id_loc; _ }
346
+ | ClassBinding { id_loc; _ }
347
+ | DeclareClassBinding { id_loc; _ }
348
+ | FunBinding { id_loc; _ }
349
+ | ComponentBinding { id_loc; _ }
350
+ | EnumBinding { id_loc; _ }
351
+ | NamespaceBinding { id_loc; _ }
352
+ | TypeBinding { id_loc; _ } ->
353
+ id_loc
354
+ | DeclareFunBinding { defs_rev; _ } ->
355
+ let (id_loc, _, _) = Nel. last defs_rev in
356
+ id_loc)
357
+ | RemoteBinding node ->
358
+ (match Remote_refs. value node with
359
+ | ImportBinding { id_loc; _ }
360
+ | ImportTypeBinding { id_loc; _ }
361
+ | ImportTypeofBinding { id_loc; _ }
362
+ | ImportNsBinding { id_loc; _ }
363
+ | ImportTypeofNsBinding { id_loc; _ } ->
364
+ id_loc)
365
+
339
366
let create_tables () =
340
367
{
341
368
locs = Locs. create () ;
@@ -608,16 +635,46 @@ module Scope = struct
608
635
| ID. ImportTypeof -> ImportTypeofNsBinding { id_loc; name; mref }
609
636
| ID. ImportType -> failwith " unexpected import type *"
610
637
611
- let bind ~type_only scope name f =
612
- let bind_value values types =
638
+ let bind ~type_only scope tbls name id_loc f =
639
+ let bind_updater ~in_global_scope f existing_binding =
640
+ (* For now, we allow $JSXIntrinsics, since we intentionally kept only a minimal version in
641
+ * the builtins, and let flow-typed supply a full one. *)
642
+ if in_global_scope && name <> " $JSXIntrinsics" then (
643
+ let (binding, legal) = f existing_binding in
644
+ (match (legal, existing_binding) with
645
+ | (false , Some existing_binding ) ->
646
+ let override_binding_loc = loc_of_binding existing_binding in
647
+ (* Consider
648
+ * ```
649
+ * declare const foo: string;
650
+ * declare const foo: number;
651
+ * ```
652
+ * The first one is considered the overriding one,
653
+ * while the second one is considered to be the existing one.
654
+ * This ensures that the builtin libdefs (which come last) will always considered to be the
655
+ * existing ones.
656
+ * *)
657
+ tbls.additional_errors < -
658
+ Signature_error. NameOverride
659
+ { name; override_binding_loc; existing_binding_loc = id_loc }
660
+ :: tbls.additional_errors
661
+ | _ -> () );
662
+ binding
663
+ ) else
664
+ let (binding, _legal) = f existing_binding in
665
+ binding
666
+ in
667
+ let bind_value ~in_global_scope f values types =
668
+ let f = bind_updater ~in_global_scope f in
613
669
match SMap. find_opt name values with
614
670
| Some _ -> SMap. update name f values
615
671
| None ->
616
672
(match SMap. find_opt name types with
617
673
| None -> SMap. update name f values
618
674
| Some _ -> values)
619
675
in
620
- let bind_type values types =
676
+ let bind_type ~in_global_scope f values types =
677
+ let f = bind_updater ~in_global_scope f in
621
678
match SMap. find_opt name types with
622
679
| Some _ -> SMap. update name f types
623
680
| None ->
@@ -627,19 +684,24 @@ module Scope = struct
627
684
in
628
685
if type_only then
629
686
match scope with
630
- | Global scope -> scope.types < - bind_type scope.values scope.types
631
- | DeclareModule scope -> scope.types < - bind_type scope.values scope.types
632
- | DeclareNamespace scope -> scope.types < - bind_type scope.values scope.types
633
- | Module scope -> scope.types < - bind_type scope.values scope.types
634
- | Lexical scope -> scope.types < - bind_type scope.values scope.types
687
+ | Global scope -> scope.types < - bind_type ~in_global_scope: true f scope.values scope.types
688
+ | DeclareModule scope ->
689
+ scope.types < - bind_type ~in_global_scope: false f scope.values scope.types
690
+ | DeclareNamespace scope ->
691
+ scope.types < - bind_type ~in_global_scope: false f scope.values scope.types
692
+ | Module scope -> scope.types < - bind_type ~in_global_scope: false f scope.values scope.types
693
+ | Lexical scope -> scope.types < - bind_type ~in_global_scope: false f scope.values scope.types
635
694
| ConditionalTypeExtends _ -> ()
636
695
else
637
696
match scope with
638
- | Global scope -> scope.values < - bind_value scope.values scope.types
639
- | DeclareModule scope -> scope.values < - bind_value scope.values scope.types
640
- | DeclareNamespace scope -> scope.values < - bind_value scope.values scope.types
641
- | Module scope -> scope.values < - bind_value scope.values scope.types
642
- | Lexical scope -> scope.values < - bind_value scope.values scope.types
697
+ | Global scope -> scope.values < - bind_value ~in_global_scope: true f scope.values scope.types
698
+ | DeclareModule scope ->
699
+ scope.values < - bind_value ~in_global_scope: false f scope.values scope.types
700
+ | DeclareNamespace scope ->
701
+ scope.values < - bind_value ~in_global_scope: false f scope.values scope.types
702
+ | Module scope -> scope.values < - bind_value ~in_global_scope: false f scope.values scope.types
703
+ | Lexical scope ->
704
+ scope.values < - bind_value ~in_global_scope: false f scope.values scope.types
643
705
| ConditionalTypeExtends _ -> ()
644
706
645
707
let rec lookup_value scope name =
@@ -704,41 +766,41 @@ module Scope = struct
704
766
| Some l when l = loc -> Some scope
705
767
| _ -> None )
706
768
707
- let bind_local scope tbls name def k =
769
+ let bind_local scope tbls name id_loc def k =
708
770
let host = find_host scope def in
709
- bind host name (function
710
- | Some _ as existing_binding -> existing_binding
771
+ bind host tbls name id_loc (function
772
+ | Some _ as existing_binding -> ( existing_binding, false )
711
773
| None ->
712
774
let node = push_local_def tbls def in
713
775
k name node;
714
- Some (LocalBinding node)
776
+ ( Some (LocalBinding node), true )
715
777
)
716
778
717
- let bind_remote scope tbls name ref =
718
- bind scope name (function
719
- | Some _ as existing_binding -> existing_binding
779
+ let bind_remote scope tbls name id_loc ref =
780
+ bind scope tbls name id_loc (function
781
+ | Some _ as existing_binding -> ( existing_binding, false )
720
782
| None ->
721
783
let node = push_remote_ref tbls ref in
722
- Some (RemoteBinding node)
784
+ ( Some (RemoteBinding node), true )
723
785
)
724
786
725
787
let bind_type scope tbls id_loc name def =
726
- bind_local ~type_only: true scope tbls name (TypeBinding { id_loc; def })
788
+ bind_local ~type_only: true scope tbls name id_loc (TypeBinding { id_loc; def })
727
789
728
790
let bind_class scope tbls id_loc name def =
729
- bind_local ~type_only: false scope tbls name (ClassBinding { id_loc; name; def })
791
+ bind_local ~type_only: false scope tbls name id_loc (ClassBinding { id_loc; name; def })
730
792
731
793
let bind_declare_class scope tbls id_loc name def =
732
- bind_local ~type_only: false scope tbls name (DeclareClassBinding { id_loc; name; def })
794
+ bind_local ~type_only: false scope tbls name id_loc (DeclareClassBinding { id_loc; name; def })
733
795
734
796
let bind_enum scope tbls id_loc name def =
735
- bind_local ~type_only: false scope tbls name (EnumBinding { id_loc; name; def })
797
+ bind_local ~type_only: false scope tbls name id_loc (EnumBinding { id_loc; name; def })
736
798
737
799
(* Function declarations preceded by declared functions are taken to have the
738
800
* type of the declared functions. This is a weird special case aimed to
739
801
* support overloaded signatures. *)
740
802
let bind_function scope tbls id_loc fn_loc name ~async ~generator ~effect :_ def k =
741
- bind ~type_only: false scope name (fun binding_opt ->
803
+ bind ~type_only: false scope tbls name id_loc (fun binding_opt ->
742
804
match binding_opt with
743
805
| None ->
744
806
let statics = SMap. empty in
@@ -747,52 +809,60 @@ module Scope = struct
747
809
in
748
810
let node = push_local_def tbls def in
749
811
k name node;
750
- Some (LocalBinding node)
751
- | Some (RemoteBinding _ ) -> binding_opt
812
+ ( Some (LocalBinding node), true )
813
+ | Some (RemoteBinding _ ) -> ( binding_opt, false )
752
814
| Some (LocalBinding node ) ->
753
815
(match Local_defs. value node with
754
816
| DeclareFunBinding _ -> k name node
755
817
| _ -> () );
756
- binding_opt
818
+ ( binding_opt, false )
757
819
)
758
820
759
821
(* Multiple declared functions with the same name in the same scope define an
760
822
* overloaded function. Note that declared functions are block scoped, so we
761
823
* don't need to walk the scope chain since the scope argument is certainly
762
824
* the host scope. *)
763
825
let bind_declare_function scope tbls id_loc fn_loc name def k =
764
- bind ~type_only: false scope name (fun binding_opt ->
826
+ bind ~type_only: false scope tbls name id_loc (fun binding_opt ->
765
827
match binding_opt with
766
828
| None ->
767
829
let defs_rev = Nel. one (id_loc, fn_loc, def) in
768
830
let def = DeclareFunBinding { name; defs_rev } in
769
831
let node = push_local_def tbls def in
770
832
k name node;
771
- Some (LocalBinding node)
772
- | Some (RemoteBinding _ ) -> binding_opt
833
+ ( Some (LocalBinding node), true )
834
+ | Some (RemoteBinding _ ) -> ( binding_opt, false )
773
835
| Some (LocalBinding node ) ->
836
+ let legal_ref = ref false in
774
837
Local_defs. modify node (function
775
838
| DeclareFunBinding { name; defs_rev } ->
776
839
k name node;
840
+ legal_ref := true ;
777
841
let defs_rev = Nel. cons (id_loc, fn_loc, def) defs_rev in
778
842
DeclareFunBinding { name; defs_rev }
779
843
| def -> def
780
844
);
781
- binding_opt
845
+ ( binding_opt, ! legal_ref)
782
846
)
783
847
784
848
let bind_component scope tbls id_loc fn_loc name def =
785
- bind_local ~type_only: false scope tbls name (ComponentBinding { id_loc; fn_loc; name; def })
849
+ bind_local
850
+ ~type_only: false
851
+ scope
852
+ tbls
853
+ name
854
+ id_loc
855
+ (ComponentBinding { id_loc; fn_loc; name; def })
786
856
787
857
let bind_var scope tbls kind id_loc name def =
788
- bind_local ~type_only: false scope tbls name (value_binding kind id_loc name def)
858
+ bind_local ~type_only: false scope tbls name id_loc (value_binding kind id_loc name def)
789
859
790
860
let bind_const scope tbls id_loc name def =
791
- bind_local ~type_only: false scope tbls name (LetConstBinding { id_loc; name; def })
861
+ bind_local ~type_only: false scope tbls name id_loc (LetConstBinding { id_loc; name; def })
792
862
793
863
let bind_const_ref scope tbls id_loc name ref_loc ref_name ref_scope =
794
864
let ref = Ref { ref_loc; name = ref_name; scope = ref_scope; resolved = None } in
795
- bind_local ~type_only: false scope tbls name (ConstRefBinding { id_loc; name; ref })
865
+ bind_local ~type_only: false scope tbls name id_loc (ConstRefBinding { id_loc; name; ref })
796
866
797
867
let bind_const_fun scope tbls id_loc name loc ~async ~generator def =
798
868
let statics = SMap. empty in
@@ -801,6 +871,7 @@ module Scope = struct
801
871
scope
802
872
tbls
803
873
name
874
+ id_loc
804
875
(ConstFunBinding { id_loc; name; loc; async; generator; def; statics })
805
876
806
877
let bind_import scope tbls kind id_loc ~local ~remote mref =
@@ -813,7 +884,7 @@ module Scope = struct
813
884
| ImportTypeof ->
814
885
true
815
886
in
816
- bind_remote ~type_only scope tbls local (import_binding kind id_loc local mref ~remote )
887
+ bind_remote ~type_only scope tbls local id_loc (import_binding kind id_loc local mref ~remote )
817
888
818
889
let bind_import_ns scope tbls kind id_loc name mref =
819
890
let mref = push_module_ref tbls mref in
@@ -825,7 +896,7 @@ module Scope = struct
825
896
| ImportTypeof ->
826
897
true
827
898
in
828
- bind_remote ~type_only scope tbls name (import_ns_binding kind id_loc name mref)
899
+ bind_remote ~type_only scope tbls name id_loc (import_ns_binding kind id_loc name mref)
829
900
830
901
let rec assign_binding =
831
902
let f prop_name prop def =
@@ -1185,6 +1256,7 @@ module Scope = struct
1185
1256
parent
1186
1257
tbls
1187
1258
name
1259
+ id_loc
1188
1260
(NamespaceBinding { id_loc; name; values; types })
1189
1261
ignore2
1190
1262
| _ -> failwith " The scope must be lexical"
@@ -1206,6 +1278,7 @@ module Scope = struct
1206
1278
scope
1207
1279
tbls
1208
1280
name
1281
+ loc
1209
1282
(NamespaceBinding { id_loc = loc; name; values; types })
1210
1283
ignore2
1211
1284
| _ -> failwith " finalize_globalThis must be called after parsing all lib files on global scope"
0 commit comments