tree-sitter-stack-graphs-javascript 0.3.0

Stack graphs definition for JavaScript using tree-sitter-javascript
Documentation
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
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Stack graphs definition for JavaScript
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Global Variables
;; ^^^^^^^^^^^^^^^^

global ROOT_NODE
global JUMP_TO_SCOPE_NODE

global FILE_PATH
global PROJECT_NAME = ""


;; Attribute Shorthands
;; ^^^^^^^^^^^^^^^^^^^^

attribute node_definition = node        => type = "pop_symbol", node_symbol = node, is_definition
attribute node_reference = node         => type = "push_symbol", node_symbol = node, is_reference
attribute pop_node = node               => type = "pop_symbol", node_symbol = node
attribute pop_scoped_node = node        => type = "pop_scoped_symbol", node_symbol = node
attribute pop_scoped_symbol = symbol    => type = "pop_scoped_symbol", symbol = symbol
attribute pop_symbol = symbol           => type = "pop_symbol", symbol = symbol
attribute push_node = node              => type = "push_symbol", node_symbol = node
attribute push_scoped_node = node       => type = "push_scoped_symbol", node_symbol = node
attribute push_scoped_symbol = symbol   => type = "push_scoped_symbol", symbol = symbol
attribute push_symbol = symbol          => type = "push_symbol", symbol = symbol
attribute scoped_node_definition = node => type = "pop_scoped_symbol", node_symbol = node, is_definition
attribute scoped_node_reference = node  => type = "push_scoped_symbol", node_symbol = node, is_reference
attribute symbol_definition = symbol    => type = "pop_symbol", symbol = symbol, is_definition
attribute symbol_reference = symbol     => type = "push_symbol", symbol = symbol, is_reference

attribute node_symbol = node            => symbol = (source-text node), source_node = node

;; Stack Graph Rules
;; ^^^^^^^^^^^^^^^^^

;; # JavaScript

;; This file defines StackGraph queries for JavaScript. It is written as a
;; semi-literate file, which is to say, comments starting with `;;` can be
;; treated as Markdown. This file can therefore be converted into a
;; corresponding pure Markdown document by removing the StackGraph comments, and
;; wrapping the uncommented code in Markdown code blocks.

;; This file has a number of sections that it's divided into, and it's useful to
;; provide an overview here. Aside from conventions, the queries are broken up
;; into groups by what kind of syntactic objects the group covers. There are
;; Programs, Statements, Expressions, Patterns, and Special Cases.

;; Within each major section, we break things down by the particular syntactic
;; form under consideration. Some particular syntactic forms are not one of the
;; main forms listed, but are tightly associated with other forms -- for
;; example, the branches of an `if` statement are not statements nor
;; expressions, they're simply components of an `if` statement. In that
;; situation, we group them with the primary statement that they're associated
;; with, where possible, and nearby when not.

;; Additionally, some syntactic constructs have arbitrary numbers of child
;; nodes,  which requires us to explain how to relate them as sequences of
;; nodes. In such cases, we have associated queries with the node type in
;; question.

;; ## Design Conventions

;; The general convention for how JavaScript is translated into a Stack Graph is
;; to treat the graph as a reverse Control Flow Graph. There are some corner
;; cases for which that analogy doesn't quite work, but it's sufficient for
;; conveying the overall approach. Many things matter for name resolution in a
;; Control Flow Graph, but one of the only ones we can really encode into a
;; Stack Graph, and arguably the most important one, is the flow of the variable
;; environment, and so we create scope nodes for those.

;; In particular, for each node, during an execution traversal, there is an
;; incoming variable environment that the node will be executed in, and an
;; outgoing variable environment that resulted from executing the node and all
;; the various variable assignments and updates inside it.

;; An example helps, so let's consider a simplified case: `if` statement with a
;; mandatory `else` branch. If we were to implement an execution function for
;; this fictionalized language, there would be some environment that goes into
;; the execution at the whole `if-then-else` statement, an environment that goes
;; into the evaluation of the test, an environment that comes out of the test
;; (because the test could be something like `x++` which changes the
;; environment), an environment which goes into each branch (which happens to be
;; the same one that came out of the test), and an environment that comes out of
;; each of the branches. So each major AST node has a pair of environments
;; associated with it: the incoming one to execute it in, and the outcoming node
;; that resulted from executing.

;; We therefore would implement `if-then-else` statements like so:

;; ``````stgdoc
;; (if_then_else
;;   (_)@test
;;   (_)@consequent
;;   (_)alternative)@if_stmt {
;;
;;   edge @test.before_scope -> @if_stmt.before_scope
;;   edge @consequent.before_scope -> @test.after_scope
;;   edge @alternative.before_scope -> @test.after_scope
;;   edge @if_stmt.after_scope -> @consequent.after_scope
;;   edge @if_stmt.after_scope -> @alternative.after_scope
;;
;; }
;; ``````
;;
;; Another important way that things build scopes is through values. When an
;; expression is executed and returns a value, that value in many ways acts like
;; a sub-environment, at least in that the value has different parts that can be
;; accessed in various ways. For a simple value, as constructed by a number
;; literal expression, the number itself has no parts. So it has an associated
;; value scope node in the graph, but no edges coming off that:

;; ``````stgdoc
;; (number)@num {
;;   attr (@num.value) pop_symbol = "NUM_VAL"
;; }
;; ``````

;; Why we use a `pop` attribute here isn't deeply important so we'll gloss over
;; it.

;; All kinds of expressions have values, what precisely goes into them depends
;; on what the expression is. Objects, for instance, would have corresponding
;; values that point to the values of each of the keys in the object. Arrays
;; would have values that point to the values of each of the indexes.

;; The primary purpose of values is to act as the targets of assignments, so
;; that names can resolve to something in code. Normally we care only about the
;; location of the assignment, of course, but we also need to be able to do
;; further lookup on the values. For instance, in an expression like `x.y.z`,
;; we need to look up `x` of course, but also then further look up `y` on that,
;; and then `z`. So whatever `x` is assigned to has to also be present in the
;; graph as something which can itself have stuff hanging off it that can be
;; used for lookup.

;; If you were to run some JavaScript to test whether or not number literals
;; have parts (really, members/fields), you'd discover that actually they do,
;; but not because of which number they are. Rather, their parts are for builtin
;; functionality like `toString`. To handle this, we can point the value to some
;; shared global variable that represents the prototype for the number, like so:

;; ``````stgdoc
;; (number)@num {
;;   attr (@num.value) pop_symbol = "NUM_VAL"
;;   edge @num.value -> @num.number_prototype
;; }
;; ``````

;; In the preamble definition for the `program` node type, we could then also
;; want to define number prototype, something like this:

;; ``````stgdoc
;; inherit .number_prototype
;;
;; (program)@prog {
;;
;;   ...
;;
;;   node @prog.number_prototype
;;   edge @prog.number_prototype -> @prog.number_prototype_toString_method_value
;;   edge @prog.number_prototype -> @prog.number_prototype_valueOf_method_value
;;   ...etc
;;
;;   ...
;;
;; }
;; ``````

;; We would then also want to have a bunch of hand-encoded graphs for the values
;; of those fields. That would then allow the number values to point to their
;; prototypes where the fields could be found and used for further lookup on
;; methods.

;; One caveat of this is that we don't want to get too deep into explaining how
;; some method might be implemented. Indeed, sometimes we *can't*, because it's
;; not implementable in JavaScript at all and is instead some primitive function
;; implemented in the host language. What we want to do, instead, is to provide
;; just enough implementation that other data can flow through as much as
;; possible, to prevent calls to primitive methods from blocking downstream name
;; resolution. For instance, it would be unfortunate if calling `toString` on a
;; number did not let you look up string fields on the resultant value of the
;; call, i.e. if `length` in `(5).toString().length` just simply could not be
;; resolved at all. In such cases, the ideal approach is to implement a bare
;; minimum of `toString` so that we can recover the fact that its returned
;; value is a string. E.g.:

;; ``````stgdoc
;; (program)@prog {
;;
;;   ...
;;
;;   edge @prog.number_prototype -> @prog.number_prototype_toString_method_value
;;   edge @prog.number_prototype_toString_method_value -> @prog.number_prototype_toString_method_value_return
;;   attr (@prog.number_prototype_toString_method_value_return) pop_symbol = "GUARD:RETURN"
;;   edge @prog.number_prototype_toString_method_value_return -> @prog.string_prototype
;;
;;   ...
;;
;; }
;; ``````

;; As currently implemented, this document does not contain any rules for
;; builtins because of the scope of that task, but they are currently in the
;; pipeline, using the above approach.

;; Something you may notice in the above code snippet is this pop node labelled
;; `GUARD:RETURN`. This is another part of the design conventions for this
;; library. We often want to use nodes in the graph to constrain name lookup.
;; In this case, we want to be able to pinpoint some of the graph nodes
;; associated with the body of a function as being the values returned by the
;; function. We do this by creating a so-called guard node, which will only be
;; traversed if there is a corresponding push somewhere else. We would generate
;; such a push node precisely when we call a function. This lets us treat the
;; function *ASTs* as if they have parts that we can inspect *in the graph*.

;; By convention, this library of queries prefixes all guard nodes with "GUARD:"
;; to distinguish them from nodes that more directly correspond to aspects of
;; execution such as member lookup (labelled with "GUARD:MEMBER") or variable lookup
;; (labelled with the variable name itself). The current names used for guard
;; nodes are

;; - `GUARD:CONSTRUCTOR` - used for the constructor method inside a `Class`
;; - `GUARD:DEFAULT` - used for the default export value
;; - `GUARD:EXPORTS` - used for the names exported by the module
;; - `GUARD:GANDALF` - used for situations where parts of a graph must be accessible
;;   in order to avoid pruning, but which never the less should not be actually reached
;;   during any normal traversals. By only ever popping, and never pushing, we can get
;;   a connected subgraph that is never the less never traversed.
;; - `GUARD:LABEL` - used for the names of labels
;; - `GUARD:MEMBER` - used for the members/fields/properties of objects
;; - `GUARD:MODULE` - used for module names
;; - `GUARD:RETURN` - used for the AST nodes for values returned by a function
;;   in the function body
;; - `GUARD:THIS` - used for the implicit `this` argument of a function inside
;; - `GUARD:PKG_INTERNAL` - used for the package internal structure that should not
;;   be accessible directly from the root

; ## Inherited

inherit .builtins_Regex_prototype
inherit .builtins_arguments_prototype
inherit .builtins_boolean
inherit .builtins_empty_object
inherit .builtins_null
inherit .builtins_number
inherit .builtins_string
inherit .builtins_undefined
inherit .class_value
inherit .constructor
inherit .export_statement
inherit .exports
inherit .hoist_point
inherit .closure_point
inherit .import_statement
inherit .pkg_pop
inherit .pkg_push
inherit .return_or_yield
inherit .containing_class_value










;; ██████  ██████   ██████   ██████  ██████   █████  ███    ███ ███████ 
;; ██   ██ ██   ██ ██    ██ ██       ██   ██ ██   ██ ████  ████ ██      
;; ██████  ██████  ██    ██ ██   ███ ██████  ███████ ██ ████ ██ ███████ 
;; ██      ██   ██ ██    ██ ██    ██ ██   ██ ██   ██ ██  ██  ██      ██ 
;; ██      ██   ██  ██████   ██████  ██   ██ ██   ██ ██      ██ ███████

;; ## Programs

;; ### Attributes Defined on Programs
;; TODO

(program)@prog {
  node @prog.after_scope
  node @prog.before_scope
  let @prog.closure_point = @prog.after_scope

  ; apparently it's perfectly cromulent to `return` from the top level scope (because control flow happens there too), so we make a top-level return node.
  node @prog.return_or_yield
}

;; ### Program Queries

(program)@prog {

    node prog_module_pop
    node prog_exports_pop
    node prog_pkg_pop_guard
    node prog_pkg_push_guard
    node prog_legacy_qname_guard
    node @prog.exports
    node @prog.hoist_point
    node @prog.pkg_pop
    node @prog.pkg_push

    attr (prog_pkg_pop_guard) pop_symbol = "GUARD:PKG_INTERNAL"
    attr (@prog.pkg_pop) pop_symbol = PROJECT_NAME
    edge ROOT_NODE -> prog_pkg_pop_guard
    edge prog_pkg_pop_guard -> @prog.pkg_pop

    attr (@prog.pkg_push) push_symbol = PROJECT_NAME
    attr (prog_pkg_push_guard) push_symbol = "GUARD:PKG_INTERNAL"
    edge @prog.pkg_push -> prog_pkg_push_guard
    edge prog_pkg_push_guard -> ROOT_NODE

    node module_guard_pop
    attr (module_guard_pop) pop_symbol = "GUARD:MODULE"
    edge @prog.pkg_pop -> module_guard_pop

    node prog_module_pop_start
    edge module_guard_pop -> prog_module_pop_start
    var module_pop_end = prog_module_pop_start
    let module_name = (replace FILE_PATH "\.js$" "")
    scan module_name {
      "([^/]+)/" {
        attr (module_pop_end) pop_symbol = $1
        node module_pop_end_next
        edge module_pop_end -> module_pop_end_next
        set module_pop_end = module_pop_end_next
      }
      "([^/]+)$" {
        attr (module_pop_end) symbol_definition = $1, source_node = @prog
        attr (module_pop_end) empty_source_span
        attr (module_pop_end) definiens_node = @prog
      }
    }

    edge @prog.before_scope -> @prog.pkg_push
    edge @prog.before_scope -> @prog.hoist_point


    attr (prog_legacy_qname_guard) push_symbol = "GUARD:LEGACY_QNAME"
    edge @prog.before_scope -> prog_legacy_qname_guard
    edge prog_legacy_qname_guard -> ROOT_NODE

    attr (prog_module_pop) empty_source_span
    attr (@prog.exports) empty_source_span
    attr (prog_exports_pop) empty_source_span
    attr (@prog.before_scope) empty_source_span
    attr (@prog.after_scope) empty_source_span

    attr (prog_exports_pop) pop_symbol = "GUARD:EXPORTS"
    edge module_pop_end -> prog_exports_pop
    edge prog_exports_pop -> @prog.exports

    ;; builtin types
    node @prog.builtins_number
    node @prog.builtins_string
    node @prog.builtins_boolean
    node @prog.builtins_null
    node @prog.builtins_undefined
    node @prog.builtins_Regex_prototype
    node @prog.builtins_arguments_prototype
    node @prog.builtins_empty_object
    ; !!!! HACK
    ; stack graphs currently make it impossible to test if an inherited variable
    ; like this is defined or not
    let @prog.containing_class_value = @prog.builtins_null

}

; programs, first statement
(program
  .
  (_)@first_stmt)@prog {

  ; scopes flow from the program into the first statement
  edge @first_stmt.before_scope -> @prog.before_scope
}

; program, between statements
(program
  (_)@left_stmt
  .
  (_)@right_stmt) {

  ; scopes flow from the left statement to the right one
  edge @right_stmt.before_scope -> @left_stmt.after_scope
}

; program, last statement
(program
  (_)@last_stmt
  .)@prog {

  ; scopes flow from the last statement to the program
  edge @prog.after_scope -> @last_stmt.after_scope
}

(hash_bang_line)@hashbang {
  node @hashbang.after_scope
  node @hashbang.before_scope
  edge @hashbang.after_scope -> @hashbang.before_scope
}

(comment)@comment {
  node @comment.after_scope
  node @comment.before_scope
  node @comment.value
  node @comment.covalue
  node @comment.new_bindings ; for object patterns
  edge @comment.after_scope -> @comment.before_scope
  node @comment.source ; for export clauses with multiple exports
}

(identifier) @identifier {
  node @identifier.before_scope
  node @identifier.after_scope
  node @identifier.value
  node @identifier.covalue
  node @identifier.new_bindings
  edge @identifier.after_scope -> @identifier.before_scope
}










;; ███████ ████████  █████  ████████ ███████ ███    ███ ███████ ███    ██ ████████ ███████ 
;; ██         ██    ██   ██    ██    ██      ████  ████ ██      ████   ██    ██    ██      
;; ███████    ██    ███████    ██    █████   ██ ████ ██ █████   ██ ██  ██    ██    ███████ 
;;      ██    ██    ██   ██    ██    ██      ██  ██  ██ ██      ██  ██ ██    ██         ██ 
;; ███████    ██    ██   ██    ██    ███████ ██      ██ ███████ ██   ████    ██    ███████

;; ## Statements

;; ### Attributes Defined on Statements
;; TODO

[
  (export_statement)
  (import_statement)
  (debugger_statement)
  (expression_statement)
  (function_declaration)
  (generator_function_declaration)
  (class_declaration)
  (lexical_declaration)
  (variable_declaration)
  (statement_block)
  (if_statement)
  (switch_statement)
  (for_statement)
  (for_in_statement)
  (while_statement)
  (do_statement)
  (try_statement)
  (with_statement)
  (break_statement)
  (continue_statement)
  (return_statement)
  (throw_statement)
  (empty_statement)
  (labeled_statement)
]@stmt {
  node @stmt.after_scope
  node @stmt.before_scope
}

;; ### Statement Queries



;; ███████ ██   ██ ██████   ██████  ██████  ████████ ███████ 
;; ██       ██ ██  ██   ██ ██    ██ ██   ██    ██    ██      
;; █████     ███   ██████  ██    ██ ██████     ██    ███████ 
;; ██       ██ ██  ██      ██    ██ ██   ██    ██         ██ 
;; ███████ ██   ██ ██       ██████  ██   ██    ██    ███████

;; #### Export

(export_statement)@export_stmt {
  let @export_stmt.export_statement = @export_stmt
}

; exports of just names
; eg
;  export { foo, bar as baz };
(export_statement
  (export_clause)@export_clause
  !source)@export_stmt {

  edge @export_clause.source -> @export_clause.before_scope

  ; scope flows through the export clause
  edge @export_clause.before_scope -> @export_stmt.before_scope
  edge @export_stmt.after_scope -> @export_clause.after_scope
}

