Repository navigation
Expand file tree
/
Copy pathflow_dot_js.ml
More file actions
254 lines (221 loc) · 8.08 KB
/
Copy pathflow_dot_js.ml
File metadata and controls
254 lines (221 loc) · 8.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
let parse_content file content =
let parse_options = Some Parser_env.({
(**
* Always parse ES proposal syntax. The user-facing config option to
* ignore/warn/enable them is handled during inference so that a clean error
* can be surfaced (rather than a more cryptic parse error).
*)
esproposal_class_instance_fields = true;
esproposal_class_static_fields = true;
esproposal_decorators = true;
esproposal_export_star_as = true;
types = true;
use_strict = false;
}) in
Parser_flow.program_file ~fail:false ~parse_options content (Some file)
let array_of_list f lst =
Array.of_list (List.map f lst)
let rec js_of_json = function
| Hh_json.JSON_Object props ->
let props = array_of_list (fun (k, v) -> k, js_of_json v) props in
Js.Unsafe.inject (Js.Unsafe.obj props)
| Hh_json.JSON_Array items ->
let items = array_of_list js_of_json items in
Js.Unsafe.inject (Js.array items)
| Hh_json.JSON_String str ->
Js.Unsafe.inject (Js.string str)
| Hh_json.JSON_Number num ->
Js.Unsafe.inject (Js.number_of_float (float_of_string num))
| Hh_json.JSON_Bool value ->
Js.Unsafe.inject (Js.bool value)
| Hh_json.JSON_Null ->
Js.Unsafe.inject Js.null
let load_lib_files ~master_cx ~metadata files
save_parse_errors save_infer_errors save_suppressions =
(* iterate in reverse override order *)
let _, result = List.rev files |> List.fold_left (
fun (exclude_syms, result) file ->
let lib_content = Sys_utils.cat file in
let lib_file = Loc.LibFile file in
match parse_content lib_file lib_content with
| (_, statements, comments), [] ->
let cx, syms = Type_inference_js.infer_lib_file
~metadata ~exclude_syms
lib_file statements comments
in
Merge_js.merge_lib_file cx master_cx save_infer_errors save_suppressions;
(* symbols loaded from this file are suppressed
if found in later ones *)
let exclude_syms = SSet.union exclude_syms (Utils_js.set_of_list syms) in
let result = (lib_file, true) :: result in
exclude_syms, result
| _, parse_errors ->
let converted = List.fold_left (fun acc err ->
Errors.(ErrorSet.add (parse_error_to_flow_error err) acc)
) Errors.ErrorSet.empty parse_errors in
save_parse_errors lib_file converted;
exclude_syms, ((lib_file, false) :: result)
) (SSet.empty, [])
in result
let stub_metadata ~root ~checked = { Context.
checked;
enable_const_params = false;
enable_unsafe_getters_and_setters = false;
enforce_strict_type_args = false;
esproposal_class_static_fields = Options.ESPROPOSAL_WARN;
esproposal_class_instance_fields = Options.ESPROPOSAL_WARN;
esproposal_decorators = Options.ESPROPOSAL_WARN;
esproposal_export_star_as = Options.ESPROPOSAL_WARN;
facebook_ignore_fbt = false;
ignore_non_literal_requires = false;
max_trace_depth = 0;
max_workers = 0;
munge_underscores = false;
root;
strip_root = true;
suppress_comments = [];
suppress_types = SSet.empty;
verbose = None;
weak = false;
}
let get_master_cx =
let master_cx = ref None in
fun root ->
match !master_cx with
| Some (prev_root, cx) -> assert (prev_root = root); cx
| None ->
let cx = Flow_js.fresh_context
(stub_metadata ~root ~checked:false)
Loc.Builtins
(Modulename.String Files.lib_module) in
master_cx := Some (root, cx);
cx
let set_libs filenames =
let root = Path.dummy_path in
let master_cx = get_master_cx root in
let metadata = stub_metadata ~root ~checked:true in
let _ = load_lib_files
~master_cx
~metadata
filenames
(fun _file _errs -> ())
(fun _file _errs -> ())
(fun _file _sups -> ()) in
Flow_js.Cache.clear();
let reason = Reason.builtin_reason "module" in
let builtin_module = Flow_js.mk_object master_cx reason in
Flow_js.flow_t master_cx (builtin_module, Flow_js.builtins master_cx);
Merge_js.ContextOptimizer.sig_context [master_cx]
let check_content ~filename ~content =
let stdin_file = Some (Path.make_unsafe filename, content) in
let root = Path.dummy_path in
let filename = Loc.SourceFile filename in
let errors = match parse_content filename content with
| ast, [] ->
(* defaults *)
let metadata = stub_metadata ~root ~checked:true in
Flow_js.Cache.clear();
let cx = Type_inference_js.infer_ast
~metadata ~filename ~module_name:(Modulename.String "-") ast
in
(* this is a VERY pared-down version of Merge_service.merge_strict_context.
it relies on the JS version only supporting libs + 1 file, so every
module you can require() must come from a lib; this skips resolving
module names and just adds them all to the `decls` list. *)
let decls = SSet.fold (fun module_name acc ->
(module_name, Modulename.String module_name, cx) :: acc
) (Context.required cx) [] in
let master_cx = get_master_cx root in
Merge_js.merge_component_strict [cx] [] [] decls master_cx;
Context.errors cx
| _, parse_errors ->
List.fold_left (fun acc err ->
Errors.(ErrorSet.add (parse_error_to_flow_error err) acc)
) Errors.ErrorSet.empty parse_errors
in
errors
|> Errors.ErrorSet.elements
|> Errors.json_of_errors_with_context ~root ~stdin_file
|> js_of_json
let check filename =
let content = Sys_utils.cat filename in
check_content ~filename ~content
let set_libs_js js_libs =
Js.to_array js_libs
|> Array.to_list
|> List.map (fun x -> Js.to_string x)
|> set_libs
let check_js js_file =
check (Js.to_string js_file)
let check_content_js js_file js_content =
let filename = Js.to_string js_file in
let content = Js.to_string js_content in
check_content ~filename ~content
let mk_loc file line col =
{
Loc.
source = Some file;
start = { Loc.line; column = col; offset = 0; };
_end = { Loc.line; column = col + 1; offset = 0; };
}
let infer_type filename content line col =
let filename = Loc.SourceFile filename in
let root = Path.dummy_path in
match parse_content filename content with
| ast, [] ->
(* defaults *)
let metadata = stub_metadata ~root ~checked:true in
Flow_js.Cache.clear();
let cx = Type_inference_js.infer_ast
~metadata ~filename ~module_name:(Modulename.String "-") ast
in
let loc = mk_loc filename line col in
let (loc, ground_t, possible_ts) = Query_types.query_type cx loc in
let ty, raw_type = match ground_t with
| None -> None, None
| Some t ->
let ty = Some (Type_printer.string_of_t cx t) in
let raw_type = Some (Debug_js.jstr_of_t ~depth:10 cx t)
in
ty, raw_type
in
let reasons =
possible_ts
|> List.map Type.reason_of_t
in
(None, Some (loc, ty, raw_type, reasons))
| _, _ -> failwith "parse error"
let handle_inferred_result (_, inferred, _, _) =
inferred
let type_at_pos js_file js_content js_line js_col =
let filename = Js.to_string js_file in
let content = Js.to_string js_content in
let line = Js.parseInt js_line in
let col = Js.parseInt js_col in
match infer_type filename content line col with
| (Some err, None) -> err
| (None, Some resp) -> handle_inferred_result resp
| (_, _) -> failwith "Error"
let exports =
if (Js.typeof (Js.Unsafe.js_expr "exports") != Js.string "undefined")
then Js.Unsafe.js_expr "exports"
else begin
let exports = Js.Unsafe.obj [||] in
Js.Unsafe.set Js.Unsafe.global "flow" exports;
exports
end
let () = Js.Unsafe.set exports "registerFile" (
Js.wrap_callback (fun name content -> Sys_js.register_file ~name ~content)
)
let () = Js.Unsafe.set exports
"setLibs" (Js.wrap_callback set_libs_js)
let () = Js.Unsafe.set exports
"check" (Js.wrap_callback check_js)
let () = Js.Unsafe.set exports
"checkContent" (Js.wrap_callback check_content_js)
let () = Js.Unsafe.set exports
"jsOfOcamlVersion" (Js.string Sys_js.js_of_ocaml_version)
let () = Js.Unsafe.set exports
"flowVersion" (Js.string FlowConfig.version)
let () = Js.Unsafe.set exports
"typeAtPos" (Js.wrap_callback type_at_pos)