diff --git a/lib/core.js b/lib/core.js index 590bc1dc798..d224ecc44ba 100644 --- a/lib/core.js +++ b/lib/core.js @@ -908,3 +908,5 @@ declare var console: { warn(...data: Array): void, ... }; + +declare var globalThis: $GlobalThis; \ No newline at end of file diff --git a/src/typing/coverage.ml b/src/typing/coverage.ml index a233fdbbefe..c664af782b7 100644 --- a/src/typing/coverage.ml +++ b/src/typing/coverage.ml @@ -189,6 +189,7 @@ class visitor = object (self) | FunProtoApplyT _ | FunProtoBindT _ | FunProtoCallT _ + | GlobalThisT _ | InternalT _ | KeysT _ | MaybeT _ diff --git a/src/typing/debug_js.ml b/src/typing/debug_js.ml index c4722c28113..32f561f763c 100644 --- a/src/typing/debug_js.ml +++ b/src/typing/debug_js.ml @@ -173,6 +173,7 @@ and _json_of_t_impl json_cx t = Hh_json.( | FunProtoApplyT _ | FunProtoBindT _ | FunProtoCallT _ + | GlobalThisT _ -> [] | DefT (_, _, FunT (static, proto, funtype)) -> [ @@ -1796,7 +1797,8 @@ let rec dump_t_ (depth, tvars) cx t = | FunProtoT _ | FunProtoApplyT _ | FunProtoBindT _ - | FunProtoCallT _ -> p t + | FunProtoCallT _ + | GlobalThisT _ -> p t | DefT (_, trust, PolyT (_, tps, c, id)) -> p ~trust:(Some trust) ~extra:(spf "%s [%s] #%d" (kid c) (String.concat "; " (Core_list.map ~f:(fun tp -> tp.name) (Nel.to_list tps))) diff --git a/src/typing/flow_js.ml b/src/typing/flow_js.ml index 27aa6780f33..a979825a8d4 100644 --- a/src/typing/flow_js.ml +++ b/src/typing/flow_js.ml @@ -6522,6 +6522,14 @@ let rec __flow cx ((l: Type.t), (u: Type.use_t)) trace = | FunProtoCallT reason, _ -> rec_flow cx trace (FunProtoT reason, u) + | GlobalThisT _, + SetPropT (_, _, Named (_, name), _, tin, _) -> + set_builtin cx ~trace name tin; + + | GlobalThisT _, _ -> + let t = builtins cx in + rec_flow cx trace (t, u) + | _, LookupT (_, _, _, propref, lookup_action) -> let use_op = use_op_of_lookup_action lookup_action in add_output cx ~trace (Error_message.EIncompatibleProp { @@ -7308,7 +7316,8 @@ and any_propagated_use cx trace use_op any l = | FunProtoCallT _ | FunProtoT _ | ObjProtoT _ - | NullProtoT _ -> + | NullProtoT _ + | GlobalThisT _ -> true (* Handled already in __flow *) @@ -7729,6 +7738,7 @@ and check_polarity cx ?trace polarity = function | FunProtoApplyT _ | FunProtoBindT _ | FunProtoCallT _ + | GlobalThisT _ | EvalT _ | InternalT (ExtendsT _) | InternalT (ChoiceKitT _) diff --git a/src/typing/members.ml b/src/typing/members.ml index efbe9420eb4..a33b2390647 100644 --- a/src/typing/members.ml +++ b/src/typing/members.ml @@ -431,6 +431,7 @@ let rec extract_type cx this_t = match this_t with | FunProtoBindT _ | FunProtoCallT _ | FunProtoT _ + | GlobalThisT _ | KeysT (_, _) | DefT (_, _, MixedT _) | NullProtoT _ diff --git a/src/typing/resolvableTypeJob.ml b/src/typing/resolvableTypeJob.ml index b2381056218..20bb67a7cdf 100644 --- a/src/typing/resolvableTypeJob.ml +++ b/src/typing/resolvableTypeJob.ml @@ -251,6 +251,7 @@ and collect_of_type ?log_unresolved cx acc = function | FunProtoT _ | NullProtoT _ | ObjProtoT _ + | GlobalThisT _ | CustomFunT (_, _) | ExistsT _ diff --git a/src/typing/sigHash.ml b/src/typing/sigHash.ml index a119ac8895a..5a80dba0018 100644 --- a/src/typing/sigHash.ml +++ b/src/typing/sigHash.ml @@ -48,6 +48,7 @@ type hash = | FunProtoApplyH | FunProtoBindH | FunProtoCallH + | GlobalThisH | ObjH | ObjProtoH | MatchingPropH @@ -222,6 +223,7 @@ let hash_of_ctor = Type.(function | FunProtoApplyT _ -> FunProtoApplyH | FunProtoBindT _ -> FunProtoBindH | FunProtoCallT _ -> FunProtoCallH + | GlobalThisT _ -> GlobalThisH | IntersectionT _ -> IntersectionH | KeysT _ -> KeysH | MaybeT _ -> MaybeH diff --git a/src/typing/ty_normalizer.ml b/src/typing/ty_normalizer.ml index cafdaff6951..65f514672a4 100644 --- a/src/typing/ty_normalizer.ml +++ b/src/typing/ty_normalizer.ml @@ -705,6 +705,7 @@ end = struct | ModuleT (reason, _, _) -> module_t env reason t | DefT (_, _, CharSetT _) + | GlobalThisT _ | NullProtoT _ -> terr ~kind:UnsupportedTypeCtor (Some t) diff --git a/src/typing/type.ml b/src/typing/type.ml index f53a38b1ce8..f5703706679 100644 --- a/src/typing/type.ml +++ b/src/typing/type.ml @@ -86,7 +86,7 @@ module rec TypeTerm : sig (* exact *) | ExactT of reason * t - | FunProtoT of reason (* Function.prototype *) + | FunProtoT of reason (* Function.prototype *) | ObjProtoT of reason (* Object.prototype *) (* Signifies the end of the prototype chain. Distinct from NullT when it @@ -97,6 +97,8 @@ module rec TypeTerm : sig | FunProtoBindT of reason (* Function.prototype.bind *) | FunProtoCallT of reason (* Function.prototype.call *) + | GlobalThisT of reason (* globalThis *) + (* generalizations of AnyT *) | AnyWithLowerBoundT of t (* any supertype of t *) | AnyWithUpperBoundT of t (* any subtype of t *) @@ -2143,6 +2145,7 @@ end = struct | FunProtoApplyT reason -> reason | FunProtoBindT reason -> reason | FunProtoCallT reason -> reason + | GlobalThisT reason -> reason | KeysT (reason, _) -> reason | ModuleT (reason, _, _) -> reason | NullProtoT reason -> reason @@ -2307,6 +2310,7 @@ end = struct | FunProtoT (reason) -> FunProtoT (f reason) | FunProtoBindT (reason) -> FunProtoBindT (f reason) | FunProtoCallT (reason) -> FunProtoCallT (f reason) + | GlobalThisT (reason) -> GlobalThisT (f reason) | KeysT (reason, t) -> KeysT (f reason, t) | ModuleT (reason, exports, is_strict) -> ModuleT (f reason, exports, is_strict) | NullProtoT reason -> NullProtoT (f reason) @@ -3118,6 +3122,7 @@ let string_of_ctor = function | FunProtoApplyT _ -> "FunProtoApplyT" | FunProtoBindT _ -> "FunProtoBindT" | FunProtoCallT _ -> "FunProtoCallT" + | GlobalThisT _ -> "GlobalThisT" | KeysT _ -> "KeysT" | ModuleT _ -> "ModuleT" | NullProtoT _ -> "NullProtoT" diff --git a/src/typing/type_annotation.ml b/src/typing/type_annotation.ml index 746fdb97f20..33fa0eb921e 100644 --- a/src/typing/type_annotation.ml +++ b/src/typing/type_annotation.ml @@ -851,6 +851,12 @@ let rec convert cx tparams_map = Ast.Type.(function reconstruct_ast (AnyT.make Annotated reason) None ) + | "$GlobalThis" -> + check_type_arg_arity cx loc t_ast targs 0 (fun () -> + let reason = mk_reason (RCustom "global this") loc in + reconstruct_ast (GlobalThisT reason) None + ) + | "Function$Prototype$Apply" -> check_type_arg_arity cx loc t_ast targs 0 (fun () -> let reason = mk_reason RFunctionType loc in diff --git a/src/typing/type_mapper.ml b/src/typing/type_mapper.ml index 04f7ce8c309..d4b470fdd04 100644 --- a/src/typing/type_mapper.ml +++ b/src/typing/type_mapper.ml @@ -107,7 +107,8 @@ class virtual ['a] t = object(self) | NullProtoT _ | FunProtoApplyT _ | FunProtoBindT _ - | FunProtoCallT _ -> t + | FunProtoCallT _ + | GlobalThisT _ -> t | AnyWithLowerBoundT t' -> let t'' = self#type_ cx map_cx t' in if t'' == t' then t diff --git a/src/typing/type_visitor.ml b/src/typing/type_visitor.ml index fc14976c7e4..7fc5319e6ed 100644 --- a/src/typing/type_visitor.ml +++ b/src/typing/type_visitor.ml @@ -37,6 +37,7 @@ class ['a] t = object(self) | FunProtoCallT _ | ObjProtoT _ | NullProtoT _ + | GlobalThisT _ -> acc | CustomFunT (_, kind) -> self#custom_fun_kind cx acc kind