(export_statement
  (export_clause)@export_clause
  source:(_)@source)@export_stmt {

  edge @export_clause.source -> @source.exports

  edge @export_clause.before_scope -> @export_stmt.before_scope
  edge @export_stmt.after_scope -> @export_clause.after_scope
}

(export_statement
  (declaration)@decl)@export_stmt {

  edge @decl.before_scope -> @export_stmt.before_scope
  edge @export_stmt.after_scope -> @decl.after_scope

}

(export_statement "default"
  (declaration)@decl)@export_stmt {

  node pop_guard_default
  attr (pop_guard_default) symbol_definition = "GUARD:DEFAULT", source_node = @decl, definiens_node = @export_stmt
  edge @export_stmt.exports -> pop_guard_default
  ;; edge export_stmt_pop_guard_default -> @decl.value ;; FIXME declarations have no .value

  ; !!!! HACK These detour nodes are a massive hack to allow find all refs land on defs
  ; for the default values of modules that have useful names like the module name or
  ; package name

  node detour_push
  node detour_pop

  scan FILE_PATH {

    "^(.+/)?([^/]+)/index\.js$" {
      let module_name = $2
      attr (detour_push) push_symbol = module_name
      attr (detour_pop) symbol_definition = module_name, source_node = @decl, definiens_node = @export_stmt
      edge pop_guard_default -> detour_push
      edge detour_push -> detour_pop
      ; edge detour_pop -> @decl.value ;; FIXME declarations have no .value
    }

    "^(.+/)?([^/]+)\.js$" {
      let module_name = $2
      attr (detour_push) push_symbol = module_name
      attr (detour_pop) symbol_definition = module_name, source_node = @decl, definiens_node = @export_stmt
      edge pop_guard_default -> detour_push
      edge detour_push -> detour_pop
      ; edge detour_pop -> @decl.value ;; FIXME declarations have no .value
    }

  }

  node default_detour_push
  node default_detour_pop

  attr (default_detour_push) push_symbol = "default"
  attr (default_detour_pop) symbol_definition = "default", source_node = @decl, definiens_node = @export_stmt
  edge pop_guard_default -> default_detour_push
  edge default_detour_push -> default_detour_pop
  ; edge default_detour_pop -> @decl.value ;; FIXME declarations have no .value

}

(export_statement "default"
  value:(_)@value)@export_stmt {

  node @export_stmt.pop_guard_default
  attr (@export_stmt.pop_guard_default) symbol_definition = "GUARD:DEFAULT", source_node = @value, definiens_node = @export_stmt
  edge @export_stmt.exports -> @export_stmt.pop_guard_default
  edge @export_stmt.pop_guard_default -> @value.value

  ; !!!! HACK These detour nodes are a massive hack to allow find all refs land on defs
  ; for the default values of modules that have useful names like the module name or
  ; package name

  node detour_push
  node @export_stmt.detour_pop

  scan FILE_PATH {

    "^(.+/)?([^/]+)/index\.js$" {
      let module_name = $2
      attr (detour_push) push_symbol = module_name
      attr (@export_stmt.detour_pop) symbol_definition = module_name, source_node = @value, definiens_node = @export_stmt
      edge @export_stmt.pop_guard_default -> detour_push
      edge detour_push -> @export_stmt.detour_pop
      edge @export_stmt.detour_pop -> @value.value
    }

    "^(.+/)?([^/]+)\.js$" {
      let module_name = $2
      attr (detour_push) push_symbol = module_name
      attr (@export_stmt.detour_pop) symbol_definition = module_name, source_node = @value, definiens_node = @export_stmt
      edge @export_stmt.pop_guard_default -> detour_push
      edge detour_push -> @export_stmt.detour_pop
      edge @export_stmt.detour_pop -> @value.value
    }

  }

  node default_detour_push
  node @export_stmt.default_detour_pop

  attr (default_detour_push) push_symbol = "default"
  attr (@export_stmt.default_detour_pop) symbol_definition = "default", source_node = @value, definiens_node = @export_stmt
  edge @export_stmt.pop_guard_default -> default_detour_push
  edge default_detour_push -> @export_stmt.default_detour_pop
  edge @export_stmt.default_detour_pop -> @value.value

}

(export_statement
  declaration: [
    (function_declaration name:(identifier)@name)
    (generator_function_declaration name:(identifier)@name)
    (class_declaration name:(identifier)@name)
    (lexical_declaration (variable_declarator name:(identifier)@name))
    (variable_declaration (variable_declarator name:(identifier)@name))
  ]@_decl)@export_stmt {

  ; TODO this doesn't support destructuring exports

  edge @export_stmt.exports -> @name.pop

}

; TODO
; export let [x,y] = [1,2];
(export_statement
  declaration: [
    (lexical_declaration
      (variable_declarator
        name: [
          (object_pattern)
          (array_pattern)
        ]@pattern))
    (variable_declaration
      (variable_declarator
        name: [
          (object_pattern)
          (array_pattern)
        ]@pattern))
  ])@export_stmt {

  edge @export_stmt.exports -> @pattern.new_bindings

}

(export_clause)@export_clause {
  node @export_clause.after_scope
  node @export_clause.before_scope
  node @export_clause.source
}

(export_clause (_)* @clauses)@export_clause {
  if (is-empty @clauses) {
    edge @export_clause.after_scope -> @export_clause.before_scope
  }
}

(export_clause
  (_)@export)@export_clause {

  edge @export.source -> @export_clause.source
}

(export_clause
  .
  (_)@first_export)@export_clause {

  edge @first_export.before_scope -> @export_clause.before_scope
}

(export_clause
  (_)@left_export
  .
  (_)@right_export) {

  edge @right_export.before_scope -> @left_export.after_scope
}

(export_clause
  (_)@last_export
  .)@export_clause {

  edge @export_clause.after_scope -> @last_export.after_scope
}

;; Export specifiers have several cases:
;;  - the reference into the source module can be default or named
;;  - the definition from this module can be default or named.
;;
;; We model this using seperate sets of rules, two rules for the reference,
;; and two rules for the definition. The `.def_to_ref` node is used to connect
;; the two.

(export_specifier)@export_specifier {

  node @export_specifier.after_scope
  node @export_specifier.before_scope
  node @export_specifier.def_to_ref
  node @export_specifier.source

  edge @export_specifier.after_scope -> @export_specifier.before_scope

}

;; Export specifier reference rules

