-
Notifications
You must be signed in to change notification settings - Fork 464
/
Copy pathdeclaration_parser.ml
432 lines (405 loc) · 14.8 KB
/
declaration_parser.ml
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
(*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*)
module Ast = Flow_ast
open Token
open Parser_common
open Parser_env
open Flow_ast
open Comment_attachment
module type DECLARATION = sig
val async : env -> bool * Loc.t Comment.t list
val generator : env -> bool * Loc.t Comment.t list
val variance : env -> bool -> bool -> Loc.t Variance.t option
val function_params : await:bool -> yield:bool -> env -> (Loc.t, Loc.t) Ast.Function.Params.t
val function_body :
env ->
async:bool ->
generator:bool ->
expression:bool ->
simple_params:bool ->
(Loc.t, Loc.t) Function.body * bool
val strict_post_check :
env ->
contains_use_strict:bool ->
(Loc.t, Loc.t) Identifier.t option ->
(Loc.t, Loc.t) Ast.Function.Params.t ->
unit
val let_ :
env ->
(Loc.t, Loc.t) Statement.VariableDeclaration.Declarator.t list
* Loc.t Ast.Comment.t list
* (Loc.t * Parse_error.t) list
val const :
env ->
(Loc.t, Loc.t) Statement.VariableDeclaration.Declarator.t list
* Loc.t Ast.Comment.t list
* (Loc.t * Parse_error.t) list
val var :
env ->
(Loc.t, Loc.t) Statement.VariableDeclaration.Declarator.t list
* Loc.t Ast.Comment.t list
* (Loc.t * Parse_error.t) list
val _function : env -> (Loc.t, Loc.t) Statement.t
val enum_declaration : env -> (Loc.t, Loc.t) Statement.t
end
module Declaration (Parse : Parser_common.PARSER) (Type : Type_parser.TYPE) : DECLARATION = struct
module Enum = Enum_parser.Enum (Parse)
let check_param =
let rec pattern ((env, _) as check_env) (loc, p) =
Pattern.(
match p with
| Object o -> _object check_env o
| Array arr -> _array check_env arr
| Identifier id -> identifier_pattern check_env id
| Expression _ ->
error_at env (loc, Parse_error.ExpectedPatternFoundExpression);
check_env
)
and _object check_env o = List.fold_left object_property check_env o.Pattern.Object.properties
and object_property check_env =
let open Pattern.Object in
function
| Property (_, property) ->
Property.(
let check_env =
match property.key with
| Identifier id -> identifier_no_dupe_check check_env id
| _ -> check_env
in
pattern check_env property.pattern
)
| RestElement (_, { Pattern.RestElement.argument; comments = _ }) ->
pattern check_env argument
and _array check_env arr = List.fold_left array_element check_env arr.Pattern.Array.elements
and array_element check_env =
let open Pattern.Array in
function
| Hole _ -> check_env
| Element (_, { Element.argument; default = _ }) -> pattern check_env argument
| RestElement (_, { Pattern.RestElement.argument; comments = _ }) ->
pattern check_env argument
and identifier_pattern check_env { Pattern.Identifier.name = id; _ } = identifier check_env id
and identifier (env, param_names) ((loc, { Identifier.name; comments = _ }) as id) =
if SSet.mem name param_names then error_at env (loc, Parse_error.StrictParamDupe);
let (env, param_names) = identifier_no_dupe_check (env, param_names) id in
(env, SSet.add name param_names)
and identifier_no_dupe_check (env, param_names) (loc, { Identifier.name; comments = _ }) =
if is_restricted name then strict_error_at env (loc, Parse_error.StrictParamName);
if is_future_reserved name || is_strict_reserved name then
strict_error_at env (loc, Parse_error.StrictReservedWord);
(env, param_names)
in
pattern
let strict_post_check env ~contains_use_strict id params =
let strict_mode = Parser_env.in_strict_mode env in
let simple = is_simple_parameter_list params in
let (_, { Ast.Function.Params.params; rest; this_ = _; comments = _ }) = params in
(* If we were already in strict mode and therefore already threw strict
errors, we want to do these checks outside of strict mode. If we
were in non-strict mode but the function contains "use strict", then
we want to do these checks in strict mode *)
let env =
if strict_mode then
with_strict false env
else
with_strict contains_use_strict env
in
if contains_use_strict || strict_mode || not simple then (
(match id with
| Some (loc, { Identifier.name; comments = _ }) ->
if is_restricted name then strict_error_at env (loc, Parse_error.StrictFunctionName);
if is_future_reserved name || is_strict_reserved name then
strict_error_at env (loc, Parse_error.StrictReservedWord)
| None -> ());
let acc =
List.fold_left
(fun acc (_, { Function.Param.argument; default = _ }) -> check_param acc argument)
(env, SSet.empty)
params
in
match rest with
| Some (_, { Function.RestParam.argument; comments = _ }) -> ignore (check_param acc argument)
| None -> ()
)
let function_params =
let rec param =
with_loc (fun env ->
if Peek.token env = T_THIS then error env Parse_error.ThisParamMustBeFirst;
let argument = Parse.pattern env Parse_error.StrictParamName in
let default =
if Peek.token env = T_ASSIGN then (
Expect.token env T_ASSIGN;
Some (Parse.assignment env)
) else
None
in
{ Function.Param.argument; default }
)
and param_list env acc =
match Peek.token env with
| (T_EOF | T_RPAREN | T_ELLIPSIS) as t ->
let rest =
if t = T_ELLIPSIS then
let leading = Peek.comments env in
let (loc, id) =
with_loc
(fun env ->
Expect.token env T_ELLIPSIS;
Parse.pattern env Parse_error.StrictParamName)
env
in
Some
( loc,
{
Function.RestParam.argument = id;
comments = Flow_ast_utils.mk_comments_opt ~leading ();
}
)
else
None
in
if Peek.token env <> T_RPAREN then error env Parse_error.ParameterAfterRestParameter;
(List.rev acc, rest)
| _ ->
let the_param = param env in
if Peek.token env <> T_RPAREN then Expect.token env T_COMMA;
param_list env (the_param :: acc)
in
let this_param_annotation env =
if should_parse_types env && Peek.token env = T_THIS then (
let leading = Peek.comments env in
let (this_loc, this_param) =
with_loc
(fun env ->
Expect.token env T_THIS;
if Peek.token env <> T_COLON then begin
error env Parse_error.ThisParamAnnotationRequired;
None
end else
Some (Type.annotation env))
env
in
match this_param with
| None -> None
| Some annot ->
if Peek.token env = T_COMMA then Eat.token env;
Some
( this_loc,
{
Ast.Function.ThisParam.annot;
comments = Flow_ast_utils.mk_comments_opt ~leading ();
}
)
) else
None
in
fun ~await ~yield ->
with_loc (fun env ->
let env =
env
|> with_allow_await await
|> with_allow_yield yield
|> with_in_formal_parameters true
in
let leading = Peek.comments env in
Expect.token env T_LPAREN;
let this_ = this_param_annotation env in
let (params, rest) = param_list env [] in
let internal = Peek.comments env in
Expect.token env T_RPAREN;
let trailing = Eat.trailing_comments env in
{
Ast.Function.Params.params;
rest;
comments = Flow_ast_utils.mk_comments_with_internal_opt ~leading ~trailing ~internal ();
this_;
}
)
let function_body env ~async ~generator ~expression ~simple_params =
let env = enter_function env ~async ~generator ~simple_params in
let (body_block, contains_use_strict) = Parse.function_block_body env ~expression in
(Function.BodyBlock body_block, contains_use_strict)
let variance env is_async is_generator =
let loc = Peek.loc env in
let variance =
match Peek.token env with
| T_PLUS ->
let leading = Peek.comments env in
Eat.token env;
Some
( loc,
{ Variance.kind = Variance.Plus; comments = Flow_ast_utils.mk_comments_opt ~leading () }
)
| T_MINUS ->
let leading = Peek.comments env in
Eat.token env;
Some
( loc,
{
Variance.kind = Variance.Minus;
comments = Flow_ast_utils.mk_comments_opt ~leading ();
}
)
| _ -> None
in
match variance with
| Some (loc, _) when is_async || is_generator ->
error_at env (loc, Parse_error.UnexpectedVariance);
None
| _ -> variance
let generator env =
if Peek.token env = T_MULT then (
let leading = Peek.comments env in
Eat.token env;
(true, leading)
) else
(false, [])
(* Returns true and consumes a token if the token is `async` and the token after it is on
the same line (see https://tc39.github.io/ecma262/#sec-async-function-definitions) *)
let async env =
if Peek.token env = T_ASYNC && not (Peek.ith_is_line_terminator ~i:1 env) then
let leading = Peek.comments env in
let () = Eat.token env in
(true, leading)
else
(false, [])
let _function =
with_loc (fun env ->
let (async, leading_async) = async env in
let (sig_loc, (generator, tparams, id, params, return, predicate, leading)) =
with_loc
(fun env ->
let leading_function = Peek.comments env in
Expect.token env T_FUNCTION;
let (generator, leading_generator) = generator env in
let leading = List.concat [leading_async; leading_function; leading_generator] in
let (tparams, id) =
match (in_export_default env, Peek.token env) with
| (true, T_LPAREN) -> (None, None)
| (true, T_LESS_THAN) ->
let tparams = type_params_remove_trailing env (Type.type_params env) in
let id =
if Peek.token env = T_LPAREN then
None
else
let id =
id_remove_trailing
env
(Parse.identifier ~restricted_error:Parse_error.StrictFunctionName env)
in
Some id
in
(tparams, id)
| _ ->
let id =
if Peek.is_identifier env then
id_remove_trailing
env
(Parse.identifier ~restricted_error:Parse_error.StrictFunctionName env)
else (
(* don't consume the identifier here like Parse.identifier does. *)
error_nameless_declaration env "function";
(Peek.loc env, { Identifier.name = ""; comments = None })
)
in
let tparams = type_params_remove_trailing env (Type.type_params env) in
(tparams, Some id)
in
let params =
let params = function_params ~await:async ~yield:generator env in
if Peek.token env = T_COLON then
params
else
function_params_remove_trailing env params
in
let (return, predicate) = Type.annotation_and_predicate_opt env in
let (return, predicate) =
match predicate with
| None -> (type_annotation_hint_remove_trailing env return, predicate)
| Some _ -> (return, predicate_remove_trailing env predicate)
in
(generator, tparams, id, params, return, predicate, leading))
env
in
let simple_params = is_simple_parameter_list params in
let (body, contains_use_strict) =
function_body env ~async ~generator ~expression:false ~simple_params
in
strict_post_check env ~contains_use_strict id params;
Statement.FunctionDeclaration
{
Function.id;
params;
body;
generator;
async;
predicate;
return;
tparams;
sig_loc;
comments = Flow_ast_utils.mk_comments_opt ~leading ();
}
)
let variable_declaration_list =
let variable_declaration env =
let (loc, (decl, err)) =
with_loc
(fun env ->
let id = Parse.pattern env Parse_error.StrictVarName in
let (init, err) =
if Eat.maybe env T_ASSIGN then
(Some (Parse.assignment env), None)
else
match id with
| (_, Ast.Pattern.Identifier _) -> (None, None)
| (loc, _) -> (None, Some (loc, Parse_error.NoUninitializedDestructuring))
in
(Ast.Statement.VariableDeclaration.Declarator.{ id; init }, err))
env
in
((loc, decl), err)
in
let rec helper env decls errs =
let (decl, err) = variable_declaration env in
let decls = decl :: decls in
let errs =
match err with
| Some x -> x :: errs
| None -> errs
in
if Eat.maybe env T_COMMA then
helper env decls errs
else
(List.rev decls, List.rev errs)
in
(fun env -> helper env [] [])
let declarations token env =
let leading = Peek.comments env in
Expect.token env token;
let (declarations, errs) = variable_declaration_list env in
(declarations, leading, errs)
let var = declarations T_VAR
let const env =
let env = env |> with_no_let true in
let (declarations, leading_comments, errs) = declarations T_CONST env in
(* Make sure all consts defined are initialized *)
let errs =
List.fold_left
(fun errs decl ->
match decl with
| (loc, { Statement.VariableDeclaration.Declarator.init = None; _ }) ->
(loc, Parse_error.NoUninitializedConst) :: errs
| _ -> errs)
errs
declarations
in
(declarations, leading_comments, List.rev errs)
let let_ env =
let env = env |> with_no_let true in
declarations T_LET env
let enum_declaration = Enum.declaration
end