Skip to content

Commit d86706d

Browse files
SamChou19815meta-codesync[bot]
authored andcommitted
[flow] Refine the custom error message
Summary: This diff refines the custom error explanation a little bit. It will have the format of ``` Description of `T`: <desc>. Example for `T`: `<code>`. ``` Changelog: [internal] Reviewed By: panagosg7 Differential Revision: D86468338 fbshipit-source-id: a425065230a6d754f80b37e28cc7c8a80fb5debc
1 parent 4f21ecd commit d86706d

File tree

8 files changed

+118
-82
lines changed

8 files changed

+118
-82
lines changed

src/typing/errors/error_message.ml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -969,8 +969,8 @@ let map_loc_of_explanation (f : 'a -> 'b) =
969969
{ name; declaration = f declaration; providers = Base.List.map ~f providers }
970970
| ExplanationConcreteEnumCasting { representation_type; casting_syntax } ->
971971
ExplanationConcreteEnumCasting { representation_type; casting_syntax }
972-
| ExplanationCustomError { custom_error_loc } ->
973-
ExplanationCustomError { custom_error_loc = f custom_error_loc }
972+
| ExplanationCustomError { name; custom_error_loc } ->
973+
ExplanationCustomError { name; custom_error_loc = f custom_error_loc }
974974
| ExplanationFunctionsWithStaticsToObject -> ExplanationFunctionsWithStaticsToObject
975975
| ExplanationInvariantSubtypingDueToMutableArray
976976
{ lower_array_loc; upper_array_loc; lower_array_desc; upper_array_desc; upper_array_reason }
@@ -1865,12 +1865,13 @@ let rec convert_type_to_type_desc ~f =
18651865
match use_op with
18661866
| Op _ -> use_op
18671867
| Frame
1868-
( OpaqueTypeCustomErrorCompatibility { lower; upper; lower_t; upper_t; custom_error_loc },
1868+
( OpaqueTypeCustomErrorCompatibility
1869+
{ lower; upper; lower_t; upper_t; name; custom_error_loc },
18691870
use_op
18701871
) ->
18711872
Frame
18721873
( OpaqueTypeCustomErrorCompatibility
1873-
{ lower; upper; lower_t = f lower_t; upper_t = f upper_t; custom_error_loc },
1874+
{ lower; upper; lower_t = f lower_t; upper_t = f upper_t; name; custom_error_loc },
18741875
map_use_op use_op
18751876
)
18761877
| Frame (frame, use_op) -> Frame (frame, map_use_op use_op)

src/typing/errors/flow_intermediate_error.ml

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -988,7 +988,8 @@ let rec make_intermediate_error :
988988
~frame:FrameAnonymous
989989
~custom_error_message
990990
| Frame
991-
( OpaqueTypeCustomErrorCompatibility { lower; upper; lower_t; upper_t; custom_error_loc },
991+
( OpaqueTypeCustomErrorCompatibility
992+
{ lower; upper; lower_t; upper_t; name; custom_error_loc },
992993
use_op
993994
) ->
994995
let lower_desc =
@@ -1012,7 +1013,7 @@ let rec make_intermediate_error :
10121013
}
10131014
)
10141015
in
1015-
let frames = ([], [ExplanationCustomError { custom_error_loc }]) in
1016+
let frames = ([], [ExplanationCustomError { name; custom_error_loc }]) in
10161017
next_with_loc
10171018
~loc:(loc_of_aloc (loc_of_reason lower))
10181019
~frames
@@ -1912,7 +1913,7 @@ let to_printable_error :
19121913
text " using ";
19131914
code example;
19141915
]
1915-
| ExplanationCustomError { custom_error_loc } ->
1916+
| ExplanationCustomError { name; custom_error_loc } ->
19161917
let loc = loc_of_aloc custom_error_loc in
19171918
let custom_docs =
19181919
Loc.source loc
@@ -1928,11 +1929,37 @@ let to_printable_error :
19281929
)
19291930
)
19301931
|> Base.Option.bind ~f:(fun jsdoc ->
1931-
jsdoc |> Jsdoc.description |> Base.Option.map ~f:Base.String.strip
1932+
let description =
1933+
jsdoc |> Jsdoc.description |> Base.Option.map ~f:Base.String.strip
1934+
in
1935+
let example =
1936+
Base.List.Assoc.find
1937+
(Jsdoc.unrecognized_tags jsdoc)
1938+
~equal:Base.String.equal
1939+
"example"
1940+
|> Base.Option.bind ~f:Base.Fn.id
1941+
|> Base.Option.map ~f:Base.String.strip
1942+
in
1943+
match (description, example) with
1944+
| (None, None) -> None
1945+
| (Some d, None) -> Some [text "Description of "; code name; text ": "; text d]
1946+
| (None, Some e) -> Some [text "Example for "; code name; text ": "; code e]
1947+
| (Some d, Some e) ->
1948+
Some
1949+
[
1950+
text "Description of ";
1951+
code name;
1952+
text ": ";
1953+
text d;
1954+
text ". Example for ";
1955+
code name;
1956+
text ": ";
1957+
code e;
1958+
]
19321959
)
19331960
in
19341961
(match custom_docs with
1935-
| Some d -> [text d]
1962+
| Some m -> m
19361963
| None -> [text "See "; hardcoded_string_desc_ref "relevant docs" custom_error_loc])
19371964
| ExplanationReactComponentPropsDeepReadOnly props_loc ->
19381965
[

src/typing/errors/flow_intermediate_error_types.ml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,10 @@ type 'loc explanation =
308308
representation_type: string;
309309
casting_syntax: Options.CastingSyntax.t;
310310
}
311-
| ExplanationCustomError of { custom_error_loc: 'loc }
311+
| ExplanationCustomError of {
312+
name: string;
313+
custom_error_loc: 'loc;
314+
}
312315
| ExplanationFunctionsWithStaticsToObject
313316
| ExplanationInvariantSubtypingDueToMutableArray of {
314317
lower_array_loc: 'loc;

src/typing/subtyping_kit.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1196,7 +1196,7 @@ module Make (Flow : INPUT) : OUTPUT = struct
11961196
NominalT
11971197
( ru,
11981198
{
1199-
nominal_id = Nominal.UserDefinedOpaqueTypeId _;
1199+
nominal_id = Nominal.UserDefinedOpaqueTypeId (_, name);
12001200
underlying_t = Nominal.CustomError { t; custom_error_loc };
12011201
_;
12021202
}
@@ -1210,6 +1210,7 @@ module Make (Flow : INPUT) : OUTPUT = struct
12101210
upper = ru;
12111211
lower_t = TypeOrTypeDesc.Type l;
12121212
upper_t = TypeOrTypeDesc.Type u;
1213+
name;
12131214
custom_error_loc;
12141215
},
12151216
use_op

src/typing/type.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,7 @@ module rec TypeTerm : sig
513513
lower_t: 'loc TypeOrTypeDesc.t;
514514
upper_t: 'loc TypeOrTypeDesc.t;
515515
custom_error_loc: 'loc;
516+
name: string;
516517
}
517518
| MappedTypeKeyCompatibility of {
518519
source_type: 'loc virtual_reason;

src/typing/typeUtil.ml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,13 +430,15 @@ let rec mod_loc_of_virtual_use_op f =
430430
OpaqueTypeLowerBoundCompatibility { lower = mod_reason lower; upper = mod_reason upper }
431431
| OpaqueTypeUpperBoundCompatibility { lower; upper } ->
432432
OpaqueTypeUpperBoundCompatibility { lower = mod_reason lower; upper = mod_reason upper }
433-
| OpaqueTypeCustomErrorCompatibility { lower; upper; lower_t; upper_t; custom_error_loc } ->
433+
| OpaqueTypeCustomErrorCompatibility { lower; upper; lower_t; upper_t; name; custom_error_loc }
434+
->
434435
OpaqueTypeCustomErrorCompatibility
435436
{
436437
lower = mod_reason lower;
437438
upper = mod_reason upper;
438439
lower_t = TypeOrTypeDesc.map_loc f lower_t;
439440
upper_t = TypeOrTypeDesc.map_loc f upper_t;
441+
name;
440442
custom_error_loc = f custom_error_loc;
441443
}
442444
| MappedTypeKeyCompatibility { source_type; mapped_type } ->

0 commit comments

Comments
 (0)