-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
estree_translator.ml
1941 lines (1921 loc) · 72 KB
/
estree_translator.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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(*
* 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
module type Config = sig
val include_locs : bool
end
module Translate (Impl : Translator_intf.S) (Config : Config) : sig
type t
val program : Offset_utils.t option -> (Loc.t, Loc.t) Ast.Program.t -> t
val expression : Offset_utils.t option -> (Loc.t, Loc.t) Ast.Expression.t -> t
val errors : (Loc.t * Parse_error.t) list -> t
end
with type t = Impl.t = struct
type t = Impl.t
type functions = {
program: (Loc.t, Loc.t) Ast.Program.t -> t;
expression: (Loc.t, Loc.t) Ast.Expression.t -> t;
}
open Ast
open Impl
let array_of_list fn list = array (List.rev_map fn list |> List.rev)
let option f = function
| Some v -> f v
| None -> null
let hint f = function
| Ast.Type.Available v -> f v
| Ast.Type.Missing _ -> null
let position p = obj [("line", int p.Loc.line); ("column", int p.Loc.column)]
let loc location =
let source =
match Loc.source location with
| Some (File_key.LibFile src)
| Some (File_key.SourceFile src)
| Some (File_key.JsonFile src)
| Some (File_key.ResourceFile src) ->
string src
| None -> null
in
obj
[
("source", source);
("start", position location.Loc.start);
("end", position location.Loc._end);
]
let errors l =
let error (location, e) =
obj [("loc", loc location); ("message", string (Parse_error.PP.error e))]
in
array_of_list error l
let format_internal_comments = function
| None -> None
| Some { Ast.Syntax.leading; trailing; internal } ->
Flow_ast_utils.mk_comments_opt ~leading ~trailing:(internal @ trailing) ()
(* This is basically a lightweight class. We close over some state and then return more than one
* function that can access that state. We don't need most class features though, so let's avoid
* the dynamic dispatch and the disruptive change. *)
let make_functions offset_table =
let range offset_table location =
Loc.(
array
[
int (Offset_utils.offset offset_table location.start);
int (Offset_utils.offset offset_table location._end);
]
)
in
let rec node _type location ?comments props =
let locs =
if Config.include_locs then
(* sorted backwards due to the rev_append below *)
let range =
match offset_table with
| Some table -> [("range", range table location)]
| None -> []
in
range @ [("loc", loc location)]
else
[]
in
let comments =
let open Ast.Syntax in
match comments with
| Some c ->
(match c with
| { leading = _ :: _ as l; trailing = _ :: _ as t; _ } ->
[("leadingComments", comment_list l); ("trailingComments", comment_list t)]
| { leading = _ :: _ as l; trailing = []; _ } -> [("leadingComments", comment_list l)]
| { leading = []; trailing = _ :: _ as t; _ } -> [("trailingComments", comment_list t)]
| _ -> [])
| None -> []
in
let prefix = locs @ comments @ [("type", string _type)] in
obj (List.rev_append prefix props)
and program (loc, { Ast.Program.statements; comments; all_comments }) =
let body = statement_list statements in
let props = [("body", body); ("comments", comment_list all_comments)] in
node ?comments "Program" loc props
and statement_list statements = array_of_list statement statements
and statement =
let open Statement in
function
| (loc, Empty { Empty.comments }) -> node ?comments "EmptyStatement" loc []
| (loc, Block b) -> block (loc, b)
| (loc, Expression { Expression.expression = expr; directive; comments }) ->
node
?comments
"ExpressionStatement"
loc
[("expression", expression expr); ("directive", option string directive)]
| (loc, If { If.test; consequent; alternate; comments }) ->
let alternate =
match alternate with
| None -> null
| Some (_, { If.Alternate.body; comments = alternate_comments }) ->
statement (Comment_attachment.statement_add_comments body alternate_comments)
in
node
?comments
"IfStatement"
loc
[
("test", expression test); ("consequent", statement consequent); ("alternate", alternate);
]
| (loc, Labeled { Labeled.label; body; comments }) ->
node
?comments
"LabeledStatement"
loc
[("label", identifier label); ("body", statement body)]
| (loc, Break { Break.label; comments }) ->
node ?comments "BreakStatement" loc [("label", option identifier label)]
| (loc, Continue { Continue.label; comments }) ->
node ?comments "ContinueStatement" loc [("label", option identifier label)]
| (loc, With { With._object; body; comments }) ->
node
?comments
"WithStatement"
loc
[("object", expression _object); ("body", statement body)]
| (loc, TypeAlias alias) -> type_alias (loc, alias)
| (loc, OpaqueType opaque_t) -> opaque_type ~declare:false (loc, opaque_t)
| (loc, Switch { Switch.discriminant; cases; comments; exhaustive_out = _ }) ->
node
?comments
"SwitchStatement"
loc
[("discriminant", expression discriminant); ("cases", array_of_list case cases)]
| (loc, Return { Return.argument; comments }) ->
node ?comments "ReturnStatement" loc [("argument", option expression argument)]
| (loc, Throw { Throw.argument; comments }) ->
node ?comments "ThrowStatement" loc [("argument", expression argument)]
| (loc, Try { Try.block = block_; handler; finalizer; comments }) ->
node
?comments
"TryStatement"
loc
[
("block", block block_);
("handler", option catch handler);
("finalizer", option block finalizer);
]
| (loc, While { While.test; body; comments }) ->
node ?comments "WhileStatement" loc [("test", expression test); ("body", statement body)]
| (loc, DoWhile { DoWhile.body; test; comments }) ->
node ?comments "DoWhileStatement" loc [("body", statement body); ("test", expression test)]
| (loc, For { For.init = init_; test; update; body; comments }) ->
let init = function
| For.InitDeclaration init -> variable_declaration init
| For.InitExpression expr -> expression expr
in
node
?comments
"ForStatement"
loc
[
("init", option init init_);
("test", option expression test);
("update", option expression update);
("body", statement body);
]
| (loc, ForIn { ForIn.left; right; body; each; comments }) ->
let left =
match left with
| ForIn.LeftDeclaration left -> variable_declaration left
| ForIn.LeftPattern left -> pattern left
in
node
?comments
"ForInStatement"
loc
[
("left", left);
("right", expression right);
("body", statement body);
("each", bool each);
]
| (loc, ForOf { ForOf.await; left; right; body; comments }) ->
let left =
match left with
| ForOf.LeftDeclaration left -> variable_declaration left
| ForOf.LeftPattern left -> pattern left
in
node
?comments
"ForOfStatement"
loc
[
("left", left);
("right", expression right);
("body", statement body);
("await", bool await);
]
| (loc, EnumDeclaration enum) -> enum_declaration (loc, enum)
| (loc, Debugger { Debugger.comments }) -> node ?comments "DebuggerStatement" loc []
| (loc, ClassDeclaration c) -> class_declaration (loc, c)
| (loc, InterfaceDeclaration i) -> interface_declaration (loc, i)
| (loc, VariableDeclaration var) -> variable_declaration (loc, var)
| (loc, FunctionDeclaration fn) -> function_declaration (loc, fn)
| (loc, DeclareVariable d) -> declare_variable (loc, d)
| (loc, DeclareFunction d) -> declare_function (loc, d)
| (loc, DeclareClass d) -> declare_class (loc, d)
| (loc, DeclareInterface i) -> declare_interface (loc, i)
| (loc, DeclareTypeAlias a) -> declare_type_alias (loc, a)
| (loc, DeclareOpaqueType t) -> opaque_type ~declare:true (loc, t)
| (loc, DeclareModule { DeclareModule.id; body; kind; comments }) ->
let id =
match id with
| DeclareModule.Literal lit -> string_literal lit
| DeclareModule.Identifier id -> identifier id
in
node
?comments
"DeclareModule"
loc
[
("id", id);
("body", block body);
( "kind",
match kind with
| DeclareModule.CommonJS -> string "CommonJS"
| DeclareModule.ES -> string "ES"
);
]
| ( loc,
DeclareExportDeclaration
{ DeclareExportDeclaration.specifiers; declaration; default; source; comments }
) ->
begin
match specifiers with
| Some (ExportNamedDeclaration.ExportBatchSpecifier (_, None)) ->
node
?comments
"DeclareExportAllDeclaration"
loc
[("source", option string_literal source)]
| _ ->
let declaration =
match declaration with
| Some (DeclareExportDeclaration.Variable v) -> declare_variable v
| Some (DeclareExportDeclaration.Function f) -> declare_function f
| Some (DeclareExportDeclaration.Class c) -> declare_class c
| Some (DeclareExportDeclaration.DefaultType t) -> _type t
| Some (DeclareExportDeclaration.NamedType t) -> type_alias t
| Some (DeclareExportDeclaration.NamedOpaqueType t) -> opaque_type ~declare:true t
| Some (DeclareExportDeclaration.Interface i) -> interface_declaration i
| None -> null
in
node
?comments
"DeclareExportDeclaration"
loc
[
( "default",
bool
(match default with
| Some _ -> true
| None -> false)
);
("declaration", declaration);
("specifiers", export_specifiers specifiers);
("source", option string_literal source);
]
end
| (loc, DeclareModuleExports { DeclareModuleExports.annot; comments }) ->
node ?comments "DeclareModuleExports" loc [("typeAnnotation", type_annotation annot)]
| ( loc,
ExportNamedDeclaration
{ ExportNamedDeclaration.specifiers; declaration; source; export_kind; comments }
) ->
begin
match specifiers with
| Some (ExportNamedDeclaration.ExportBatchSpecifier (_, exported)) ->
node
?comments
"ExportAllDeclaration"
loc
[
("source", option string_literal source);
("exported", option identifier exported);
("exportKind", string (string_of_export_kind export_kind));
]
| _ ->
node
?comments
"ExportNamedDeclaration"
loc
[
("declaration", option statement declaration);
("specifiers", export_specifiers specifiers);
("source", option string_literal source);
("exportKind", string (string_of_export_kind export_kind));
]
end
| ( loc,
ExportDefaultDeclaration
{
ExportDefaultDeclaration.declaration;
default = _ (* TODO: confirm we shouldn't use this *);
comments;
}
) ->
let declaration =
match declaration with
| ExportDefaultDeclaration.Declaration stmt -> statement stmt
| ExportDefaultDeclaration.Expression expr -> expression expr
in
node
?comments
"ExportDefaultDeclaration"
loc
[
("declaration", declaration);
("exportKind", string (string_of_export_kind Statement.ExportValue));
]
| ( loc,
ImportDeclaration { ImportDeclaration.specifiers; default; import_kind; source; comments }
) ->
let specifiers =
match specifiers with
| Some (ImportDeclaration.ImportNamedSpecifiers specifiers) ->
List.map
(fun { ImportDeclaration.local; remote; kind } ->
import_named_specifier local remote kind)
specifiers
| Some (ImportDeclaration.ImportNamespaceSpecifier id) -> [import_namespace_specifier id]
| None -> []
in
let specifiers =
match default with
| Some default -> import_default_specifier default :: specifiers
| None -> specifiers
in
let import_kind =
match import_kind with
| ImportDeclaration.ImportType -> "type"
| ImportDeclaration.ImportTypeof -> "typeof"
| ImportDeclaration.ImportValue -> "value"
in
node
?comments
"ImportDeclaration"
loc
[
("specifiers", array specifiers);
("source", string_literal source);
("importKind", string import_kind);
]
and expression =
let open Expression in
function
| (loc, This { This.comments }) -> node ?comments "ThisExpression" loc []
| (loc, Super { Super.comments }) -> node ?comments "Super" loc []
| (loc, Array { Array.elements; comments }) ->
node
?comments:(format_internal_comments comments)
"ArrayExpression"
loc
[("elements", array_of_list array_element elements)]
| (loc, Object { Object.properties; comments }) ->
node
?comments:(format_internal_comments comments)
"ObjectExpression"
loc
[("properties", array_of_list object_property properties)]
| (loc, Function _function) -> function_expression (loc, _function)
| ( loc,
ArrowFunction
{
Function.params = (_, { Function.Params.comments = params_comments; _ }) as params;
async;
predicate = predicate_;
tparams;
return;
body;
comments = func_comments;
sig_loc = _;
(* TODO: arrows shouldn't have these: *)
id = _;
generator = _;
}
) ->
let (body, expression) =
match body with
| Function.BodyBlock b -> (block b, false)
| Function.BodyExpression expr -> (expression expr, true)
in
let return =
match return with
| Ast.Type.Missing _ -> None
| Ast.Type.Available t -> Some t
in
let comments =
Flow_ast_utils.merge_comments
~outer:func_comments
~inner:(format_internal_comments params_comments)
in
node
?comments
"ArrowFunctionExpression"
loc
[
("id", null);
("params", function_params params);
("body", body);
("async", bool async);
("generator", bool false);
("predicate", option predicate predicate_);
("expression", bool expression);
("returnType", option type_annotation return);
("typeParameters", option type_parameter_declaration tparams);
]
| (loc, Sequence { Sequence.expressions; comments }) ->
node
?comments
"SequenceExpression"
loc
[("expressions", array_of_list expression expressions)]
| (loc, Unary { Unary.operator; argument; comments }) ->
Unary.(
(match operator with
| Await -> node ?comments "AwaitExpression" loc [("argument", expression argument)]
| _ ->
let operator =
match operator with
| Minus -> "-"
| Plus -> "+"
| Not -> "!"
| BitNot -> "~"
| Typeof -> "typeof"
| Void -> "void"
| Delete -> "delete"
| Await -> failwith "matched above"
in
node
?comments
"UnaryExpression"
loc
[
("operator", string operator);
("prefix", bool true);
("argument", expression argument);
])
)
| (loc, Binary { Binary.left; operator; right; comments }) ->
node
?comments
"BinaryExpression"
loc
[
("operator", string (Flow_ast_utils.string_of_binary_operator operator));
("left", expression left);
("right", expression right);
]
| (loc, TypeCast { TypeCast.expression = expr; annot; comments }) ->
node
?comments
"TypeCastExpression"
loc
[("expression", expression expr); ("typeAnnotation", type_annotation annot)]
| (loc, Assignment { Assignment.left; operator; right; comments }) ->
let operator =
match operator with
| None -> "="
| Some op -> Flow_ast_utils.string_of_assignment_operator op
in
node
?comments
"AssignmentExpression"
loc
[("operator", string operator); ("left", pattern left); ("right", expression right)]
| (loc, Update { Update.operator; argument; prefix; comments }) ->
let operator =
match operator with
| Update.Increment -> "++"
| Update.Decrement -> "--"
in
node
?comments
"UpdateExpression"
loc
[
("operator", string operator); ("argument", expression argument); ("prefix", bool prefix);
]
| (loc, Logical { Logical.left; operator; right; comments }) ->
let operator =
match operator with
| Logical.Or -> "||"
| Logical.And -> "&&"
| Logical.NullishCoalesce -> "??"
in
node
?comments
"LogicalExpression"
loc
[("operator", string operator); ("left", expression left); ("right", expression right)]
| (loc, Conditional { Conditional.test; consequent; alternate; comments }) ->
node
?comments
"ConditionalExpression"
loc
[
("test", expression test);
("consequent", expression consequent);
("alternate", expression alternate);
]
| (loc, New { New.callee; targs; arguments; comments }) ->
let (arguments, comments) =
match arguments with
| Some ((_, { ArgList.comments = args_comments; _ }) as arguments) ->
( arg_list arguments,
Flow_ast_utils.merge_comments
~inner:(format_internal_comments args_comments)
~outer:comments
)
| None -> (array [], comments)
in
node
?comments
"NewExpression"
loc
[
("callee", expression callee);
("typeArguments", option call_type_args targs);
("arguments", arguments);
]
| ( loc,
Call
({ Call.comments; arguments = (_, { ArgList.comments = args_comments; _ }); _ } as call)
) ->
let comments =
Flow_ast_utils.merge_comments
~inner:(format_internal_comments args_comments)
~outer:comments
in
node ?comments "CallExpression" loc (call_node_properties call)
| ( loc,
OptionalCall
{
OptionalCall.call =
{ Call.comments; arguments = (_, { ArgList.comments = args_comments; _ }); _ } as
call;
optional;
filtered_type = _;
}
) ->
let comments =
Flow_ast_utils.merge_comments
~inner:(format_internal_comments args_comments)
~outer:comments
in
node
?comments
"OptionalCallExpression"
loc
(call_node_properties call @ [("optional", bool optional)])
| (loc, Member ({ Member.comments; _ } as member)) ->
node ?comments "MemberExpression" loc (member_node_properties member)
| ( loc,
OptionalMember
{
OptionalMember.member = { Member.comments; _ } as member;
optional;
filtered_type = _;
}
) ->
node
?comments
"OptionalMemberExpression"
loc
(member_node_properties member @ [("optional", bool optional)])
| (loc, Yield { Yield.argument; delegate; comments }) ->
node
?comments
"YieldExpression"
loc
[("argument", option expression argument); ("delegate", bool delegate)]
| (loc, Comprehension { Comprehension.blocks; filter }) ->
node
"ComprehensionExpression"
loc
[
("blocks", array_of_list comprehension_block blocks);
("filter", option expression filter);
]
| (loc, Generator { Generator.blocks; filter }) ->
node
"GeneratorExpression"
loc
[
("blocks", array_of_list comprehension_block blocks);
("filter", option expression filter);
]
| (_loc, Identifier id) -> identifier id
| (loc, Literal ({ Literal.value = Ast.Literal.BigInt _; _ } as lit)) ->
bigint_literal (loc, lit)
| (loc, Literal lit) -> literal (loc, lit)
| (loc, TemplateLiteral lit) -> template_literal (loc, lit)
| (loc, TaggedTemplate tagged) -> tagged_template (loc, tagged)
| (loc, Class c) -> class_expression (loc, c)
| (loc, JSXElement element) -> jsx_element (loc, element)
| (loc, JSXFragment fragment) -> jsx_fragment (loc, fragment)
| (loc, MetaProperty { MetaProperty.meta; property; comments }) ->
node
?comments
"MetaProperty"
loc
[("meta", identifier meta); ("property", identifier property)]
| (loc, Import { Import.argument; comments }) ->
node ?comments "ImportExpression" loc [("source", expression argument)]
and function_declaration
( loc,
{
Function.id;
params = (_, { Function.Params.comments = params_comments; _ }) as params;
async;
generator;
predicate = predicate_;
tparams;
return;
body;
comments = func_comments;
sig_loc = _;
}
) =
let body =
match body with
| Function.BodyBlock b -> b
| Function.BodyExpression _ -> failwith "Unexpected FunctionDeclaration with BodyExpression"
in
let return =
match return with
| Ast.Type.Missing _ -> None
| Ast.Type.Available t -> Some t
in
let comments =
Flow_ast_utils.merge_comments
~outer:func_comments
~inner:(format_internal_comments params_comments)
in
node
?comments
"FunctionDeclaration"
loc
[
(* estree hasn't come around to the idea that function decls can have
optional ids, but acorn, babel, espree and esprima all have, so let's
do it too. see https://github.com/estree/estree/issues/98 *)
("id", option identifier id);
("params", function_params params);
("body", block body);
("async", bool async);
("generator", bool generator);
("predicate", option predicate predicate_);
("expression", bool false);
("returnType", option type_annotation return);
("typeParameters", option type_parameter_declaration tparams);
]
and function_expression
( loc,
{
Function.id;
params = (_, { Function.Params.comments = params_comments; _ }) as params;
async;
generator;
predicate = predicate_;
tparams;
return;
body;
comments = func_comments;
sig_loc = _;
}
) =
let body =
match body with
| Function.BodyBlock b -> b
| Function.BodyExpression _ -> failwith "Unexpected FunctionExpression with BodyExpression"
in
let return =
match return with
| Ast.Type.Missing _ -> None
| Ast.Type.Available t -> Some t
in
let comments =
Flow_ast_utils.merge_comments
~outer:func_comments
~inner:(format_internal_comments params_comments)
in
node
?comments
"FunctionExpression"
loc
[
("id", option identifier id);
("params", function_params params);
("body", block body);
("async", bool async);
("generator", bool generator);
("predicate", option predicate predicate_);
("expression", bool false);
("returnType", option type_annotation return);
("typeParameters", option type_parameter_declaration tparams);
]
and identifier (loc, { Identifier.name; comments }) =
node
"Identifier"
?comments
loc
[("name", string name); ("typeAnnotation", null); ("optional", bool false)]
and private_identifier (loc, { PrivateName.name; comments }) =
node
?comments
"PrivateIdentifier"
loc
[("name", string name); ("typeAnnotation", null); ("optional", bool false)]
and pattern_identifier
loc { Pattern.Identifier.name = (_, { Identifier.name; comments }); annot; optional } =
node
?comments
"Identifier"
loc
[
("name", string name);
("typeAnnotation", hint type_annotation annot);
("optional", bool optional);
]
and arg_list (_loc, { Expression.ArgList.arguments; comments = _ }) =
(* ESTree does not have a unique node for argument lists, so there's nowhere to
include the loc. *)
array_of_list expression_or_spread arguments
and case (loc, { Statement.Switch.Case.test; consequent; comments }) =
node
?comments
"SwitchCase"
loc
[("test", option expression test); ("consequent", array_of_list statement consequent)]
and catch (loc, { Statement.Try.CatchClause.param; body; comments }) =
node ?comments "CatchClause" loc [("param", option pattern param); ("body", block body)]
and block (loc, { Statement.Block.body; comments }) =
node
?comments:(format_internal_comments comments)
"BlockStatement"
loc
[("body", statement_list body)]
and declare_variable (loc, { Statement.DeclareVariable.id; annot; comments }) =
let id_loc = Loc.btwn (fst id) (fst annot) in
node
?comments
"DeclareVariable"
loc
[
( "id",
pattern_identifier
id_loc
{ Pattern.Identifier.name = id; annot = Ast.Type.Available annot; optional = false }
);
]
and declare_function
(loc, { Statement.DeclareFunction.id; annot; predicate = predicate_; comments }) =
let id_loc = Loc.btwn (fst id) (fst annot) in
node
?comments
"DeclareFunction"
loc
[
( "id",
pattern_identifier
id_loc
{ Pattern.Identifier.name = id; annot = Ast.Type.Available annot; optional = false }
);
("predicate", option predicate predicate_);
]
and declare_class
(loc, { Statement.DeclareClass.id; tparams; body; extends; implements; mixins; comments }) =
(* TODO: extends shouldn't return an array *)
let extends =
match extends with
| Some extends -> array [interface_extends extends]
| None -> array []
in
let implements =
match implements with
| Some (_, { Class.Implements.interfaces; comments = _ }) ->
array_of_list class_implements interfaces
| None -> array []
in
node
?comments
"DeclareClass"
loc
[
("id", identifier id);
("typeParameters", option type_parameter_declaration tparams);
("body", object_type ~include_inexact:false body);
("extends", extends);
("implements", implements);
("mixins", array_of_list interface_extends mixins);
]
and declare_interface (loc, { Statement.Interface.id; tparams; body; extends; comments }) =
node
?comments
"DeclareInterface"
loc
[
("id", identifier id);
("typeParameters", option type_parameter_declaration tparams);
("body", object_type ~include_inexact:false body);
("extends", array_of_list interface_extends extends);
]
and string_of_export_kind = function
| Statement.ExportType -> "type"
| Statement.ExportValue -> "value"
and export_specifiers =
let open Statement.ExportNamedDeclaration in
function
| Some (ExportSpecifiers specifiers) -> array_of_list export_specifier specifiers
| Some (ExportBatchSpecifier (loc, Some name)) ->
array [node "ExportNamespaceSpecifier" loc [("exported", identifier name)]]
| Some (ExportBatchSpecifier (_, None)) ->
(* this should've been handled by callers, since this represents an
ExportAllDeclaration, not a specifier. *)
array []
| None -> array []
and declare_type_alias (loc, { Statement.TypeAlias.id; tparams; right; comments }) =
node
?comments
"DeclareTypeAlias"
loc
[
("id", identifier id);
("typeParameters", option type_parameter_declaration tparams);
("right", _type right);
]
and type_alias (loc, { Statement.TypeAlias.id; tparams; right; comments }) =
node
?comments
"TypeAlias"
loc
[
("id", identifier id);
("typeParameters", option type_parameter_declaration tparams);
("right", _type right);
]
and opaque_type
~declare (loc, { Statement.OpaqueType.id; tparams; impltype; supertype; comments }) =
let name =
if declare then
"DeclareOpaqueType"
else
"OpaqueType"
in
node
?comments
name
loc
[
("id", identifier id);
("typeParameters", option type_parameter_declaration tparams);
("impltype", option _type impltype);
("supertype", option _type supertype);
]
and class_declaration ast = class_helper "ClassDeclaration" ast
and class_expression ast = class_helper "ClassExpression" ast
and class_helper
node_type (loc, { Class.id; extends; body; tparams; implements; class_decorators; comments })
=
let (super, super_targs, comments) =
match extends with
| Some (_, { Class.Extends.expr; targs; comments = extends_comments }) ->
(Some expr, targs, Flow_ast_utils.merge_comments ~outer:comments ~inner:extends_comments)
| None -> (None, None, comments)
in
let (implements, comments) =
match implements with
| Some (_, { Class.Implements.interfaces; comments = implements_comments }) ->
( array_of_list class_implements interfaces,
Flow_ast_utils.merge_comments ~outer:comments ~inner:implements_comments
)
| None -> (array [], comments)
in
node
?comments
node_type
loc
[
(* estree hasn't come around to the idea that class decls can have
optional ids, but acorn, babel, espree and esprima all have, so let's
do it too. see https://github.com/estree/estree/issues/98 *)
("id", option identifier id);
("body", class_body body);
("typeParameters", option type_parameter_declaration tparams);
("superClass", option expression super);
("superTypeParameters", option type_args super_targs);
("implements", implements);
("decorators", array_of_list class_decorator class_decorators);
]
and class_decorator (loc, { Class.Decorator.expression = expr; comments }) =
node ?comments "Decorator" loc [("expression", expression expr)]
and class_implements (loc, { Class.Implements.Interface.id; targs }) =
node "ClassImplements" loc [("id", identifier id); ("typeParameters", option type_args targs)]
and class_body (loc, { Class.Body.body; comments }) =
node ?comments "ClassBody" loc [("body", array_of_list class_element body)]
and class_element =
Class.Body.(
function
| Method m -> class_method m
| PrivateField p -> class_private_field p
| Property p -> class_property p
)
and class_method (loc, { Class.Method.key; value; kind; static; decorators; comments }) =
let (key, computed, comments) =
let open Expression.Object.Property in
match key with
| Literal lit -> (literal lit, false, comments)
| Identifier id -> (identifier id, false, comments)
| PrivateName name -> (private_identifier name, false, comments)
| Computed (_, { ComputedKey.expression = expr; comments = computed_comments }) ->
( expression expr,
true,
Flow_ast_utils.merge_comments ~outer:comments ~inner:computed_comments
)
in
let kind =
Class.Method.(
match kind with
| Constructor -> "constructor"
| Method -> "method"
| Get -> "get"
| Set -> "set"
)
in
node
?comments
"MethodDefinition"
loc
[
("key", key);
("value", function_expression value);
("kind", string kind);
("static", bool static);
("computed", bool computed);
("decorators", array_of_list class_decorator decorators);
]
and class_private_field
(loc, { Class.PrivateField.key; value; annot; static; variance = variance_; comments }) =
let (value, declare) =
match value with
| Class.Property.Declared -> (None, true)
| Class.Property.Uninitialized -> (None, false)
| Class.Property.Initialized x -> (Some x, false)
in
let props =
[
("key", private_identifier key);
("value", option expression value);
("typeAnnotation", hint type_annotation annot);
("computed", bool false);
("static", bool static);
("variance", option variance variance_);
]
@