2 * Copyright (c) 2016, Facebook, Inc.
5 * This source code is licensed under the MIT license found in the
6 * LICENSE file in the "hack" directory of this source tree.
12 let opt_field ~v_opt ~label ~f
=
13 Option.value_map v_opt ~f
:(fun x
-> [(label
, f x
)]) ~default
:[]
15 (* There are fields that Nuclide doesn't use anymore, but the RPC framework
16 * still requires them in responses. Stub them with some default values in the
18 let deprecated_pos_field = Pos.json
(Pos.to_absolute
Pos.none
)
20 let deprecated_int_field = Hh_json.int_
0
22 let deprecated_bool_field = JSON_Bool
false
24 (* Instead of "assert false" *)
25 let should_not_happen = JSON_Object
[("this_should", JSON_String
"not_happen")]
27 let infer_type_response_to_json (type_string
, type_json
) =
29 ([("type", opt_string_to_json type_string
); ("pos", deprecated_pos_field)]
32 | Some json
-> [("full_type", json_of_string json
)]
35 let infer_type_error_response_to_json
39 expected_type_json
) =
44 Some
("actual_type", opt_string_to_json actual_type_string
);
46 ~f
:(fun ty
-> ("full_actual_type", json_of_string ty
))
48 Some
("expected_type", opt_string_to_json expected_type_string
);
50 ~f
:(fun ty
-> ("full_expected_type", json_of_string ty
))
54 let tast_holes_response_to_json ~print_file holes
=
57 Pos.to_absolute pos
|> Pos.multiline_json
59 Pos.multiline_json_no_filename pos
69 ("actual_type", Hh_json.string_ actual_type_str
);
70 ("full_actual_type", json_of_string actual_type_json
);
71 ("expected_type", Hh_json.string_ expected_type_str
);
72 ("full_expected_type", json_of_string expected_type_json
);
76 Hh_json.JSON_Array
(List.map ~
f holes
)
78 let identify_symbol_response_to_json results
=
79 let get_definition_data = function
82 let pos = Pos.json x
.pos in
83 let span = Pos.multiline_json x
.span in
84 let id = opt_string_to_json x
.id in
86 | None
-> (JSON_Null
, JSON_Null
, JSON_Null
)
88 let symbol_to_json (occurrence
, definition
) =
89 let (definition_pos
, definition_span
, definition_id
) =
90 get_definition_data definition
95 ("name", JSON_String occurrence
.name
);
96 ("result_type", JSON_String
(kind_to_string occurrence
.type_
));
97 ("pos", Pos.json occurrence
.pos);
98 ("definition_pos", definition_pos
);
99 ("definition_span", definition_span
);
100 ("definition_id", definition_id
);
103 JSON_Array
(List.map results ~
f:symbol_to_json)
105 let rec definition_to_json def
=
109 (List.map def
.modifiers ~
f:(fun x
-> JSON_String
(string_of_modifier x
)))
115 ~
f:outline_response_to_json
118 opt_field ~v_opt
:def
.params ~label
:"params" ~
f:outline_response_to_json
121 opt_field ~v_opt
:def
.docblock ~label
:"docblock" ~
f:(fun x
->
126 ("kind", JSON_String
(string_of_kind def
.kind
));
127 ("name", JSON_String def
.name
);
128 ("id", opt_string_to_json def
.id);
129 ("position", Pos.json def
.pos);
130 ("span", Pos.multiline_json def
.span);
131 ("modifiers", modifiers);
137 and outline_response_to_json x
=
138 Hh_json.JSON_Array
(List.map x ~
f:definition_to_json)
140 let symbol_by_id_response_to_json = function
141 | Some def
-> definition_to_json def
144 let highlight_references_response_to_json l
=
146 (List.map l ~
f:(fun x
->
150 ("line", Hh_json.int_ x
.st
.line
);
151 ("char_start", Hh_json.int_ x
.st
.column
);
152 ("char_end", Hh_json.int_ x
.ed
.column
);
155 let print_json json
= Hh_json.json_to_string json
|> print_endline