Source file 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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
(*
 * 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; interpreter; comments; all_comments }) =
      let body = statement_list statements in
      let props = [("body", body); ("comments", comment_list all_comments)] in
      let props =
        match interpreter with
        | Some (loc, value) ->
          let directive = node "InterpreterDirective" loc [("value", string value)] in
          props @ [("interpreter", directive)]
        | None -> props
      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; return_out = _ }) ->
        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, ComponentDeclaration c) -> component_declaration (loc, c)
      | (loc, DeclareVariable d) -> declare_variable (loc, d)
      | (loc, DeclareFunction d) -> declare_function (loc, d)
      | (loc, DeclareClass d) -> declare_class (loc, d)
      | (loc, DeclareComponent d) -> declare_component (loc, d)
      | (loc, DeclareEnum enum) -> declare_enum (loc, enum)
      | (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; 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)]
      | (loc, DeclareNamespace { DeclareNamespace.id; body; comments }) ->
        let id = identifier id in
        node ?comments "DeclareNamespace" loc [("id", id); ("body", block body)]
      | ( 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.Component c) -> declare_component 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
            | Some (DeclareExportDeclaration.Enum enum) -> declare_enum enum
            | 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; remote_name_def_loc = _; 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;
              effect = _;
              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 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", function_return_type 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, AsExpression { AsExpression.expression = expr; annot = (_, annot); comments }) ->
        node
          ?comments
          "AsExpression"
          loc
          [("expression", expression expr); ("typeAnnotation", _type annot)]
      | (loc, TSSatisfies { TSSatisfies.expression = expr; annot = (_, annot); comments }) ->
        node
          ?comments
          "SatisfiesExpression"
          loc
          [("expression", expression expr); ("typeAnnotation", _type annot)]
      | (loc, AsConstExpression { AsConstExpression.expression = expr; comments }) ->
        node ?comments "AsConstExpression" loc [("expression", expression expr)]
      | (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_out = _;
            }
        ) ->
        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_out = _ }
        ) ->
        node
          ?comments
          "OptionalMemberExpression"
          loc
          (member_node_properties member @ [("optional", bool optional)])
      | (loc, Yield { Yield.argument; delegate; comments; result_out = _ }) ->
        node
          ?comments
          "YieldExpression"
          loc
          [("argument", option expression argument); ("delegate", bool delegate)]
      | (_loc, Identifier id) -> identifier id
      | (loc, StringLiteral lit) -> string_literal (loc, lit)
      | (loc, BooleanLiteral lit) -> boolean_literal (loc, lit)
      | (loc, NullLiteral lit) -> null_literal (loc, lit)
      | (loc, NumberLiteral lit) -> number_literal (loc, lit)
      | (loc, BigIntLiteral lit) -> bigint_literal (loc, lit)
      | (loc, RegExpLiteral lit) -> regexp_literal (loc, lit)
      | (loc, ModuleRefLiteral lit) -> module_ref_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;
            effect;
            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 comments =
        Flow_ast_utils.merge_comments
          ~outer:func_comments
          ~inner:(format_internal_comments params_comments)
      in
      let (node_name, nonhook_attrs) =
        if effect = Function.Hook then
          ("HookDeclaration", [])
        else
          ( "FunctionDeclaration",
            [
              ("async", bool async);
              ("generator", bool generator);
              ("predicate", option predicate predicate_);
              ("expression", bool false);
            ]
          )
      in
      node
        ?comments
        node_name
        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);
           ("returnType", function_return_type return);
           ("typeParameters", option type_parameter_declaration tparams);
         ]
        @ nonhook_attrs
        )
    and function_expression
        ( loc,
          {
            Function.id;
            params = (_, { Function.Params.comments = params_comments; _ }) as params;
            async;
            generator;
            effect = _;
            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 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", function_return_type 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; kind; comments }) =
      let id_loc = Loc.btwn (fst id) (fst annot) in
      let kind = variable_kind kind in
      node
        ?comments
        "DeclareVariable"
        loc
        [
          ( "id",
            pattern_identifier
              id_loc
              { Pattern.Identifier.name = id; annot = Ast.Type.Available annot; optional = false }
          );
          ("kind", string kind);
        ]
    and declare_function
        (loc, { Statement.DeclareFunction.id; annot; predicate = predicate_; comments }) =
      let id_loc = Loc.btwn (fst id) (fst annot) in
      let (name, predicate) =
        match annot with
        | (_, (_, Type.Function { Type.Function.effect = Function.Hook; _ })) -> ("DeclareHook", [])
        | _ -> ("DeclareFunction", [("predicate", option predicate predicate_)])
      in
      node
        ?comments
        name
        loc
        ([
           ( "id",
             pattern_identifier
               id_loc
               { Pattern.Identifier.name = id; annot = Ast.Type.Available annot; optional = false }
           );
         ]
        @ 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_component (loc, component) =
      let {
        Statement.DeclareComponent.id;
        tparams;
        params = (_, { Type.Component.Params.comments = params_comments; _ }) as params;
        renders;
        comments = component_comments;
      } =
        component
      in
      let comments =
        Flow_ast_utils.merge_comments
          ~outer:component_comments
          ~inner:(format_internal_comments params_comments)
      in
      let (_, { Type.Component.Params.params = param_list; rest; comments = _ }) = params in
      node
        ?comments
        "DeclareComponent"
        loc
        [
          ("id", identifier id);
          ("params", component_type_params param_list);
          ("rest", option component_type_rest_param rest);
          ("params", component_type_params param_list);
          ("rendersType", renders_annotation renders);
          ("typeParameters", option type_parameter_declaration tparams);
        ]
    and component_type (loc, component) =
      let {
        Type.Component.tparams;
        params = (_, { Type.Component.Params.comments = params_comments; _ }) as params;
        renders;
        comments = component_comments;
      } =
        component
      in
      let comments =
        Flow_ast_utils.merge_comments
          ~outer:component_comments
          ~inner:(format_internal_comments params_comments)
      in
      let (_, { Type.Component.Params.params = param_list; rest; comments = _ }) = params in
      node
        ?comments
        "ComponentTypeAnnotation"
        loc
        [
          ("params", component_type_params param_list);
          ("rest", option component_type_rest_param rest);
          ("rendersType", renders_annotation renders);
          ("typeParameters", option type_parameter_declaration tparams);
        ]
    and component_type_params params =
      let open Type.Component in
      let params =
        List.map
          (fun (loc, { Param.name; annot; optional }) ->
            let (_, annot') = annot in
            component_type_param ~optional loc (Some name) annot')
          params
      in
      array params
    and component_type_rest_param rest =
      let open Type.Component in
      let (loc, { RestParam.argument; annot; optional; comments }) = rest in
      component_type_param
        ?comments
        ~optional
        loc
        (Option.map (fun i -> Statement.ComponentDeclaration.Param.Identifier i) argument)
        annot
    and component_type_param ?comments ~optional loc name annot =
      let name' =
        match name with
        | Some (Statement.ComponentDeclaration.Param.Identifier id) -> option identifier (Some id)
        | Some (Statement.ComponentDeclaration.Param.StringLiteral id) ->
          option string_literal (Some id)
        | None -> option identifier None
      in
      node
        ?comments
        "ComponentTypeParameter"
        loc
        [("name", name'); ("typeAnnotation", _type annot); ("optional", bool optional)]
    and declare_enum (loc, { Statement.EnumDeclaration.id; body; comments }) =
      node ?comments "DeclareEnum" loc [("id", identifier id); ("body", enum_body body)]
    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
        | StringLiteral lit -> (string_literal lit, false, comments)
        | NumberLiteral lit -> (number_literal lit, false, comments)
        | BigIntLiteral lit -> (bigint_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_;
            decorators;
            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_);
        ]
        @ ( if decorators = [] then
            []
          else
            [("decorators", array_of_list class_decorator decorators)]
          )
        @
        if declare then
          [("declare", bool declare)]
        else
          []
      in
      node ?comments "PropertyDefinition" loc props
    and class_property
        ( loc,
          { Class.Property.key; value; annot; static; variance = variance_; decorators; comments }
        ) =
      let (key, computed, comments) =
        match key with
        | Expression.Object.Property.StringLiteral lit -> (string_literal lit, false, comments)
        | Expression.Object.Property.NumberLiteral lit -> (number_literal lit, false, comments)
        | Expression.Object.Property.BigIntLiteral lit -> (bigint_literal lit, false, comments)
        | Expression.Object.Property.Identifier id -> (identifier id, false, comments)
        | Expression.Object.Property.PrivateName _ ->
          failwith "Internal Error: Private name found in class prop"
        | Expression.Object.Property.Computed
            (_, { ComputedKey.expression = expr; comments = key_comments }) ->
          (expression expr, true, Flow_ast_utils.merge_comments ~outer:comments ~inner:key_comments)
      in
      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", key);
          ("value", option expression value);
          ("typeAnnotation", hint type_annotation annot);
          ("computed", bool computed);
          ("static", bool static);
          ("variance", option variance variance_);
        ]
        @ ( if decorators = [] then
            []
          else
            [("decorators", array_of_list class_decorator decorators)]
          )
        @
        if declare then
          [("declare", bool declare)]
        else
          []
      in
      node ?comments "PropertyDefinition" loc props
    and component_declaration (loc, component) =
      let open Statement.ComponentDeclaration in
      let {
        id;
        tparams;
        params = (_, { Params.comments = params_comments; _ }) as params;
        body;
        renders;
        comments = component_comments;
        sig_loc = _;
      } =
        component
      in
      let comments =
        Flow_ast_utils.merge_comments
          ~outer:component_comments
          ~inner:(format_internal_comments params_comments)
      in
      node
        ?comments
        "ComponentDeclaration"
        loc
        [
          ("body", block body);
          ("id", identifier id);
          ("params", component_params params);
          ("rendersType", renders_annotation renders);
          ("typeParameters", option type_parameter_declaration tparams);
        ]
    and component_params =
      let open Statement.ComponentDeclaration.Params in
      function
      | ( _,
          {
            params;
            rest = Some (rest_loc, { Statement.ComponentDeclaration.RestParam.argument; comments });
            comments = _;
          }
        ) ->
        let rest = node ?comments "RestElement" rest_loc [("argument", pattern argument)] in
        let rev_params = List.rev_map component_param params in
        let params = List.rev (rest :: rev_params) in
        array params
      | (_, { params; rest = None; comments = _ }) ->
        let params = List.map component_param params in
        array params
    and component_param param =
      let open Statement.ComponentDeclaration.Param in
      let (loc, { name; local; default; shorthand }) = param in
      let name' =
        match name with
        | Identifier id -> identifier id
        | StringLiteral id -> string_literal id
      in
      let local' =
        match default with
        | Some default ->
          node "AssignmentPattern" loc [("left", pattern local); ("right", expression default)]
        | None -> pattern local
      in
      node
        "ComponentParameter"
        loc
        [("name", name'); ("local", local'); ("shorthand", bool shorthand)]
    and enum_body body =
      let open Statement.EnumDeclaration in
      match body with
      | (loc, BooleanBody { BooleanBody.members; explicit_type; has_unknown_members; comments }) ->
        node
          ?comments:(format_internal_comments comments)
          "EnumBooleanBody"
          loc
          [
            ( "members",
              array_of_list
                (fun (loc, { InitializedMember.id; init }) ->
                  node
                    "EnumBooleanMember"
                    loc
                    [("id", identifier id); ("init", boolean_literal init)])
                members
            );
            ("explicitType", bool explicit_type);
            ("hasUnknownMembers", bool has_unknown_members);
          ]
      | (loc, NumberBody { NumberBody.members; explicit_type; has_unknown_members; comments }) ->
        node
          ?comments:(format_internal_comments comments)
          "EnumNumberBody"
          loc
          [
            ( "members",
              array_of_list
                (fun (loc, { InitializedMember.id; init }) ->
                  node "EnumNumberMember" loc [("id", identifier id); ("init", number_literal init)])
                members
            );
            ("explicitType", bool explicit_type);
            ("hasUnknownMembers", bool has_unknown_members);
          ]
      | (loc, StringBody { StringBody.members; explicit_type; has_unknown_members; comments }) ->
        let members =
          match members with
          | StringBody.Defaulted defaulted_members ->
            List.map
              (fun (loc, { DefaultedMember.id }) ->
                node "EnumDefaultedMember" loc [("id", identifier id)])
              defaulted_members
          | StringBody.Initialized initialized_members ->
            List.map
              (fun (loc, { InitializedMember.id; init }) ->
                node "EnumStringMember" loc [("id", identifier id); ("init", string_literal init)])
              initialized_members
        in
        node
          ?comments:(format_internal_comments comments)
          "EnumStringBody"
          loc
          [
            ("members", array members);
            ("explicitType", bool explicit_type);
            ("hasUnknownMembers", bool has_unknown_members);
          ]
      | (loc, SymbolBody { SymbolBody.members; has_unknown_members; comments }) ->
        node
          ?comments:(format_internal_comments comments)
          "EnumSymbolBody"
          loc
          [
            ( "members",
              array_of_list
                (fun (loc, { DefaultedMember.id }) ->
                  node "EnumDefaultedMember" loc [("id", identifier id)])
                members
            );
            ("hasUnknownMembers", bool has_unknown_members);
          ]
      | (loc, BigIntBody { BigIntBody.members; explicit_type; has_unknown_members; comments }) ->
        node
          ?comments:(format_internal_comments comments)
          "EnumBigIntBody"
          loc
          [
            ( "members",
              array_of_list
                (fun (loc, { InitializedMember.id; init }) ->
                  node "EnumBigIntMember" loc [("id", identifier id); ("init", bigint_literal init)])
                members
            );
            ("explicitType", bool explicit_type);
            ("hasUnknownMembers", bool has_unknown_members);
          ]
    and enum_declaration (loc, { Statement.EnumDeclaration.id; body; comments }) =
      node ?comments "EnumDeclaration" loc [("id", identifier id); ("body", enum_body body)]
    and interface_declaration (loc, { Statement.Interface.id; tparams; body; extends; comments }) =
      node
        ?comments
        "InterfaceDeclaration"
        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 interface_extends (loc, { Type.Generic.id; targs; comments }) =
      let id =
        match id with
        | Type.Generic.Identifier.Unqualified id -> identifier id
        | Type.Generic.Identifier.Qualified q -> generic_type_qualified_identifier q
      in
      node ?comments "InterfaceExtends" loc [("id", id); ("typeParameters", option type_args targs)]
    and pattern =
      Pattern.(
        function
        | (loc, Object { Object.properties; annot; comments }) ->
          node
            ?comments:(format_internal_comments comments)
            "ObjectPattern"
            loc
            [
              ("properties", array_of_list object_pattern_property properties);
              ("typeAnnotation", hint type_annotation annot);
            ]
        | (loc, Array { Array.elements; annot; comments }) ->
          node
            ?comments:(format_internal_comments comments)
            "ArrayPattern"
            loc
            [
              ("elements", array_of_list array_pattern_element elements);
              ("typeAnnotation", hint type_annotation annot);
            ]
        | (loc, Identifier pattern_id) -> pattern_identifier loc pattern_id
        | (_loc, Expression expr) -> expression expr
      )
    and function_param (loc, { Ast.Function.Param.argument; default }) =
      match default with
      | Some default ->
        node "AssignmentPattern" loc [("left", pattern argument); ("right", expression default)]
      | None -> pattern argument
    and this_param (loc, { Function.ThisParam.annot; comments }) =
      node
        ?comments
        "Identifier"
        loc
        [("name", string "this"); ("typeAnnotation", type_annotation annot)]
    and function_params =
      let open Ast.Function.Params in
      function
      | ( _,
          {
            params;
            rest = Some (rest_loc, { Function.RestParam.argument; comments });
            comments = _;
            this_;
          }
        ) ->
        let rest = node ?comments "RestElement" rest_loc [("argument", pattern argument)] in
        let rev_params = List.rev_map function_param params in
        let params = List.rev (rest :: rev_params) in
        let params =
          match this_ with
          | Some this -> this_param this :: params
          | None -> params
        in
        array params
      | (_, { params; rest = None; this_; comments = _ }) ->
        let params = List.map function_param params in
        let params =
          match this_ with
          | Some this -> this_param this :: params
          | None -> params
        in
        array params
    and rest_element loc { Pattern.RestElement.argument; comments } =
      node ?comments "RestElement" loc [("argument", pattern argument)]
    and array_pattern_element =
      let open Pattern.Array in
      function
      | Hole _ -> null
      | Element (loc, { Element.argument; default = Some default }) ->
        node "AssignmentPattern" loc [("left", pattern argument); ("right", expression default)]
      | Element (_loc, { Element.argument; default = None }) -> pattern argument
      | RestElement (loc, el) -> rest_element loc el
    and function_return_type = function
      | Ast.Function.ReturnAnnot.Missing _ -> null
      | Ast.Function.ReturnAnnot.TypeGuard (loc, g) -> type_guard_annotation (loc, g)
      | Ast.Function.ReturnAnnot.Available t -> type_annotation t
    and object_property =
      let open Expression.Object in
      function
      | Property (loc, prop) ->
        Property.(
          let (key, value, kind, method_, shorthand, comments) =
            match prop with
            | Init { key; value; shorthand } ->
              (key, expression value, "init", false, shorthand, None)
            | Method { key; value = (loc, func) } ->
              (key, function_expression (loc, func), "init", true, false, None)
            | Get { key; value = (loc, func); comments } ->
              (key, function_expression (loc, func), "get", false, false, comments)
            | Set { key; value = (loc, func); comments } ->
              (key, function_expression (loc, func), "set", false, false, comments)
          in
          let (key, computed, comments) =
            match key with
            | StringLiteral lit -> (string_literal lit, false, comments)
            | NumberLiteral lit -> (number_literal lit, false, comments)
            | BigIntLiteral lit -> (bigint_literal lit, false, comments)
            | Identifier id -> (identifier id, false, comments)
            | PrivateName _ -> failwith "Internal Error: Found private field in object props"
            | Computed (_, { ComputedKey.expression = expr; comments = key_comments }) ->
              ( expression expr,
                true,
                Flow_ast_utils.merge_comments ~outer:comments ~inner:key_comments
              )
          in
          node
            ?comments
            "Property"
            loc
            [
              ("key", key);
              ("value", value);
              ("kind", string kind);
              ("method", bool method_);
              ("shorthand", bool shorthand);
              ("computed", bool computed);
            ]
        )
      | SpreadProperty (loc, { SpreadProperty.argument; comments }) ->
        node ?comments "SpreadElement" loc [("argument", expression argument)]
    and object_pattern_property =
      let open Pattern.Object in
      function
      | Property (loc, { Property.key; pattern = patt; default; shorthand }) ->
        let (key, computed, comments) =
          match key with
          | Property.StringLiteral lit -> (string_literal lit, false, None)
          | Property.NumberLiteral lit -> (number_literal lit, false, None)
          | Property.BigIntLiteral lit -> (bigint_literal lit, false, None)
          | Property.Identifier id -> (identifier id, false, None)
          | Property.Computed (_, { ComputedKey.expression = expr; comments }) ->
            (expression expr, true, comments)
        in
        let value =
          match default with
          | Some default ->
            let loc = Loc.btwn (fst patt) (fst default) in
            node "AssignmentPattern" loc [("left", pattern patt); ("right", expression default)]
          | None -> pattern patt
        in
        node
          ?comments
          "Property"
          loc
          [
            ("key", key);
            ("value", value);
            ("kind", string "init");
            ("method", bool false);
            ("shorthand", bool shorthand);
            ("computed", bool computed);
          ]
      | RestElement (loc, el) -> rest_element loc el
    and spread_element (loc, { Expression.SpreadElement.argument; comments }) =
      node ?comments "SpreadElement" loc [("argument", expression argument)]
    and expression_or_spread =
      let open Expression in
      function
      | Expression expr -> expression expr
      | Spread spread -> spread_element spread
    and array_element =
      let open Expression.Array in
      function
      | Hole _ -> null
      | Expression expr -> expression expr
      | Spread spread -> spread_element spread
    and number_literal (loc, { NumberLiteral.value; raw; comments }) =
      node ?comments "Literal" loc [("value", number value); ("raw", string raw)]
    and bigint_literal (loc, { BigIntLiteral.value; raw; comments }) =
      (* https://github.com/estree/estree/blob/master/es2020.md#bigintliteral
       * `bigint` property is the string representation of the `BigInt` value.
       * It must contain only decimal digits and not include numeric separators `_` or the suffix `n`.
       *)
      let bigint =
        match value with
        | Some value -> Int64.to_string value
        | None ->
          String.sub raw 0 (String.length raw - 1) |> String.split_on_char '_' |> String.concat ""
      in
      node ?comments "Literal" loc [("value", null); ("bigint", string bigint); ("raw", string raw)]
    and string_literal (loc, { StringLiteral.value; raw; comments }) =
      node ?comments "Literal" loc [("value", string value); ("raw", string raw)]
    and boolean_literal (loc, { BooleanLiteral.value; comments }) =
      let raw =
        if value then
          "true"
        else
          "false"
      in
      node ?comments "Literal" loc [("value", bool value); ("raw", string raw)]
    and regexp_literal (loc, { RegExpLiteral.pattern; flags; raw; comments; _ }) =
      let value = regexp loc pattern flags in
      let regex = obj [("pattern", string pattern); ("flags", string flags)] in
      node ?comments "Literal" loc [("value", value); ("raw", string raw); ("regex", regex)]
    and null_literal (loc, comments) =
      node ?comments "Literal" loc [("value", null); ("raw", string "null")]
    and module_ref_literal (loc, { ModuleRefLiteral.value; raw; comments; _ }) =
      string_literal (loc, { StringLiteral.value; raw; comments })
    and template_literal (loc, { Expression.TemplateLiteral.quasis; expressions; comments }) =
      node
        ?comments
        "TemplateLiteral"
        loc
        [
          ("quasis", array_of_list template_element quasis);
          ("expressions", array_of_list expression expressions);
        ]
    and template_element
        ( loc,
          {
            Expression.TemplateLiteral.Element.value =
              { Expression.TemplateLiteral.Element.raw; cooked };
            tail;
          }
        ) =
      let value = obj [("raw", string raw); ("cooked", string cooked)] in
      node "TemplateElement" loc [("value", value); ("tail", bool tail)]
    and tagged_template (loc, { Expression.TaggedTemplate.tag; quasi; comments }) =
      node
        ?comments
        "TaggedTemplateExpression"
        loc
        [("tag", expression tag); ("quasi", template_literal quasi)]
    and variable_kind kind =
      match kind with
      | Variable.Var -> "var"
      | Variable.Let -> "let"
      | Variable.Const -> "const"
    and variable_declaration (loc, { Statement.VariableDeclaration.kind; declarations; comments }) =
      let kind = variable_kind kind in
      node
        ?comments
        "VariableDeclaration"
        loc
        [("declarations", array_of_list variable_declarator declarations); ("kind", string kind)]
    and variable_declarator (loc, { Statement.VariableDeclaration.Declarator.id; init }) =
      node "VariableDeclarator" loc [("id", pattern id); ("init", option expression init)]
    and variance (loc, { Variance.kind; comments }) =
      let open Variance in
      let kind_str =
        match kind with
        | Plus -> "plus"
        | Minus -> "minus"
        | Readonly -> "readonly"
        | In -> "in"
        | Out -> "out"
        | InOut -> "in-out"
      in
      node ?comments "Variance" loc [("kind", string kind_str)]
    and _type (loc, t) =
      Type.(
        match t with
        | Any comments -> any_type loc comments
        | Mixed comments -> mixed_type loc comments
        | Empty comments -> empty_type loc comments
        | Void comments -> void_type loc comments
        | Null comments -> null_type loc comments
        | Symbol comments -> symbol_type loc comments
        | Number comments -> number_type loc comments
        | BigInt comments -> bigint_type loc comments
        | String comments -> string_type loc comments
        | Boolean { raw = _; comments } -> boolean_type loc comments
        | Nullable t -> nullable_type loc t
        | Function fn -> function_type (loc, fn)
        | Component c -> component_type (loc, c)
        | Object o -> object_type ~include_inexact:true (loc, o)
        | Interface i -> interface_type (loc, i)
        | Array t -> array_type loc t
        | Conditional t -> conditional_type loc t
        | Infer t -> infer_type loc t
        | Generic g -> generic_type (loc, g)
        | IndexedAccess ia -> indexed_access (loc, ia)
        | OptionalIndexedAccess ia -> optional_indexed_access (loc, ia)
        | Union t -> union_type (loc, t)
        | Intersection t -> intersection_type (loc, t)
        | Typeof t -> typeof_type (loc, t)
        | Keyof t -> keyof_type (loc, t)
        | Renders renders -> render_type loc renders
        | ReadOnly t -> read_only_type (loc, t)
        | Tuple t -> tuple_type (loc, t)
        | StringLiteral s -> string_literal_type (loc, s)
        | NumberLiteral n -> number_literal_type (loc, n)
        | BigIntLiteral n -> bigint_literal_type (loc, n)
        | BooleanLiteral b -> boolean_literal_type (loc, b)
        | Exists comments -> exists_type loc comments
        | Unknown comments -> unknown_type loc comments
        | Never comments -> never_type loc comments
        | Undefined comments -> undefined_type loc comments
      )
    and any_type loc comments = node ?comments "AnyTypeAnnotation" loc []
    and mixed_type loc comments = node ?comments "MixedTypeAnnotation" loc []
    and empty_type loc comments = node ?comments "EmptyTypeAnnotation" loc []
    and void_type loc comments = node ?comments "VoidTypeAnnotation" loc []
    and null_type loc comments = node ?comments "NullLiteralTypeAnnotation" loc []
    and symbol_type loc comments = node ?comments "SymbolTypeAnnotation" loc []
    and number_type loc comments = node ?comments "NumberTypeAnnotation" loc []
    and bigint_type loc comments = node ?comments "BigIntTypeAnnotation" loc []
    and string_type loc comments = node ?comments "StringTypeAnnotation" loc []
    and boolean_type loc comments = node ?comments "BooleanTypeAnnotation" loc []
    and nullable_type loc { Type.Nullable.argument; comments } =
      node ?comments "NullableTypeAnnotation" loc [("typeAnnotation", _type argument)]
    and unknown_type loc comments = node ?comments "UnknownTypeAnnotation" loc []
    and never_type loc comments = node ?comments "NeverTypeAnnotation" loc []
    and undefined_type loc comments = node ?comments "UndefinedTypeAnnotation" loc []
    and return_annotation = function
      | Ast.Type.Function.TypeAnnotation t -> _type t
      | Ast.Type.Function.TypeGuard g -> type_guard g
    and type_guard (loc, { Ast.Type.TypeGuard.kind; guard = (x, t); comments }) =
      let kind =
        let open Ast.Type.TypeGuard in
        match kind with
        | Default -> null
        | Asserts -> string "asserts"
        | Implies -> string "implies"
      in
      node
        ?comments:(format_internal_comments comments)
        "TypePredicate"
        loc
        [("parameterName", identifier x); ("typeAnnotation", option _type t); ("kind", kind)]
    and function_type
        ( loc,
          {
            Type.Function.params =
              (_, { Type.Function.Params.this_; params; rest; comments = params_comments });
            return;
            tparams;
            effect;
            comments = func_comments;
          }
        ) =
      let comments =
        Flow_ast_utils.merge_comments
          ~inner:(format_internal_comments params_comments)
          ~outer:func_comments
      in
      let name =
        if effect = Function.Hook then
          "HookTypeAnnotation"
        else
          "FunctionTypeAnnotation"
      in
      node
        ?comments
        name
        loc
        ([
           ("params", array_of_list function_type_param params);
           ("returnType", return_annotation return);
           ("rest", option function_type_rest rest);
           ("typeParameters", option type_parameter_declaration tparams);
         ]
        @
        if effect = Function.Hook then
          []
        else
          [("this", option function_type_this_constraint this_)]
        )
    and function_type_param ?comments (loc, { Type.Function.Param.name; annot; optional }) =
      node
        ?comments
        "FunctionTypeParam"
        loc
        [
          ("name", option identifier name);
          ("typeAnnotation", _type annot);
          ("optional", bool optional);
        ]
    and function_type_rest (_loc, { Type.Function.RestParam.argument; comments }) =
      (* TODO: add a node for the rest param itself, including the `...`,
         like we do with RestElement on normal functions. This should be
         coordinated with Babel, ast-types, etc. so keeping the status quo for
         now. Here's an example: *)
      (* node "FunctionTypeRestParam" loc [
           "argument", function_type_param argument;
         ] *)
      function_type_param ?comments argument
    and function_type_this_constraint (loc, { Type.Function.ThisParam.annot = (_, annot); comments })
        =
      node
        ?comments
        "FunctionTypeParam"
        loc
        [
          ("name", option identifier None); ("typeAnnotation", _type annot); ("optional", bool false);
        ]
    and object_type ~include_inexact (loc, { Type.Object.properties; exact; inexact; comments }) =
      Type.Object.(
        let (props, ixs, calls, slots) =
          List.fold_left
            (fun (props, ixs, calls, slots) -> function
              | Property p ->
                let prop = object_type_property p in
                (prop :: props, ixs, calls, slots)
              | SpreadProperty p ->
                let prop = object_type_spread_property p in
                (prop :: props, ixs, calls, slots)
              | Indexer i ->
                let ix = object_type_indexer i in
                (props, ix :: ixs, calls, slots)
              | CallProperty c ->
                let call = object_type_call_property c in
                (props, ixs, call :: calls, slots)
              | InternalSlot s ->
                let slot = object_type_internal_slot s in
                (props, ixs, calls, slot :: slots)
              | MappedType m ->
                let mapped_type = object_type_mapped_type m in
                (mapped_type :: props, ixs, calls, slots))
            ([], [], [], [])
            properties
        in
        let fields =
          [
            ("exact", bool exact);
            ("properties", array (List.rev props));
            ("indexers", array (List.rev ixs));
            ("callProperties", array (List.rev calls));
            ("internalSlots", array (List.rev slots));
          ]
        in
        let fields =
          if include_inexact then
            ("inexact", bool inexact) :: fields
          else
            fields
        in
        node ?comments:(format_internal_comments comments) "ObjectTypeAnnotation" loc fields
      )
    and object_type_property
        ( loc,
          {
            Type.Object.Property.key;
            value;
            optional;
            static;
            proto;
            variance = variance_;
            _method;
            comments;
          }
        ) =
      let key =
        match key with
        | Expression.Object.Property.StringLiteral lit -> string_literal lit
        | Expression.Object.Property.NumberLiteral lit -> number_literal lit
        | Expression.Object.Property.BigIntLiteral lit -> bigint_literal lit
        | Expression.Object.Property.Identifier id -> identifier id
        | Expression.Object.Property.PrivateName _ ->
          failwith "Internal Error: Found private field in object props"
        | Expression.Object.Property.Computed _ ->
          failwith "There should not be computed object type property keys"
      in
      let (value, kind) =
        match value with
        | Type.Object.Property.Init value -> (_type value, "init")
        | Type.Object.Property.Get (loc, f) -> (function_type (loc, f), "get")
        | Type.Object.Property.Set (loc, f) -> (function_type (loc, f), "set")
      in
      node
        ?comments
        "ObjectTypeProperty"
        loc
        [
          ("key", key);
          ("value", value);
          ("method", bool _method);
          ("optional", bool optional);
          ("static", bool static);
          ("proto", bool proto);
          ("variance", option variance variance_);
          ("kind", string kind);
        ]
    and object_type_spread_property (loc, { Type.Object.SpreadProperty.argument; comments }) =
      node ?comments "ObjectTypeSpreadProperty" loc [("argument", _type argument)]
    and object_type_indexer
        (loc, { Type.Object.Indexer.id; key; value; static; variance = variance_; comments }) =
      node
        ?comments
        "ObjectTypeIndexer"
        loc
        [
          ("id", option identifier id);
          ("key", _type key);
          ("value", _type value);
          ("static", bool static);
          ("variance", option variance variance_);
        ]
    and object_type_call_property (loc, { Type.Object.CallProperty.value; static; comments }) =
      node
        ?comments
        "ObjectTypeCallProperty"
        loc
        [("value", function_type value); ("static", bool static)]
    and object_type_mapped_type
        ( mt_loc,
          {
            Type.Object.MappedType.key_tparam;
            prop_type;
            source_type;
            variance = variance_;
            comments;
            optional;
          }
        ) =
      let optional_flag flag =
        Type.Object.MappedType.(
          match flag with
          | PlusOptional -> string "PlusOptional"
          | MinusOptional -> string "MinusOptional"
          | Optional -> string "Optional"
          | NoOptionalFlag -> null
        )
      in
      node
        ?comments
        "ObjectTypeMappedTypeProperty"
        mt_loc
        [
          ("keyTparam", type_param key_tparam);
          ("propType", _type prop_type);
          ("sourceType", _type source_type);
          ("variance", option variance variance_);
          ("optional", optional_flag optional);
        ]
    and object_type_internal_slot
        (loc, { Type.Object.InternalSlot.id; optional; static; _method; value; comments }) =
      node
        ?comments
        "ObjectTypeInternalSlot"
        loc
        [
          ("id", identifier id);
          ("optional", bool optional);
          ("static", bool static);
          ("method", bool _method);
          ("value", _type value);
        ]
    and interface_type (loc, { Type.Interface.extends; body; comments }) =
      node
        ?comments
        "InterfaceTypeAnnotation"
        loc
        [
          ("extends", array_of_list interface_extends extends);
          ("body", object_type ~include_inexact:false body);
        ]
    and array_type loc { Type.Array.argument; comments } =
      node ?comments "ArrayTypeAnnotation" loc [("elementType", _type argument)]
    and conditional_type
        loc { Type.Conditional.check_type; extends_type; true_type; false_type; comments } =
      node
        ?comments
        "ConditionalTypeAnnotation"
        loc
        [
          ("checkType", _type check_type);
          ("extendsType", _type extends_type);
          ("trueType", _type true_type);
          ("falseType", _type false_type);
        ]
    and infer_type loc { Type.Infer.tparam; comments } =
      node ?comments "InferTypeAnnotation" loc [("typeParameter", type_param tparam)]
    and generic_type_qualified_identifier (loc, { Type.Generic.Identifier.id; qualification }) =
      let qualification =
        match qualification with
        | Type.Generic.Identifier.Unqualified id -> identifier id
        | Type.Generic.Identifier.Qualified q -> generic_type_qualified_identifier q
      in
      node "QualifiedTypeIdentifier" loc [("qualification", qualification); ("id", identifier id)]
    and generic_type (loc, { Type.Generic.id; targs; comments }) =
      let id =
        match id with
        | Type.Generic.Identifier.Unqualified id -> identifier id
        | Type.Generic.Identifier.Qualified q -> generic_type_qualified_identifier q
      in
      node
        ?comments
        "GenericTypeAnnotation"
        loc
        [("id", id); ("typeParameters", option type_args targs)]
    and indexed_access_properties { Type.IndexedAccess._object; index; comments = _ } =
      [("objectType", _type _object); ("indexType", _type index)]
    and indexed_access (loc, ({ Type.IndexedAccess.comments; _ } as ia)) =
      node ?comments "IndexedAccessType" loc (indexed_access_properties ia)
    and optional_indexed_access
        ( loc,
          {
            Type.OptionalIndexedAccess.indexed_access =
              { Type.IndexedAccess.comments; _ } as indexed_access;
            optional;
          }
        ) =
      node
        ?comments
        "OptionalIndexedAccessType"
        loc
        (indexed_access_properties indexed_access @ [("optional", bool optional)])
    and union_type (loc, { Type.Union.types = (t0, t1, ts); comments }) =
      node ?comments "UnionTypeAnnotation" loc [("types", array_of_list _type (t0 :: t1 :: ts))]
    and intersection_type (loc, { Type.Intersection.types = (t0, t1, ts); comments }) =
      node
        ?comments
        "IntersectionTypeAnnotation"
        loc
        [("types", array_of_list _type (t0 :: t1 :: ts))]
    and typeof_type (loc, { Type.Typeof.argument; targs; comments }) =
      let targs_field =
        match targs with
        | None -> []
        | Some targs -> [("typeArguments", type_args targs)]
      in
      node ?comments "TypeofTypeAnnotation" loc (("argument", typeof_expr argument) :: targs_field)
    and typeof_expr id =
      match id with
      | Type.Typeof.Target.Unqualified id -> identifier id
      | Type.Typeof.Target.Qualified q -> typeof_qualifier q
    and typeof_qualifier (loc, { Type.Typeof.Target.id; qualification }) =
      let qualification = typeof_expr qualification in
      node "QualifiedTypeofIdentifier" loc [("qualification", qualification); ("id", identifier id)]
    and keyof_type (loc, { Type.Keyof.argument; comments }) =
      node ?comments "KeyofTypeAnnotation" loc [("argument", _type argument)]
    and renders_annotation = function
      | Ast.Type.AvailableRenders (loc, v) -> render_type loc v
      | Ast.Type.MissingRenders _ -> null
    and render_type loc { Type.Renders.operator_loc = _; comments; variant; argument } =
      let operator =
        match variant with
        | Type.Renders.Normal -> "renders"
        | Type.Renders.Maybe -> "renders?"
        | Type.Renders.Star -> "renders*"
      in
      flow_type_operator loc comments operator argument
    and flow_type_operator loc comments operator operand =
      node
        ?comments
        "TypeOperator"
        loc
        [("operator", string operator); ("typeAnnotation", _type operand)]
    and read_only_type (loc, { Type.ReadOnly.argument; comments }) =
      flow_type_operator loc comments "readonly" argument
    and tuple_type (loc, { Type.Tuple.elements; inexact; comments }) =
      node
        ?comments
        "TupleTypeAnnotation"
        loc
        [
          ( "elementTypes",
            array_of_list
              (function
                | (_, Type.Tuple.UnlabeledElement annot) -> _type annot
                | (loc, Type.Tuple.LabeledElement e) -> tuple_labeled_element loc e
                | (loc, Type.Tuple.SpreadElement e) -> tuple_spread_element loc e)
              elements
          );
          ("inexact", bool inexact);
        ]
    and tuple_labeled_element
        ?comments loc { Type.Tuple.LabeledElement.name; annot; variance = variance_; optional } =
      node
        ?comments
        "TupleTypeLabeledElement"
        loc
        [
          ("label", identifier name);
          ("elementType", _type annot);
          ("variance", option variance variance_);
          ("optional", bool optional);
        ]
    and tuple_spread_element ?comments loc { Type.Tuple.SpreadElement.name; annot } =
      node
        ?comments
        "TupleTypeSpreadElement"
        loc
        [("label", option identifier name); ("typeAnnotation", _type annot)]
    and string_literal_type (loc, { Ast.StringLiteral.value; raw; comments }) =
      node
        ?comments
        "StringLiteralTypeAnnotation"
        loc
        [("value", string value); ("raw", string raw)]
    and number_literal_type (loc, { Ast.NumberLiteral.value; raw; comments }) =
      node
        ?comments
        "NumberLiteralTypeAnnotation"
        loc
        [("value", number value); ("raw", string raw)]
    and bigint_literal_type (loc, { Ast.BigIntLiteral.raw; comments; _ }) =
      node ?comments "BigIntLiteralTypeAnnotation" loc [("value", null); ("raw", string raw)]
    and boolean_literal_type (loc, { Ast.BooleanLiteral.value; comments }) =
      node
        ?comments
        "BooleanLiteralTypeAnnotation"
        loc
        [
          ("value", bool value);
          ( "raw",
            string
              ( if value then
                "true"
              else
                "false"
              )
          );
        ]
    and exists_type loc comments = node ?comments "ExistsTypeAnnotation" loc []
    and type_annotation (loc, ty) = node "TypeAnnotation" loc [("typeAnnotation", _type ty)]
    and type_guard_annotation (loc, (loc1, guard)) =
      node "TypeAnnotation" loc [("typeAnnotation", type_guard (loc1, guard))]
    and type_parameter_declaration (loc, { Type.TypeParams.params; comments }) =
      node
        ?comments:(format_internal_comments comments)
        "TypeParameterDeclaration"
        loc
        [("params", array_of_list type_param params)]
    and type_param
        ( loc,
          {
            Type.TypeParam.name = (_, { Identifier.name; comments });
            bound;
            bound_kind;
            variance = tp_var;
            default;
          }
        ) =
      node
        ?comments
        "TypeParameter"
        loc
        ([
           (* we track the location of the name, but don't expose it here for
              backwards-compatibility. TODO: change this? *)
           ("name", string name);
           ("bound", hint type_annotation bound);
           ("variance", option variance tp_var);
           ("default", option _type default);
         ]
        @
        match bound_kind with
        | Type.TypeParam.Colon -> []
        | Type.TypeParam.Extends -> [("usesExtendsBound", bool true)]
        )
    and type_args (loc, { Type.TypeArgs.arguments; comments }) =
      node
        ?comments:(format_internal_comments comments)
        "TypeParameterInstantiation"
        loc
        [("params", array_of_list _type arguments)]
    and call_type_args (loc, { Expression.CallTypeArgs.arguments; comments }) =
      node
        ?comments:(format_internal_comments comments)
        "TypeParameterInstantiation"
        loc
        [("params", array_of_list call_type_arg arguments)]
    and call_type_arg x =
      match x with
      | Expression.CallTypeArg.Explicit t -> _type t
      | Expression.CallTypeArg.Implicit (loc, { Expression.CallTypeArg.Implicit.comments }) ->
        generic_type
          ( loc,
            {
              Type.Generic.id =
                Type.Generic.Identifier.Unqualified (Flow_ast_utils.ident_of_source (loc, "_"));
              targs = None;
              comments;
            }
          )
    and jsx_element
        (loc, { JSX.opening_element; closing_element; children = (_loc, children); comments }) =
      node
        ?comments
        "JSXElement"
        loc
        [
          ("openingElement", jsx_opening opening_element);
          ("closingElement", option jsx_closing closing_element);
          ("children", array_of_list jsx_child children);
        ]
    and jsx_fragment
        ( loc,
          {
            JSX.frag_opening_element;
            frag_closing_element;
            frag_children = (_loc, frag_children);
            frag_comments;
          }
        ) =
      node
        ?comments:frag_comments
        "JSXFragment"
        loc
        [
          ("openingFragment", jsx_opening_fragment frag_opening_element);
          ("children", array_of_list jsx_child frag_children);
          ("closingFragment", jsx_closing_fragment frag_closing_element);
        ]
    and jsx_opening (loc, { JSX.Opening.name; targs; attributes; self_closing }) =
      node
        "JSXOpeningElement"
        loc
        ([
           ("name", jsx_name name);
           ("attributes", array_of_list jsx_opening_attribute attributes);
           ("selfClosing", bool self_closing);
         ]
        @
        match targs with
        | Some targs -> [("typeArguments", call_type_args targs)]
        | None -> []
        )
    and jsx_opening_fragment loc = node "JSXOpeningFragment" loc []
    and jsx_opening_attribute =
      JSX.Opening.(
        function
        | Attribute attribute -> jsx_attribute attribute
        | SpreadAttribute attribute -> jsx_spread_attribute attribute
      )
    and jsx_closing (loc, { JSX.Closing.name }) =
      node "JSXClosingElement" loc [("name", jsx_name name)]
    and jsx_closing_fragment loc = node "JSXClosingFragment" loc []
    and jsx_child =
      JSX.(
        function
        | (loc, Element element) -> jsx_element (loc, element)
        | (loc, Fragment fragment) -> jsx_fragment (loc, fragment)
        | (loc, ExpressionContainer expr) -> jsx_expression_container (loc, expr)
        | (loc, SpreadChild spread) -> jsx_spread_child (loc, spread)
        | (loc, Text str) -> jsx_text (loc, str)
      )
    and jsx_name =
      JSX.(
        function
        | Identifier id -> jsx_identifier id
        | NamespacedName namespaced_name -> jsx_namespaced_name namespaced_name
        | MemberExpression member -> jsx_member_expression member
      )
    and jsx_attribute (loc, { JSX.Attribute.name; value }) =
      let name =
        match name with
        | JSX.Attribute.Identifier id -> jsx_identifier id
        | JSX.Attribute.NamespacedName namespaced_name -> jsx_namespaced_name namespaced_name
      in
      node "JSXAttribute" loc [("name", name); ("value", option jsx_attribute_value value)]
    and jsx_attribute_value =
      JSX.Attribute.(
        function
        | StringLiteral (loc, value) -> string_literal (loc, value)
        | ExpressionContainer (loc, expr) -> jsx_expression_container (loc, expr)
      )
    and jsx_spread_attribute (loc, { JSX.SpreadAttribute.argument; comments }) =
      node ?comments "JSXSpreadAttribute" loc [("argument", expression argument)]
    and jsx_expression_container (loc, { JSX.ExpressionContainer.expression = expr; comments }) =
      let expression =
        match expr with
        | JSX.ExpressionContainer.Expression expr -> expression expr
        | JSX.ExpressionContainer.EmptyExpression ->
          let empty_loc =
            let open Loc in
            {
              loc with
              start = { loc.start with column = loc.start.column + 1 };
              _end = { loc._end with column = loc._end.column - 1 };
            }
          in

          node "JSXEmptyExpression" empty_loc []
      in
      node
        ?comments:(format_internal_comments comments)
        "JSXExpressionContainer"
        loc
        [("expression", expression)]
    and jsx_spread_child (loc, { JSX.SpreadChild.expression = expr; comments }) =
      node ?comments "JSXSpreadChild" loc [("expression", expression expr)]
    and jsx_text (loc, { JSX.Text.value; raw }) =
      node "JSXText" loc [("value", string value); ("raw", string raw)]
    and jsx_member_expression (loc, { JSX.MemberExpression._object; property }) =
      let _object =
        match _object with
        | JSX.MemberExpression.Identifier id -> jsx_identifier id
        | JSX.MemberExpression.MemberExpression member -> jsx_member_expression member
      in
      node "JSXMemberExpression" loc [("object", _object); ("property", jsx_identifier property)]
    and jsx_namespaced_name (loc, { JSX.NamespacedName.namespace; name }) =
      node
        "JSXNamespacedName"
        loc
        [("namespace", jsx_identifier namespace); ("name", jsx_identifier name)]
    and jsx_identifier (loc, { JSX.Identifier.name; comments }) =
      node ?comments "JSXIdentifier" loc [("name", string name)]
    and export_specifier (loc, { Statement.ExportNamedDeclaration.ExportSpecifier.exported; local })
        =
      let exported =
        match exported with
        | Some exported -> identifier exported
        | None -> identifier local
      in
      node "ExportSpecifier" loc [("local", identifier local); ("exported", exported)]
    and import_default_specifier
        { Statement.ImportDeclaration.identifier = id; remote_default_name_def_loc = _ } =
      node "ImportDefaultSpecifier" (fst id) [("local", identifier id)]
    and import_namespace_specifier (loc, id) =
      node "ImportNamespaceSpecifier" loc [("local", identifier id)]
    and import_named_specifier local_id remote_id kind =
      let span_loc =
        match local_id with
        | Some local_id -> Loc.btwn (fst remote_id) (fst local_id)
        | None -> fst remote_id
      in
      let local_id =
        match local_id with
        | Some id -> id
        | None -> remote_id
      in
      node
        "ImportSpecifier"
        span_loc
        [
          ("imported", identifier remote_id);
          ("local", identifier local_id);
          ( "importKind",
            match kind with
            | Some Statement.ImportDeclaration.ImportType -> string "type"
            | Some Statement.ImportDeclaration.ImportTypeof -> string "typeof"
            | Some Statement.ImportDeclaration.ImportValue
            | None ->
              null
          );
        ]
    and comment_list comments = array_of_list comment comments
    and comment (loc, c) =
      Comment.(
        let (_type, value) =
          match c with
          | { kind = Line; text = s; _ } -> ("Line", s)
          | { kind = Block; text = s; _ } -> ("Block", s)
        in
        node _type loc [("value", string value)]
      )
    and predicate (loc, { Ast.Type.Predicate.kind; comments }) =
      let open Ast.Type.Predicate in
      let (_type, value) =
        match kind with
        | Declared e -> ("DeclaredPredicate", [("value", expression e)])
        | Inferred -> ("InferredPredicate", [])
      in
      node ?comments _type loc value
    and call_node_properties { Expression.Call.callee; targs; arguments; comments = _ } =
      [
        ("callee", expression callee);
        ("typeArguments", option call_type_args targs);
        ("arguments", arg_list arguments);
      ]
    and member_node_properties { Expression.Member._object; property; comments = _ } =
      let (property, computed) =
        match property with
        | Expression.Member.PropertyIdentifier id -> (identifier id, false)
        | Expression.Member.PropertyPrivateName name -> (private_identifier name, false)
        | Expression.Member.PropertyExpression expr -> (expression expr, true)
      in
      [("object", expression _object); ("property", property); ("computed", bool computed)]
    in
    { program; expression }

  let program offset_table = (make_functions offset_table).program

  let expression offset_table = (make_functions offset_table).expression
end