Skip to content

Commit

Permalink
Improve reason for mapped object keys
Browse files Browse the repository at this point in the history
Summary:
The type of the key mapped through $ObjMapi currently has the reason description
"object mapi" which isn't great. We know the type is a string literal, so we
can use that better description.

Reviewed By: mroch

Differential Revision: D4194731

fbshipit-source-id: a4a7003da4002b171fe0016cee33eb54989a150c
  • Loading branch information
samwgoldman authored and Facebook Github Bot committed Nov 17, 2016
1 parent e4ed1fa commit b857236
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
38 changes: 25 additions & 13 deletions src/typing/flow_js.ml
Original file line number Diff line number Diff line change
Expand Up @@ -4054,11 +4054,14 @@ let rec __flow cx ((l: Type.t), (u: Type.use_t)) trace =
(* function types can be mapped over a structure *)
(**************************************************)

| ArrT (_, t, ts), MapTypeT (reason, TupleMap, funt, k) ->
let f x = mk_tvar_where cx reason (fun t ->
rec_flow cx trace (funt, CallT (reason, mk_functiontype [x] t))
| ArrT (_, t, ts), MapTypeT (reason_op, TupleMap, funt, k) ->
let f x = mk_tvar_where cx reason_op (fun t ->
rec_flow cx trace (funt, CallT (reason_op, mk_functiontype [x] t))
) in
let t = ArrT (reason, f t, List.map f ts) in
let t =
let reason = replace_reason_const RArrayType reason_op in
ArrT (reason, f t, List.map f ts)
in
continue cx trace t k

| _, MapTypeT (reason, TupleMap, funt, k) ->
Expand All @@ -4069,10 +4072,10 @@ let rec __flow cx ((l: Type.t), (u: Type.use_t)) trace =
let t = ArrT (reason, elem_t, []) in
rec_flow cx trace (t, MapTypeT (reason, TupleMap, funt, k))

| ObjT (_, o), MapTypeT (reason, ObjectMap, funt, k) ->
let map_t t = mk_tvar_where cx reason (fun t' ->
| ObjT (_, o), MapTypeT (reason_op, ObjectMap, funt, k) ->
let map_t t = mk_tvar_where cx reason_op (fun t' ->
let funtype = mk_functiontype [t] t' in
rec_flow cx trace (funt, CallT (reason, funtype))
rec_flow cx trace (funt, CallT (reason_op, funtype))
) in
let props_tmap =
Context.find_props cx o.props_tmap
Expand All @@ -4083,15 +4086,21 @@ let rec __flow cx ((l: Type.t), (u: Type.use_t)) trace =
let value = map_t dict.value in
{dict with value}
) o.dict_t in
let mapped_t = ObjT (reason, {o with props_tmap; dict_t}) in
let mapped_t =
let reason = replace_reason_const RObjectType reason_op in
ObjT (reason, {o with props_tmap; dict_t})
in
continue cx trace mapped_t k

| ObjT (_, o), MapTypeT (reason, ObjectMapi, funt, k) ->
let mapi_t key t = mk_tvar_where cx reason (fun t' ->
| ObjT (_, o), MapTypeT (reason_op, ObjectMapi, funt, k) ->
let mapi_t key t = mk_tvar_where cx reason_op (fun t' ->
let funtype = mk_functiontype [key; t] t' in
rec_flow cx trace (funt, CallT (reason, funtype))
rec_flow cx trace (funt, CallT (reason_op, funtype))
) in
let mapi_field key t = mapi_t (SingletonStrT (reason, key)) t in
let mapi_field key t =
let reason = replace_reason_const (RStringLit key) reason_op in
mapi_t (SingletonStrT (reason, key)) t
in
let props_tmap =
Context.find_props cx o.props_tmap
|> Properties.mapi_fields mapi_field
Expand All @@ -4101,7 +4110,10 @@ let rec __flow cx ((l: Type.t), (u: Type.use_t)) trace =
let value = mapi_t dict.key dict.value in
{dict with value}
) o.dict_t in
let mapped_t = ObjT (reason, {o with props_tmap; dict_t}) in
let mapped_t =
let reason = replace_reason_const RObjectType reason_op in
ObjT (reason, {o with props_tmap; dict_t})
in
continue cx trace mapped_t k

(***********************************************)
Expand Down
2 changes: 1 addition & 1 deletion tests/objmap/objmap.exp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
objmap.js:10
10: (o.FOO : 'BAR'); // error, 'FOO' incompatible with 'BAR'
^^^^^ object mapi. Expected string literal `BAR`, got `FOO` instead
^^^^^ string literal `FOO`. Expected string literal `BAR`, got `FOO` instead
10: (o.FOO : 'BAR'); // error, 'FOO' incompatible with 'BAR'
^^^^^ string literal `BAR`

Expand Down

0 comments on commit b857236

Please sign in to comment.