Skip to content

Commit

Permalink
[typing] Add globalThis type
Browse files Browse the repository at this point in the history
  • Loading branch information
goodmind committed Jul 9, 2019
1 parent c48b92c commit dd3ec7f
Show file tree
Hide file tree
Showing 12 changed files with 37 additions and 4 deletions.
2 changes: 2 additions & 0 deletions lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -908,3 +908,5 @@ declare var console: {
warn(...data: Array<any>): void,
...
};

declare var globalThis: $GlobalThis;
1 change: 1 addition & 0 deletions src/typing/coverage.ml
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ class visitor = object (self)
| FunProtoApplyT _
| FunProtoBindT _
| FunProtoCallT _
| GlobalThisT _
| InternalT _
| KeysT _
| MaybeT _
Expand Down
4 changes: 3 additions & 1 deletion src/typing/debug_js.ml
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ and _json_of_t_impl json_cx t = Hh_json.(
| FunProtoApplyT _
| FunProtoBindT _
| FunProtoCallT _
| GlobalThisT _
-> []

| DefT (_, _, FunT (static, proto, funtype)) -> [
Expand Down Expand Up @@ -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)))
Expand Down
12 changes: 11 additions & 1 deletion src/typing/flow_js.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 *)
Expand Down Expand Up @@ -7729,6 +7738,7 @@ and check_polarity cx ?trace polarity = function
| FunProtoApplyT _
| FunProtoBindT _
| FunProtoCallT _
| GlobalThisT _
| EvalT _
| InternalT (ExtendsT _)
| InternalT (ChoiceKitT _)
Expand Down
1 change: 1 addition & 0 deletions src/typing/members.ml
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,7 @@ let rec extract_type cx this_t = match this_t with
| FunProtoBindT _
| FunProtoCallT _
| FunProtoT _
| GlobalThisT _
| KeysT (_, _)
| DefT (_, _, MixedT _)
| NullProtoT _
Expand Down
1 change: 1 addition & 0 deletions src/typing/resolvableTypeJob.ml
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ and collect_of_type ?log_unresolved cx acc = function
| FunProtoT _
| NullProtoT _
| ObjProtoT _
| GlobalThisT _
| CustomFunT (_, _)

| ExistsT _
Expand Down
2 changes: 2 additions & 0 deletions src/typing/sigHash.ml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type hash =
| FunProtoApplyH
| FunProtoBindH
| FunProtoCallH
| GlobalThisH
| ObjH
| ObjProtoH
| MatchingPropH
Expand Down Expand Up @@ -222,6 +223,7 @@ let hash_of_ctor = Type.(function
| FunProtoApplyT _ -> FunProtoApplyH
| FunProtoBindT _ -> FunProtoBindH
| FunProtoCallT _ -> FunProtoCallH
| GlobalThisT _ -> GlobalThisH
| IntersectionT _ -> IntersectionH
| KeysT _ -> KeysH
| MaybeT _ -> MaybeH
Expand Down
1 change: 1 addition & 0 deletions src/typing/ty_normalizer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,7 @@ end = struct
| ModuleT (reason, _, _) -> module_t env reason t

| DefT (_, _, CharSetT _)
| GlobalThisT _
| NullProtoT _ ->
terr ~kind:UnsupportedTypeCtor (Some t)

Expand Down
7 changes: 6 additions & 1 deletion src/typing/type.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 *)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -3118,6 +3122,7 @@ let string_of_ctor = function
| FunProtoApplyT _ -> "FunProtoApplyT"
| FunProtoBindT _ -> "FunProtoBindT"
| FunProtoCallT _ -> "FunProtoCallT"
| GlobalThisT _ -> "GlobalThisT"
| KeysT _ -> "KeysT"
| ModuleT _ -> "ModuleT"
| NullProtoT _ -> "NullProtoT"
Expand Down
6 changes: 6 additions & 0 deletions src/typing/type_annotation.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/typing/type_mapper.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/typing/type_visitor.ml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class ['a] t = object(self)
| FunProtoCallT _
| ObjProtoT _
| NullProtoT _
| GlobalThisT _
-> acc

| CustomFunT (_, kind) -> self#custom_fun_kind cx acc kind
Expand Down

0 comments on commit dd3ec7f

Please sign in to comment.