Skip to content

Commit a3f8425

Browse files
SamChou19815facebook-github-bot
authored andcommitted
[flow][try] Dev-only config to show the underlying type representation
Summary: Changelog: [internal] Reviewed By: panagosg7 Differential Revision: D55766240 fbshipit-source-id: fd485cfdf62fb830e527c40b512175b306282ebd
1 parent 871e1e5 commit a3f8425

File tree

3 files changed

+44
-18
lines changed

3 files changed

+44
-18
lines changed

src/flow_dot_js.ml

+30-18
Original file line numberDiff line numberDiff line change
@@ -474,24 +474,29 @@ let infer_type filename content line col js_config_object : Loc.t * (string, str
474474
let (cx, typed_ast) = infer_and_merge ~root filename js_config_object docblock ast file_sig in
475475
let loc = mk_loc filename line col in
476476
let open Query_types in
477-
let result =
478-
type_at_pos_type
479-
~cx
480-
~file_sig
481-
~omit_targ_defaults:false
482-
~typed_ast
483-
~verbose_normalizer:false
484-
~max_depth:50
485-
~no_typed_ast_for_imports:false
486-
loc
487-
in
488-
begin
489-
match result with
490-
| FailureNoMatch -> (Loc.none, Error "No match")
491-
| FailureUnparseable (loc, _, _) -> (loc, Error "Unparseable")
492-
| Success (loc, result) ->
493-
(loc, Ok (Ty_printer.string_of_type_at_pos_result ~exact_by_default:true result))
494-
end
477+
if Js.Unsafe.get js_config_object "dev_only.type_repr" |> Js.to_bool then
478+
match dump_type_at_pos ~cx ~typed_ast loc with
479+
| None -> (Loc.none, Error "No match")
480+
| Some (loc, s) -> (loc, Ok (Utils_js.spf "type_repr: %s" s))
481+
else
482+
let result =
483+
type_at_pos_type
484+
~cx
485+
~file_sig
486+
~omit_targ_defaults:false
487+
~typed_ast
488+
~verbose_normalizer:false
489+
~max_depth:50
490+
~no_typed_ast_for_imports:false
491+
loc
492+
in
493+
begin
494+
match result with
495+
| FailureNoMatch -> (Loc.none, Error "No match")
496+
| FailureUnparseable (loc, _, _) -> (loc, Error "Unparseable")
497+
| Success (loc, result) ->
498+
(loc, Ok (Ty_printer.string_of_type_at_pos_result ~exact_by_default:true result))
499+
end
495500

496501
let signature_help filename content line col js_config_object :
497502
((ServerProt.Response.func_details_result list * int) option, string) result =
@@ -802,6 +807,13 @@ let () =
802807
"default": false,
803808
"desc": "Changes the default type of 'catch' variables from 'any' to 'mixed'."
804809
},
810+
{
811+
"key": "dev_only.type_repr",
812+
"kind": "option",
813+
"type": "bool",
814+
"default": false,
815+
"desc": "Show the underlying type representation for debugging purposes."
816+
},
805817
{
806818
"key": "deprecated-type",
807819
"kind": "lint",

src/typing/query_types.ml

+5
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ let result_of_normalizer_error loc t err =
2424

2525
let max_size_of_evaluated_type = 100
2626

27+
let dump_type_at_pos ~cx ~typed_ast loc =
28+
match find_type_at_pos_annotation cx typed_ast loc with
29+
| None -> None
30+
| Some (loc, _, t) -> Some (loc, Debug_js.dump_t cx ~depth:3 t)
31+
2732
let type_at_pos_type
2833
~cx
2934
~file_sig

website/src/try-flow/configured-monaco.js

+9
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,15 @@ monaco.languages.registerHoverProvider('flow', {
188188
// instead of a JS string, where the string value is hidden in a
189189
// `c` property.
190190
const typeAtPos = typeof result === 'string' ? result : result.c;
191+
if (typeAtPos.startsWith('type_repr: ')) {
192+
return {
193+
contents: [
194+
{
195+
value: `\`\`\`ocaml\n${typeAtPos.substring('type_repr: '.length)}\n\`\`\``,
196+
},
197+
],
198+
};
199+
}
191200
return {
192201
contents: [{value: `\`\`\`flow\n${typeAtPos}\n\`\`\``}],
193202
};

0 commit comments

Comments
 (0)