forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflow_ast_mapper.ml
2681 lines (2424 loc) · 109 KB
/
flow_ast_mapper.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
let map_opt : 'node. ('node -> 'node) -> 'node option -> 'node option =
fun map opt ->
match opt with
| Some item ->
let item' = map item in
if item == item' then
opt
else
Some item'
| None -> opt
let id_loc : 'node 'a. ('loc -> 'node -> 'node) -> 'loc -> 'node -> 'a -> ('node -> 'a) -> 'a =
fun map loc item same diff ->
let item' = map loc item in
if item == item' then
same
else
diff item'
let id : 'node 'a. ('node -> 'node) -> 'node -> 'a -> ('node -> 'a) -> 'a =
fun map item same diff ->
let item' = map item in
if item == item' then
same
else
diff item'
let map_loc : 'node. ('loc -> 'node -> 'node) -> 'loc * 'node -> 'loc * 'node =
fun map same ->
let (loc, item) = same in
id_loc map loc item same (fun diff -> (loc, diff))
let map_loc_opt : 'node. ('loc -> 'node -> 'node) -> ('loc * 'node) option -> ('loc * 'node) option
=
fun map same ->
map_opt
(fun same ->
let (loc, item) = same in
id_loc map loc item same (fun diff -> (loc, diff)))
same
let map_list map lst =
let (rev_lst, changed) =
List.fold_left
(fun (lst', changed) item ->
let item' = map item in
(item' :: lst', changed || item' != item))
([], false)
lst
in
if changed then
List.rev rev_lst
else
lst
let map_list_multiple map lst =
let (rev_lst, changed) =
List.fold_left
(fun (lst', changed) item ->
match map item with
| [] -> (lst', true)
| [item'] -> (item' :: lst', changed || item != item')
| items' -> (List.rev_append items' lst', true))
([], false)
lst
in
if changed then
List.rev rev_lst
else
lst
class ['loc] mapper =
object (this)
method program (program : ('loc, 'loc) Ast.Program.t) =
let open Ast.Program in
let (loc, { statements; comments; all_comments }) = program in
let statements' = this#toplevel_statement_list statements in
let comments' = this#syntax_opt comments in
let all_comments' = map_list this#comment all_comments in
if statements == statements' && comments == comments' && all_comments == all_comments' then
program
else
(loc, { statements = statements'; comments = comments'; all_comments = all_comments' })
method statement (stmt : ('loc, 'loc) Ast.Statement.t) =
let open Ast.Statement in
match stmt with
| (loc, Block block) -> id_loc this#block loc block stmt (fun block -> (loc, Block block))
| (loc, Break break) -> id_loc this#break loc break stmt (fun break -> (loc, Break break))
| (loc, ClassDeclaration cls) ->
id_loc this#class_declaration loc cls stmt (fun cls -> (loc, ClassDeclaration cls))
| (loc, Continue cont) -> id_loc this#continue loc cont stmt (fun cont -> (loc, Continue cont))
| (loc, Debugger dbg) -> id_loc this#debugger loc dbg stmt (fun dbg -> (loc, Debugger dbg))
| (loc, DeclareClass stuff) ->
id_loc this#declare_class loc stuff stmt (fun stuff -> (loc, DeclareClass stuff))
| (loc, DeclareExportDeclaration decl) ->
id_loc this#declare_export_declaration loc decl stmt (fun decl ->
(loc, DeclareExportDeclaration decl)
)
| (loc, DeclareFunction stuff) ->
id_loc this#declare_function loc stuff stmt (fun stuff -> (loc, DeclareFunction stuff))
| (loc, DeclareInterface stuff) ->
id_loc this#declare_interface loc stuff stmt (fun stuff -> (loc, DeclareInterface stuff))
| (loc, DeclareModule m) ->
id_loc this#declare_module loc m stmt (fun m -> (loc, DeclareModule m))
| (loc, DeclareTypeAlias stuff) ->
id_loc this#declare_type_alias loc stuff stmt (fun stuff -> (loc, DeclareTypeAlias stuff))
| (loc, DeclareVariable stuff) ->
id_loc this#declare_variable loc stuff stmt (fun stuff -> (loc, DeclareVariable stuff))
| (loc, DeclareModuleExports annot) ->
id_loc this#declare_module_exports loc annot stmt (fun annot ->
(loc, DeclareModuleExports annot)
)
| (loc, DoWhile stuff) ->
id_loc this#do_while loc stuff stmt (fun stuff -> (loc, DoWhile stuff))
| (loc, Empty empty) -> id_loc this#empty loc empty stmt (fun empty -> (loc, Empty empty))
| (loc, EnumDeclaration enum) ->
id_loc this#enum_declaration loc enum stmt (fun enum -> (loc, EnumDeclaration enum))
| (loc, ExportDefaultDeclaration decl) ->
id_loc this#export_default_declaration loc decl stmt (fun decl ->
(loc, ExportDefaultDeclaration decl)
)
| (loc, ExportNamedDeclaration decl) ->
id_loc this#export_named_declaration loc decl stmt (fun decl ->
(loc, ExportNamedDeclaration decl)
)
| (loc, Expression expr) ->
id_loc this#expression_statement loc expr stmt (fun expr -> (loc, Expression expr))
| (loc, For for_stmt) ->
id_loc this#for_statement loc for_stmt stmt (fun for_stmt -> (loc, For for_stmt))
| (loc, ForIn stuff) ->
id_loc this#for_in_statement loc stuff stmt (fun stuff -> (loc, ForIn stuff))
| (loc, ForOf stuff) ->
id_loc this#for_of_statement loc stuff stmt (fun stuff -> (loc, ForOf stuff))
| (loc, FunctionDeclaration func) ->
id_loc this#function_declaration loc func stmt (fun func -> (loc, FunctionDeclaration func))
| (loc, If if_stmt) ->
id_loc this#if_statement loc if_stmt stmt (fun if_stmt -> (loc, If if_stmt))
| (loc, ImportDeclaration decl) ->
id_loc this#import_declaration loc decl stmt (fun decl -> (loc, ImportDeclaration decl))
| (loc, InterfaceDeclaration stuff) ->
id_loc this#interface_declaration loc stuff stmt (fun stuff ->
(loc, InterfaceDeclaration stuff)
)
| (loc, Labeled label) ->
id_loc this#labeled_statement loc label stmt (fun label -> (loc, Labeled label))
| (loc, OpaqueType otype) ->
id_loc this#opaque_type loc otype stmt (fun otype -> (loc, OpaqueType otype))
| (loc, Return ret) -> id_loc this#return loc ret stmt (fun ret -> (loc, Return ret))
| (loc, Switch switch) ->
id_loc this#switch loc switch stmt (fun switch -> (loc, Switch switch))
| (loc, Throw throw) -> id_loc this#throw loc throw stmt (fun throw -> (loc, Throw throw))
| (loc, Try try_stmt) ->
id_loc this#try_catch loc try_stmt stmt (fun try_stmt -> (loc, Try try_stmt))
| (loc, VariableDeclaration decl) ->
id_loc this#variable_declaration loc decl stmt (fun decl -> (loc, VariableDeclaration decl))
| (loc, While stuff) -> id_loc this#while_ loc stuff stmt (fun stuff -> (loc, While stuff))
| (loc, With stuff) -> id_loc this#with_ loc stuff stmt (fun stuff -> (loc, With stuff))
| (loc, TypeAlias stuff) ->
id_loc this#type_alias loc stuff stmt (fun stuff -> (loc, TypeAlias stuff))
| (loc, DeclareOpaqueType otype) ->
id_loc this#opaque_type loc otype stmt (fun otype -> (loc, OpaqueType otype))
method comment (c : 'loc Ast.Comment.t) = c
method syntax_opt
: 'internal. ('loc, 'internal) Ast.Syntax.t option -> ('loc, 'internal) Ast.Syntax.t option
=
map_opt this#syntax
method syntax : 'internal. ('loc, 'internal) Ast.Syntax.t -> ('loc, 'internal) Ast.Syntax.t =
fun attached ->
let open Ast.Syntax in
let { leading; trailing; internal } = attached in
let leading' = map_list this#comment leading in
let trailing' = map_list this#comment trailing in
if leading == leading' && trailing == trailing' then
attached
else
{ leading = leading'; trailing = trailing'; internal }
method expression (expr : ('loc, 'loc) Ast.Expression.t) =
let open Ast.Expression in
match expr with
| (loc, Array x) -> id_loc this#array loc x expr (fun x -> (loc, Array x))
| (loc, ArrowFunction x) ->
id_loc this#arrow_function loc x expr (fun x -> (loc, ArrowFunction x))
| (loc, Assignment x) -> id_loc this#assignment loc x expr (fun x -> (loc, Assignment x))
| (loc, Binary x) -> id_loc this#binary loc x expr (fun x -> (loc, Binary x))
| (loc, Call x) -> id_loc this#call loc x expr (fun x -> (loc, Call x))
| (loc, Class x) -> id_loc this#class_expression loc x expr (fun x -> (loc, Class x))
| (loc, Comprehension x) ->
id_loc this#comprehension loc x expr (fun x -> (loc, Comprehension x))
| (loc, Conditional x) -> id_loc this#conditional loc x expr (fun x -> (loc, Conditional x))
| (loc, Function x) -> id_loc this#function_expression loc x expr (fun x -> (loc, Function x))
| (loc, Generator x) -> id_loc this#generator loc x expr (fun x -> (loc, Generator x))
| (loc, Identifier x) -> id this#identifier x expr (fun x -> (loc, Identifier x))
| (loc, Import x) -> id (this#import loc) x expr (fun x -> (loc, Import x))
| (loc, JSXElement x) -> id_loc this#jsx_element loc x expr (fun x -> (loc, JSXElement x))
| (loc, JSXFragment x) -> id_loc this#jsx_fragment loc x expr (fun x -> (loc, JSXFragment x))
| (loc, Literal x) -> id_loc this#literal loc x expr (fun x -> (loc, Literal x))
| (loc, Logical x) -> id_loc this#logical loc x expr (fun x -> (loc, Logical x))
| (loc, Member x) -> id_loc this#member loc x expr (fun x -> (loc, Member x))
| (loc, MetaProperty x) ->
id_loc this#meta_property loc x expr (fun x -> (loc, MetaProperty x))
| (loc, New x) -> id_loc this#new_ loc x expr (fun x -> (loc, New x))
| (loc, Object x) -> id_loc this#object_ loc x expr (fun x -> (loc, Object x))
| (loc, OptionalCall x) -> id (this#optional_call loc) x expr (fun x -> (loc, OptionalCall x))
| (loc, OptionalMember x) ->
id_loc this#optional_member loc x expr (fun x -> (loc, OptionalMember x))
| (loc, Sequence x) -> id_loc this#sequence loc x expr (fun x -> (loc, Sequence x))
| (loc, Super x) -> id_loc this#super_expression loc x expr (fun x -> (loc, Super x))
| (loc, TaggedTemplate x) ->
id_loc this#tagged_template loc x expr (fun x -> (loc, TaggedTemplate x))
| (loc, TemplateLiteral x) ->
id_loc this#template_literal loc x expr (fun x -> (loc, TemplateLiteral x))
| (loc, This x) -> id_loc this#this_expression loc x expr (fun x -> (loc, This x))
| (loc, TypeCast x) -> id_loc this#type_cast loc x expr (fun x -> (loc, TypeCast x))
| (loc, Unary x) -> id_loc this#unary_expression loc x expr (fun x -> (loc, Unary x))
| (loc, Update x) -> id_loc this#update_expression loc x expr (fun x -> (loc, Update x))
| (loc, Yield x) -> id_loc this#yield loc x expr (fun x -> (loc, Yield x))
method array _loc (expr : ('loc, 'loc) Ast.Expression.Array.t) =
let open Ast.Expression in
let { Array.elements; comments } = expr in
let elements' = map_list this#array_element elements in
let comments' = this#syntax_opt comments in
if elements == elements' && comments == comments' then
expr
else
{ Array.elements = elements'; comments = comments' }
method array_element element =
let open Ast.Expression.Array in
match element with
| Expression expr -> id this#expression expr element (fun expr -> Expression expr)
| Spread spread -> id this#spread_element spread element (fun spread -> Spread spread)
| Hole _ -> element
method arrow_function loc (expr : ('loc, 'loc) Ast.Function.t) = this#function_ loc expr
method assignment _loc (expr : ('loc, 'loc) Ast.Expression.Assignment.t) =
let open Ast.Expression.Assignment in
let { operator = _; left; right; comments } = expr in
let left' = this#assignment_pattern left in
let right' = this#expression right in
let comments' = this#syntax_opt comments in
if left == left' && right == right' && comments == comments' then
expr
else
{ expr with left = left'; right = right'; comments = comments' }
method binary _loc (expr : ('loc, 'loc) Ast.Expression.Binary.t) =
let open Ast.Expression.Binary in
let { operator = _; left; right; comments } = expr in
let left' = this#expression left in
let right' = this#expression right in
let comments' = this#syntax_opt comments in
if left == left' && right == right' && comments == comments' then
expr
else
{ expr with left = left'; right = right'; comments = comments' }
method block _loc (stmt : ('loc, 'loc) Ast.Statement.Block.t) =
let open Ast.Statement.Block in
let { body; comments } = stmt in
let body' = this#statement_list body in
let comments' = this#syntax_opt comments in
if body == body' && comments == comments' then
stmt
else
{ body = body'; comments = comments' }
method break _loc (break : 'loc Ast.Statement.Break.t) =
let open Ast.Statement.Break in
let { label; comments } = break in
let label' = map_opt this#label_identifier label in
let comments' = this#syntax_opt comments in
if label == label' && comments == comments' then
break
else
{ label = label'; comments = comments' }
method call _loc (expr : ('loc, 'loc) Ast.Expression.Call.t) =
let open Ast.Expression.Call in
let { callee; targs; arguments; comments } = expr in
let callee' = this#expression callee in
let targs' = map_opt this#call_type_args targs in
let arguments' = this#call_arguments arguments in
let comments' = this#syntax_opt comments in
if callee == callee' && targs == targs' && arguments == arguments' && comments == comments'
then
expr
else
{ callee = callee'; targs = targs'; arguments = arguments'; comments = comments' }
method call_arguments (arg_list : ('loc, 'loc) Ast.Expression.ArgList.t) =
let open Ast.Expression.ArgList in
let (loc, { arguments; comments }) = arg_list in
let arguments' = map_list this#expression_or_spread arguments in
let comments' = this#syntax_opt comments in
if arguments == arguments' && comments == comments' then
arg_list
else
(loc, { arguments = arguments'; comments = comments' })
method optional_call loc (expr : ('loc, 'loc) Ast.Expression.OptionalCall.t) =
let open Ast.Expression.OptionalCall in
let { call; optional = _; filtered_out = _ } = expr in
let call' = this#call loc call in
if call == call' then
expr
else
{ expr with call = call' }
method call_type_args (targs : ('loc, 'loc) Ast.Expression.CallTypeArgs.t) =
let open Ast.Expression.CallTypeArgs in
let (loc, { arguments; comments }) = targs in
let arguments' = map_list this#call_type_arg arguments in
let comments' = this#syntax_opt comments in
if arguments == arguments' && comments == comments' then
targs
else
(loc, { arguments = arguments'; comments = comments' })
method call_type_arg t =
let open Ast.Expression.CallTypeArg in
match t with
| Explicit x ->
let x' = this#type_ x in
if x' == x then
t
else
Explicit x'
| Implicit (loc, { Implicit.comments }) ->
let comments' = this#syntax_opt comments in
if comments == comments' then
t
else
Implicit (loc, { Implicit.comments = comments' })
method catch_body (body : 'loc * ('loc, 'loc) Ast.Statement.Block.t) = map_loc this#block body
method catch_clause _loc (clause : ('loc, 'loc) Ast.Statement.Try.CatchClause.t') =
let open Ast.Statement.Try.CatchClause in
let { param; body; comments } = clause in
let param' = map_opt this#catch_clause_pattern param in
let body' = this#catch_body body in
let comments' = this#syntax_opt comments in
if param == param' && body == body' && comments == comments' then
clause
else
{ param = param'; body = body'; comments = comments' }
method class_declaration loc (cls : ('loc, 'loc) Ast.Class.t) = this#class_ loc cls
method class_expression loc (cls : ('loc, 'loc) Ast.Class.t) = this#class_ loc cls
method class_ _loc (cls : ('loc, 'loc) Ast.Class.t) =
let open Ast.Class in
let { id; body; tparams; extends; implements; class_decorators; comments } = cls in
let id' = map_opt this#class_identifier id in
let tparams' = map_opt this#type_params tparams in
let body' = this#class_body body in
let extends' = map_opt (map_loc this#class_extends) extends in
let implements' = map_opt this#class_implements implements in
let class_decorators' = map_list this#class_decorator class_decorators in
let comments' = this#syntax_opt comments in
if
id == id'
&& body == body'
&& extends == extends'
&& implements == implements'
&& class_decorators == class_decorators'
&& comments == comments'
&& tparams == tparams'
then
cls
else
{
id = id';
body = body';
extends = extends';
implements = implements';
class_decorators = class_decorators';
comments = comments';
tparams = tparams';
}
method class_extends _loc (extends : ('loc, 'loc) Ast.Class.Extends.t') =
let open Ast.Class.Extends in
let { expr; targs; comments } = extends in
let expr' = this#expression expr in
let targs' = map_opt this#type_args targs in
let comments' = this#syntax_opt comments in
if expr == expr' && targs == targs' && comments == comments' then
extends
else
{ expr = expr'; targs = targs'; comments = comments' }
method class_identifier (ident : ('loc, 'loc) Ast.Identifier.t) =
this#pattern_identifier ~kind:Ast.Statement.VariableDeclaration.Let ident
method class_body (cls_body : ('loc, 'loc) Ast.Class.Body.t) =
let open Ast.Class.Body in
let (loc, { body; comments }) = cls_body in
let body' = map_list this#class_element body in
let comments' = this#syntax_opt comments in
if body == body' && comments == comments' then
cls_body
else
(loc, { body = body'; comments = comments' })
method class_decorator (dec : ('loc, 'loc) Ast.Class.Decorator.t) =
let open Ast.Class.Decorator in
let (loc, { expression; comments }) = dec in
let expression' = this#expression expression in
let comments' = this#syntax_opt comments in
if expression == expression' && comments == comments' then
dec
else
(loc, { expression = expression'; comments = comments' })
method class_element (elem : ('loc, 'loc) Ast.Class.Body.element) =
let open Ast.Class.Body in
match elem with
| Method (loc, meth) -> id_loc this#class_method loc meth elem (fun meth -> Method (loc, meth))
| Property (loc, prop) ->
id_loc this#class_property loc prop elem (fun prop -> Property (loc, prop))
| PrivateField (loc, field) ->
id_loc this#class_private_field loc field elem (fun field -> PrivateField (loc, field))
method class_implements (implements : ('loc, 'loc) Ast.Class.Implements.t) =
let open Ast.Class.Implements in
let (loc, { interfaces; comments }) = implements in
let interfaces' = map_list this#class_implements_interface interfaces in
let comments' = this#syntax_opt comments in
if interfaces == interfaces' && comments == comments' then
implements
else
(loc, { interfaces = interfaces'; comments = comments' })
method class_implements_interface (interface : ('loc, 'loc) Ast.Class.Implements.Interface.t) =
let open Ast.Class.Implements.Interface in
let (loc, { id; targs }) = interface in
let id' = this#type_identifier_reference id in
let targs' = map_opt this#type_args targs in
if id == id' && targs == targs' then
interface
else
(loc, { id = id'; targs = targs' })
method class_method _loc (meth : ('loc, 'loc) Ast.Class.Method.t') =
let open Ast.Class.Method in
let { kind = _; key; value; static = _; decorators; comments } = meth in
let key' = this#object_key key in
let value' = map_loc this#function_expression_or_method value in
let decorators' = map_list this#class_decorator decorators in
let comments' = this#syntax_opt comments in
if key == key' && value == value' && decorators == decorators' && comments == comments' then
meth
else
{ meth with key = key'; value = value'; decorators = decorators'; comments = comments' }
method class_property _loc (prop : ('loc, 'loc) Ast.Class.Property.t') =
let open Ast.Class.Property in
let { key; value; annot; static = _; variance; comments } = prop in
let key' = this#object_key key in
let value' = this#class_property_value value in
let annot' = this#type_annotation_hint annot in
let variance' = this#variance_opt variance in
let comments' = this#syntax_opt comments in
if
key == key'
&& value == value'
&& annot' == annot
&& variance' == variance
&& comments' == comments
then
prop
else
{
prop with
key = key';
value = value';
annot = annot';
variance = variance';
comments = comments';
}
method class_property_value (value : ('loc, 'loc) Ast.Class.Property.value) =
let open Ast.Class.Property in
match value with
| Declared -> value
| Uninitialized -> value
| Initialized x ->
let x' = this#expression x in
if x == x' then
value
else
Initialized x'
method class_private_field _loc (prop : ('loc, 'loc) Ast.Class.PrivateField.t') =
let open Ast.Class.PrivateField in
let { key; value; annot; static = _; variance; comments } = prop in
let key' = this#private_name key in
let value' = this#class_property_value value in
let annot' = this#type_annotation_hint annot in
let variance' = this#variance_opt variance in
let comments' = this#syntax_opt comments in
if
key == key'
&& value == value'
&& annot' == annot
&& variance' == variance
&& comments' == comments
then
prop
else
{
prop with
key = key';
value = value';
annot = annot';
variance = variance';
comments = comments';
}
(* TODO *)
method comprehension _loc (expr : ('loc, 'loc) Ast.Expression.Comprehension.t) = expr
method conditional _loc (expr : ('loc, 'loc) Ast.Expression.Conditional.t) =
let open Ast.Expression.Conditional in
let { test; consequent; alternate; comments } = expr in
let test' = this#predicate_expression test in
let consequent' = this#expression consequent in
let alternate' = this#expression alternate in
let comments' = this#syntax_opt comments in
if
test == test'
&& consequent == consequent'
&& alternate == alternate'
&& comments == comments'
then
expr
else
{ test = test'; consequent = consequent'; alternate = alternate'; comments = comments' }
method continue _loc (cont : 'loc Ast.Statement.Continue.t) =
let open Ast.Statement.Continue in
let { label; comments } = cont in
let label' = map_opt this#label_identifier label in
let comments' = this#syntax_opt comments in
if label == label' && comments == comments' then
cont
else
{ label = label'; comments = comments' }
method debugger _loc (dbg : 'loc Ast.Statement.Debugger.t) =
let open Ast.Statement.Debugger in
let { comments } = dbg in
let comments' = this#syntax_opt comments in
if comments == comments' then
dbg
else
{ comments = comments' }
method declare_class _loc (decl : ('loc, 'loc) Ast.Statement.DeclareClass.t) =
let open Ast.Statement.DeclareClass in
let { id = ident; tparams; body; extends; mixins; implements; comments } = decl in
let id' = this#class_identifier ident in
let tparams' = map_opt this#type_params tparams in
let body' = map_loc this#object_type body in
let extends' = map_opt (map_loc this#generic_type) extends in
let mixins' = map_list (map_loc this#generic_type) mixins in
let implements' = map_opt this#class_implements implements in
let comments' = this#syntax_opt comments in
if
id' == ident
&& tparams' == tparams
&& body' == body
&& extends' == extends
&& mixins' == mixins
&& implements' == implements
&& comments' == comments
then
decl
else
{
id = id';
tparams = tparams';
body = body';
extends = extends';
mixins = mixins';
implements = implements';
comments = comments';
}
method declare_export_declaration
_loc (decl : ('loc, 'loc) Ast.Statement.DeclareExportDeclaration.t) =
let open Ast.Statement.DeclareExportDeclaration in
let { default; source; specifiers; declaration; comments } = decl in
let source' = map_loc_opt this#export_source source in
let specifiers' = map_opt this#export_named_specifier specifiers in
let declaration' = map_opt this#declare_export_declaration_decl declaration in
let comments' = this#syntax_opt comments in
if
source == source'
&& specifiers == specifiers'
&& declaration == declaration'
&& comments == comments'
then
decl
else
{
default;
source = source';
specifiers = specifiers';
declaration = declaration';
comments = comments';
}
method declare_export_declaration_decl
(decl : ('loc, 'loc) Ast.Statement.DeclareExportDeclaration.declaration) =
let open Ast.Statement.DeclareExportDeclaration in
match decl with
| Variable (loc, dv) ->
let dv' = this#declare_variable loc dv in
if dv' == dv then
decl
else
Variable (loc, dv')
| Function (loc, df) ->
let df' = this#declare_function loc df in
if df' == df then
decl
else
Function (loc, df')
| Class (loc, dc) ->
let dc' = this#declare_class loc dc in
if dc' == dc then
decl
else
Class (loc, dc')
| DefaultType t ->
let t' = this#type_ t in
if t' == t then
decl
else
DefaultType t'
| NamedType (loc, ta) ->
let ta' = this#type_alias loc ta in
if ta' == ta then
decl
else
NamedType (loc, ta')
| NamedOpaqueType (loc, ot) ->
let ot' = this#opaque_type loc ot in
if ot' == ot then
decl
else
NamedOpaqueType (loc, ot')
| Interface (loc, i) ->
let i' = this#interface loc i in
if i' == i then
decl
else
Interface (loc, i')
method declare_function _loc (decl : ('loc, 'loc) Ast.Statement.DeclareFunction.t) =
let open Ast.Statement.DeclareFunction in
let { id = ident; annot; predicate; comments } = decl in
let id' = this#function_identifier ident in
let annot' = this#type_annotation annot in
let predicate' = map_opt this#predicate predicate in
let comments' = this#syntax_opt comments in
if id' == ident && annot' == annot && predicate' == predicate && comments' == comments then
decl
else
{ id = id'; annot = annot'; predicate = predicate'; comments = comments' }
method declare_interface loc (decl : ('loc, 'loc) Ast.Statement.Interface.t) =
this#interface loc decl
method declare_module _loc (m : ('loc, 'loc) Ast.Statement.DeclareModule.t) =
let open Ast.Statement.DeclareModule in
let { id; body; kind; comments } = m in
let body' = map_loc this#block body in
let comments' = this#syntax_opt comments in
if body' == body && comments == comments' then
m
else
{ id; body = body'; kind; comments = comments' }
method declare_module_exports _loc (exports : ('loc, 'loc) Ast.Statement.DeclareModuleExports.t)
=
let open Ast.Statement.DeclareModuleExports in
let { annot; comments } = exports in
let annot' = this#type_annotation annot in
let comments' = this#syntax_opt comments in
if annot == annot' && comments == comments' then
exports
else
{ annot = annot'; comments = comments' }
method declare_type_alias loc (decl : ('loc, 'loc) Ast.Statement.TypeAlias.t) =
this#type_alias loc decl
method declare_variable _loc (decl : ('loc, 'loc) Ast.Statement.DeclareVariable.t) =
let open Ast.Statement.DeclareVariable in
let { id = ident; annot; comments } = decl in
let id' = this#pattern_identifier ~kind:Ast.Statement.VariableDeclaration.Var ident in
let annot' = this#type_annotation annot in
let comments' = this#syntax_opt comments in
if id' == ident && annot' == annot && comments' == comments then
decl
else
{ id = id'; annot = annot'; comments = comments' }
method do_while _loc (stuff : ('loc, 'loc) Ast.Statement.DoWhile.t) =
let open Ast.Statement.DoWhile in
let { body; test; comments } = stuff in
let body' = this#statement body in
let test' = this#predicate_expression test in
let comments' = this#syntax_opt comments in
if body == body' && test == test' && comments == comments' then
stuff
else
{ body = body'; test = test'; comments = comments' }
method empty _loc empty =
let open Ast.Statement.Empty in
let { comments } = empty in
let comments' = this#syntax_opt comments in
if comments == comments' then
empty
else
{ comments = comments' }
method enum_declaration _loc (enum : ('loc, 'loc) Ast.Statement.EnumDeclaration.t) =
let open Ast.Statement.EnumDeclaration in
let { id = ident; body; comments } = enum in
let id' = this#pattern_identifier ~kind:Ast.Statement.VariableDeclaration.Const ident in
let body' = this#enum_body body in
let comments' = this#syntax_opt comments in
if ident == id' && body == body' && comments == comments' then
enum
else
{ id = id'; body = body'; comments = comments' }
method enum_body (body : 'loc Ast.Statement.EnumDeclaration.body) =
let open Ast.Statement.EnumDeclaration in
match body with
| (loc, BooleanBody boolean_body) ->
id this#enum_boolean_body boolean_body body (fun body -> (loc, BooleanBody body))
| (loc, NumberBody number_body) ->
id this#enum_number_body number_body body (fun body -> (loc, NumberBody body))
| (loc, StringBody string_body) ->
id this#enum_string_body string_body body (fun body -> (loc, StringBody body))
| (loc, SymbolBody symbol_body) ->
id this#enum_symbol_body symbol_body body (fun body -> (loc, SymbolBody body))
method enum_boolean_body (body : 'loc Ast.Statement.EnumDeclaration.BooleanBody.t) =
let open Ast.Statement.EnumDeclaration.BooleanBody in
let { members; explicit_type = _; has_unknown_members = _; comments } = body in
let members' = map_list this#enum_boolean_member members in
let comments' = this#syntax_opt comments in
if members == members' && comments == comments' then
body
else
{ body with members = members'; comments = comments' }
method enum_number_body (body : 'loc Ast.Statement.EnumDeclaration.NumberBody.t) =
let open Ast.Statement.EnumDeclaration.NumberBody in
let { members; explicit_type = _; has_unknown_members = _; comments } = body in
let members' = map_list this#enum_number_member members in
let comments' = this#syntax_opt comments in
if members == members' && comments == comments' then
body
else
{ body with members = members'; comments = comments' }
method enum_string_body (body : 'loc Ast.Statement.EnumDeclaration.StringBody.t) =
let open Ast.Statement.EnumDeclaration.StringBody in
let { members; explicit_type = _; has_unknown_members = _; comments } = body in
let members' =
match members with
| Defaulted m -> id (map_list this#enum_defaulted_member) m members (fun m -> Defaulted m)
| Initialized m -> id (map_list this#enum_string_member) m members (fun m -> Initialized m)
in
let comments' = this#syntax_opt comments in
if members == members' && comments == comments' then
body
else
{ body with members = members'; comments = comments' }
method enum_symbol_body (body : 'loc Ast.Statement.EnumDeclaration.SymbolBody.t) =
let open Ast.Statement.EnumDeclaration.SymbolBody in
let { members; has_unknown_members = _; comments } = body in
let members' = map_list this#enum_defaulted_member members in
let comments' = this#syntax_opt comments in
if members == members' && comments == comments' then
body
else
{ body with members = members'; comments = comments' }
method enum_defaulted_member (member : 'loc Ast.Statement.EnumDeclaration.DefaultedMember.t) =
let open Ast.Statement.EnumDeclaration.DefaultedMember in
let (loc, { id = ident }) = member in
let id' = this#enum_member_identifier ident in
if ident == id' then
member
else
(loc, { id = id' })
method enum_boolean_member
(member :
('loc Ast.BooleanLiteral.t, 'loc) Ast.Statement.EnumDeclaration.InitializedMember.t
) =
let open Ast.Statement.EnumDeclaration.InitializedMember in
let (loc, { id = ident; init }) = member in
let id' = this#enum_member_identifier ident in
if ident == id' then
member
else
(loc, { id = id'; init })
method enum_number_member
(member : ('loc Ast.NumberLiteral.t, 'loc) Ast.Statement.EnumDeclaration.InitializedMember.t)
=
let open Ast.Statement.EnumDeclaration.InitializedMember in
let (loc, { id = ident; init }) = member in
let id' = this#enum_member_identifier ident in
if ident == id' then
member
else
(loc, { id = id'; init })
method enum_string_member
(member : ('loc Ast.StringLiteral.t, 'loc) Ast.Statement.EnumDeclaration.InitializedMember.t)
=
let open Ast.Statement.EnumDeclaration.InitializedMember in
let (loc, { id = ident; init }) = member in
let id' = this#enum_member_identifier ident in
if ident == id' then
member
else
(loc, { id = id'; init })
method enum_member_identifier (id : ('loc, 'loc) Ast.Identifier.t) = this#identifier id
method export_default_declaration
_loc (decl : ('loc, 'loc) Ast.Statement.ExportDefaultDeclaration.t) =
let open Ast.Statement.ExportDefaultDeclaration in
let { default; declaration; comments } = decl in
let declaration' = this#export_default_declaration_decl declaration in
let comments' = this#syntax_opt comments in
if declaration' == declaration && comments' == comments then
decl
else
{ default; declaration = declaration'; comments = comments' }
method export_default_declaration_decl
(decl : ('loc, 'loc) Ast.Statement.ExportDefaultDeclaration.declaration) =
let open Ast.Statement.ExportDefaultDeclaration in
match decl with
| Declaration stmt -> id this#statement stmt decl (fun stmt -> Declaration stmt)
| Expression expr -> id this#expression expr decl (fun expr -> Expression expr)
method export_named_declaration _loc (decl : ('loc, 'loc) Ast.Statement.ExportNamedDeclaration.t)
=
let open Ast.Statement.ExportNamedDeclaration in
let { export_kind; source; specifiers; declaration; comments } = decl in
let source' = map_loc_opt this#export_source source in
let specifiers' = map_opt this#export_named_specifier specifiers in
let declaration' = map_opt this#statement declaration in
let comments' = this#syntax_opt comments in
if
source == source'
&& specifiers == specifiers'
&& declaration == declaration'
&& comments == comments'
then
decl
else
{
export_kind;
source = source';
specifiers = specifiers';
declaration = declaration';
comments = comments';
}
method export_named_declaration_specifier
(spec : 'loc Ast.Statement.ExportNamedDeclaration.ExportSpecifier.t) =
let open Ast.Statement.ExportNamedDeclaration.ExportSpecifier in
let (loc, { local; exported }) = spec in
let local' = this#identifier local in
let exported' = map_opt this#identifier exported in
if local == local' && exported == exported' then
spec
else
(loc, { local = local'; exported = exported' })
method export_batch_specifier
(spec : 'loc Ast.Statement.ExportNamedDeclaration.ExportBatchSpecifier.t) =
let (loc, id_opt) = spec in
let id_opt' = map_opt this#identifier id_opt in
if id_opt == id_opt' then
spec
else
(loc, id_opt')
method export_named_specifier (spec : 'loc Ast.Statement.ExportNamedDeclaration.specifier) =
let open Ast.Statement.ExportNamedDeclaration in
match spec with
| ExportSpecifiers spec_list ->
let spec_list' = map_list this#export_named_declaration_specifier spec_list in
if spec_list == spec_list' then
spec
else
ExportSpecifiers spec_list'
| ExportBatchSpecifier batch ->
let batch' = this#export_batch_specifier batch in
if batch == batch' then
spec
else
ExportBatchSpecifier batch'
method export_source _loc (source : 'loc Ast.StringLiteral.t) =
let open Ast.StringLiteral in
let { value; raw; comments } = source in
let comments' = this#syntax_opt comments in
if comments == comments' then
source
else
{ value; raw; comments = comments' }
method expression_statement _loc (stmt : ('loc, 'loc) Ast.Statement.Expression.t) =
let open Ast.Statement.Expression in
let { expression = expr; directive; comments } = stmt in
let expr' = this#expression expr in
let comments' = this#syntax_opt comments in
if expr == expr' && comments == comments' then
stmt
else
{ expression = expr'; directive; comments = comments' }
method expression_or_spread expr_or_spread =
let open Ast.Expression in
match expr_or_spread with
| Expression expr -> id this#expression expr expr_or_spread (fun expr -> Expression expr)
| Spread spread -> id this#spread_element spread expr_or_spread (fun spread -> Spread spread)
method for_in_statement _loc (stmt : ('loc, 'loc) Ast.Statement.ForIn.t) =
let open Ast.Statement.ForIn in
let { left; right; body; each; comments } = stmt in
let left' = this#for_in_statement_lhs left in
let right' = this#expression right in
let body' = this#statement body in
let comments' = this#syntax_opt comments in
if left == left' && right == right' && body == body' && comments == comments' then
stmt
else
{ left = left'; right = right'; body = body'; each; comments = comments' }
method for_in_statement_lhs (left : ('loc, 'loc) Ast.Statement.ForIn.left) =
let open Ast.Statement.ForIn in
match left with
| LeftDeclaration decl ->
id this#for_in_left_declaration decl left (fun decl -> LeftDeclaration decl)
| LeftPattern patt ->
id this#for_in_assignment_pattern patt left (fun patt -> LeftPattern patt)
method for_in_left_declaration left =
let (loc, decl) = left in
id_loc this#variable_declaration loc decl left (fun decl -> (loc, decl))
method for_of_statement _loc (stuff : ('loc, 'loc) Ast.Statement.ForOf.t) =
let open Ast.Statement.ForOf in
let { left; right; body; await; comments } = stuff in
let left' = this#for_of_statement_lhs left in
let right' = this#expression right in
let body' = this#statement body in
let comments' = this#syntax_opt comments in
if left == left' && right == right' && body == body' && comments == comments' then
stuff
else
{ left = left'; right = right'; body = body'; await; comments = comments' }
method for_of_statement_lhs (left : ('loc, 'loc) Ast.Statement.ForOf.left) =