; export { default } from ...
; export { default as ... } from ...
(
  (export_specifier
    name:(_)@name
  )@export_specifier

  (#not-eq? @name "default")
) {

  node name_push
  attr (name_push) node_reference = @name
  edge @export_specifier.def_to_ref -> name_push
  edge name_push -> @export_specifier.source

}

; export { foo } from ...
; export { foo as ... } from ...
(
  (export_specifier
    name:(_)@name
  )@export_specifier

  (#eq? @name "default")
) {

  node push_guard_default

  attr (push_guard_default) symbol_reference = "GUARD:DEFAULT", source_node = @name
  edge @export_specifier.def_to_ref -> push_guard_default
  edge push_guard_default -> @export_specifier.source

}

;; Export specifier definition rules

; export { foo } from ...
; export { ... as foo } from ...
( [
    (export_specifier
      name:(_)@alias
      !alias)@export_specifier
    (export_specifier
      name:(_)
      alias:(_)@alias)@export_specifier
  ]

  (#not-eq? @alias "default")
) {

  node name_pop

  attr (name_pop) node_definition = @alias, definiens_node = @export_specifier.export_statement
  edge @export_specifier.exports -> name_pop
  edge name_pop -> @export_specifier.def_to_ref

}

; export { default } from ...
; export { ... as default } from ...
( [
    (export_specifier
      name:(_)@alias
      !alias)@export_specifier
    (export_specifier
      name:(_)
      alias:(_)@alias)@export_specifier
  ]

  (#eq? @alias "default")
) {

  node pop_guard_default

  attr (pop_guard_default) symbol_definition = "GUARD:DEFAULT", source_node = @alias, definiens_node = @export_specifier.export_statement
  edge @export_specifier.exports -> pop_guard_default
  edge pop_guard_default -> @export_specifier.def_to_ref

}

; simple default exports
; export default ...;

(export_statement
  value:(_)@default_expr)@export_stmt {

  edge @default_expr.before_scope -> @export_stmt.before_scope
  edge @export_stmt.after_scope -> @default_expr.after_scope

}

; aggregated exports
; export * from "foo.js";
(export_statement
  .
  source:(_)@source)@export_statement {

  edge @export_statement.after_scope -> @export_statement.before_scope

  edge @export_statement.exports -> @source.exports

}

; namespace exports
; export * as foo from "bar.js";
(export_statement
  (namespace_export (_)@alias)
  source:(_)@source)@export_statement {

  node alias_pop
  node alias_pop_dot
  node source_push
  node source_push_guard_exports

  edge @export_statement.after_scope -> @export_statement.before_scope

  attr (alias_pop) node_definition = @alias, definiens_node = @export_statement.export_statement
  attr (alias_pop_dot) pop_symbol = "GUARD:MEMBER"
  edge @export_statement.exports -> alias_pop
  edge alias_pop -> alias_pop_dot
  edge alias_pop_dot -> @source.exports

}



;; ██ ███    ███ ██████   ██████  ██████  ████████ ███████ 
;; ██ ████  ████ ██   ██ ██    ██ ██   ██    ██    ██      
;; ██ ██ ████ ██ ██████  ██    ██ ██████     ██    ███████ 
;; ██ ██  ██  ██ ██      ██    ██ ██   ██    ██         ██ 
;; ██ ██      ██ ██       ██████  ██   ██    ██    ███████

;; #### Import

;; We distinguish two kinds of imports, based on the shape of the path:
;;
;;  - Relative imports, whose path starts with `.` or `..`. These are resolved relative
;;    to the current module, in the same package.
;;  - Non-relative or bare imports, which do not start with `.`, `..`, or `/`. These are
;;    resolved as package names. Note that the package definitions are introduced in the
;;    Go code based on `package.json`, not in this query file.
;;
;; Import paths may include optional `.js` extensions, but behave the same regardless of
;; whether the extension is present.
;;
;; ## Limitations
;;
;;  - Absolute imports, whose paths start with `/`, are not supported.
;;  - Non-relative imports can resolve into a package (i.e., start with package name
;;    components and then module name components inside that package). However, because
;;    we don't detect source roots for JavaScript, this might not always work. For example,
;;    a module `mod.js` inside a `src/` directory of package `foo` would be accessible as
;;    `foo/src/mod`, while `foo/mod` is probably intended.
;;
;; ## References
;;
;;  - ES6: https://nodejs.org/api/esm.html, https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import
;;  - CommonJS: https://nodejs.org/api/modules.html

(import_statement)@import_stmt {
  let @import_stmt.import_statement = @import_stmt
}

[
  (import_statement source:(_)@source)
  (export_statement source:(_)@source)
  ( (call_expression function:(_)@_require arguments:(arguments (string)@source)) (#eq? @_require "require") )
  ( (call_expression function:(_)@_import arguments:(arguments (string)@source)) (#eq? @_import "import") )
] {

  node source_push_guard_exports
  node source_push_guard_pkg
  node @source.exports

  attr (source_push_guard_exports) push_symbol = "GUARD:EXPORTS"

  scan (source-text @source) {
    "^[\"']((\.|\.\.)/.*)[\"']$" {
      ; relative import
      let name = (replace (path-normalize (path-join (path-dir FILE_PATH) $1)) "\.js$" "")

      node module_guard_push
      attr (module_guard_push) push_symbol = "GUARD:MODULE"
      edge module_guard_push -> @source.pkg_push

      node source_push_end
      edge source_push_end -> module_guard_push
      var push_start = source_push_end
      scan name {
        "([^/]+)/" {
          attr (push_start) push_symbol = $1
          node push_start_prev
          edge push_start_prev -> push_start
          set push_start = push_start_prev
        }
        "([^/]+)$" {
          attr (push_start) push_symbol = $1
        }
      }
      scan $1 {
        "/$" {
          node push_start_prev
          edge push_start_prev -> push_start
          set push_start = push_start_prev
          attr (push_start) push_symbol = "index"
        }
      }
      attr (push_start) is_reference, source_node = @source

      edge source_push_guard_exports -> push_start
    }
    "^[\"']([^\./].*)[\"']$" {
      ; package import
      let name = (replace $1 "\.js$" "")

      node source_push_end
      var push_start = source_push_end
      scan name {
        "([^/]+)/" {
          attr (push_start) push_symbol = $1
          node push_start_prev
          edge push_start_prev -> push_start
          set push_start = push_start_prev
        }
        "([^/]+)$" {
          attr (push_start) symbol_reference = $1, source_node = @source
        }
      }

      edge source_push_guard_exports -> push_start
      edge source_push_end -> source_push_guard_pkg
      attr (source_push_guard_pkg) push_symbol = "GUARD:PKG"
      edge source_push_guard_pkg -> @source.pkg_push
    }
  }

  edge @source.exports -> source_push_guard_exports

}

; import "foo.js";
; only used for side effects not imports.
(import_statement . source:(_))@import_stmt {
  edge @import_stmt.after_scope -> @import_stmt.before_scope
}

(import_clause)@import_clause {
  node @import_clause.after_scope
  node @import_clause.before_scope
  node @import_clause.source
}

; import * as name from "module-name";
(import_clause
  (namespace_import)@namespace_import)@import_clause {

  edge @namespace_import.before_scope -> @import_clause.before_scope
  edge @import_clause.after_scope -> @namespace_import.after_scope
  edge @namespace_import.source -> @import_clause.source
}

(namespace_import (identifier)@imported_as)@namespace_import {

  node imported_as_pop
  node imported_as_pop_dot
  node @namespace_import.after_scope
  node @namespace_import.before_scope
  node @namespace_import.source

  edge @namespace_import.after_scope -> @namespace_import.before_scope

  attr (imported_as_pop) node_definition = @imported_as, definiens_node = @namespace_import.import_statement
  attr (imported_as_pop_dot) pop_symbol = "GUARD:MEMBER"
  edge imported_as_pop -> imported_as_pop_dot
  edge imported_as_pop_dot -> @namespace_import.source

  edge @namespace_import.after_scope -> imported_as_pop

}

; import { export1 } from "module-name";
; import { export1 as alias1 } from "module-name";
; import { export1 , export2 } from "module-name";
; import { export1 , export2 as alias2 , [...] } from "module-name";

(import_statement
  (import_clause)@import_clause
  source:(_)@source)@import_stmt {

  edge @import_stmt.after_scope -> @import_stmt.before_scope

  edge @import_clause.before_scope -> @import_stmt.before_scope
  edge @import_stmt.hoist_point -> @import_clause.after_scope

  edge @import_clause.source -> @source.exports

}

(import_clause
  (named_imports)@named_imports)@import_clause {

  edge @named_imports.before_scope -> @import_clause.before_scope
  edge @import_clause.after_scope -> @named_imports.after_scope
  edge @named_imports.source -> @import_clause.source

}

(named_imports)@named_imports {
  node @named_imports.after_scope
  node @named_imports.before_scope
  node @named_imports.source
}

(named_imports (_)* @specs)@named_imports {
  if (is-empty @specs) {
    edge @named_imports.after_scope -> @named_imports.before_scope
  }
}

(named_imports
  (import_specifier)@import_specifier)@named_imports {

  edge @import_specifier.source -> @named_imports.source
}

(named_imports
  .
  (import_specifier)@first_import)@named_imports {

  edge @first_import.before_scope -> @named_imports.before_scope
}

(named_imports
  (import_specifier)@left_import
  .
  (import_specifier)@right_import) {

  edge @right_import.before_scope -> @left_import.after_scope
}

(named_imports
  (import_specifier)@last_import
  .)@named_imports {

  edge @named_imports.after_scope -> @last_import.after_scope
}

;; Import specifiers have several cases:
;;  - the reference into the source module can be default or named
;;  - the definition from this module can be default or named.
;; The case where the definition is default instead of named is invalid.
;;
;; We model this using seperate sets of rules, two rules for the reference,
;; and one rules for the definition. The `.def_to_ref` node is used to connect
;; the two.

(import_specifier)@import_specifier {
  node @import_specifier.after_scope
  node @import_specifier.before_scope
  node @import_specifier.def_to_ref
  node @import_specifier.source

  edge @import_specifier.after_scope -> @import_specifier.before_scope
}

;; Import specifier reference rules

(
  (import_specifier
    name:(_)@name
  )@import_specifier

  (#not-eq? @name "default")
) {

  node name_push

  attr (name_push) node_reference = @name
  edge name_push -> @import_specifier.source
  edge @import_specifier.def_to_ref -> name_push

}

(
  (import_specifier
    name:(_)@name
  )@import_specifier

  (#eq? @name "default")
) {

  node push_guard_default

  attr (push_guard_default) symbol_reference = "GUARD:DEFAULT", source_node = @name
  edge push_guard_default -> @import_specifier.source
  edge @import_specifier.def_to_ref -> push_guard_default

}

;; Import specifier definition rules

( [
    (import_specifier
      name:(_)@alias
      !alias)@import_specifier
    (import_specifier
      name:(_)
      alias:(_)@alias)@import_specifier
  ]

  (#not-eq? @alias "default")
) {

  node name_pop

  attr (name_pop) node_definition = @alias, definiens_node = @import_specifier.import_statement
  edge name_pop -> @import_specifier.def_to_ref
  edge @import_specifier.after_scope -> name_pop

}

; (import_statement
;   (import_clause
;     (named_imports
;       (import_specifier
;         name:(_)@name
;         alias:(_)@alias)))
;   source: (_)@mod_name)@import_stmt {
;
;   ; scope passes through, augmented by the identifier
;   scan @mod_name {
;     "\"([^/\"]+)\.js\"$" {
;       attr (@mod_name.push) symbol_reference = $1, source_node = @mod_name
;     }
;   }
;   edge @mod_name.push -> @import_stmt.pkg_push
;
;   attr (@name) node_reference = @name
;   attr (name_push_dot) push_symbol = "GUARD:MEMBER"
;   edge name_push_dot -> @mod_name.push
;   edge name -> @name_push_dot
;
;   attr (@alias) node_definition = @alias
;   edge @alias -> @name
;
;   edge @import_stmt.after_scope -> @alias
; }


; TODO import defaultExport, { export1 [ , [...] ] } from "module-name";
; TODO import defaultExport, * as name from "module-name";
; TODO var promise = import("module-name");

; import defaultExport from "module-name";
(import_clause
  (identifier)@default_name)@import_clause {

  node default_name_pop
  node default_name_push_guard_default

  edge @import_clause.after_scope -> @import_clause.before_scope

  attr (default_name_pop) node_definition = @default_name, definiens_node = @import_clause.import_statement
  attr (default_name_push_guard_default) symbol_reference = "GUARD:DEFAULT", source_node = @default_name
  edge default_name_pop -> default_name_push_guard_default
  edge default_name_push_guard_default -> @import_clause.source

  edge @import_clause.after_scope -> default_name_pop

}



;; ███    ██  ██████  ██████  ███    ███  █████  ██                                        
;; ████   ██ ██    ██ ██   ██ ████  ████ ██   ██ ██                                        
;; ██ ██  ██ ██    ██ ██████  ██ ████ ██ ███████ ██                                        
;; ██  ██ ██ ██    ██ ██   ██ ██  ██  ██ ██   ██ ██                                        
;; ██   ████  ██████  ██   ██ ██      ██ ██   ██ ███████                                   

;; ███████ ████████  █████  ████████ ███████ ███    ███ ███████ ███    ██ ████████ ███████ 
;; ██         ██    ██   ██    ██    ██      ████  ████ ██      ████   ██    ██    ██      
;; ███████    ██    ███████    ██    █████   ██ ████ ██ █████   ██ ██  ██    ██    ███████ 
;;      ██    ██    ██   ██    ██    ██      ██  ██  ██ ██      ██  ██ ██    ██         ██ 
;; ███████    ██    ██   ██    ██    ███████ ██      ██ ███████ ██   ████    ██    ███████ 

;; #### Debugger

(debugger_statement)@debugger_stmt {
  ; scopes flow through unchanged
  edge @debugger_stmt.after_scope -> @debugger_stmt.before_scope
}



;; #### Expression

(expression_statement (_)@inner)@expr_stmt {

  ; scopes flow in then back out
  edge @inner.before_scope -> @expr_stmt.before_scope
  edge @expr_stmt.after_scope -> @inner.after_scope
}



;; #### Declarations

;; ##### Variable Declarations

(variable_declaration
  (variable_declarator
    name:(identifier)@name))@variable_decl
{

    node @name.pop
    attr (@name.pop) node_definition = @name
    edge @variable_decl.after_scope -> @name.pop
    attr (@variable_decl.after_scope -> @name.pop) precedence = 1

}

(lexical_declaration
  (variable_declarator
    name:(identifier)@name))@lexical_decl
{

    node @name.pop
    attr (@name.pop) node_definition = @name
    edge @lexical_decl.after_scope -> @name.pop
    attr (@lexical_decl.after_scope -> @name.pop) precedence = 1

}

(variable_declaration
  (variable_declarator
    !value))@decl {

  edge @decl.after_scope -> @decl.before_scope
}

(lexical_declaration
  (variable_declarator
    !value))@decl {

  edge @decl.after_scope -> @decl.before_scope
}

(variable_declaration
  (variable_declarator
    name:(identifier)@name
    value:(_)@initializer))@variable_decl
{

  edge @name.pop -> @initializer.value
  edge @initializer.before_scope -> @variable_decl.before_scope
  edge @variable_decl.after_scope -> @initializer.after_scope

}

(lexical_declaration
  (variable_declarator
    name:(identifier)@name
    value:(_)@initializer))@lexical_decl
{

    edge @name.pop -> @initializer.value
    edge @initializer.before_scope -> @lexical_decl.before_scope
    edge @lexical_decl.after_scope -> @initializer.after_scope
    attr (@lexical_decl.after_scope -> @initializer.after_scope) precedence = 0

}

(variable_declaration
  (variable_declarator
    name:[(object_pattern) (array_pattern)]@pat
    value:(_)@initializer))@decl {

  edge @initializer.before_scope -> @decl.before_scope
  edge @pat.before_scope -> @initializer.after_scope
  edge @decl.after_scope -> @pat.after_scope

  edge @pat.covalue -> @initializer.value
}

(lexical_declaration
  (variable_declarator
    name:[(object_pattern) (array_pattern)]@pat
    value:(_)@initializer))@decl {

  edge @initializer.before_scope -> @decl.before_scope
  edge @pat.before_scope -> @initializer.after_scope
  edge @decl.after_scope -> @pat.after_scope

  edge @pat.covalue -> @initializer.value
}



;; ##### Function Declarations

(function_declaration
  name:(_)@name
  parameters:(_)@call_sig
  body:(_)@body)@fun_decl {

  node call_sig_arguments_pop
  node call_sig_arguments_push
  node call_sig_this_pop
  node call_sig_this_push
  node @name.pop
  node fun_decl_function_value
  node @fun_decl.return_or_yield
  node @fun_decl.value_arg_scope
  node fun_decl_value_call
  node fun_decl_value_drop
  node fun_decl_value_return
  node fun_decl_value_this
  node fun_decl_value_this_guard
  let @body.closure_point = @body.after_scope

  attr (@name.pop) syntax_type = "function"

  ; scope flows across the decl
  edge @fun_decl.after_scope -> @fun_decl.before_scope

  ; with an augmentation for the function
  attr (@name.pop) node_definition = @name
  edge @fun_decl.hoist_point -> @name.pop
  edge @name.pop -> fun_decl_function_value

  ; function values have drop nodes that handle closures, that points to the
  ; before scope for the function
  attr (fun_decl_value_drop) type = "drop_scopes"
  edge fun_decl_value_drop -> @fun_decl.closure_point

  ; the call sig's before scope comes from the drop node then flows into the body
  edge @call_sig.before_scope -> fun_decl_value_drop
  attr (call_sig_this_pop) symbol_definition = "this", source_node = @call_sig
  attr (call_sig_this_push) push_symbol = "this"
  edge call_sig_this_pop -> call_sig_this_push
  edge call_sig_this_push -> @fun_decl.value_arg_scope
  edge @call_sig.before_scope -> call_sig_this_pop
  attr (call_sig_arguments_pop) symbol_definition = "arguments", source_node = @call_sig
  attr (call_sig_arguments_push) push_symbol = "arguments"
  edge call_sig_arguments_pop -> call_sig_arguments_push
  edge call_sig_arguments_push -> @fun_decl.value_arg_scope
  edge @call_sig.before_scope -> call_sig_arguments_pop
  edge @body.before_scope -> @call_sig.after_scope


  ; function values have call nodes
  attr (fun_decl_value_call) pop_scoped_symbol = "()"
  edge fun_decl_function_value -> fun_decl_value_call

  ; function values have return nodes which need to be visible for returns
  attr (fun_decl_value_return) pop_symbol = "GUARD:RETURN"
  edge fun_decl_value_call -> fun_decl_value_return
  let @body.return_or_yield = fun_decl_value_return

  ; method values have this nodes which need to be visible for constructor calls
  attr (fun_decl_value_this) push_symbol = "this"
  attr (fun_decl_value_this_guard) pop_symbol = "GUARD:THIS"
  edge fun_decl_value_call -> fun_decl_value_this_guard
  edge fun_decl_value_this_guard -> fun_decl_value_this
  edge fun_decl_value_this -> @body.after_scope

  ; function values have a jump node that lets params connect up to actual arguments
  edge @fun_decl.value_arg_scope -> JUMP_TO_SCOPE_NODE
}

(function_declaration
  parameters:
    (formal_parameters (_)@param))@fun_decl {

  node param_arg_index

  ; parameters jump to the pushed argument scope
  attr (param_arg_index) push_symbol = (named-child-index @param)
  edge @param.covalue -> param_arg_index
  edge param_arg_index -> @fun_decl.value_arg_scope
}


;; ##### Generator Function Declarations

(generator_function_declaration
  name:(_)@name
  parameters:(_)@call_sig
  body:(_)@body)@fun_decl {

  node call_sig_arguments_pop
  node call_sig_arguments_push
  node call_sig_this_pop
  node call_sig_this_push
  node @name.pop
  node fun_decl_function_value
  node @fun_decl.return_or_yield
  node @fun_decl.value_arg_scope
  node fun_decl_value_call
  node fun_decl_value_drop
  node fun_decl_value_return
  node fun_decl_value_this
  node fun_decl_value_this_guard
  let @body.closure_point = @body.after_scope

  attr (@name.pop) syntax_type = "function"

  ; scope flows across the decl
  edge @fun_decl.after_scope -> @fun_decl.before_scope

  ; with an augmentation for the function
  attr (@name.pop) node_definition = @name
  edge @fun_decl.hoist_point -> @name.pop
  edge @name.pop -> fun_decl_function_value

  ; function values have drop nodes that handle closures, that points to the
  ; before scope of the declaration
  attr (fun_decl_value_drop) type = "drop_scopes"
  edge fun_decl_value_drop -> @fun_decl.closure_point


  ; the call sig's before scope comes from the drop node then flows into the body
  edge @call_sig.before_scope -> fun_decl_value_drop
  attr (call_sig_this_pop) symbol_definition = "this", source_node = @call_sig
  attr (call_sig_this_push) push_symbol = "this"
  edge call_sig_this_pop -> call_sig_this_push
  edge call_sig_this_push -> @fun_decl.value_arg_scope
  edge @call_sig.before_scope -> call_sig_this_pop
  attr (call_sig_arguments_pop) symbol_definition = "arguments", source_node = @call_sig
  attr (call_sig_arguments_push) push_symbol = "arguments"
  edge call_sig_arguments_pop -> call_sig_arguments_push
  edge call_sig_arguments_push -> @fun_decl.value_arg_scope
  edge @call_sig.before_scope -> call_sig_arguments_pop
  edge @body.before_scope -> @call_sig.after_scope

  ; function values have call nodes
  attr (fun_decl_value_call) pop_scoped_symbol = "()"
  edge fun_decl_function_value -> fun_decl_value_call

  ; function values have return nodes which need to be visible for returns
  attr (fun_decl_value_return) pop_symbol = "GUARD:RETURN"
  edge fun_decl_value_call -> fun_decl_value_return
  let @body.return_or_yield = fun_decl_value_return

  ; method values have this nodes which need to be visible for constructor calls
  attr (fun_decl_value_this) push_symbol = "this"
  attr (fun_decl_value_this_guard) pop_symbol = "GUARD:THIS"
  edge fun_decl_value_call -> fun_decl_value_this_guard
  edge fun_decl_value_this_guard -> fun_decl_value_this
  edge fun_decl_value_this -> @body.after_scope

  ; function values have a jump node that lets params connect up to actual arguments
  edge @fun_decl.value_arg_scope -> JUMP_TO_SCOPE_NODE
}

(generator_function_declaration
  parameters:
    (formal_parameters (_)@param))@fun_decl {

  node param_arg_index

  ; parameters jump to the pushed argument scope
  attr (param_arg_index) push_symbol = (named-child-index @param)
  edge @param.covalue -> param_arg_index
  edge param_arg_index -> @fun_decl.value_arg_scope
}



;; #### Classes

(class_declaration
  name:(_)@name
  body:(_)@body)@class_decl {

  node @name.pop
  node @class_decl.class_value
  let @class_decl.containing_class_value = @class_decl.class_value
  node guard_prototype
  node @class_decl.prototype
  node @class_decl.constructor

  attr (@name.pop) syntax_type = "class"

  attr (@name.pop) node_definition = @name
  attr (guard_prototype) pop_symbol = "GUARD:PROTOTYPE"
  edge @class_decl.after_scope -> @name.pop
  edge @name.pop -> @class_decl.class_value
  edge @class_decl.class_value -> guard_prototype
  edge guard_prototype -> @class_decl.prototype

  edge @body.before_scope -> @class_decl.closure_point
  edge @class_decl.prototype -> @body.after_scope
  edge @class_decl.after_scope -> @class_decl.before_scope
}

(class_declaration
  (class_heritage (_)@name))@class_decl {

  node guard_prototype
  node guard_constructor
  attr (guard_prototype) push_symbol = "GUARD:PROTOTYPE"
  attr (guard_constructor) push_symbol = "GUARD:CONSTRUCTOR"
  edge @name.before_scope -> @class_decl.before_scope
  edge @class_decl.prototype -> guard_prototype
  edge guard_prototype -> @name.value
  edge @class_decl.constructor -> guard_constructor
  edge guard_constructor -> @name.value
}


(class_body)@class_body {
  node @class_body.after_scope
  node @class_body.before_scope
}

(class_body (_)* @decls)@class_body {
  if (is-empty @decls) {
    edge @class_body.after_scope -> @class_body.before_scope
  }
}

(class_body
  .
  (_)@first_decl)@class_body {

  edge @first_decl.before_scope -> @class_body.before_scope
}

(class_body
  (_)@left_decl
  .
  (_)@right_decl) {

  edge @right_decl.before_scope -> @left_decl.after_scope
}

(class_body
  (_)@last_decl
  .)@class_body {

  edge @class_body.after_scope -> @last_decl.after_scope
}

(method_definition name:(_)@name)@method_def {

  node @name.pop
  node @method_def.after_scope
  node @method_def.before_scope
  node @method_def.method_value

}

(
  (method_definition
    name:(_)@name)@method_def
  (#eq? @name "constructor")
) {

  ; augmentation for the constructor
  attr (@name.pop) symbol_definition = "GUARD:CONSTRUCTOR", source_node = @name
  edge @method_def.class_value -> @name.pop
  edge @name.pop -> @method_def.constructor
  edge @method_def.constructor -> @method_def.method_value
}

(
  (method_definition
    name:(_)@name)@method_def
  (#not-eq? @name "constructor")
) {

  node name_pop_dot
  ; augmentation for the method
  attr (@name.pop) node_definition = @name
  attr (name_pop_dot) pop_symbol = "GUARD:MEMBER"
  edge @method_def.after_scope -> name_pop_dot
  edge name_pop_dot -> @name.pop
  edge @name.pop -> @method_def.method_value
}

(method_definition
  name:(_)@name
  parameters:(_)@call_sig
  body:(_)@body)@method_def {

  node call_sig_arguments_pop
  node call_sig_arguments_push
  node @method_def.method_value_arg_scope
  node method_def_method_value_call
  node method_def_method_value_drop
  node method_def_method_value_return
  node method_def_method_value_this
  node method_def_method_value_this_guard

  attr (@name.pop) syntax_type = "method"

  ; scope flows across the decl
  edge @method_def.after_scope -> @method_def.before_scope

  ; method values have drop nodes that handle closures, that points to the
  ; before scope from method def
  attr (method_def_method_value_drop) type = "drop_scopes"
  edge method_def_method_value_drop -> @method_def.before_scope

  ; the call sig's before scope comes from the drop node then flows into the body
  edge @call_sig.before_scope -> method_def_method_value_drop
  attr (call_sig_arguments_pop) symbol_definition = "arguments", source_node = @call_sig
  attr (call_sig_arguments_push) push_symbol = "arguments"
  edge call_sig_arguments_pop -> call_sig_arguments_push
  edge call_sig_arguments_push -> @method_def.method_value_arg_scope
  edge @call_sig.before_scope -> call_sig_arguments_pop
  edge @body.before_scope -> @call_sig.after_scope

  ; method values have call nodes
  attr (method_def_method_value_call) pop_scoped_symbol = "()"
  edge @method_def.method_value -> method_def_method_value_call

  ; method values have return nodes which need to be visible for returns
  attr (method_def_method_value_return) pop_symbol = "GUARD:RETURN"
  edge method_def_method_value_call -> method_def_method_value_return
  let @body.return_or_yield = method_def_method_value_return

  ; method values have this nodes which need to be visible for constructor calls
  attr (method_def_method_value_this) push_symbol = "this"
  attr (method_def_method_value_this_guard) pop_symbol = "GUARD:THIS"
  edge method_def_method_value_call -> method_def_method_value_this_guard
  edge method_def_method_value_this_guard -> method_def_method_value_this
  edge method_def_method_value_this -> @body.after_scope

  ; method values have a jump node that lets params connect up to actual arguments
  edge @method_def.method_value_arg_scope -> JUMP_TO_SCOPE_NODE
}

(method_definition
  parameters:
    (formal_parameters (_)@param))@method_def {

  node param_arg_index

  ; parameters jump to the pushed argument scope
  attr (param_arg_index) push_symbol = (named-child-index @param)
  edge @param.covalue -> param_arg_index
  edge param_arg_index -> @method_def.method_value_arg_scope
}



(field_definition)@field_def {
  node @field_def.after_scope
  node @field_def.before_scope
}

(field_definition
  property:(_)@property)@field_def {

  node @property.pop
  node property_pop_dot

  attr (@property.pop) node_definition = @property
  attr (property_pop_dot) pop_symbol = "GUARD:MEMBER"
  edge @field_def.after_scope -> property_pop_dot
  edge property_pop_dot -> @property.pop
}

(field_definition
  !value)@field_def {

  edge @field_def.after_scope -> @field_def.before_scope
}

(field_definition
  property:(_)@property
  value:(_)@value)@field_def {

  edge @value.before_scope -> @field_def.before_scope
  edge @field_def.after_scope -> @value.after_scope
  edge @property.pop -> @value.value
}

(class_static_block body: (_) @body) @static_block {
  node @static_block.before_scope
  node @static_block.after_scope

  edge @body.before_scope -> @static_block.before_scope

  edge @static_block.after_scope -> @static_block.before_scope
}



;; #### Statement Block

(statement_block)@statement_block {

  node @statement_block.hoist_point
  edge @statement_block.before_scope -> @statement_block.hoist_point

}

(statement_block (_)* @stmts)@statement_block {
  if (is-empty @stmts) {
    edge @statement_block.after_scope -> @statement_block.before_scope
  }
}

; statement block, first statement
(statement_block
  .
  (_)@first_stmt)@block {

  ; scope flows from block to first statement
  edge @first_stmt.before_scope -> @block.before_scope
}

; statement block, between statements
(statement_block
  (_)@left_stmt
  .
  (_)@right_stmt) {
  ; scope flows from left to right
  edge @right_stmt.before_scope -> @left_stmt.after_scope
}

; statement block, last statement
(statement_block
  (_)@last_stmt
  .)@block {
  ; scope flows from last statement to block
  edge @block.after_scope -> @last_stmt.after_scope
}





;; #### If

(if_statement
  consequence:(_)@consequence)@if_stmt {

    let @if_stmt.hoist_point = @consequence.before_scope

}

(if_statement condition:(_)@condition)@if_stmt {
    ; scopes flow from the if statement to the condition
    edge @condition.before_scope -> @if_stmt.before_scope
}

(if_statement
  condition:(_)@condition
  consequence:(_)@consequence)@if_stmt
{
    ; scopes flow from the condition to the consequence, then to the if statement
    edge @consequence.before_scope -> @condition.after_scope
    edge @if_stmt.after_scope -> @consequence.after_scope

}

(if_statement
  condition:(_)@condition
  alternative:(_)@alternative)@if_stmt
{
    ; scopes flow from the condition to the alternative, then to the if statement
    edge @alternative.before_scope -> @condition.after_scope
    edge @if_stmt.after_scope -> @alternative.after_scope
}

(else_clause)@else_clause {
    node @else_clause.after_scope
    node @else_clause.before_scope
}

(else_clause
  . (_)@inner)@else_clause {

    let @else_clause.hoist_point = @inner.before_scope

}

(else_clause (_)@inner)@else_clause {
    ; scopes flow in and right back out
    edge @inner.before_scope -> @else_clause.before_scope
    edge @else_clause.after_scope -> @inner.after_scope
}




;; #### Switch

(switch_statement
  value:(_)@value
  body:(switch_body)@body)@switch_stmt
{
    ; scopes flow into the value then into the body then back out to the switch
    edge @value.before_scope -> @switch_stmt.before_scope
    edge @body.before_scope -> @value.after_scope
    edge @switch_stmt.after_scope -> @body.after_scope
}

(switch_body)@switch_body {
  node @switch_body.after_scope
  node @switch_body.before_scope
  node @switch_body.hoist_point

  edge @switch_body.before_scope -> @switch_body.hoist_point
}

(switch_body (_)* @choices)@switch_body {
  if (is-empty @choices) {
    edge @switch_body.after_scope -> @switch_body.before_scope
  }
}

; switch body, first choice
(switch_body
  .
  (_)@first_choice)@switch_body
{
    ; scopes flow from the body into the first choice
    edge @first_choice.before_scope -> @switch_body.before_scope
}

; switch body, between choices
(switch_body
  (_)@left_choice
  .
  (_)@right_choice)
{
    ; scopes flow left to right
    edge @right_choice.before_scope -> @left_choice.after_scope
}

; switch body, last choice
(switch_body
  (_)@last_choice
  .)@switch_body
{
    ; scope flows out to the switch body
    edge @switch_body.after_scope -> @last_choice.after_scope
}

(switch_case)@switch_case {
  node @switch_case.after_scope
  node @switch_case.before_scope
}

(switch_case (_)* @stmts)@switch_case {
  if (is-empty @stmts) {
    edge @switch_case.after_scope -> @switch_case.before_scope
  }
}

; switch case, non-empty statements, first statement
(switch_case
  value:(_)@value
  .
  (_)@first_stmt)@switch_case
{
    ; scopes flow into the value then into the first statement
    edge @value.before_scope -> @switch_case.before_scope
    edge @first_stmt.before_scope -> @value.after_scope
}

; switch case, non-empty statements, between statement
(switch_case
  value:(_)
  (_)@left_stmt
  .
  (_)@right_stmt)
{
    ; scopes flow left to right
    edge @right_stmt.before_scope -> @left_stmt.after_scope
}

; switch case, non-empty statements, last statement
(switch_case
  value:(_)
  (_)@last_stmt
  .)@switch_case {

  ; scopes flow out from the last statement to the case
  edge @switch_case.after_scope -> @last_stmt.after_scope
}

(switch_default)@switch_default {
  node @switch_default.after_scope
  node @switch_default.before_scope
}

(switch_default (_)* @defaults)@switch_default {
  if (is-empty @defaults) {
    edge @switch_default.after_scope -> @switch_default.before_scope
  }
}

; switch default, non-empty statements, first statement
(switch_default
  .
  (_)@first_stmt)@switch_default
{
    ; scopes flow into the first statement
    edge @first_stmt.before_scope -> @switch_default.before_scope
}

; switch default, non-empty statements, between statements
(switch_default
  (_)@left_stmt
  .
  (_)@right_stmt)
{

    ; scopes flow left to right
    edge @right_stmt.before_scope -> @left_stmt.after_scope
}

; switch default, non-empty statements, last statement
(switch_default
  (_)@last_stmt
  .)@switch_default
{

    ; scopes flow out to the default
    edge @switch_default.after_scope -> @last_stmt.after_scope
}



;; #### For

(for_statement
  body:(_)@body)@for_stmt {

    let @for_stmt.hoist_point = @body.before_scope

}

(for_statement
  initializer:(_)@initializer
  condition:(_)@condition
  increment:(_)@increment
  body:(_)@body)@for_stmt
{

    ; scopes flow from statement to initializer then test then body then increment
    edge @initializer.before_scope -> @for_stmt.before_scope
    edge @condition.before_scope -> @initializer.after_scope
    edge @body.before_scope -> @condition.after_scope
    edge @increment.before_scope -> @body.after_scope

    ; scopes also from from the body back into the condition
    edge @condition.before_scope -> @increment.after_scope

    ; scopes also flow from condition out to statement
    edge @for_stmt.after_scope -> @condition.after_scope

}



;; #### For In

(for_in_statement
  body:(_)@body)@for_in_stmt {

    let @for_in_stmt.hoist_point = @body.before_scope

}

(for_in_statement
  left:(_)@_left
  right:(_)@right
  body:(_)@body)@for_in_stmt
{

    ; scopes flow from statement to right then to body then back out
    edge @right.before_scope -> @for_in_stmt.before_scope
    edge @body.before_scope -> @right.after_scope
    edge @for_in_stmt.after_scope -> @body.after_scope
}

(for_in_statement
  left:(identifier)@left
  right:(_)@right
  body:(_)@body)@_for_in_stmt
{

    node for_in_stmt_pop
    attr (for_in_stmt_pop) node_definition = @left
    edge for_in_stmt_pop -> @right.value
    edge @body.before_scope -> for_in_stmt_pop

}



;; #### While

(while_statement
  body:(_)@body)@while_stmt {

    let @while_stmt.hoist_point = @body.before_scope

}

(while_statement
  condition:(_)@condition
  body:(_)@body)@while_stmt
{

    ; scopes flow from while to condition then to body then back out
    edge @condition.before_scope -> @while_stmt.before_scope
    edge @body.before_scope -> @condition.after_scope
    edge @while_stmt.after_scope -> @body.after_scope

    ; scopes also flow back into the condition
    edge @condition.before_scope -> @body.after_scope

}



;; #### Do

(do_statement
  body:(_)@body)@do_stmt {

    let @do_stmt.hoist_point = @body.before_scope

}

(do_statement
  body:(_)@body
  condition:(_)@condition)@do_stmt
{

    ; scopes flow from statement to body then condition then back to statement
    edge @body.before_scope -> @do_stmt.before_scope
    edge @condition.before_scope -> @body.after_scope
    edge @do_stmt.after_scope -> @condition.after_scope

    ; scopes also flow back to the body from the condition
    edge @body.before_scope -> @condition.after_scope

}



;; #### Try

(try_statement
  body:(_)@body)@try_stmt
{

    ; scopes flow into the body then back out
    edge @body.before_scope -> @try_stmt.before_scope
    edge @try_stmt.after_scope -> @body.after_scope

}

(try_statement
  body:(_)@body
  handler:(_)@handler)@try_stmt
{

    ; scopes flow from body to handler then back out
    edge @handler.before_scope -> @body.after_scope
    edge @try_stmt.after_scope -> @handler.after_scope
}

(try_statement
  body:(_)@body
  finalizer:(_)@finalizer)@_try_stmt
{

    ; scopes flow from body to finalizer then back out
    edge @finalizer.before_scope -> @body.after_scope

}

(try_statement
  handler:(_)@handler
  finalizer:(_)@finalizer)@try_stmt
{

    ; scopes flow from handler to finalizer then back out
    edge @finalizer.before_scope -> @handler.after_scope
    edge @try_stmt.after_scope -> @finalizer.after_scope
}

(catch_clause body:(_)@body)@catch_clause {
    node @catch_clause.after_scope
    node @catch_clause.before_scope
    ; scopes flow in then back out
    edge @body.before_scope -> @catch_clause.before_scope
    edge @catch_clause.after_scope -> @body.after_scope
}

(catch_clause
  parameter:(identifier)@name
  body:(_)@body)@_catch_clause
{

    node catch_clause_pop
    attr (catch_clause_pop) node_definition = @name
    edge @body.before_scope -> catch_clause_pop

}

(finally_clause body:(_)@body)@finally_clause {
    node @finally_clause.after_scope
    node @finally_clause.before_scope
    ; scopes flow in thenback out
    edge @body.before_scope -> @finally_clause.before_scope
    edge @finally_clause.after_scope -> @body.after_scope
}



;; #### With

(with_statement body:(_)@body)@with_stmt {

  let @with_stmt.hoist_point = @body.before_scope

}

(with_statement
  object:(_)@object
  body:(_)@body)@with_stmt
{

    node with_stmt_push_dot

    ; scopes flow from the statement into the object then into the body then back out
    edge @object.before_scope -> @with_stmt.before_scope
    edge @body.before_scope -> @object.after_scope
    edge @with_stmt.after_scope -> @body.after_scope
    edge @with_stmt.after_scope -> @with_stmt.before_scope
    attr (@with_stmt.after_scope -> @with_stmt.before_scope) precedence = 1

    attr (with_stmt_push_dot) push_symbol = "GUARD:MEMBER"
    edge with_stmt_push_dot -> @object.value
    edge @body.before_scope -> with_stmt_push_dot
    attr (@body.before_scope -> with_stmt_push_dot) precedence = 1
}



;; #### Break

(break_statement)@break_stmt {
    ; scopes flow through unchanged
    edge @break_stmt.after_scope -> @break_stmt.before_scope
}

(break_statement (_)@label)@break_stmt {

    node break_stmt_label_guard
    node break_stmt_label_push

    attr (break_stmt_label_guard) push_symbol = "GUARD:LABEL"
    attr (break_stmt_label_push) node_reference = @label

    edge break_stmt_label_push -> break_stmt_label_guard
    edge break_stmt_label_guard -> @break_stmt.before_scope
}



;; #### Continue

(continue_statement)@continue_stmt {
    ; scopes flow through unchanged
    edge @continue_stmt.after_scope -> @continue_stmt.before_scope
}

(continue_statement (_)@label)@continue_stmt {

    node continue_stmt_label_guard
    node continue_stmt_label_push

    attr (continue_stmt_label_guard) push_symbol = "GUARD:LABEL"
    attr (continue_stmt_label_push) node_reference = @label

    edge continue_stmt_label_push -> continue_stmt_label_guard
    edge continue_stmt_label_guard -> @continue_stmt.before_scope
}



;; #### Return

; LATER-TODO tree sitter doesn't let us express empty returns currently
(return_statement)@return_stmt {
  ; scopes flow through unchanged
  edge @return_stmt.after_scope -> @return_stmt.before_scope
}

(return_statement
  (_)@returned_expr)@return_stmt {
  ; scopes flow through the returned expresssion
  edge @returned_expr.before_scope -> @return_stmt.before_scope
  edge @return_stmt.after_scope -> @returned_expr.after_scope

  ; return statements hook up to the call node of the function value
  edge @return_stmt.return_or_yield -> @returned_expr.value
}



;; #### Throw

(throw_statement (_)@thrown_expr)@throw_stmt {
  ; scopes flow through the returned expresssion
  edge @thrown_expr.before_scope -> @throw_stmt.before_scope
  edge @throw_stmt.after_scope -> @thrown_expr.after_scope
}



;; #### Empty

(empty_statement)@empty_stmt {
  ; scopes flow through unchaged
  edge @empty_stmt.after_scope -> @empty_stmt.before_scope
}



;; #### Labeled

(labeled_statement
    label:(_)@label
    body:(_)@inner)@labeled_stmt
{

    node labeled_stmt_label_guard
    node labeled_stmt_label_pop

    attr (labeled_stmt_label_guard) pop_symbol = "GUARD:LABEL"
    attr (labeled_stmt_label_pop) node_definition = @label

    ; scopes flow through the inner statement then back out
    edge @inner.before_scope -> @labeled_stmt.before_scope
    edge @inner.before_scope -> labeled_stmt_label_guard
    edge labeled_stmt_label_guard -> labeled_stmt_label_pop
    edge @labeled_stmt.after_scope -> @inner.after_scope
}










;; ███████ ██   ██ ██████  ██████  ███████ ███████ ███████ ██  ██████  ███    ██ ███████ 
;; ██       ██ ██  ██   ██ ██   ██ ██      ██      ██      ██ ██    ██ ████   ██ ██      
;; █████     ███   ██████  ██████  █████   ███████ ███████ ██ ██    ██ ██ ██  ██ ███████ 
;; ██       ██ ██  ██      ██   ██ ██           ██      ██ ██ ██    ██ ██  ██ ██      ██ 
;; ███████ ██   ██ ██      ██   ██ ███████ ███████ ███████ ██  ██████  ██   ████ ███████

;; ## Expressions

;; ### Attributes Defined on Expressions
;; TODO

[
  (subscript_expression)
  (member_expression)
  (parenthesized_expression)
  (undefined)
  (this)
  (super)
  (number)
  (string)
  (template_string)
  (template_substitution)
  (string_fragment)
  (escape_sequence)
  (regex)
  (true)
  (false)
  (null)
  (sequence_expression)
  (import)
  (object)
  (array)
  (function_expression)
  (arrow_function)
  (generator_function)
  (class)
  (meta_property)
  (call_expression)
  (assignment_expression)
  (augmented_assignment_expression)
  (await_expression)
  (unary_expression)
  (binary_expression)
  (ternary_expression)
  (update_expression)
  (new_expression)
  (yield_expression)
  (spread_element)
]@expr {

  node @expr.after_scope
  node @expr.before_scope
  node @expr.value

}

[
  (function_expression body:(_)@body)
  (arrow_function body:(_)@body)
  (generator_function body:(_)@body)
] {

  let @body.closure_point = @body.after_scope

}

;; ### Expression Queries


;; #### Parenthesized

(parenthesized_expression (_)@inner)@parens {
  ; scopes flow right through
  edge @inner.before_scope -> @parens.before_scope
  edge @parens.after_scope -> @inner.after_scope

  ; as do values
  edge @parens.value -> @inner.value
}


;; #### Strings

(string)@string {
  ; scopes don't change
  edge @string.after_scope -> @string.before_scope

  ; the value of a string is the string primitive
  edge @string.value -> @string.builtins_string
}


;; #### Template Strings

; template_strings w/ no substitutions
(template_string (_)* @parts)@template_string {
  if (is-empty @parts) {
    edge @template_string.after_scope -> @template_string.before_scope
  }
}

; nonempty template string, value
; LATER-TODO this isn't really right, but it gets flows through the template string
; which may be useful
(template_string (_)@part)@template_string {
  ; the value of a template string is a template string value built from the values of its substitutions
  ; attr (@template_string.value) "template_string_value"
  edge @template_string.value -> @part.value
}

; nonempty template string, first substitution
(template_string . (_)@first)@template_string {
  ; scopes propagate into the first subtitution of the template string
  edge @first.before_scope -> @template_string.before_scope
}

; nonempty template string, between substitutions
(template_string
  (_) @left
  .
  (_) @right) {
  ; scopes propagate from left substitutions to right substitutions
  edge @right.before_scope -> @left.after_scope
}

; nonempty template string, last substitution
(template_string (_) @last .)@template_string {
  ; scopes propagate out of the last substitution to the template string
  edge @template_string.after_scope -> @last.after_scope
}

[
  (string_fragment)
  (escape_sequence)
]@part {
  edge @part.after_scope -> @part.before_scope
}

(template_substitution (_)@expr)@subst {
  edge @expr.before_scope -> @subst.before_scope
  edge @subst.after_scope -> @expr.after_scope
  edge @subst.value -> @expr.value
}


;; #### Numbers

(number)@number {
  ; scopes don't change
  edge @number.after_scope -> @number.before_scope

  ; the value of a number is the number primitive
  edge @number.value -> @number.builtins_number
}


;; #### Variables

[
  (primary_expression/identifier)@variable
  (member_expression object:(identifier)@variable)
] {
  ; value is a lookup, ie a push
  attr (@variable.value) node_reference = @variable
  edge @variable.value -> @variable.before_scope
}


;; #### Booleans

(true)@true {
  ; scopes don't change
  edge @true.after_scope -> @true.before_scope

  ; the value of true is a boolean primitive
  edge @true.value -> @true.builtins_boolean
}

(false)@false {
  ; scopes don't change
  edge @false.after_scope -> @false.before_scope

  ; the value of false is a boolean primitive
  edge @false.value -> @false.builtins_boolean
}


;; #### This

(this)@this {
  ; scopes don't change
  edge @this.after_scope -> @this.before_scope

  ; this is a lookup, ie a push
  attr (@this.value) symbol_reference = "this", source_node = @this
  edge @this.value -> @this.before_scope

  node pop_this
  attr (pop_this) symbol_definition = "this"
  node guard_prototype
  attr (guard_prototype) push_symbol = "GUARD:PROTOTYPE"
  edge @this.value -> pop_this
  edge pop_this -> guard_prototype
  edge guard_prototype -> @this.containing_class_value
}


;; #### Super

(super)@super {
  ; scopes don't change
  edge @super.after_scope -> @super.before_scope
}


;; #### Null

(null)@null {
  ; scopes don't change
  edge @null.after_scope -> @null.before_scope

  ; the value of null is the null primitive
  edge @null.value -> @null.builtins_null
}


;; #### Undefined

(undefined)@undefined {
  ; scopes don't change
  edge @undefined.after_scope -> @undefined.before_scope

  ; the value of undefined is the undefined primitive
  edge @undefined.value -> @undefined.builtins_undefined

  ; !!!! HACK: `undefined` is a perfectly cromulent name for a parameter, but the parser thinks it means this node instead. For the moment, work around the problem this causes by giving all `undefined` nodes covalues like parameters enjoy.
  node @undefined.covalue
}


;; #### Regular Expressions

(regex)@regex {
  ; scopes don't change
  edge @regex.after_scope -> @regex.before_scope

  ; the value of a regex is the Regex prototype
  edge @regex.value -> @regex.builtins_Regex_prototype
}


;; #### Spread Elements

(spread_element (_)@expr)@spread_elem {
  ; scopes flow in then right back out
  edge @expr.before_scope -> @spread_elem.before_scope
  edge @spread_elem.after_scope -> @expr.after_scope
}


;; #### Objects

(object)@object {

    node @object.member_pop
    attr (@object.member_pop) pop_symbol = "GUARD:MEMBER"
    edge @object.value -> @object.member_pop

    node @object.class_value
    node @object.constructor

}

; empty objects
(object (_)* @entries)@object_expr {
  if (is-empty @entries) {
    edge @object_expr.after_scope -> @object_expr.before_scope
  }
}

; non-empty objects, scopes, first entry
(object
  .
  (_)@first_entry)@object_expr {
  ; scopes propagate from the object to the first entry
  edge @first_entry.before_scope -> @object_expr.before_scope
}

; non-empty objects, scopes, between entries
(object
  (_)@left_entry
  .
  (_)@right_entry
)@_object_expr {
  ; scopes propagate from left entries to right entries
  edge @right_entry.before_scope -> @left_entry.after_scope
}

; non-empty objects, scopes, last entry
(object
  (_)@last_entry
  .)@object_expr {
  ; scopes propagate from the last entry back to the object
  edge @object_expr.after_scope -> @last_entry.after_scope
}

; shorthand property identifier
(shorthand_property_identifier)@shorthand_property_identifier {

  node @shorthand_property_identifier.after_scope
  node @shorthand_property_identifier.before_scope

}

(object (shorthand_property_identifier)@keyval)@object {

  node rhsvar_before_scope
  node rhsvar_after_scope
  node rhsvar_value
  node key_pop

  attr (rhsvar_value) node_reference = @keyval
  attr (key_pop) node_definition = @keyval

  ; scopes flow into rhsvar, and also straight across b/c they can't be modified
  edge rhsvar_before_scope -> @keyval.before_scope
  edge rhsvar_after_scope -> rhsvar_before_scope
  edge @keyval.after_scope -> rhsvar_after_scope

  edge rhsvar_value -> rhsvar_before_scope

  ; shorthand property identifiers augment the object value with a member binding
  edge key_pop -> rhsvar_value
  edge @object.member_pop -> key_pop
}

; pairs

(computed_property_name)@computed_property_name {
    node @computed_property_name.after_scope
    node @computed_property_name.before_scope
}

(computed_property_name (_)@expr)@computed_property_name {

    edge @expr.before_scope -> @computed_property_name.before_scope
    edge @computed_property_name.after_scope -> @expr.after_scope

}

(object
  (pair
    key:(_)@_key
    value: (_)@value)@pair)@object {

    ; pairs augment the object value with a member binding

    ; This is done differently depending on what the key is. See next rules.
    ; attr @key.pop "pop" = @key, "definition"

    node @pair.key_pop
    edge @pair.key_pop -> @value.value
    edge @object.member_pop -> @pair.key_pop

}

(pair)@pair {
  node @pair.after_scope
  node @pair.before_scope
}

(pair
    key:(property_identifier)@key
    value:(_)@value)@pair
{

    attr (@pair.key_pop) node_definition = @key

    ; scopes flow into the value, then back to the pair
    edge @value.before_scope -> @pair.before_scope
    edge @pair.after_scope -> @value.after_scope

}

(pair
  key:(string)@key
    value:(_)@value)@pair
{

  node @key.pop
  attr (@key.pop) pop_symbol = (replace (source-text @key) "\"" "")

    ; scopes flow into the value, then back to the pair
    edge @value.before_scope -> @pair.before_scope
    edge @pair.after_scope -> @value.after_scope

}

(pair
    key:(number)@key
    value:(_)@value)@pair
{

    attr (@pair.key_pop) node_definition = @key

    ; scopes flow into the value, then back to the pair
    edge @value.before_scope -> @pair.before_scope
    edge @pair.after_scope -> @value.after_scope

}

(pair
    key:(computed_property_name)@key
    value:(_)@value)@pair
{

    ; scopes flow into the key, then out to the value, then back to the pair
    edge @key.before_scope -> @pair.before_scope
    edge @value.before_scope -> @key.after_scope
    edge @pair.after_scope -> @value.after_scope

}



;; #### Arrays

(array)@array_expr {

    node @array_expr.element_pop_dot
    attr (@array_expr.element_pop_dot) pop_symbol = "GUARD:MEMBER"
    edge @array_expr.value -> @array_expr.element_pop_dot

}

; empty arrays
(array (_)* @elems)@array_expr {
  if (is-empty @elems) {
    edge @array_expr.after_scope -> @array_expr.before_scope
  }
}

; nonempty arrays, first element
(array
  .
  (_)@first_element)@array_expr {
  ; scopes propagate into the first element of the array
  edge @first_element.before_scope -> @array_expr.before_scope
}

; nonempty arrays, between elements
(array
  (_)@left_element
  .
  (_)@right_element) {
  ; scopes propagate from left elements to right elements
  edge @right_element.before_scope -> @left_element.after_scope
}

; nonempty arrays, last element
(array
  (_)@last_element
  .)@array_expr {
  ; scopes propagate out of the last element to the array
  edge @array_expr.after_scope -> @last_element.after_scope
}

; elements at indices
(array (_)@element)@array_expr {

    node element_index_pop

    attr (element_index_pop) pop_symbol = (named-child-index @element)
    edge @array_expr.element_pop_dot -> element_index_pop
    edge element_index_pop -> @element.value

}



;; #### Formal Parameters

(formal_parameters)@formal_params {
  node @formal_params.after_scope
  node @formal_params.before_scope
}

(formal_parameters (_)* @params)@formal_params {
  if (is-empty @params) {
    edge @formal_params.after_scope -> @formal_params.before_scope
  }
}

; first parameter
(formal_parameters
  .
  (_)@first_param)@formal_params {

  edge @first_param.before_scope -> @formal_params.before_scope
}

; between parameters
(formal_parameters
  (_)@left_param
  .
  (_)@right_param) {

  ; scope flows left to right
  edge @right_param.before_scope -> @left_param.after_scope
}

; last parameter
(formal_parameters
  (_)@last_param
  .)@formal_params {

  ; scope flows from the param to the call sig
  edge @formal_params.after_scope -> @last_param.after_scope
}



;; #### Function Literals

; functions with names
(function_expression
  name:(_)@name
  parameters:(_)@call_sig)@fun {

  node @name.pop
  attr (@name.pop) syntax_type = "function"

  ; if the function has a name, this is bound the callsig's before scope
  attr (@name.pop) node_definition = @name
  edge @call_sig.before_scope -> @name.pop
  edge @name.pop -> @fun.value
}


; function
(function_expression
  parameters:(_)@call_sig
  body:(_)@body)@fun {

  node call_sig_arguments_pop
  node call_sig_arguments_push
  node call_sig_this_pop
  node call_sig_this_push
  node @fun.return_or_yield
  node @fun.value_arg_scope
  node fun_value_call
  node fun_value_drop
  node fun_value_return
  node fun_value_this
  node fun_value_this_guard

  ; scope flows across the decl
  edge @fun.after_scope -> @fun.before_scope

  ; function values have drop nodes that handle closures, that points to the
  ; before scope from the function
  attr (fun_value_drop) type = "drop_scopes"
  edge fun_value_drop -> @fun.closure_point

  ; the call sig's before scope comes from the drop node,
  ; then flows into the body, and includes a variable binding for "this"
  edge @call_sig.before_scope -> fun_value_drop
  attr (call_sig_this_pop) symbol_definition = "this", source_node = @call_sig
  attr (call_sig_this_push) push_symbol = "this"
  edge call_sig_this_pop -> call_sig_this_push
  edge call_sig_this_push -> @fun.value_arg_scope
  edge @call_sig.before_scope -> call_sig_this_pop
  attr (call_sig_arguments_pop) symbol_definition = "arguments", source_node = @call_sig
  attr (call_sig_arguments_push) push_symbol = "arguments"
  edge call_sig_arguments_pop -> call_sig_arguments_push
  edge call_sig_arguments_push -> @fun.value_arg_scope
  edge @call_sig.before_scope -> call_sig_arguments_pop
  edge @body.before_scope -> @call_sig.after_scope

  ; function values have call nodes
  attr (fun_value_call) pop_scoped_symbol = "()"
  edge @fun.value -> fun_value_call

  ; function values have return nodes which need to be visible for returns
  attr (fun_value_return) pop_symbol = "GUARD:RETURN"
  edge fun_value_call -> fun_value_return
  let @body.return_or_yield = fun_value_return

  ; function values have this nodes which need to be visible for method calls
  attr (fun_value_this) push_symbol = "this"
  attr (fun_value_this_guard) pop_symbol = "GUARD:THIS"
  edge fun_value_call -> fun_value_this_guard
  edge fun_value_this_guard -> fun_value_this
  edge fun_value_this -> @body.after_scope

  ; function values have a jump node that lets params connect up to actual arguments
  edge @fun.value_arg_scope -> JUMP_TO_SCOPE_NODE
}

(function_expression
  parameters:
    (formal_parameters (_)@param))@fun {

  node param_arg_index

  ; parameters jump to the pushed argument scope
  attr (param_arg_index) push_symbol = (named-child-index @param)
  edge @param.covalue -> param_arg_index
  edge param_arg_index -> @fun.value_arg_scope
}



;; #### Arrow Function Literals

; function
[
  (arrow_function
    parameters:(_)@call_sig
    body:(_)@body)@fun

  (arrow_function
    parameter:(_)@call_sig
      body:(_)@body)@fun
] {

  node @fun.return_or_yield
  node @fun.value_arg_scope
  node fun_value_call
  node fun_value_drop
  node fun_value_return
  node fun_value_this
  node fun_value_this_guard

  ; scope flows across the decl
  edge @fun.after_scope -> @fun.before_scope

  ; function values have drop nodes that handle closures, that points to the
  ; before scope from the function
  attr (fun_value_drop) type = "drop_scopes"
  edge fun_value_drop -> @fun.closure_point

  ; the call sig's before scope comes from the drop node then flows into the body
  edge @call_sig.before_scope -> fun_value_drop
  edge @body.before_scope -> @call_sig.after_scope

  ; function values have call nodes
  attr (fun_value_call) pop_scoped_symbol = "()"
  edge @fun.value -> fun_value_call

  ; function values have return nodes which need to be visible for returns
  attr (fun_value_return) pop_symbol = "GUARD:RETURN"
  edge fun_value_call -> fun_value_return
  edge fun_value_return -> @fun.return_or_yield

  ; function values have this nodes which need to be visible for method calls
  attr (fun_value_this) push_symbol = "this"
  attr (fun_value_this_guard) pop_symbol = "GUARD:THIS"
  edge fun_value_call -> fun_value_this_guard
  edge fun_value_this_guard -> fun_value_this
  edge fun_value_this -> @body.after_scope

  ; function values have a jump node that lets params connect up to actual arguments
  edge @fun.value_arg_scope -> JUMP_TO_SCOPE_NODE
}

; arrow functions returning exprs need special rules for getting the return value hooked up
(arrow_function
  body:(expression)@return_expr) {
  edge @return_expr.return_or_yield -> @return_expr.value
}

(arrow_function
  parameter:(_)@param)@fun {
  node param_arg_index
  node param_pop

  ; but augmented with a pop, b/c it's not a pattern
  attr (param_pop) node_definition = @param
  edge param_pop -> @param.covalue
  edge @param.after_scope -> param_pop

  ; parameters jump to the pushed argument scope
  attr (param_arg_index) push_symbol = "0"
  edge @param.covalue -> param_arg_index
  edge param_arg_index -> @fun.value_arg_scope
}

(arrow_function
  parameters:
    (formal_parameters (_)@param))@fun {

  node param_arg_index

  ; parameters jump to the pushed argument scope
  attr (param_arg_index) push_symbol = (named-child-index @param)
  edge @param.covalue -> param_arg_index
  edge param_arg_index -> @fun.value_arg_scope
}



;; #### Generator Function Literals

; generator functions with names
(generator_function
  name:(_)@name
  parameters:(_)@call_sig)@fun {

  node @name.pop
  attr (@name.pop) syntax_type = "function"

  ; if the function has a name, this is bound the callsig's before scope
  attr (@name.pop) node_definition = @name
  edge @call_sig.before_scope -> @name.pop
  edge @name.pop -> @fun.value
}


; generator function
(generator_function
  parameters:(_)@call_sig
  body:(_)@body)@fun {

  node call_sig_arguments_pop
  node call_sig_arguments_push
  node call_sig_this_pop
  node call_sig_this_push
  node @fun.return_or_yield
  node @fun.value_arg_scope
  node fun_value_call
  node fun_value_drop
  node fun_value_return
  node fun_value_this
  node fun_value_this_guard

  ; scope flows across the decl
  edge @fun.after_scope -> @fun.before_scope

  ; function values have drop nodes that handle closures, that points to the
  ; before scope from the function
  attr (fun_value_drop) type = "drop_scopes"
  edge fun_value_drop -> @fun.closure_point

  ; the call sig's before scope comes from the drop node,
  ; then flows into the body, and includes a variable binding for "this"
  edge @call_sig.before_scope -> fun_value_drop
  attr (call_sig_this_pop) symbol_definition = "this", source_node = @call_sig
  attr (call_sig_this_push) push_symbol = "this"
  edge call_sig_this_pop -> call_sig_this_push
  edge call_sig_this_push -> @fun.value_arg_scope
  edge @call_sig.before_scope -> call_sig_this_pop
  attr (call_sig_arguments_pop) symbol_definition = "arguments", source_node = @call_sig
  attr (call_sig_arguments_push) push_symbol = "arguments"
  edge call_sig_arguments_pop -> call_sig_arguments_push
  edge call_sig_arguments_push -> @fun.value_arg_scope
  edge @call_sig.before_scope -> call_sig_arguments_pop
  edge @body.before_scope -> @call_sig.after_scope

  ; function values have call nodes
  attr (fun_value_call) pop_scoped_symbol = "()"
  edge @fun.value -> fun_value_call

  ; function values have return nodes which need to be visible for returns
  attr (fun_value_return) pop_symbol = "GUARD:RETURN"
  edge fun_value_call -> fun_value_return
  let @body.return_or_yield = fun_value_return

  ; function values have this nodes which need to be visible for method calls
  attr (fun_value_this) push_symbol = "this"
  attr (fun_value_this_guard) pop_symbol = "GUARD:THIS"
  edge fun_value_call -> fun_value_this_guard
  edge fun_value_this_guard -> fun_value_this
  edge fun_value_this -> @body.after_scope

  ; function values have a jump node that lets params connect up to actual arguments
  edge @fun.value_arg_scope -> JUMP_TO_SCOPE_NODE
}

(generator_function
  parameters:
    (formal_parameters (_)@param))@fun {

  node param_arg_index

  ; parameters jump to the pushed argument scope
  attr (param_arg_index) push_symbol = (named-child-index @param)
  edge @param.covalue -> param_arg_index
  edge param_arg_index -> @fun.value_arg_scope
}


;; #### Function Calls

; calls, functions
(call_expression
  function:(_)@function
  arguments:(_)@arguments)@call_expr {

  ; scopes flow from call expressions into the function
  edge @function.before_scope -> @call_expr.before_scope
  edge @arguments.before_scope -> @function.after_scope
  edge @call_expr.after_scope -> @arguments.after_scope
}

; calls, values
(call_expression
  function:(_)@function
  arguments:(_)@arguments)@call_expr {

  node arguments_arg_arguments
  node arguments_arg_arguments_dot
  node @arguments.arg_scope
  attr (@arguments.arg_scope) is_exported
  node @arguments.arg_scope_no_this
  node @arguments.arg_this
  node call_expr_call
  node call_expr_return_guard

  ; value is a call, ie a push "()" node w/ "push-scope" @arguments
  attr (call_expr_return_guard) push_symbol = "GUARD:RETURN"
  attr (call_expr_call) push_scoped_symbol = "()", scope = @arguments.arg_scope
  edge @call_expr.value -> call_expr_return_guard
  edge call_expr_return_guard -> call_expr_call
  edge call_expr_call -> @function.value

  attr (@arguments.arg_this) symbol_definition = "this", source_node = @arguments
  edge @arguments.arg_scope -> @arguments.arg_this

  edge @arguments.arg_scope -> @arguments.arg_scope_no_this

  attr (arguments_arg_arguments) symbol_definition = "arguments", source_node = @arguments
  attr (arguments_arg_arguments_dot) pop_symbol = "GUARD:MEMBER"
  edge @arguments.arg_scope -> arguments_arg_arguments
  edge arguments_arg_arguments -> arguments_arg_arguments_dot
  edge arguments_arg_arguments_dot -> @arguments.arg_scope_no_this
  edge arguments_arg_arguments -> @call_expr.builtins_arguments_prototype
}

; special case to make `this` bind correctly in calls of the forms `x.f(...)`
; and `x[f](...)`
(call_expression
  function:[
    (member_expression object:(_)@object)
    (subscript_expression object:(_)@object)
  ]
  arguments:(_)@arguments) {

  edge @arguments.arg_this -> @object.value
}


; TODO this should eventually be removed and replaced with a version that only
; applies to the negation of (member_expression), but that's not supported by
; tree-sitter currently
(call_expression
  function: (_)@_function
  arguments:(_)@arguments)@call_expr {

  edge @arguments.arg_this -> @call_expr.builtins_null
}
(call_expression
  arguments:(arguments (_)@arg)@arguments) {

  node arg_arg_index
  attr (arg_arg_index) pop_symbol = (named-child-index @arg)
  edge @arguments.arg_scope_no_this -> arg_arg_index
  edge arg_arg_index -> @arg.value
}



;; #### Arguments

(arguments)@arguments {
  node @arguments.after_scope
  node @arguments.before_scope
}

(arguments (_)* @args)@arguments {
  if (is-empty @args) {
    edge @arguments.after_scope -> @arguments.before_scope
  }
}

(arguments
  .
  (_)@first_arg)@arguments {

  edge @first_arg.before_scope -> @arguments.before_scope
}

(arguments
  (_)@left_arg
  .
  (_)@right_arg) {

  edge @right_arg.before_scope -> @left_arg.after_scope
}

(arguments
  (_)@last_arg
  .)@arguments {

  edge @arguments.after_scope -> @last_arg.after_scope
}



;; #### Property Access

;; ##### Member Expressions

(member_expression
  object:(_)@object property:(_)@property)@member_expr
{

    node member_push
    node property_push

  ; scopes flow into object then back out
  edge @object.before_scope -> @member_expr.before_scope
  edge @member_expr.after_scope -> @object.after_scope

  ; value is a member projection on the value of the object ie. a push then push dot
    attr (member_push) push_symbol = "GUARD:MEMBER"
    attr (property_push) node_reference = @property
    edge property_push -> member_push
    edge @member_expr.value -> property_push
    edge member_push -> @object.value

  ; (member_expression) nodes can occur in patterns
  node @member_expr.covalue
  node @member_expr.new_bindings
}

;; ##### Subscript Expressions

(subscript_expression
  object: (_)@object
  index: (_)@index)@subscript_expr {

  node @subscript_expr.index_push
  node subscript_expr_push_dot

  ; scopes flow left to right
  edge @object.before_scope -> @subscript_expr.before_scope
  edge @index.before_scope -> @object.after_scope
  edge @subscript_expr.after_scope -> @index.after_scope

  ; value is a subscript lookup, ie a push then push dot
  attr (subscript_expr_push_dot) push_symbol = "GUARD:MEMBER"
  edge subscript_expr_push_dot -> @object.value

  ; this is done differently depending on what the index is
  edge @subscript_expr.value -> @subscript_expr.index_push
  edge @subscript_expr.index_push -> subscript_expr_push_dot

  ; subscript expressions can appear in array patterns, on the left, and thus require a covalue & bindings
  node @subscript_expr.covalue
  node @subscript_expr.new_bindings
}

(subscript_expression
  object: (_)@_object
  index: (string)@index)@subscript_expr
{

    attr (@subscript_expr.index_push) symbol_reference = (replace (source-text @index) "[\"\']" ""), source_node = @index

}

(subscript_expression
  object: (_)@_object
  index: (number)@index)@subscript_expr
{

    attr (@subscript_expr.index_push) node_reference = @index

}



;; #### Constructor Calls

(new_expression
  constructor:(_)@constructor
  arguments:(_)@arguments)@new_expr {

  node @arguments.arg_scope
  attr (@arguments.arg_scope) is_exported
  node @arguments.arg_this
  node constructor_constructor
  node new_expr_call
  node new_expr_guard_this

  edge @constructor.before_scope -> @new_expr.before_scope
  edge @arguments.before_scope -> @constructor.after_scope
  edge @new_expr.after_scope -> @arguments.after_scope

  attr (new_expr_call) push_scoped_symbol = "()", scope = @arguments.arg_scope

  ; we guard for constructors for the case where we have a "true" class
  attr (constructor_constructor) push_symbol = "GUARD:CONSTRUCTOR"
  edge new_expr_call -> constructor_constructor
  edge constructor_constructor -> @constructor.value

  ; and also just go right to the value incase we have a function-as-constructor
  edge new_expr_call -> @constructor.value



  ; value coming from the constructor call
  attr (new_expr_guard_this) push_symbol = "GUARD:THIS"
  edge @new_expr.value -> new_expr_guard_this
  edge new_expr_guard_this -> new_expr_call

  ; value also coming from the prototype
  node guard_prototype
  attr (guard_prototype) push_symbol = "GUARD:PROTOTYPE"
  edge @new_expr.value -> guard_prototype
  edge guard_prototype -> @constructor.value

  attr (@arguments.arg_this) symbol_definition = "this", source_node = @arguments
  edge @arguments.arg_scope -> @arguments.arg_this
  edge @arguments.arg_this -> @new_expr.builtins_empty_object
}

(new_expression
  arguments:(arguments (_)@arg)@arguments) {

  node arg_arg_index

  attr (arg_arg_index) pop_symbol = (named-child-index @arg)
  edge @arguments.arg_scope -> arg_arg_index
  edge arg_arg_index -> @arg.value
}



;; #### Await

(await_expression (_)@awaited)@await_expr {
  ; scopes flow into the inner expression then back out
  edge @awaited.before_scope -> @await_expr.before_scope
  edge @await_expr.after_scope -> @awaited.after_scope

  ; value is just propagated up
  edge @await_expr.value -> @awaited.value
}



;; #### Update Expressions

(update_expression argument: (_)@argument)@update_expr {

  node update_expr_pop
  ; scope propagates through the operand then is updated by the expr
  edge @argument.before_scope -> @update_expr.before_scope
  edge @update_expr.after_scope -> @argument.after_scope
  ; LATER-TODO this isn't quite right because the update argument can't be an arbitrary expr
  ; eg f(x)++ doesn't make any sense, you have to have something more like
  ; (update_expression argument: (lvar)@argument)
  attr (update_expr_pop) node_definition = @argument
  edge @update_expr.value -> @argument.value
  edge @update_expr.after_scope -> update_expr_pop
  edge update_expr_pop -> @argument.value

}


;; #### Binary Expressions

(binary_expression left: (_)@left right: (_)@right)@binary_expr {
  ; scopes propagate left to right through the operands unchanged by the binop itself
  edge @left.before_scope -> @binary_expr.before_scope
  edge @right.before_scope -> @left.after_scope
  edge @binary_expr.after_scope -> @right.after_scope

  ; value is a binary op value built from the operands
  ; LATER-TODO this isn't quite correct but it permits flow through the expression
  ; which can be useful
  ; attr (@binary_expr.value) "binary_operation_value"
  edge @binary_expr.value -> @left.value
  edge @binary_expr.value -> @right.value
}


;; #### Unary Expressions

(unary_expression argument: (_)@argument)@unary_expr {
  ; scope propagates through the operand
  edge @argument.before_scope -> @unary_expr.before_scope
  edge @unary_expr.after_scope -> @argument.after_scope

  ; value is a unaryop value built from the operand
  ; LATER-TODO this isn't quite correct but it permits flow through the expression
  ; which can be useful
  ; attr (@unary_expr.value) "unary_operation_value"
  edge @unary_expr.value -> @argument.value
}



;; #### Assignment Expressions;

; scopes on RHS, values
(assignment_expression
  left: (_)@_left
  right: (_)@right)@assignment_expr {

  ; scopes flow into the RHS then back out to the whole expr,
  ; augmented (in subsequent rules) by the LHS
  edge @right.before_scope -> @assignment_expr.before_scope

  ; value of the whole thing is value of the RHS
  edge @assignment_expr.value -> @right.value
}

; augmentation of scope via identifiers
(assignment_expression
  left: (identifier)@name
  right: (_)@right)@assignment_expr {

    node @name.pop

    ; augments the scope by adding a lookup edge, ie. a pop
    attr (@name.pop) node_definition = @name
    edge @assignment_expr.after_scope -> @name.pop
    edge @name.pop -> @right.value

    ; ensure the scope flows through the identifier
    edge @assignment_expr.after_scope -> @right.after_scope

}

(assignment_expression
  left: [
    (member_expression)
    (subscript_expression)
  ]@left
  right: (_)@right)@assignment_expr {

    ; scope flows from LHS into pattern then back to assignment
    edge @left.before_scope -> @assignment_expr.before_scope

    ; ensure the scope flows through the identifier
    edge @assignment_expr.after_scope -> @right.after_scope

}

; assignment to direct fields on `this`
(assignment_expression
  left: (member_expression
    object:(this)@_this
    property:(_)@property)
  right: (_)@right)@assignment_expr {

  node property_pop
  node this_drop
  node this_pop
  node this_pop_dot

  ; HACK
  attr (this_drop) type = "drop_scopes"
  edge this_drop -> this_pop
  attr (this_pop) pop_symbol = "this"
  attr (this_pop_dot) pop_symbol = "GUARD:MEMBER"
  attr (property_pop) node_definition = @property
  edge @assignment_expr.after_scope -> this_drop
  edge @assignment_expr.after_scope -> this_pop
  edge this_pop -> this_pop_dot
  edge this_pop_dot -> property_pop
  edge property_pop -> @right.value
}

; augmentation of scope via _destructuring_patterns
(assignment_expression
  left: [
    (object_pattern)
    (array_pattern)
    (assignment_pattern)
  ]@left
  right: (_)@right)@assignment_expr {

  ; scope flows from LHS into pattern then back to assignment
  edge @left.before_scope -> @right.after_scope
  edge @assignment_expr.after_scope -> @left.after_scope

}



;; #### Augmented Assignment Expressions

(augmented_assignment_expression
  left: (_)@_left
  right: (_)@right)@augmented_assignment_expr {

  ; scopes flow into the RHS then back out to the whole expr, augmented by the LHS
  edge @right.before_scope -> @augmented_assignment_expr.before_scope
  edge @augmented_assignment_expr.after_scope -> @right.after_scope
}

(augmented_assignment_expression
  left:(identifier)@left
  right:(_)@right)@augmented_assignment_expr {

  node augmented_assignment_expr_pop
  node augmented_assignment_expr_push

  ; augment the scope
    attr (augmented_assignment_expr_pop) node_definition = @left
    attr (augmented_assignment_expr_push) node_reference = @left
    edge augmented_assignment_expr_push -> @augmented_assignment_expr.before_scope
    edge augmented_assignment_expr_pop -> augmented_assignment_expr_push
    edge augmented_assignment_expr_pop -> @right.value
    edge @augmented_assignment_expr.after_scope -> augmented_assignment_expr_pop

}


;; #### Comma Operator / Sequence Expressions

(sequence_expression (_)* @elems)@sequence_expr {
  if (is-empty @elems) {
    edge @sequence_expr.after_scope -> @sequence_expr.before_scope
  }
}

(sequence_expression . (_)@first)@sequence_expr {
  edge @first.before_scope -> @sequence_expr.before_scope
}

(sequence_expression (_)@left . (_)@right) {
  edge @right.before_scope -> @left.after_scope
}

(sequence_expression (_)@last .)@sequence_expr {
  edge @sequence_expr.after_scope -> @last.after_scope
  edge @sequence_expr.value -> @last.value
}



;; #### Ternary Expression

(ternary_expression
  condition: (_)@condition
  consequence: (_)@consequence
  alternative: (_)@alternative)@ternary_expr {

  ; scopes propagate into condition, then into each branch
  edge @condition.before_scope -> @ternary_expr.before_scope
  edge @consequence.before_scope -> @condition.after_scope
  edge @alternative.before_scope -> @condition.after_scope
  edge @ternary_expr.after_scope -> @consequence.after_scope
  edge @ternary_expr.after_scope -> @alternative.after_scope

  ; value of the whole thing is a conditional value from the operands
  edge @ternary_expr.value -> @consequence.value
  edge @ternary_expr.value -> @alternative.value
}



;; #### Yield

(yield_expression (_)@yielded_expr)@yield_expr {
  ; scopes flow in to the yielded expression then back out
  edge @yielded_expr.before_scope -> @yield_expr.before_scope
  edge @yield_expr.after_scope -> @yielded_expr.after_scope

  ; yield expressions hook up to the call node of the function value
  edge @yield_expr.return_or_yield -> @yielded_expr.value
}



;; #### Class Expressions

(class
  body:(_)@body)@class {

  node @class.class_value
  let @class.containing_class_value = @class.class_value
  node guard_prototype
  node @class.prototype
  node @class.constructor
  attr (guard_prototype) pop_symbol = "GUARD:PROTOTYPE"

  edge @body.before_scope -> @class.closure_point
  edge @class.value -> @class.class_value
  edge @class.class_value -> guard_prototype
  edge guard_prototype -> @class.prototype
  edge @class.class_value -> @class.constructor
  edge @class.prototype -> @body.after_scope
  edge @class.after_scope -> @class.before_scope
}

(class
  name:(_)@name
  body:(_)@body)@class {

  node @name.pop
  attr (@name.pop) syntax_type = "class"

  attr (@name.pop) node_definition = @name
  edge @body.before_scope -> @name.pop
  edge @name.pop -> @class.value
}

(class
  (class_heritage (_)@name))@class {

  node guard_prototype
  node guard_constructor
  attr (guard_prototype) push_symbol = "GUARD:PROTOTYPE"
  attr (guard_constructor) push_symbol = "GUARD:CONSTRUCTOR"
  edge @name.before_scope -> @class.before_scope
  edge @class.prototype -> guard_prototype
  edge guard_prototype -> @name.value
  edge @class.constructor -> guard_constructor
  edge guard_constructor -> @name.value
  edge @class.after_scope -> @name.after_scope
}

(jsx_element
  open_tag:(_)@open_tag
  close_tag:(_)@close_tag
  (_)*@children)@jsx_element {

  node @jsx_element.before_scope
  node @jsx_element.after_scope
  node @jsx_element.value

  edge @open_tag.before_scope -> @jsx_element.before_scope
  edge @jsx_element.after_scope -> @close_tag.after_scope

  if (is-empty @children) {
    edge @close_tag.before_scope -> @open_tag.after_scope
  }

}

(jsx_element
  open_tag:(_)@open_tag
  .
  [
    (jsx_text)
    (jsx_element)
    (jsx_self_closing_element)
    (jsx_expression)
  ]@first_child
) {
  edge @first_child.before_scope -> @open_tag.after_scope
}

(jsx_element
  [
    (jsx_text)
    (jsx_element)
    (jsx_self_closing_element)
    (jsx_expression)
  ]@left_child
  .
  [
    (jsx_text)
    (jsx_element)
    (jsx_self_closing_element)
    (jsx_expression)
  ]@right_child
) {
  edge @right_child.before_scope -> @left_child.after_scope
}

(jsx_element
  [
    (jsx_text)
    (jsx_element)
    (jsx_self_closing_element)
    (jsx_expression)
  ]@last_child
  .
  close_tag:(_)@close_tag
) {
  edge @close_tag.before_scope -> @last_child.after_scope
}

(jsx_text)@jsx_text {
  node @jsx_text.before_scope
  node @jsx_text.after_scope

  edge @jsx_text.after_scope -> @jsx_text.before_scope
}

(jsx_opening_element)@jsx_opening_element {

  node @jsx_opening_element.before_scope
  node @jsx_opening_element.after_scope

}

(jsx_opening_element
  name:(_)@element_name)@jsx_opening_element {

  edge @element_name.before_scope -> @jsx_opening_element.before_scope

}

(jsx_opening_element
  !name)@jsx_opening_element
{

  edge @jsx_opening_element.after_scope -> @jsx_opening_element.before_scope

}

(jsx_opening_element
  name:(_)@element_name
  !attribute)@jsx_opening_element
{

  edge @jsx_opening_element.after_scope -> @element_name.after_scope

}

(jsx_opening_element
  name:(_)@element_name
  .
  attribute:(_)@first_attr
) {

  edge @first_attr.before_scope -> @element_name.after_scope

}

(jsx_opening_element
  attribute:(_)@left_attr
  .
  attribute:(_)@right_attr
) {

  edge @right_attr.before_scope -> @left_attr.after_scope

}

(jsx_opening_element
  attribute:(_)@last_attr
  .)@jsx_opening_element
{

  edge @jsx_opening_element.after_scope -> @last_attr.after_scope

}

(jsx_attribute (_) . (_)?@attr_value)@jsx_attribute {

  node @jsx_attribute.before_scope
  node @jsx_attribute.after_scope

  if none @attr_value {
    edge @jsx_attribute.after_scope -> @jsx_attribute.before_scope
  } else {
    edge @attr_value.before_scope -> @jsx_attribute.before_scope
    edge @jsx_attribute.after_scope -> @attr_value.after_scope
  }

}

(jsx_namespace_name (_) @lhs (_) @rhs)@name {
  node @name.before_scope
  node @name.after_scope

  edge @lhs.before_scope -> @name.before_scope
  edge @rhs.before_scope -> @lhs.after_scope
  edge @name.after_scope -> @name.before_scope
}

(jsx_self_closing_element
  name:(_)@element_name)@jsx_self_closing_element {

  node @jsx_self_closing_element.before_scope
  node @jsx_self_closing_element.after_scope
  node @jsx_self_closing_element.value

  edge @element_name.before_scope -> @jsx_self_closing_element.before_scope

}

(jsx_self_closing_element
  !name
  !attribute)@jsx_self_closing_element
{

  edge @jsx_self_closing_element.after_scope -> @jsx_self_closing_element.before_scope

}

(jsx_self_closing_element
  name:(_)@element_name
  !attribute)@jsx_self_closing_element
{

  edge @jsx_self_closing_element.after_scope -> @element_name.after_scope

}

(jsx_self_closing_element
  name:(_)@element_name
  .
  attribute:(_)@first_attr
) {

  edge @first_attr.before_scope -> @element_name.after_scope

}

(jsx_self_closing_element
  attribute:(_)@left_attr
  .
  attribute:(_)@right_attr
) {

  edge @right_attr.before_scope -> @left_attr.after_scope

}

(jsx_self_closing_element
  attribute:(_)@last_attr
  .)@jsx_self_closing_element
{

  edge @jsx_self_closing_element.after_scope -> @last_attr.after_scope

}

(jsx_expression)@jsx_expression {

  node @jsx_expression.before_scope
  node @jsx_expression.after_scope

}

(jsx_expression (_)?@child)@jsx_expression {

  if none @child {
    edge @jsx_expression.after_scope -> @jsx_expression.before_scope
  } else {
    edge @child.before_scope -> @jsx_expression.before_scope
    edge @jsx_expression.after_scope -> @child.after_scope
  }

}

(jsx_closing_element)@jsx_closing_element
{

  node @jsx_closing_element.before_scope
  node @jsx_closing_element.after_scope

}

(jsx_closing_element
  !name)@jsx_closing_element
{

  edge @jsx_closing_element.after_scope -> @jsx_closing_element.before_scope

}

(jsx_closing_element
  name:(_)@element_name)@jsx_closing_element
{

  edge @element_name.before_scope -> @jsx_closing_element.before_scope
  edge @jsx_closing_element.after_scope -> @element_name.after_scope

}

[
  (jsx_opening_element
    name:(identifier)@element_name)
  (jsx_self_closing_element
    name:(identifier)@element_name)
  (jsx_closing_element
    name:(identifier)@element_name)
]
{
  scan (source-text @element_name) {
    ; standard HTML elements
    "^(a|abbr|acronym|address|applet|area|article|aside|audio|b|base|basefont|bdi|bdo|big|blockquote|body|br|button|canvas|caption|center|cite|code|col|colgroup|data|datalist|dd|del|details|dfn|dialog|dir|div|dl|dt|em|embed|fieldset|figcaption|figure|font|footer|form|frame|frameset|h1|h2|h3|h4|h5|h6|head|header|hgroup|hr|html|i|iframe|input|ins|kbd|label|legend|li|link|main|map|mark|menu|meta|meter|nav|noframes|noscript|object|ol|optgroup|option|output|p|param|picture|pre|progress|q|rp|rt|ruby|s|samp|script|search|section|select|small|source|span|strike|strong|style|sub|summary|sup|svg|table|tbody|td|template|textarea|tfoot|th|thead|time|title|tr|track|tt|u|ul|var|video|wbr)$" {
      ; do nothing!
    }

    ; everything else
    "^.+$" {
      node element_name_pop
      attr (element_name_pop) node_reference = @element_name
      edge element_name_pop -> @element_name.before_scope
    }
  }

}




;; ██████   █████  ████████ ████████ ███████ ██████  ███    ██ ███████ 
;; ██   ██ ██   ██    ██       ██    ██      ██   ██ ████   ██ ██      
;; ██████  ███████    ██       ██    █████   ██████  ██ ██  ██ ███████ 
;; ██      ██   ██    ██       ██    ██      ██   ██ ██  ██ ██      ██ 
;; ██      ██   ██    ██       ██    ███████ ██   ██ ██   ████ ███████

;; ## Patterns

;; Patterns introduce at least two interesting problems to the task of name
;; resolution. On the one hand, patterns by themselves are rich in structure
;; and binding possibilities. On the other, they function to pull apart
;; structure in ways that we would like to make use of in resolving names. Let's
;; look at these in order.

;; ### Binding Possibilities

;; If names could only be bound directly either on the LHS of an assignment, or
;; as the formal parameters to a function, or in related places like as
;; arguments to increment or decrement operators, then there would be little
;; trouble saying where a name is bound. But consider a destructuring assignment
;; such as this:

;; ``````javascript
;; let [x,y,z] = arr;
;; ``````

;; This assignment has variable names on the LHS of an assignment, sure, but not
;; directly as the LHS. Rather they're buried down inside a pattern. Here they
;; aren't very deep, just one node below the root of the LHS, but they can be
;; arbitrarily far down:

;; ``````javascript
;; let [x, [y, [z, w], q], r] = arr;
;; ``````

;; On top of this, patterns in JavaScript permit default assignments, such as

;; ``````javascript
;; let [x, y = 1] = arr;
;; ``````

;; These default values can be the result of a computation itself containing
;; variables, such as

;; ``````javascript
;; let [x, y = 2*z] = arr;
;; ``````

;; Additionally, those variables referenced in the default value are evaluated
;; *after* the RHS of the assignment. In the following code, `z` is incremented
;; and becomes `2`, and then `x` is assigned to that value. The array on the RHS
;; is only one element long, so the `y` variable gets assigned the default value
;; of `2*z`, which is computed *after* the increment, and so is `2*2` or `4`.

;; ``````javascript
;; let z = 1;
;; let [x, y = 2*z] = [z++];
;; ``````

;; To complicated matters further, the default value can reference the *bound
;; variables to its left*. For instance:

;; ``````javascript
;; let [x, y = x+1] = arr;
;; ``````

;; All of this leads to some very interesting and tricky problems for name
;; resolution. The flow of the environment is as follows: First, the environment
;; flows into the RHS of the assignment and is updated by whatever evaluations
;; happen there, then it flows out of the RHS and into the LHS, where it goes
;; through each pattern in the LHS parse tree, in a left-to-right, depth-first
;; traversal. Patterns with no default assignments do nothing to the environment
;; but patterns with a default will pass the environment to the default value,
;; where it may be updated by the evaluation, and then passed further along.
;; Each variable, whether bare or on the left of an assignment, also has to
;; extend the environment because those variables come into scope further along.

;; ### Structure Decomposition

;; Let's now look at how patterns decompose structure. Let's consider the effect
;; of an assignment such as this:

;; ``````javascript
;; let [x,y] = [1,2];
;; ``````

;; This assignment binds `x` to `1` and `y` to `2`. It's equivalent to doing

;; ``````javascript
;; let x = 1, y = 2;
;; ``````

;; Except, unlike the latter, the array destructuring does not have any obvious
;; pairing up of the names with the expressions that give them their values.
;; Now, perhaps there's some clever hack we can perform for this special case
;; where the RHS is a structure like `[1,2]` where it's manifestly clear out to
;; pair things up, but of course the RHS can come from anywhere. It can come
;; from a local variable assignment:

;; ``````javascript
;; let arr = [1,2];
;; let [x,y] = arr;
;; ``````

;; Or a function call:

;; ``````javascript
;; function foo(arr) {
;;   let [x,y] = arr;
;;   ...
;; }
;; foo([1,2]);
;; ``````

;; Or any other number of places. We would like *all* of these to permit at
;; least *some* amount of name resolution so that we can find that `x` is `1`
;; and `y` is `2`. This should extend also to objects, not just arrays.

;; The approach we take is to recognize a general pattern of equivalence, which
;; the above double-let assignment is related special case. For arrays, all
;; destructuring assignments of the form

;; ``````javascript
;; let [..., pat_i, ...] = arr;
;; ``````

;; are equivalent to

;; ``````javascript
;; let pat_i = arr[i];
;; ``````

;; For any pattern `pat` and expression `arr`. So for instance the simple case
;; of a pattern expression and an array literal

;; ``````javascript
;; let [x,y] = [1,2];
;; ``````

;; is equivalent to a pair of lets

;; ``````javascript
;; let x = [1,2][0];
;; let y = [1,2][1];
;; ``````

;; Modulo any change in side effects from duplicating the array syntactically,
;; these two are equivalent and this would be a valid code transformation. Or if
;; the pattern were an embedded array destructuring like so:

;; ``````javascript
;; let [x, [y,z]] = [1, [2,3]];
;; ``````

;; then this would be equivalent to

;; ``````javascript
;; let x = [1, [2,3]][0];
;; let [y,z] = [1, [2,3]][1];
;; ``````

;; And of course the second let here would similarly unfold to be equivalent to
;; a pair of assignments, giving us

;; ``````javascript
;; let x = [1, [2,3]][0];
;; let y = [1, [2,3]][1][0];
;; let z = [1, [2,3]][1][1];
;; ``````

;; Similarly for objects, we have the following destructuring equivalence that
;; says an assignment like this:

;; ``````javascript
;; let {..., k: pat, ...} = obj;
;; ``````

;; is equivalent to

;; ``````javascript
;; let pat = obj[k];
;; ``````

;; for all patterns `pat` and expressions `obj`.

;; This lets us then conclude that whatever the graphs are that we generate for
;; patterns ought to be equivalent to the graphs we would general for using
;; array indexing and object indexing. Since array and object indexing are fully
;; capable of participating in name resolution, if we can achieve this
;; equivalence, patterns can also fully participate as well, and we'll be able
;; to say that the assignment `let [x,y] = [1,2]` yields the name `x` resolving
;; to `1` and `y` to `2`, as well as many many more complicated cases.

;; As mentioned in the intro to this doc, assignments point to values. The
;; simplest way to do this, in the absence of patterns, is just to have an edge
;; from the after scope of the assignment to a pop node for the variable,
;; and then to the value node of the RHS, so for `let x = 1;` it'd be like so:

;; ``````
;; after_scope ---> POP "x" ---> value_node_for_1
;; ``````

;; But the above discussion of destructuring complicates this. What we do to
;; address this is introduce the notion of a "covalue". Just as we can say that
;; an expression *has* a value, or produces a value, etc., we'll say that a
;; pattern *consumes* a value. Expressions have values going "out", while
;; patterns have values coming "in". And so like expressions have an associated
;; `value` node in the graph, patterns have an associated `covalue` node. The
;; covalue corresponds to an incoming value.

;; We build covalues similar to how we build values. Consider the value for an
;; array such as `["foo", "bar"]`, which will be a scope node with pop nodes
;; going out to the values of the strings, like so:

;; ``````
;;                            ,---> POP 0 ---> value_node_of_foo
;; value_node_of_the_array ---|
;;                            `---> POP 1 ---> value_node_of_bar
;; ``````

;; Similarly, a covalue for an array pattern will also have nodes for the
;; sub-patterns and nodes for the first and second indexes. But rather than pop
;; nodes, which show you where the 0th and 1st elements are, they'll be push
;; nodes to establish the lookup of those elements. The edges will therefore
;; go the other way around. So for a pattern like `[x,y]`, we have the graph

;; ``````
;;                                 ,--- PUSH 0 <--- covalue_node_of_x
;; covalue_node_of_the_pattern <---|
;;                                 `--- PUSH 1 <--- covalue_node_of_y
;; ``````

;; For readers familiar with category theory's notion of duality, this explains
;; why these are called "covalues". The general schema here is that where values
;; have pops, covalues have pushes, and all the arrows get flipped.

;; ### Attributes Defined on Patterns
;; TODO
[
  (assignment_pattern)@pattern
  (object_pattern)@pattern
  (array_pattern)@pattern
  (rest_pattern)@pattern
  (pair_pattern)@pattern
  (pattern/property_identifier)@pattern
  (object_assignment_pattern)@pattern
  (shorthand_property_identifier_pattern)@pattern
] {

  node @pattern.after_scope
  node @pattern.before_scope
  node @pattern.covalue
  node @pattern.new_bindings

}

;; ### Pattern Queries

;; #### Variable Patterns

; scope propagation through identifier patterns
(pattern/identifier)@ident_pat {

  node ident_pat_pop

  ; scope flows through, binding via a pop edge that goes to an unknown value
  attr (ident_pat_pop) node_definition = @ident_pat
  edge ident_pat_pop -> @ident_pat.covalue
  edge @ident_pat.after_scope -> ident_pat_pop

  edge @ident_pat.new_bindings -> ident_pat_pop
}


;; #### Object Patterns

(object_pattern (_)* @entries)@object_pat {
  if (is-empty @entries) {
    edge @object_pat.after_scope -> @object_pat.before_scope
  }
}

; scope propagation through object patterns, first entry
(object_pattern
  .
  (_)@first_entry)@object_pat {

  ; scope propagates from object pattern to entry
  edge @first_entry.before_scope -> @object_pat.before_scope
}

; scope propagation through object patterns, between entries
(object_pattern
  (_)@left_entry
  .
  (_)@right_entry) {

  ; scope propagates from left entry to right entry
  edge @right_entry.before_scope -> @left_entry.after_scope
}

; scope propagation through object patterns, last entry
(object_pattern
  (_)@last_entry
  .)@object_pat {

  ; scope propagates out from last entry to object pattern
  edge @object_pat.after_scope -> @last_entry.after_scope
}

; covalue propagation through object patterns
(object_pattern
  (_)@entry)@object_pat {

  ; covalues flow into entries unchanged
  edge @entry.covalue -> @object_pat.covalue

  edge @object_pat.new_bindings -> @entry.new_bindings
}

; object entry pair patterns
(pair_pattern
  key:(_)@key
  value:(_)@value_pat)@pair_pat {

  node @key.push
  node key_push_dot

  ; covalues flow in dotted
  attr (key_push_dot) push_symbol = "GUARD:MEMBER"
  edge @value_pat.covalue -> @key.push
  edge @key.push -> key_push_dot
  edge key_push_dot -> @pair_pat.covalue
  ; scope flows into value pattern then back out
  edge @value_pat.before_scope -> @pair_pat.before_scope
  edge @pair_pat.after_scope -> @value_pat.after_scope

  edge @pair_pat.new_bindings -> @value_pat.new_bindings
}

(pair_pattern
  key:(property_identifier)@key)@_pair_pattern {

    attr (@key.push) node_reference = @key
}

(pair_pattern
  key:(string)@key)@_pair_pattern {

    attr (@key.push) symbol_reference = (replace (source-text @key) "\"" ""), source_node = @key
}

; LATER-TODO the left pattern has to be a name, it cant be another pattern
; object entry assignment patterns
(object_assignment_pattern
  left:(_)@left_pat
  right:(_)@right_expr)@object_assignment_pat {

  ; scope flows both THROUGH and AROUND the RHS, because it's a
  ; by-passable default not a guaranteed value

  ; here we go around
  edge @left_pat.before_scope -> @object_assignment_pat.before_scope

  ; and here we go through
  edge @right_expr.before_scope -> @object_assignment_pat.before_scope
  edge @left_pat.before_scope -> @right_expr.after_scope

  ; and in either case we come out the LHS
  edge @object_assignment_pat.after_scope -> @left_pat.after_scope

  ; covalues flow both in from the outside and also from the right expression
  edge @left_pat.covalue -> @object_assignment_pat.covalue
  edge @left_pat.covalue -> @right_expr.value

  edge @object_assignment_pat.new_bindings -> @left_pat.new_bindings

}

(shorthand_property_identifier_pattern)@shorthand_prop_pat {
  node pat_pop
  node pat_push
  node pat_push_dot

  edge @shorthand_prop_pat.after_scope -> @shorthand_prop_pat.before_scope

  attr (pat_push) node_reference = @shorthand_prop_pat
  attr (pat_push_dot) push_symbol = "GUARD:MEMBER"
  attr (pat_pop) node_definition = @shorthand_prop_pat
  edge pat_pop -> pat_push
  edge pat_push -> pat_push_dot
  edge pat_push_dot -> @shorthand_prop_pat.covalue
  edge @shorthand_prop_pat.after_scope -> pat_pop

  edge @shorthand_prop_pat.new_bindings -> pat_pop
}


;; #### Array Patterns

(array_pattern (_)* @pats)@array_pat {
  if (is-empty @pats) {
    edge @array_pat.after_scope -> @array_pat.before_scope
  }
}

; scope propagation through array patterns, first element
(array_pattern
  .
  (_)@first_el_pat)@array_pat {

  ; scope flows into the first element
  edge @first_el_pat.before_scope -> @array_pat.before_scope
}

; scope propagation through array patterns, between element
(array_pattern
  (_)@left_el_pat
  .
  (_)@right_el_pat) {

  ; scope flows from left to right
  edge @right_el_pat.before_scope -> @left_el_pat.after_scope
}

; scope propagation through array patterns, last element
(array_pattern
  (_)@last_el_pat
  .)@array_pat {

  ; scope flow out from the last element
  edge @array_pat.after_scope -> @last_el_pat.after_scope
}

; array pattern
(array_pattern)@array_pat {
  node @array_pat.element_index_push_dot
  edge @array_pat.element_index_push_dot -> @array_pat.covalue
  attr (@array_pat.element_index_push_dot) push_symbol = "GUARD:MEMBER"
}

; array pattern elements
(array_pattern (_)@element_pat)@array_pat {

  node element_pat_element_index_push

  attr (element_pat_element_index_push) push_symbol = (named-child-index @element_pat)
  edge @element_pat.covalue -> element_pat_element_index_push
  edge element_pat_element_index_push -> @array_pat.element_index_push_dot

  edge @array_pat.new_bindings -> @element_pat.new_bindings
}


;; #### Assignment Patterns

; scope propagation through assignment patterns
(assignment_pattern
  left:(_)@left_pat
  right:(_)@right_expr)@assignment_pat {

  ; scope flows both THROUGH and AROUND the RHS, because it's a
  ; by-passable default not a guaranteed value

  ; here we go around
  edge @left_pat.before_scope -> @assignment_pat.before_scope

  ; and here we go through
  edge @right_expr.before_scope -> @assignment_pat.before_scope
  edge @left_pat.before_scope -> @right_expr.after_scope

  ; the pattern's covalue is the whole thing's, and also the RHS
  edge @left_pat.covalue -> @assignment_pat.covalue
  edge @left_pat.covalue -> @right_expr.value

  ; and in either case we come out the LHS
  edge @assignment_pat.after_scope -> @left_pat.after_scope

  edge @assignment_pat.new_bindings -> @left_pat.new_bindings
}


;; #### Rest Patterns

(rest_pattern (_)@name)@rest_pat {
  node rest_pat_pop
  ; scope flows through, binding via a pop edge that goes to an unknown value

  attr (rest_pat_pop) node_definition = @name
  edge @rest_pat.after_scope -> @rest_pat.before_scope
  edge @rest_pat.after_scope -> rest_pat_pop
}










;; ███████ ██████  ███████  ██████ ██  █████  ██      
;; ██      ██   ██ ██      ██      ██ ██   ██ ██      
;; ███████ ██████  █████   ██      ██ ███████ ██      
;;      ██ ██      ██      ██      ██ ██   ██ ██      
;; ███████ ██      ███████  ██████ ██ ██   ██ ███████

;; ## Special Cases
;;
;; There are a number of annoying features that libraries make use of to
;; effectively add features to JavaScript. While they don't technically change
;; the language in any way, they're broad design patterns that are meant to be
;; used *as if* these things were more language level than not. These often make
;; it hard to do analysis without actually running code, and so instead, we
;; define some special case queries that treat these techniques as if they were
;; indeed core features of the language.
;;
;; ### Extend
;;
;; The extend method is a mass assignment of values to keys on objects and gets
;; used a bunch for building module export objects. We special case it here so
;; that we can do lookup on it because the extend method itself is dependent on
;; state and mutability, and has no good analytical explanation within the
;; Stack Graph formalism.
;;
;; Since we can't extend the actual value, but only the syntactic references to
;; it in the SG formalism, we treat extend as a kind of shadowing binder,
;; similar to how we treat `+=` or `*=`.

(
  (call_expression
    function: (member_expression
      object: (identifier)@object
      property: (_)@_extend)
    arguments: (arguments (object)@new_fields))@call_expr
  (#eq? @_extend "extend")
) {

  node object_pop
  attr (object_pop) node_definition = @object
  edge @call_expr.after_scope -> object_pop
  edge object_pop -> @new_fields.value

}

;; ### CommonJS-style Exports

;; CommonJS introduced an export style for pre-ES6 JavaScript that permitted
;; modules to export functions using an exports object bound to a top-level
;; variable `exports`. For instance, to export something as `foo`, we would do:

;; ``````javascript
;; exports.foo = 1;
;; ``````

;; If we then imported with `require`, the exports object would have `foo` as
;; a field. Alternatively, we can also specify the entire export object, using

;; ``````javascript
;; module.exports = my_exported_object;
;; ``````

(
  (assignment_expression
    left: [
      ( ; exports.foo = ...
        (member_expression
          object:(_)@exports
          property:(_)@property)
        (#eq? @exports "exports")
      )
      ( ; module.exports.foo = ...
        (member_expression
          object:(member_expression
            object:(_)@_module
            property:(_)@exports)
          property:(_)@property)
        (#eq? @_module "module")
        (#eq? @exports "exports")
      )
    ]
    right: (_)@right)@assignment_expr

) {

  node pop_default_guard
  node pop_dot
  node @assignment_expr.pop_name

  attr (pop_default_guard) symbol_definition = "GUARD:DEFAULT", source_node = @exports
  edge @assignment_expr.exports -> pop_default_guard

  attr (pop_dot) pop_symbol = "GUARD:MEMBER"
  edge pop_default_guard -> pop_dot

  attr (@assignment_expr.pop_name) node_definition = @property
  attr (@assignment_expr.pop_name) definiens_node = @assignment_expr
  edge pop_dot -> @assignment_expr.pop_name
  edge @assignment_expr.pop_name -> @right.value

  ;; For ES6 interoperability, expose members as named exports
  edge @assignment_expr.exports -> @assignment_expr.pop_name

  node detour_push
  node @assignment_expr.detour_pop

  scan FILE_PATH {

    "^(.+/)?([^/]+)/index\.js$" {
      let module_name = $2
      attr (detour_push) push_symbol = module_name
      attr (@assignment_expr.detour_pop) symbol_definition = module_name, source_node = @assignment_expr, definiens_node = @assignment_expr
      edge pop_default_guard -> detour_push
      edge detour_push -> @assignment_expr.detour_pop
      edge @assignment_expr.detour_pop -> @right.value
    }

    "^(.+/)?([^/]+)\.js$" {
      let module_name = $2
      attr (detour_push) push_symbol = module_name
      attr (@assignment_expr.detour_pop) symbol_definition = module_name, source_node = @assignment_expr, definiens_node = @assignment_expr
      edge pop_default_guard -> detour_push
      edge detour_push -> @assignment_expr.detour_pop
      edge @assignment_expr.detour_pop -> @right.value
    }

  }

  node default_detour_push
  node @assignment_expr.default_detour_pop

  attr (default_detour_push) push_symbol = "default"
  attr (@assignment_expr.default_detour_pop) symbol_definition = "default", source_node = @assignment_expr, definiens_node = @assignment_expr
  edge pop_default_guard -> default_detour_push
  edge default_detour_push -> @assignment_expr.default_detour_pop
  edge @assignment_expr.default_detour_pop -> @right.value

}

(
  (assignment_expression
    left: (member_expression
      object:(_)@_module
      property:(_)@exports)
    right: (_)@right)@assignment_expr
  (#eq? @_module "module")
  (#eq? @exports "exports")
) {

  node @assignment_expr.pop_default_guard
  node pop_dot

  attr (@assignment_expr.pop_default_guard) symbol_definition = "GUARD:DEFAULT", source_node = @exports
  attr (@assignment_expr.pop_default_guard) definiens_node = @assignment_expr
  edge @assignment_expr.exports -> @assignment_expr.pop_default_guard
  edge @assignment_expr.pop_default_guard -> @right.value

  ;; For ES6 interoperability, expose members as named exports
  attr (pop_dot) pop_symbol = "GUARD:MEMBER"
  edge @assignment_expr.exports -> pop_dot
  edge pop_dot -> @right.value

  node detour_push
  node @assignment_expr.detour_pop

  scan FILE_PATH {

    "^(.+/)?([^/]+)/index\.js$" {
      let module_name = $2
      attr (detour_push) push_symbol = module_name
      attr (@assignment_expr.detour_pop) symbol_definition = module_name, source_node = @assignment_expr, definiens_node = @assignment_expr
      edge @assignment_expr.pop_default_guard -> detour_push
      edge detour_push -> @assignment_expr.detour_pop
      edge @assignment_expr.detour_pop -> @right.value
    }

    "^(.+/)?([^/]+)\.js$" {
      let module_name = $2
      attr (detour_push) push_symbol = module_name
      attr (@assignment_expr.detour_pop) symbol_definition = module_name, source_node = @assignment_expr, definiens_node = @assignment_expr
      edge @assignment_expr.pop_default_guard -> detour_push
      edge detour_push -> @assignment_expr.detour_pop
      edge @assignment_expr.detour_pop -> @right.value
    }

  }

  node default_detour_push
  node @assignment_expr.default_detour_pop

  attr (default_detour_push) push_symbol = "default"
  attr (@assignment_expr.default_detour_pop) symbol_definition = "default", source_node = @assignment_expr, definiens_node = @assignment_expr
  edge @assignment_expr.pop_default_guard -> default_detour_push
  edge default_detour_push -> @assignment_expr.default_detour_pop
  edge @assignment_expr.default_detour_pop -> @right.value

}

;; ### CommonJS-style Imports

;; Similar to exports, CommonJS also defines a way to do imports. In general,
;; these look like `require(expr)`, but in practice the expression is a string
;; constant, which is the only case we handle.

(
  (call_expression
    function:(identifier)@_require
    arguments:(arguments (string)@source))@call_expr
  (#eq? @_require "require")
) {

  node default_guard_push

  attr (default_guard_push) symbol_reference = "GUARD:DEFAULT", source_node = @source
  edge @call_expr.value -> default_guard_push
  edge default_guard_push -> @source.exports

}

;; ### Dynamic Imports

;; Both ES6 and CommonJS modules can be imported using an import function.
;; In general, these look like `import(expr)`, but in practice the expression
;; is a string constant, which is the only case we handle.
;;
;; The return value of the import function is an object whose properties are
;; the exports of the module. The default export is assigned to the `default`
;; property.
;;
;; The import function is async and returns a promise. Since we do not support
;; async functions and promises, we only support the case where the function
;; is called as `await import(...)`.

(
  (await_expression
    (call_expression
      function:(_)@_import
      arguments:(arguments (string)@source))
  )@await_expr
  (#eq? @_import "import")
) {

  node pop_dot
  node pop_default
  node push_guard_default

  attr (pop_dot) pop_symbol = "GUARD:MEMBER"
  edge @await_expr.value -> pop_dot
  edge pop_dot -> @source.exports

  attr (pop_default) pop_symbol = "default"
  edge pop_dot -> pop_default

  attr (push_guard_default) symbol_reference = "GUARD:DEFAULT", source_node = @source
  edge pop_default -> push_guard_default
  edge push_guard_default -> @source.exports

}

;; ### ES6 and CommonJS interoperability

;; Nodes supports some interoperability between ES6 and CommonJS modules.
;;
;; A CommonJS module can be imported in an ES6 module:
;;
;;  - `import foo from "cjs_module"` binds `foo` to the value of `module.exports`.
;;  - `import { foo } from "cjs_module"` binds `foo` to the value of `module.exports.foo`.
;;  - `import("cjs_module")` returns (a promise to) an object with
;;     - property `default` bound to the value of `module.exports`, and
;;     - other properties bound to the value of those properties in `module.exports`
;;       (e.g., `foo` binds `module.exports.foo`).
;;
;; A ES6 module can be import in an CommonJS module:
;;
;;  - `import("es6_module")` returns (a promise to) an object with
;;    - property default bound to the value of `export default`, and
;;    - other properties bound to named exports (e.g., `foo` binds `export { foo }`).
;;  - `require("es6_module")` is not supported.
;;
;; References:
;;
;;  - https://nodejs.org/api/esm.html#interoperability-with-commonjs
;;







;; ██████  ███████ ███████ ██ ███    ██ ██ ███████ ███    ██ ███████ 
;; ██   ██ ██      ██      ██ ████   ██ ██ ██      ████   ██ ██      
;; ██   ██ █████   █████   ██ ██ ██  ██ ██ █████   ██ ██  ██ ███████ 
;; ██   ██ ██      ██      ██ ██  ██ ██ ██ ██      ██  ██ ██      ██ 
;; ██████  ███████ ██      ██ ██   ████ ██ ███████ ██   ████ ███████

;; ## Definiens Rules

;; These rules explain how defined names relate to syntactic definitions
;; of various forms. Sometimes that's declared function names mapping to
;; the entire function declaration, and sometimes that's variables in an
;; assignment being mapped to the thing it's assigned to. The purpose of
;; these is not to augment stack graphs, per se, but to permit syntax
;; oriented tools that need to know about approximate call graphs, etc.

;; ### Basic Definiens Rules

;; These rules are all about declarations and terms that have the names
;; directly in them.

(
  (class_declaration
    name:(_)@name
    body:(class_body
      member:(method_definition
        name:(_)@_method_name)@constructor
    )
  )

  (#eq? @_method_name "constructor")
) {

  attr (@name.pop) definiens_node = @constructor

}

(function_declaration
  name:(_)@name
  parameters:(_)@_call_sig
  body:(_)@_body)@fun_decl {

  attr (@name.pop) definiens_node = @fun_decl

}

(generator_function_declaration
  name:(_)@name
  parameters:(_)@_call_sig
  body:(_)@_body)@fun_decl {

  attr (@name.pop) definiens_node = @fun_decl

}

(method_definition
  name:(_)@name
  parameters:(_)@_call_sig
  body:(_)@_body)@method_def {

  attr (@name.pop) definiens_node = @method_def

}

(function_expression
  name:(_)@name
  parameters:(_)@_call_sig)@fun {

  attr (@name.pop) definiens_node = @fun

}

(generator_function
  name:(_)@name
  parameters:(_)@_call_sig)@fun {

  attr (@name.pop) definiens_node = @fun

}

(
  (class
    name:(_)@name
    body:(class_body
      member:(method_definition
        name:(_)@_method_name)@constructor
    )
  )

  (#eq? @_method_name "constructor")
) {

  attr (@name.pop) definiens_node = @constructor

}

;; ### Assignment-like Rules

;; These rules make up for the fact that JavaScript permits way more
;; kinds of definitions/declarations than just those that show up in
;; syntactic declarations of the thing in question.

;; These rules are currently way less precise than we would like but
;; do provide at least some information about definiens for these
;; kinds of definitions.

(assignment_expression
  left: (identifier)@left
  right: (_))@assignment_expr {

  attr (@left.pop) definiens_node = @assignment_expr

}

(
  (assignment_expression
    left: (member_expression
      object:(identifier)@_object
      property:(_)@left
    )
    right: (_))@assignment_expr
  (#not-eq? @_object "module")
  (#not-eq? @left "exports")
) {

  node left_definiens_hook
  node left_ignore_guard

  attr (left_ignore_guard) pop_symbol = "GUARD:GANDALF"
  attr (left_definiens_hook) node_definition = @left
  attr (left_definiens_hook) definiens_node = @assignment_expr
  edge @assignment_expr.pkg_pop -> left_ignore_guard
  edge left_ignore_guard -> left_definiens_hook
}

(
  (assignment_expression
    left: (member_expression
      object:(identifier)@_object
      property:(_)@left
    )
    right: (_))@assignment_expr
  (#eq? @_object "module")
  (#eq? @left "exports")
) {

  node left_definiens_hook
  node left_ignore_guard

  attr (left_ignore_guard) pop_symbol = "GUARD:GANDALF"
  attr (left_definiens_hook) node_definition = @left
  attr (left_definiens_hook) definiens_node = @assignment_expr
  edge @assignment_expr.pkg_pop -> left_ignore_guard
  edge left_ignore_guard -> left_definiens_hook

}

(variable_declaration
  (variable_declarator
    name:(identifier)@name))@decl {

  attr (@name.pop) definiens_node = @decl

}

(lexical_declaration
  (variable_declarator
    name:(identifier)@name))@decl {

  attr (@name.pop) definiens_node = @decl

}

[
  (variable_declaration
    (variable_declarator
      name:(identifier)@name
      value: [
        (function_expression)
        (generator_function)
        (arrow_function)
      ]))
  (lexical_declaration
    (variable_declarator
      name:(identifier)@name
      value: [
        (function_expression)
        (generator_function)
        (arrow_function)
      ]))
  (assignment_expression
    left: [
      (identifier)@name
    ; (member_expression property:(_)@name) ; FIXME member expressions are references and have no .pop
    ]
    right: [
      (function_expression)
      (generator_function)
      (arrow_function)
    ])
] {

  attr (@name.pop) syntax_type = "function"

}

(
  (assignment_expression
    left: [
      ( ; exports.foo = ...
        (member_expression
          object:(_)@_exports
          property:(_)@_property)
        (#eq? @_exports "exports")
      )
      ( ; module.exports.foo = ...
        (member_expression
          object:(member_expression
            object:(_)@_module
            property:(_)@_exports)
          property:(_)@_property)
        (#eq? @_module "module")
        (#eq? @_exports "exports")
      )
    ]
    right: [
      (function_expression)
      (generator_function)
      (arrow_function)
    ])@assignment_expr

) {

  attr (@assignment_expr.pop_name) syntax_type = "function"
  attr (@assignment_expr.detour_pop) syntax_type = "function"
  attr (@assignment_expr.default_detour_pop) syntax_type = "function"

}

(
  (assignment_expression
    left: (member_expression
      object:(_)@_module
      property:(_)@_exports)
    right: [
      (function_expression)
      (generator_function)
      (arrow_function)
    ])@assignment_expr
  (#eq? @_module "module")
  (#eq? @_exports "exports")
) {

  attr (@assignment_expr.pop_default_guard) syntax_type = "function"
  attr (@assignment_expr.detour_pop) syntax_type = "function"
  attr (@assignment_expr.default_detour_pop) syntax_type = "function"

}

(export_statement "default"
  value:[
      (function_expression)
      (generator_function)
      (arrow_function)
    ])@export_stmt {

  attr (@export_stmt.pop_guard_default) syntax_type = "function"
  attr (@export_stmt.detour_pop) syntax_type = "function"
  attr (@export_stmt.default_detour_pop) syntax_type = "function"

}

(pair
  key: (_)@name
  value: [
    (function_expression)
    (generator_function)
    (arrow_function)
  ]) {

  attr (@name.definiens_hook) syntax_type = "function"

}


[
  (variable_declaration
    (variable_declarator
      name:(identifier)@name
      value: (class)))
  (lexical_declaration
    (variable_declarator
      name:(identifier)@name
      value: (class)))
  (assignment_expression
    left: [
      (identifier)@name
    ; (member_expression property:(_)@name) ; FIXME member expressions are references and have no .pop
    ]
    right: (class))
] {

  attr (@name.pop) syntax_type = "class"

}

(
  (assignment_expression
    left: [
      ( ; exports.foo = ...
        (member_expression
          object:(_)@_exports
          property:(_)@_property)
        (#eq? @_exports "exports")
      )
      ( ; module.exports.foo = ...
        (member_expression
          object:(member_expression
            object:(_)@_module
            property:(_)@_exports)
          property:(_)@_property)
        (#eq? @_module "module")
        (#eq? @_exports "exports")
      )
    ]
    right: (class))@assignment_expr

) {

  attr (@assignment_expr.pop_name) syntax_type = "class"
  attr (@assignment_expr.detour_pop) syntax_type = "class"
  attr (@assignment_expr.default_detour_pop) syntax_type = "class"

}

(
  (assignment_expression
    left: (member_expression
      object:(_)@_module
      property:(_)@_exports)
    right: (class))@assignment_expr
  (#eq? @_module "module")
  (#eq? @_exports "exports")
) {

  attr (@assignment_expr.pop_default_guard) syntax_type = "class"
  attr (@assignment_expr.detour_pop) syntax_type = "class"
  attr (@assignment_expr.default_detour_pop) syntax_type = "class"

}

(export_statement "default"
  value:(class))@export_stmt {

  attr (@export_stmt.pop_guard_default) syntax_type = "class"
  attr (@export_stmt.detour_pop) syntax_type = "class"
  attr (@export_stmt.default_detour_pop) syntax_type = "class"

}

(pair
  key: (_)@name
  value: (class)) {

  attr (@name.definiens_hook) syntax_type = "class"

}

(pair
  key: (_)@name
  value: (_))@pair_expr {

  node @name.definiens_hook
  node name_ignore_guard

  attr (name_ignore_guard) pop_symbol = "GUARD:GANDALF"
  attr (@name.definiens_hook) node_definition = @name
  attr (@name.definiens_hook) definiens_node = @pair_expr
  edge @pair_expr.pkg_pop -> name_ignore_guard
  edge name_ignore_guard -> @name.definiens_hook

}