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 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493 6494 6495 6496 6497 6498 6499 6500 6501 6502 6503 6504 6505 6506 6507 6508 6509 6510 6511 6512 6513 6514 6515 6516 6517 6518 6519 6520 6521 6522 6523 6524 6525 6526 6527 6528 6529 6530 6531 6532 6533 6534 6535 6536 6537 6538 6539 6540 6541 6542 6543 6544 6545 6546 6547 6548 6549 6550 6551 6552 6553 6554 6555 6556 6557 6558 6559 6560 6561 6562 6563 6564 6565 6566 6567 6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603 6604 6605 6606 6607 6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624 6625 6626 6627 6628 6629 6630 6631 6632 6633 6634 6635 6636 6637 6638 6639 6640 6641 6642 6643 6644 6645 6646 6647 6648 6649 6650 6651 6652 6653 6654 6655 6656 6657 6658 6659 6660 6661 6662 6663 6664 6665 6666 6667 6668 6669 6670 6671 6672 6673 6674 6675 6676 6677 6678 6679 6680 6681 6682 6683 6684 6685 6686 6687 6688 6689 6690 6691 6692 6693 6694 6695 6696 6697 6698 6699 6700 6701 6702 6703 6704 6705 6706 6707 6708 6709 6710 6711 6712 6713 6714 6715 6716 6717 6718 6719 6720 6721 6722 6723 6724 6725 6726 6727 6728 6729 6730 6731 6732 6733 6734 6735 6736 6737 6738 6739 6740 6741 6742 6743 6744 6745 6746 6747 6748 6749 6750 6751 6752 6753 6754 6755 6756 6757 6758 6759 6760 6761 6762 6763 6764 6765 6766 6767 6768 6769 6770 6771 6772 6773 6774 6775 6776 6777 6778 6779 6780 6781 6782 6783 6784 6785 6786 6787 6788 6789 6790 6791 6792 6793 6794 6795 6796 6797 6798 6799 6800 6801 6802 6803 6804 6805 6806 6807 6808 6809 6810 6811 6812 6813 6814 6815 6816 6817 6818 6819 6820 6821 6822 6823 6824 6825 6826 6827 6828 6829 6830 6831 6832 6833 6834 6835 6836 6837 6838 6839 6840 6841 6842 6843 6844 6845 6846 6847 6848 6849 6850 6851 6852 6853 6854 6855 6856 6857 6858 6859 6860 6861 6862 6863 6864 6865 6866 6867 6868 6869 6870 6871 6872 6873 6874 6875 6876 6877 6878 6879 6880 6881 6882 6883 6884 6885 6886 6887 6888 6889 6890 6891 6892 6893 6894 6895 6896 6897 6898 6899 6900 6901 6902 6903 6904 6905 6906 6907 6908 6909 6910 6911 6912 6913 6914 6915 6916 6917 6918 6919 6920 6921 6922 6923 6924 6925 6926 6927 6928 6929 6930 6931 6932 6933 6934 6935 6936 6937 6938 6939 6940 6941 6942 6943 6944 6945 6946 6947 6948 6949 6950 6951 6952 6953 6954 6955 6956 6957 6958 6959 6960 6961 6962 6963 6964 6965 6966 6967 6968 6969 6970 6971 6972 6973 6974 6975 6976 6977 6978 6979 6980 6981 6982 6983 6984 6985 6986 6987 6988 6989 6990 6991 6992 6993 6994 6995 6996 6997 6998 6999 7000 7001 7002 7003 7004 7005 7006 7007 7008 7009 7010 7011 7012 7013 7014 7015 7016 7017 7018 7019 7020 7021 7022 7023 7024 7025 7026 7027 7028 7029 7030 7031 7032 7033 7034 7035 7036 7037 7038 7039 7040 7041 7042 7043 7044 7045 7046 7047 7048 7049 7050 7051 7052 7053 7054 7055 7056 7057 7058 7059 7060 7061 7062 7063 7064 7065 7066 7067 7068 7069 7070 7071 7072 7073 7074 7075 7076 7077 7078 7079 7080 7081 7082 7083 7084 7085 7086 7087 7088 7089 7090 7091 7092 7093 7094 7095 7096 7097 7098 7099 7100 7101 7102 7103 7104 7105 7106 7107 7108 7109 7110 7111 7112 7113 7114 7115 7116 7117 7118 7119 7120 7121 7122 7123 7124 7125 7126 7127 7128 7129 7130 7131 7132 7133 7134 7135 7136 7137 7138 7139 7140 7141 7142 7143 7144 7145 7146 7147 7148 7149 7150 7151 7152 7153 7154 7155 7156 7157 7158 7159 7160 7161 7162 7163 7164 7165 7166 7167 7168 7169 7170 7171 7172 7173 7174 7175 7176 7177 7178 7179 7180 7181 7182 7183 7184 7185 7186 7187 7188 7189 7190 7191 7192 7193 7194 7195 7196 7197 7198 7199 7200 7201 7202 7203 7204 7205 7206 7207 7208 7209 7210 7211 7212 7213 7214 7215 7216 7217 7218 7219 7220 7221 7222 7223 7224 7225 7226 7227 7228 7229 7230 7231 7232 7233 7234 7235 7236 7237 7238 7239 7240 7241 7242 7243 7244 7245 7246 7247 7248 7249 7250 7251 7252 7253 7254 7255 7256 7257 7258 7259 7260 7261 7262 7263 7264 7265 7266 7267 7268 7269 7270 7271 7272 7273 7274 7275 7276 7277 7278 7279 7280 7281 7282 7283 7284 7285 7286 7287 7288 7289 7290 7291 7292 7293 7294 7295 7296 7297 7298 7299 7300 7301 7302 7303 7304 7305 7306 7307 7308 7309 7310 7311 7312 7313 7314 7315 7316 7317 7318 7319 7320 7321 7322 7323 7324 7325 7326 7327 7328 7329 7330 7331 7332 7333 7334 7335 7336 7337 7338 7339 7340 7341 7342 7343 7344 7345 7346 7347 7348 7349 7350 7351 7352 7353 7354 7355 7356 7357 7358 7359 7360 7361 7362 7363 7364 7365 7366 7367 7368 7369 7370 7371 7372 7373 7374 7375 7376 7377 7378 7379 7380 7381 7382 7383 7384 7385 7386 7387 7388 7389 7390 7391 7392 7393 7394 7395 7396 7397 7398 7399 7400 7401 7402 7403 7404 7405 7406 7407 7408 7409 7410 7411 7412 7413 7414 7415 7416 7417 7418 7419 7420 7421 7422 7423 7424 7425 7426 7427 7428 7429 7430 7431 7432 7433 7434 7435 7436 7437 7438 7439 7440 7441 7442 7443 7444 7445 7446 7447 7448 7449 7450 7451 7452 7453 7454 7455 7456 7457 7458 7459 7460 7461 7462 7463 7464 7465 7466 7467 7468 7469 7470 7471 7472 7473 7474 7475 7476 7477 7478 7479 7480 7481 7482 7483 7484 7485 7486 7487 7488 7489 7490 7491 7492 7493 7494 7495 7496 7497 7498 7499 7500 7501 7502 7503 7504 7505 7506 7507 7508 7509 7510 7511 7512 7513 7514 7515 7516 7517 7518 7519 7520 7521 7522 7523 7524 7525 7526 7527 7528 7529 7530 7531 7532 7533 7534 7535 7536 7537 7538 7539 7540 7541 7542 7543 7544 7545 7546 7547 7548 7549 7550 7551 7552 7553 7554 7555 7556 7557 7558 7559 7560 7561 7562 7563 7564 7565 7566 7567 7568 7569 7570 7571 7572 7573 7574 7575 7576 7577 7578 7579 7580 7581 7582 7583 7584 7585 7586 7587 7588 7589 7590 7591 7592 7593 7594 7595 7596 7597 7598 7599 7600 7601 7602 7603 7604 7605 7606 7607 7608 7609 7610 7611 7612 7613 7614 7615 7616 7617 7618 7619 7620 7621 7622 7623 7624 7625 7626 7627 7628 7629 7630 7631 7632 7633 7634 7635 7636 7637 7638 7639 7640 7641 7642 7643 7644 7645 7646 7647 7648 7649 7650 7651 7652 7653 7654 7655 7656 7657 7658 7659 7660 7661 7662 7663 7664 7665 7666 7667 7668 7669 7670 7671 7672 7673 7674 7675 7676 7677 7678 7679 7680 7681 7682 7683 7684 7685 7686 7687 7688 7689 7690 7691 7692 7693 7694 7695 7696 7697 7698 7699 7700 7701 7702 7703 7704 7705 7706 7707 7708 7709 7710 7711 7712 7713 7714 7715 7716 7717 7718 7719 7720 7721 7722 7723 7724 7725 7726 7727 7728 7729 7730 7731 7732 7733 7734 7735 7736 7737 7738 7739 7740 7741 7742 7743 7744 7745 7746 7747 7748 7749 7750 7751 7752 7753 7754 7755 7756 7757 7758 7759 7760 7761 7762 7763 7764 7765 7766 7767 7768 7769 7770 7771 7772 7773 7774 7775 7776 7777 7778 7779 7780 7781 7782 7783 7784 7785 7786 7787 7788 7789 7790 7791 7792 7793 7794 7795 7796 7797 7798 7799 7800 7801 7802 7803 7804 7805 7806 7807 7808 7809 7810 7811 7812 7813 7814 7815 7816 7817 7818 7819 7820 7821 7822 7823 7824 7825 7826 7827 7828 7829 7830 7831 7832 7833 7834 7835 7836 7837 7838 7839 7840 7841 7842 7843 7844 7845 7846 7847 7848 7849 7850 7851 7852 7853 7854 7855 7856 7857 7858 7859 7860 7861 7862 7863 7864 7865 7866 7867 7868 7869 7870 7871 7872 7873 7874 7875 7876 7877 7878 7879 7880 7881 7882 7883 7884 7885 7886 7887 7888 7889 7890 7891 7892 7893 7894 7895 7896 7897 7898 7899 7900 7901 7902 7903 7904 7905 7906 7907 7908 7909 7910 7911 7912 7913 7914 7915 7916 7917 7918 7919 7920 7921 7922 7923 7924 7925 7926 7927 7928 7929 7930 7931 7932 7933 7934 7935 7936 7937 7938 7939 7940 7941 7942 7943 7944 7945 7946 7947 7948 7949 7950 7951 7952 7953 7954 7955 7956 7957 7958 7959 7960 7961 7962 7963 7964 7965 7966 7967 7968 7969 7970 7971 7972 7973 7974 7975 7976 7977 7978 7979 7980 7981 7982 7983 7984 7985 7986 7987 7988 7989 7990 7991 7992 7993 7994 7995 7996 7997 7998 7999 8000 8001 8002 8003 8004 8005 8006 8007 8008 8009 8010 8011 8012 8013 8014 8015 8016 8017 8018 8019 8020 8021 8022 8023 8024 8025 8026 8027 8028 8029 8030 8031 8032 8033 8034 8035 8036 8037 8038 8039 8040 8041 8042 8043 8044 8045 8046 8047 8048 8049 8050 8051 8052 8053 8054 8055 8056 8057 8058 8059 8060 8061 8062 8063 8064 8065 8066 8067 8068 8069 8070 8071 8072 8073 8074 8075 8076 8077 8078 8079 8080 8081 8082 8083 8084 8085 8086 8087 8088 8089 8090 8091 8092 8093 8094 8095 8096 8097 8098 8099 8100 8101 8102 8103 8104 8105 8106 8107 8108 8109 8110 8111 8112 8113 8114 8115 8116 8117 8118 8119 8120 8121 8122 8123 8124 8125 8126 8127 8128 8129 8130 8131 8132 8133 8134 8135 8136 8137 8138 8139 8140 8141 8142 8143 8144 8145 8146 8147 8148 8149 8150 8151 8152 8153 8154 8155 8156 8157 8158 8159 8160 8161 8162 8163 8164 8165 8166 8167 8168 8169 8170 8171 8172 8173 8174 8175 8176 8177 8178 8179 8180 8181 8182 8183 8184 8185 8186 8187 8188 8189 8190 8191 8192 8193 8194 8195 8196 8197 8198 8199 8200 8201 8202 8203 8204 8205 8206 8207 8208 8209 8210 8211 8212 8213 8214 8215 8216 8217 8218 8219 8220 8221 8222 8223 8224 8225 8226 8227 8228 8229 8230 8231 8232 8233 8234 8235 8236 8237 8238 8239 8240 8241 8242 8243 8244 8245 8246 8247 8248 8249 8250 8251 8252 8253 8254 8255 8256 8257 8258 8259 8260 8261 8262 8263 8264 8265 8266 8267 8268 8269 8270 8271 8272 8273 8274 8275 8276 8277 8278 8279 8280 8281 8282 8283 8284 8285 8286 8287 8288 8289 8290 8291 8292 8293 8294 8295 8296 8297 8298 8299 8300 8301 8302 8303 8304 8305 8306 8307 8308 8309 8310 8311 8312 8313 8314 8315 8316 8317 8318 8319 8320 8321 8322 8323 8324 8325 8326 8327 8328 8329 8330 8331 8332 8333 8334 8335 8336 8337 8338 8339 8340 8341 8342 8343 8344 8345 8346 8347 8348 8349 8350 8351 8352 8353 8354 8355 8356 8357 8358 8359 8360 8361 8362 8363 8364 8365 8366 8367 8368 8369 8370 8371 8372 8373 8374 8375 8376 8377 8378 8379 8380 8381 8382 8383 8384 8385 8386 8387 8388 8389 8390 8391 8392 8393 8394 8395 8396 8397 8398 8399 8400 8401 8402 8403 8404 8405 8406 8407 8408 8409 8410 8411 8412 8413 8414 8415 8416 8417 8418 8419 8420 8421 8422 8423 8424 8425 8426 8427 8428 8429 8430 8431 8432 8433 8434 8435 8436 8437 8438 8439 8440 8441 8442 8443 8444 8445 8446 8447 8448 8449 8450 8451 8452 8453 8454 8455 8456 8457 8458 8459 8460 8461 8462 8463 8464 8465 8466 8467 8468 8469 8470 8471 8472 8473 8474 8475 8476 8477 8478 8479 8480 8481 8482 8483 8484 8485 8486 8487 8488 8489 8490 8491 8492 8493 8494 8495 8496 8497 8498 8499 8500 8501 8502 8503 8504 8505 8506 8507 8508 8509 8510 8511 8512 8513 8514 8515 8516 8517 8518 8519 8520 8521 8522 8523 8524 8525 8526 8527 8528 8529 8530 8531 8532 8533 8534 8535 8536 8537 8538 8539 8540 8541 8542 8543 8544 8545 8546 8547 8548 8549 8550 8551 8552 8553 8554 8555 8556 8557 8558 8559 8560 8561 8562 8563 8564 8565 8566 8567 8568 8569 8570 8571 8572 8573 8574 8575 8576 8577 8578 8579 8580 8581 8582 8583 8584 8585 8586 8587 8588 8589 8590 8591 8592 8593 8594 8595 8596 8597 8598 8599 8600 8601 8602 8603 8604 8605 8606 8607 8608 8609 8610 8611 8612 8613 8614 8615 8616 8617 8618 8619 8620 8621 8622 8623 8624 8625 8626 8627 8628 8629 8630 8631 8632 8633 8634 8635 8636 8637 8638 8639 8640 8641 8642 8643 8644 8645 8646 8647 8648 8649 8650 8651 8652 8653 8654 8655 8656 8657 8658 8659 8660 8661 8662 8663 8664 8665 8666 8667 8668 8669 8670 8671 8672 8673 8674 8675 8676 8677 8678 8679 8680 8681 8682 8683 8684 8685 8686 8687 8688 8689 8690 8691 8692 8693 8694 8695 8696 8697 8698 8699 8700 8701 8702 8703 8704 8705 8706 8707 8708 8709 8710 8711 8712 8713 8714 8715 8716 8717 8718 8719 8720 8721 8722 8723 8724 8725 8726 8727 8728 8729 8730 8731 8732 8733 8734 8735 8736 8737 8738 8739 8740 8741 8742 8743 8744 8745 8746 8747 8748 8749 8750 8751 8752 8753 8754 8755 8756 8757 8758 8759 8760 8761 8762 8763 8764 8765 8766 8767 8768 8769 8770 8771 8772 8773 8774 8775 8776 8777 8778 8779 8780 8781 8782 8783 8784 8785 8786 8787 8788 8789 8790 8791 8792 8793 8794 8795 8796 8797 8798 8799 8800 8801 8802 8803 8804 8805 8806 8807 8808 8809 8810 8811 8812 8813 8814 8815 8816 8817 8818 8819 8820 8821 8822 8823 8824 8825 8826 8827 8828 8829 8830 8831 8832 8833 8834 8835 8836 8837 8838 8839 8840 8841 8842 8843 8844 8845 8846 8847 8848 8849 8850 8851 8852 8853 8854 8855 8856 8857 8858 8859 8860 8861 8862 8863 8864 8865 8866 8867 8868 8869 8870 8871 8872 8873 8874 8875 8876 8877 8878 8879 8880 8881 8882 8883 8884 8885 8886 8887 8888 8889 8890 8891 8892 8893 8894 8895 8896 8897 8898 8899 8900 8901 8902 8903 8904 8905 8906 8907 8908 8909 8910 8911 8912 8913 8914 8915 8916 8917 8918 8919 8920 8921 8922 8923 8924 8925 8926 8927 8928 8929 8930 8931 8932 8933 8934 8935 8936 8937 8938 8939 8940 8941 8942 8943 8944 8945 8946 8947 8948 8949 8950 8951 8952 8953 8954 8955 8956 8957 8958 8959 8960 8961 8962 8963 8964 8965 8966 8967 8968 8969 8970 8971 8972 8973 8974 8975 8976 8977 8978 8979 8980 8981 8982 8983 8984 8985 8986 8987 8988 8989 8990 8991 8992 8993 8994 8995 8996 8997 8998 8999 9000 9001 9002 9003 9004 9005 9006 9007 9008 9009 9010 9011 9012 9013 9014 9015 9016 9017 9018 9019 9020 9021 9022 9023 9024 9025 9026 9027 9028 9029 9030 9031 9032 9033 9034 9035 9036 9037 9038 9039 9040 9041 9042 9043 9044 9045 9046 9047 9048 9049 9050 9051 9052 9053 9054 9055 9056 9057 9058 9059 9060 9061 9062 9063 9064 9065 9066 9067 9068 9069 9070 9071 9072 9073 9074 9075 9076 9077 9078 9079 9080 9081 9082 9083 9084 9085 9086 9087 9088 9089 9090 9091 9092 9093 9094 9095 9096 9097 9098 9099 9100 9101 9102 9103 9104 9105 9106 9107 9108 9109 9110 9111 9112 9113 9114 9115 9116 9117 9118 9119 9120 9121 9122 9123 9124 9125 9126 9127 9128 9129 9130 9131 9132 9133 9134 9135 9136 9137 9138 9139 9140 9141 9142 9143 9144 9145 9146 9147 9148 9149 9150 9151 9152 9153 9154 9155 9156 9157 9158 9159 9160 9161 9162 9163 9164 9165 9166 9167 9168 9169 9170 9171 9172 9173 9174 9175 9176 9177 9178 9179 9180 9181 9182 9183 9184 9185 9186 9187 9188 9189 9190 9191 9192 9193 9194 9195 9196 9197 9198 9199 9200 9201 9202 9203 9204 9205 9206 9207 9208 9209 9210 9211 9212 9213 9214 9215 9216 9217 9218 9219 9220 9221 9222 9223 9224 9225 9226 9227 9228 9229 9230 9231 9232 9233 9234 9235 9236 9237 9238 9239 9240 9241 9242 9243 9244 9245 9246 9247 9248 9249 9250 9251 9252 9253 9254 9255 9256 9257 9258 9259 9260 9261 9262 9263 9264 9265 9266 9267 9268 9269 9270 9271 9272 9273 9274 9275 9276 9277 9278 9279 9280 9281 9282 9283 9284 9285 9286 9287 9288 9289 9290 9291 9292 9293 9294 9295 9296 9297 9298 9299 9300 9301 9302 9303 9304 9305 9306 9307 9308 9309 9310 9311 9312 9313 9314 9315 9316 9317 9318 9319 9320 9321 9322 9323 9324 9325 9326 9327 9328 9329 9330 9331 9332 9333 9334 9335 9336 9337 9338 9339 9340 9341 9342 9343 9344 9345 9346 9347 9348 9349 9350 9351 9352 9353 9354 9355 9356 9357 9358 9359 9360 9361 9362 9363 9364 9365 9366 9367 9368 9369 9370 9371 9372 9373 9374 9375 9376 9377 9378 9379 9380 9381 9382 9383 9384 9385 9386 9387 9388 9389 9390 9391 9392 9393 9394 9395 9396 9397 9398 9399 9400 9401 9402 9403 9404 9405 9406 9407 9408 9409 9410 9411 9412 9413 9414 9415 9416 9417 9418 9419 9420 9421 9422 9423 9424 9425 9426 9427 9428 9429 9430 9431 9432 9433 9434 9435 9436 9437 9438 9439 9440 9441 9442 9443 9444 9445 9446 9447 9448 9449 9450 9451 9452 9453 9454 9455 9456 9457 9458 9459 9460 9461 9462 9463 9464 9465 9466 9467 9468 9469 9470 9471 9472 9473 9474 9475 9476 9477 9478 9479 9480 9481 9482 9483 9484 9485 9486 9487 9488 9489 9490 9491 9492 9493 9494 9495 9496 9497 9498 9499 9500 9501 9502 9503 9504 9505 9506 9507 9508 9509 9510 9511 9512 9513 9514 9515 9516 9517 9518 9519 9520 9521 9522 9523 9524 9525 9526 9527 9528 9529 9530 9531 9532 9533 9534 9535 9536 9537 9538 9539 9540 9541 9542 9543 9544 9545 9546 9547 9548 9549 9550 9551 9552 9553 9554 9555 9556 9557 9558 9559 9560 9561 9562 9563 9564 9565 9566 9567 9568 9569 9570 9571 9572 9573 9574 9575 9576 9577 9578 9579 9580 9581 9582 9583 9584 9585 9586 9587 9588 9589 9590 9591 9592 9593 9594 9595 9596 9597 9598 9599 9600 9601 9602 9603 9604 9605 9606 9607 9608 9609 9610 9611 9612 9613 9614 9615 9616 9617 9618 9619 9620 9621 9622 9623 9624 9625 9626 9627 9628 9629 9630 9631 9632 9633 9634 9635 9636 9637 9638 9639 9640 9641 9642 9643 9644 9645 9646 9647 9648 9649 9650 9651 9652 9653 9654 9655 9656 9657 9658 9659 9660 9661 9662 9663 9664 9665 9666 9667 9668 9669 9670 9671 9672 9673 9674 9675 9676 9677 9678 9679 9680 9681 9682 9683 9684 9685 9686 9687 9688 9689 9690 9691 9692 9693 9694 9695 9696 9697 9698 9699 9700 9701 9702 9703 9704 9705 9706 9707 9708 9709 9710 9711 9712 9713 9714 9715 9716 9717 9718 9719 9720 9721 9722 9723 9724 9725 9726 9727 9728 9729 9730 9731 9732 9733 9734 9735 9736 9737 9738 9739 9740 9741 9742 9743 9744 9745 9746 9747 9748 9749 9750 9751 9752 9753 9754 9755 9756 9757 9758 9759 9760 9761 9762 9763 9764 9765 9766 9767 9768 9769 9770 9771 9772 9773 9774 9775 9776 9777 9778 9779 9780 9781 9782 9783 9784 9785 9786 9787 9788 9789 9790 9791 9792 9793 9794 9795 9796 9797 9798 9799 9800 9801 9802 9803 9804 9805 9806 9807 9808 9809 9810 9811 9812 9813 9814 9815 9816 9817 9818 9819 9820 9821 9822 9823 9824 9825 9826 9827 9828 9829 9830 9831 9832 9833 9834 9835 9836 9837 9838 9839 9840 9841 9842 9843 9844 9845 9846 9847 9848 9849 9850 9851 9852 9853 9854 9855 9856 9857 9858 9859 9860 9861 9862 9863 9864 9865 9866 9867 9868 9869 9870 9871 9872 9873 9874 9875 9876 9877 9878 9879 9880 9881 9882 9883 9884 9885 9886 9887 9888 9889 9890 9891 9892 9893 9894 9895 9896 9897 9898 9899 9900 9901 9902 9903 9904 9905 9906 9907 9908 9909 9910 9911 9912 9913 9914 9915 9916 9917 9918 9919 9920 9921 9922 9923 9924 9925 9926 9927 9928 9929 9930 9931 9932 9933 9934 9935 9936 9937 9938 9939 9940 9941 9942 9943 9944 9945 9946 9947 9948 9949 9950 9951 9952 9953 9954 9955 9956 9957 9958 9959 9960 9961 9962 9963 9964 9965 9966 9967 9968 9969 9970 9971 9972 9973 9974 9975 9976 9977 9978 9979 9980 9981 9982 9983 9984 9985 9986 9987 9988 9989 9990 9991 9992 9993 9994 9995 9996 9997 9998 9999 10000 10001 10002 10003 10004 10005 10006 10007 10008 10009 10010 10011 10012 10013 10014 10015 10016 10017 10018 10019 10020 10021 10022 10023 10024 10025 10026 10027 10028 10029 10030 10031 10032 10033 10034 10035 10036 10037 10038 10039 10040 10041 10042 10043 10044 10045 10046 10047 10048 10049 10050 10051 10052 10053 10054 10055 10056 10057 10058 10059 10060 10061 10062 10063 10064 10065 10066 10067 10068 10069 10070 10071 10072 10073 10074 10075 10076 10077 10078 10079 10080 10081 10082 10083 10084 10085 10086 10087 10088 10089 10090 10091 10092 10093 10094 10095 10096 10097 10098 10099 10100 10101 10102 10103 10104 10105 10106 10107 10108 10109 10110 10111 10112 10113 10114 10115 10116 10117 10118 10119 10120 10121 10122 10123 10124 10125 10126 10127 10128 10129 10130 10131 10132 10133 10134 10135 10136 10137 10138 10139 10140 10141 10142 10143 10144 10145 10146 10147 10148 10149 10150 10151 10152 10153 10154 10155 10156 10157 10158 10159 10160 10161 10162 10163 10164 10165 10166 10167 10168 10169 10170 10171 10172 10173 10174 10175 10176 10177 10178 10179 10180 10181 10182 10183 10184 10185 10186 10187 10188 10189 10190 10191 10192 10193 10194 10195 10196 10197 10198 10199 10200 10201 10202 10203 10204 10205 10206 10207 10208 10209 10210 10211 10212 10213 10214 10215 10216 10217 10218 10219 10220 10221 10222 10223 10224 10225 10226 10227 10228 10229 10230 10231 10232 10233 10234 10235 10236 10237 10238 10239 10240 10241 10242 10243 10244 10245 10246 10247 10248 10249 10250 10251 10252 10253 10254 10255 10256 10257 10258 10259 10260 10261 10262 10263 10264 10265 10266 10267 10268 10269 10270 10271 10272 10273 10274 10275 10276 10277 10278 10279 10280 10281 10282 10283 10284 10285 10286 10287 10288 10289 10290 10291 10292 10293 10294 10295 10296 10297 10298 10299 10300 10301 10302 10303 10304 10305 10306 10307 10308 10309 10310 10311 10312 10313 10314 10315 10316 10317 10318 10319 10320 10321 10322 10323 10324 10325 10326 10327 10328 10329 10330 10331 10332 10333 10334 10335 10336 10337 10338 10339 10340 10341 10342 10343 10344 10345 10346 10347 10348 10349 10350 10351 10352 10353 10354 10355 10356 10357 10358 10359 10360 10361 10362 10363 10364 10365 10366 10367 10368 10369 10370 10371 10372 10373 10374 10375 10376 10377 10378 10379 10380 10381 10382 10383 10384 10385 10386 10387 10388 10389 10390 10391 10392 10393 10394 10395 10396 10397 10398 10399 10400 10401 10402 10403 10404 10405 10406 10407 10408 10409 10410 10411 10412 10413 10414 10415 10416 10417 10418 10419 10420 10421 10422 10423 10424 10425 10426 10427 10428 10429 10430 10431 10432 10433 10434 10435 10436 10437 10438 10439 10440 10441 10442 10443 10444 10445 10446 10447 10448 10449 10450 10451 10452 10453 10454 10455 10456 10457 10458 10459 10460 10461 10462 10463 10464 10465 10466 10467 10468 10469 10470 10471 10472 10473 10474 10475 10476 10477 10478 10479 10480 10481 10482 10483 10484 10485 10486 10487 10488 10489 10490 10491 10492 10493 10494 10495 10496 10497 10498 10499 10500 10501 10502 10503 10504 10505 10506 10507 10508 10509 10510 10511 10512 10513 10514 10515 10516 10517 10518 10519 10520 10521 10522 10523 10524 10525 10526 10527 10528 10529 10530 10531 10532 10533 10534 10535 10536 10537 10538 10539 10540 10541 10542 10543 10544 10545 10546 10547 10548 10549 10550 10551 10552 10553 10554 10555 10556 10557 10558 10559 10560 10561 10562 10563 10564 10565 10566 10567 10568 10569 10570 10571 10572 10573 10574 10575 10576 10577 10578 10579 10580 10581 10582 10583 10584 10585 10586 10587 10588 10589 10590 10591 10592 10593 10594 10595 10596 10597 10598 10599 10600 10601 10602 10603 10604 10605 10606 10607 10608 10609 10610 10611 10612 10613 10614 10615 10616 10617 10618 10619 10620 10621 10622 10623 10624 10625 10626 10627 10628 10629 10630 10631 10632 10633 10634 10635 10636 10637 10638 10639 10640 10641 10642 10643 10644 10645 10646 10647 10648 10649 10650 10651 10652 10653 10654 10655 10656 10657 10658 10659 10660 10661 10662 10663 10664 10665 10666 10667 10668 10669 10670 10671 10672 10673 10674 10675 10676 10677 10678 10679 10680 10681 10682 10683 10684 10685 10686 10687 10688 10689 10690 10691 10692 10693 10694 10695 10696 10697 10698 10699 10700 10701 10702 10703 10704 10705 10706 10707 10708 10709 10710 10711 10712 10713 10714 10715 10716 10717 10718 10719 10720 10721 10722 10723 10724 10725 10726 10727 10728 10729 10730 10731 10732 10733 10734 10735 10736 10737 10738 10739 10740 10741 10742 10743 10744 10745 10746 10747 10748 10749 10750 10751 10752 10753 10754 10755 10756 10757 10758 10759 10760 10761 10762 10763 10764 10765 10766 10767 10768 10769 10770 10771 10772 10773 10774 10775 10776 10777 10778 10779 10780 10781 10782 10783 10784 10785
|
<!DOCTYPE html>
<html>
<head>
<title>nodes.coffee</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, target-densitydpi=160dpi, initial-scale=1.0; maximum-scale=1.0; user-scalable=0;">
<link rel="stylesheet" media="all" href="docco.css" />
</head>
<body>
<div id="container">
<div id="background"></div>
<ul id="jump_to">
<li>
<a class="large" href="javascript:void(0);">Jump To …</a>
<a class="small" href="javascript:void(0);">+</a>
<div id="jump_wrapper">
<div id="jump_page_wrapper">
<div id="jump_page">
<a class="source" href="browser.html">
browser.coffee
</a>
<a class="source" href="cake.html">
cake.coffee
</a>
<a class="source" href="coffeescript.html">
coffeescript.coffee
</a>
<a class="source" href="command.html">
command.coffee
</a>
<a class="source" href="grammar.html">
grammar.coffee
</a>
<a class="source" href="helpers.html">
helpers.coffee
</a>
<a class="source" href="index.html">
index.coffee
</a>
<a class="source" href="lexer.html">
lexer.coffee
</a>
<a class="source" href="nodes.html">
nodes.coffee
</a>
<a class="source" href="optparse.html">
optparse.coffee
</a>
<a class="source" href="register.html">
register.coffee
</a>
<a class="source" href="repl.html">
repl.coffee
</a>
<a class="source" href="rewriter.html">
rewriter.coffee
</a>
<a class="source" href="scope.html">
scope.litcoffee
</a>
<a class="source" href="sourcemap.html">
sourcemap.litcoffee
</a>
</div>
</div>
</li>
</ul>
<ul class="sections">
<li id="title">
<div class="annotation">
<h1>nodes.coffee</h1>
</div>
</li>
<li id="section-1">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-1">§</a>
</div>
<p><code>nodes.coffee</code> contains all of the node classes for the syntax tree. Most
nodes are created as the result of actions in the <a href="grammar.html">grammar</a>,
but some are created by other nodes as a method of code generation. To convert
the syntax tree into a string of JavaScript code, call <code>compile()</code> on the root.</p>
</div>
<div class="content"><div class='highlight'><pre>
<span class="hljs-built_in">Error</span>.stackTraceLimit = <span class="hljs-literal">Infinity</span>
{Scope} = <span class="hljs-built_in">require</span> <span class="hljs-string">'./scope'</span>
{isUnassignable, JS_FORBIDDEN} = <span class="hljs-built_in">require</span> <span class="hljs-string">'./lexer'</span></pre></div></div>
</li>
<li id="section-2">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-2">§</a>
</div>
<p>Import the helpers we plan to use.</p>
</div>
<div class="content"><div class='highlight'><pre>{compact, flatten, extend, merge, del, starts, ends, some,
addDataToNode, attachCommentsToNode, locationDataToString,
throwSyntaxError, replaceUnicodeCodePointEscapes,
isFunction, isPlainObject, isNumber, parseNumber} = <span class="hljs-built_in">require</span> <span class="hljs-string">'./helpers'</span></pre></div></div>
</li>
<li id="section-3">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-3">§</a>
</div>
<p>Functions required by parser.</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-built_in">exports</span>.extend = extend
<span class="hljs-built_in">exports</span>.addDataToNode = addDataToNode</pre></div></div>
</li>
<li id="section-4">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-4">§</a>
</div>
<p>Constant functions for nodes that don’t need customization.</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-function"><span class="hljs-title">YES</span> = -></span> <span class="hljs-literal">yes</span>
<span class="hljs-function"><span class="hljs-title">NO</span> = -></span> <span class="hljs-literal">no</span>
<span class="hljs-function"><span class="hljs-title">THIS</span> = -></span> this
<span class="hljs-function"><span class="hljs-title">NEGATE</span> = -></span> @negated = <span class="hljs-keyword">not</span> @negated; this</pre></div></div>
</li>
<li id="section-5">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-5">§</a>
</div>
<h3 id="codefragment">CodeFragment</h3>
</div>
</li>
<li id="section-6">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-6">§</a>
</div>
<p>The various nodes defined below all compile to a collection of <strong>CodeFragment</strong> objects.
A CodeFragments is a block of generated code, and the location in the source file where the code
came from. CodeFragments can be assembled together into working code just by catting together
all the CodeFragments’ <code>code</code> snippets, in order.</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-built_in">exports</span>.CodeFragment = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">CodeFragment</span></span>
constructor: <span class="hljs-function"><span class="hljs-params">(parent, code)</span> -></span>
@code = <span class="hljs-string">"<span class="hljs-subst">#{code}</span>"</span>
@type = parent?.constructor?.name <span class="hljs-keyword">or</span> <span class="hljs-string">'unknown'</span>
@locationData = parent?.locationData
@comments = parent?.comments
toString: <span class="hljs-function">-></span></pre></div></div>
</li>
<li id="section-7">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-7">§</a>
</div>
<p>This is only intended for debugging.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-string">"<span class="hljs-subst">#{@code}</span><span class="hljs-subst">#{<span class="hljs-keyword">if</span> @locationData <span class="hljs-keyword">then</span> <span class="hljs-string">": "</span> + locationDataToString(@locationData) <span class="hljs-keyword">else</span> <span class="hljs-string">''</span>}</span>"</span></pre></div></div>
</li>
<li id="section-8">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-8">§</a>
</div>
<p>Convert an array of CodeFragments into a string.</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-function"><span class="hljs-title">fragmentsToText</span> = <span class="hljs-params">(fragments)</span> -></span>
(fragment.code <span class="hljs-keyword">for</span> fragment <span class="hljs-keyword">in</span> fragments).join(<span class="hljs-string">''</span>)</pre></div></div>
</li>
<li id="section-9">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-9">§</a>
</div>
<h3 id="base">Base</h3>
</div>
</li>
<li id="section-10">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-10">§</a>
</div>
<p>The <strong>Base</strong> is the abstract base class for all nodes in the syntax tree.
Each subclass implements the <code>compileNode</code> method, which performs the
code generation for that node. To compile a node to JavaScript,
call <code>compile</code> on it, which wraps <code>compileNode</code> in some generic extra smarts,
to know when the generated code needs to be wrapped up in a closure.
An options hash is passed and cloned throughout, containing information about
the environment from higher in the tree (such as if a returned value is
being requested by the surrounding function), information about the current
scope, and indentation level.</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-built_in">exports</span>.Base = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Base</span></span>
compile: <span class="hljs-function"><span class="hljs-params">(o, lvl)</span> -></span>
fragmentsToText @compileToFragments o, lvl</pre></div></div>
</li>
<li id="section-11">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-11">§</a>
</div>
<p>Occasionally a node is compiled multiple times, for example to get the name
of a variable to add to scope tracking. When we know that a “premature”
compilation won’t result in comments being output, set those comments aside
so that they’re preserved for a later <code>compile</code> call that will result in
the comments being included in the output.</p>
</div>
<div class="content"><div class='highlight'><pre> compileWithoutComments: <span class="hljs-function"><span class="hljs-params">(o, lvl, method = <span class="hljs-string">'compile'</span>)</span> -></span>
<span class="hljs-keyword">if</span> @comments
@ignoreTheseCommentsTemporarily = @comments
<span class="hljs-keyword">delete</span> @comments
unwrapped = @unwrapAll()
<span class="hljs-keyword">if</span> unwrapped.comments
unwrapped.ignoreTheseCommentsTemporarily = unwrapped.comments
<span class="hljs-keyword">delete</span> unwrapped.comments
fragments = @[method] o, lvl
<span class="hljs-keyword">if</span> @ignoreTheseCommentsTemporarily
@comments = @ignoreTheseCommentsTemporarily
<span class="hljs-keyword">delete</span> @ignoreTheseCommentsTemporarily
<span class="hljs-keyword">if</span> unwrapped.ignoreTheseCommentsTemporarily
unwrapped.comments = unwrapped.ignoreTheseCommentsTemporarily
<span class="hljs-keyword">delete</span> unwrapped.ignoreTheseCommentsTemporarily
fragments
compileNodeWithoutComments: <span class="hljs-function"><span class="hljs-params">(o, lvl)</span> -></span>
@compileWithoutComments o, lvl, <span class="hljs-string">'compileNode'</span></pre></div></div>
</li>
<li id="section-12">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-12">§</a>
</div>
<p>Common logic for determining whether to wrap this node in a closure before
compiling it, or to compile directly. We need to wrap if this node is a
<em>statement</em>, and it’s not a <em>pureStatement</em>, and we’re not at
the top level of a block (which would be unnecessary), and we haven’t
already been asked to return the result (because statements know how to
return results).</p>
</div>
<div class="content"><div class='highlight'><pre> compileToFragments: <span class="hljs-function"><span class="hljs-params">(o, lvl)</span> -></span>
o = extend {}, o
o.level = lvl <span class="hljs-keyword">if</span> lvl
node = @unfoldSoak(o) <span class="hljs-keyword">or</span> this
node.tab = o.indent
fragments = <span class="hljs-keyword">if</span> o.level <span class="hljs-keyword">is</span> LEVEL_TOP <span class="hljs-keyword">or</span> <span class="hljs-keyword">not</span> node.isStatement(o)
node.compileNode o
<span class="hljs-keyword">else</span>
node.compileClosure o
@compileCommentFragments o, node, fragments
fragments
compileToFragmentsWithoutComments: <span class="hljs-function"><span class="hljs-params">(o, lvl)</span> -></span>
@compileWithoutComments o, lvl, <span class="hljs-string">'compileToFragments'</span></pre></div></div>
</li>
<li id="section-13">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-13">§</a>
</div>
<p>Statements converted into expressions via closure-wrapping share a scope
object with their parent closure, to preserve the expected lexical scope.</p>
</div>
<div class="content"><div class='highlight'><pre> compileClosure: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
@checkForPureStatementInExpression()
o.sharedScope = <span class="hljs-literal">yes</span>
func = <span class="hljs-keyword">new</span> Code [], Block.wrap [this]
args = []
<span class="hljs-keyword">if</span> @contains (<span class="hljs-function"><span class="hljs-params">(node)</span> -></span> node <span class="hljs-keyword">instanceof</span> SuperCall)
func.bound = <span class="hljs-literal">yes</span>
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (argumentsNode = @contains isLiteralArguments) <span class="hljs-keyword">or</span> @contains isLiteralThis
args = [<span class="hljs-keyword">new</span> ThisLiteral]
<span class="hljs-keyword">if</span> argumentsNode
meth = <span class="hljs-string">'apply'</span>
args.push <span class="hljs-keyword">new</span> IdentifierLiteral <span class="hljs-string">'arguments'</span>
<span class="hljs-keyword">else</span>
meth = <span class="hljs-string">'call'</span>
func = <span class="hljs-keyword">new</span> Value func, [<span class="hljs-keyword">new</span> Access <span class="hljs-keyword">new</span> PropertyName meth]
parts = (<span class="hljs-keyword">new</span> Call func, args).compileNode o
<span class="hljs-keyword">switch</span>
<span class="hljs-keyword">when</span> func.isGenerator <span class="hljs-keyword">or</span> func.base?.isGenerator
parts.unshift @makeCode <span class="hljs-string">"(yield* "</span>
parts.push @makeCode <span class="hljs-string">")"</span>
<span class="hljs-keyword">when</span> func.isAsync <span class="hljs-keyword">or</span> func.base?.isAsync
parts.unshift @makeCode <span class="hljs-string">"(await "</span>
parts.push @makeCode <span class="hljs-string">")"</span>
parts
compileCommentFragments: <span class="hljs-function"><span class="hljs-params">(o, node, fragments)</span> -></span>
<span class="hljs-keyword">return</span> fragments <span class="hljs-keyword">unless</span> node.comments</pre></div></div>
</li>
<li id="section-14">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-14">§</a>
</div>
<p>This is where comments, that are attached to nodes as a <code>comments</code>
property, become <code>CodeFragment</code>s. “Inline block comments,” e.g.
<code>/* */</code>-delimited comments that are interspersed within code on a line,
are added to the current <code>fragments</code> stream. All other fragments are
attached as properties to the nearest preceding or following fragment,
to remain stowaways until they get properly output in <code>compileComments</code>
later on.</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-function"> <span class="hljs-title">unshiftCommentFragment</span> = <span class="hljs-params">(commentFragment)</span> -></span>
<span class="hljs-keyword">if</span> commentFragment.unshift</pre></div></div>
</li>
<li id="section-15">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-15">§</a>
</div>
<p>Find the first non-comment fragment and insert <code>commentFragment</code>
before it.</p>
</div>
<div class="content"><div class='highlight'><pre> unshiftAfterComments fragments, commentFragment
<span class="hljs-keyword">else</span>
<span class="hljs-keyword">if</span> fragments.length <span class="hljs-keyword">isnt</span> <span class="hljs-number">0</span>
precedingFragment = fragments[fragments.length - <span class="hljs-number">1</span>]
<span class="hljs-keyword">if</span> commentFragment.newLine <span class="hljs-keyword">and</span> precedingFragment.code <span class="hljs-keyword">isnt</span> <span class="hljs-string">''</span> <span class="hljs-keyword">and</span>
<span class="hljs-keyword">not</span> <span class="hljs-regexp">/\n\s*$/</span>.test precedingFragment.code
commentFragment.code = <span class="hljs-string">"\n<span class="hljs-subst">#{commentFragment.code}</span>"</span>
fragments.push commentFragment
<span class="hljs-keyword">for</span> comment <span class="hljs-keyword">in</span> node.comments <span class="hljs-keyword">when</span> comment <span class="hljs-keyword">not</span> <span class="hljs-keyword">in</span> @compiledComments
@compiledComments.push comment <span class="hljs-comment"># Don’t output this comment twice.</span></pre></div></div>
</li>
<li id="section-16">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-16">§</a>
</div>
<p>For block/here comments, denoted by <code>###</code>, that are inline comments
like <code>1 + ### comment ### 2</code>, create fragments and insert them into
the fragments array.
Otherwise attach comment fragments to their closest fragment for now,
so they can be inserted into the output later after all the newlines
have been added.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> comment.here <span class="hljs-comment"># Block comment, delimited by `###`.</span>
commentFragment = <span class="hljs-keyword">new</span> HereComment(comment).compileNode o
<span class="hljs-keyword">else</span> <span class="hljs-comment"># Line comment, delimited by `#`.</span>
commentFragment = <span class="hljs-keyword">new</span> LineComment(comment).compileNode o
<span class="hljs-keyword">if</span> (commentFragment.isHereComment <span class="hljs-keyword">and</span> <span class="hljs-keyword">not</span> commentFragment.newLine) <span class="hljs-keyword">or</span>
node.includeCommentFragments()</pre></div></div>
</li>
<li id="section-17">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-17">§</a>
</div>
<p>Inline block comments, like <code>1 + /* comment */ 2</code>, or a node whose
<code>compileToFragments</code> method has logic for outputting comments.</p>
</div>
<div class="content"><div class='highlight'><pre> unshiftCommentFragment commentFragment
<span class="hljs-keyword">else</span>
fragments.push @makeCode <span class="hljs-string">''</span> <span class="hljs-keyword">if</span> fragments.length <span class="hljs-keyword">is</span> <span class="hljs-number">0</span>
<span class="hljs-keyword">if</span> commentFragment.unshift
fragments[<span class="hljs-number">0</span>].precedingComments ?= []
fragments[<span class="hljs-number">0</span>].precedingComments.push commentFragment
<span class="hljs-keyword">else</span>
fragments[fragments.length - <span class="hljs-number">1</span>].followingComments ?= []
fragments[fragments.length - <span class="hljs-number">1</span>].followingComments.push commentFragment
fragments</pre></div></div>
</li>
<li id="section-18">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-18">§</a>
</div>
<p>If the code generation wishes to use the result of a complex expression
in multiple places, ensure that the expression is only ever evaluated once,
by assigning it to a temporary variable. Pass a level to precompile.</p>
<p>If <code>level</code> is passed, then returns <code>[val, ref]</code>, where <code>val</code> is the compiled value, and <code>ref</code>
is the compiled reference. If <code>level</code> is not passed, this returns <code>[val, ref]</code> where
the two values are raw nodes which have not been compiled.</p>
</div>
<div class="content"><div class='highlight'><pre> cache: <span class="hljs-function"><span class="hljs-params">(o, level, shouldCache)</span> -></span>
complex = <span class="hljs-keyword">if</span> shouldCache? <span class="hljs-keyword">then</span> shouldCache this <span class="hljs-keyword">else</span> @shouldCache()
<span class="hljs-keyword">if</span> complex
ref = <span class="hljs-keyword">new</span> IdentifierLiteral o.scope.freeVariable <span class="hljs-string">'ref'</span>
sub = <span class="hljs-keyword">new</span> Assign ref, this
<span class="hljs-keyword">if</span> level <span class="hljs-keyword">then</span> [sub.compileToFragments(o, level), [@makeCode(ref.value)]] <span class="hljs-keyword">else</span> [sub, ref]
<span class="hljs-keyword">else</span>
ref = <span class="hljs-keyword">if</span> level <span class="hljs-keyword">then</span> @compileToFragments o, level <span class="hljs-keyword">else</span> this
[ref, ref]</pre></div></div>
</li>
<li id="section-19">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-19">§</a>
</div>
<p>Occasionally it may be useful to make an expression behave as if it was ‘hoisted’, whereby the
result of the expression is available before its location in the source, but the expression’s
variable scope corresponds to the source position. This is used extensively to deal with executable
class bodies in classes.</p>
<p>Calling this method mutates the node, proxying the <code>compileNode</code> and <code>compileToFragments</code>
methods to store their result for later replacing the <code>target</code> node, which is returned by the
call.</p>
</div>
<div class="content"><div class='highlight'><pre> hoist: <span class="hljs-function">-></span>
@hoisted = <span class="hljs-literal">yes</span>
target = <span class="hljs-keyword">new</span> HoistTarget @
compileNode = @compileNode
compileToFragments = @compileToFragments
@compileNode = <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
target.update compileNode, o
@compileToFragments = <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
target.update compileToFragments, o
target
cacheToCodeFragments: <span class="hljs-function"><span class="hljs-params">(cacheValues)</span> -></span>
[fragmentsToText(cacheValues[<span class="hljs-number">0</span>]), fragmentsToText(cacheValues[<span class="hljs-number">1</span>])]</pre></div></div>
</li>
<li id="section-20">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-20">§</a>
</div>
<p>Construct a node that returns the current node’s result.
Note that this is overridden for smarter behavior for
many statement nodes (e.g. <code>If</code>, <code>For</code>).</p>
</div>
<div class="content"><div class='highlight'><pre> makeReturn: <span class="hljs-function"><span class="hljs-params">(results, mark)</span> -></span>
<span class="hljs-keyword">if</span> mark</pre></div></div>
</li>
<li id="section-21">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-21">§</a>
</div>
<p>Mark this node as implicitly returned, so that it can be part of the
node metadata returned in the AST.</p>
</div>
<div class="content"><div class='highlight'><pre> @canBeReturned = <span class="hljs-literal">yes</span>
<span class="hljs-keyword">return</span>
node = @unwrapAll()
<span class="hljs-keyword">if</span> results
<span class="hljs-keyword">new</span> Call <span class="hljs-keyword">new</span> Literal(<span class="hljs-string">"<span class="hljs-subst">#{results}</span>.push"</span>), [node]
<span class="hljs-keyword">else</span>
<span class="hljs-keyword">new</span> Return node</pre></div></div>
</li>
<li id="section-22">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-22">§</a>
</div>
<p>Does this node, or any of its children, contain a node of a certain kind?
Recursively traverses down the <em>children</em> nodes and returns the first one
that verifies <code>pred</code>. Otherwise return undefined. <code>contains</code> does not cross
scope boundaries.</p>
</div>
<div class="content"><div class='highlight'><pre> contains: <span class="hljs-function"><span class="hljs-params">(pred)</span> -></span>
node = <span class="hljs-literal">undefined</span>
@traverseChildren <span class="hljs-literal">no</span>, <span class="hljs-function"><span class="hljs-params">(n)</span> -></span>
<span class="hljs-keyword">if</span> pred n
node = n
<span class="hljs-keyword">return</span> <span class="hljs-literal">no</span>
node</pre></div></div>
</li>
<li id="section-23">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-23">§</a>
</div>
<p>Pull out the last node of a node list.</p>
</div>
<div class="content"><div class='highlight'><pre> lastNode: <span class="hljs-function"><span class="hljs-params">(list)</span> -></span>
<span class="hljs-keyword">if</span> list.length <span class="hljs-keyword">is</span> <span class="hljs-number">0</span> <span class="hljs-keyword">then</span> <span class="hljs-literal">null</span> <span class="hljs-keyword">else</span> list[list.length - <span class="hljs-number">1</span>]</pre></div></div>
</li>
<li id="section-24">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-24">§</a>
</div>
<p>Debugging representation of the node, for inspecting the parse tree.
This is what <code>coffee --nodes</code> prints out.</p>
</div>
<div class="content"><div class='highlight'><pre> toString: <span class="hljs-function"><span class="hljs-params">(idt = <span class="hljs-string">''</span>, name = @constructor.name)</span> -></span>
tree = <span class="hljs-string">'\n'</span> + idt + name
tree += <span class="hljs-string">'?'</span> <span class="hljs-keyword">if</span> @soak
@eachChild (node) -> tree += node.toString idt + TAB
tree
checkForPureStatementInExpression: <span class="hljs-function">-></span>
<span class="hljs-keyword">if</span> jumpNode = @jumps()
jumpNode.error <span class="hljs-string">'cannot use a pure statement in an expression'</span></pre></div></div>
</li>
<li id="section-25">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-25">§</a>
</div>
<p>Plain JavaScript object representation of the node, that can be serialized
as JSON. This is what the <code>ast</code> option in the Node API returns.
We try to follow the <a href="https://github.com/babel/babel/blob/master/packages/babel-parser/ast/spec.md">Babel AST spec</a>
as closely as possible, for improved interoperability with other tools.
<strong>WARNING: DO NOT OVERRIDE THIS METHOD IN CHILD CLASSES.</strong>
Only override the component <code>ast*</code> methods as needed.</p>
</div>
<div class="content"><div class='highlight'><pre> ast: <span class="hljs-function"><span class="hljs-params">(o, level)</span> -></span></pre></div></div>
</li>
<li id="section-26">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-26">§</a>
</div>
<p>Merge <code>level</code> into <code>o</code> and perform other universal checks.</p>
</div>
<div class="content"><div class='highlight'><pre> o = @astInitialize o, level</pre></div></div>
</li>
<li id="section-27">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-27">§</a>
</div>
<p>Create serializable representation of this node.</p>
</div>
<div class="content"><div class='highlight'><pre> astNode = @astNode o</pre></div></div>
</li>
<li id="section-28">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-28">§</a>
</div>
<p>Mark AST nodes that correspond to expressions that (implicitly) return.
We can’t do this as part of <code>astNode</code> because we need to assemble child
nodes first before marking the parent being returned.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> @astNode? <span class="hljs-keyword">and</span> @canBeReturned
<span class="hljs-built_in">Object</span>.assign astNode, {returns: <span class="hljs-literal">yes</span>}
astNode
astInitialize: <span class="hljs-function"><span class="hljs-params">(o, level)</span> -></span>
o = <span class="hljs-built_in">Object</span>.assign {}, o
o.level = level <span class="hljs-keyword">if</span> level?
<span class="hljs-keyword">if</span> o.level > LEVEL_TOP
@checkForPureStatementInExpression()</pre></div></div>
</li>
<li id="section-29">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-29">§</a>
</div>
<p><code>@makeReturn</code> must be called before <code>astProperties</code>, because the latter may call
<code>.ast()</code> for child nodes and those nodes would need the return logic from <code>makeReturn</code>
already executed by then.</p>
</div>
<div class="content"><div class='highlight'><pre> @makeReturn <span class="hljs-literal">null</span>, <span class="hljs-literal">yes</span> <span class="hljs-keyword">if</span> @isStatement(o) <span class="hljs-keyword">and</span> o.level <span class="hljs-keyword">isnt</span> LEVEL_TOP <span class="hljs-keyword">and</span> o.scope?
o
astNode: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span></pre></div></div>
</li>
<li id="section-30">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-30">§</a>
</div>
<p>Every abstract syntax tree node object has four categories of properties:</p>
<ul>
<li>type, stored in the <code>type</code> field and a string like <code>NumberLiteral</code>.</li>
<li>location data, stored in the <code>loc</code>, <code>start</code>, <code>end</code> and <code>range</code> fields.</li>
<li>properties specific to this node, like <code>parsedValue</code>.</li>
<li>properties that are themselves child nodes, like <code>body</code>.
These fields are all intermixed in the Babel spec; <code>type</code> and <code>start</code> and
<code>parsedValue</code> are all top level fields in the AST node object. We have
separate methods for returning each category, that we merge together here.</li>
</ul>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-built_in">Object</span>.assign {}, {type: @astType(o)}, @astProperties(o), @astLocationData()</pre></div></div>
</li>
<li id="section-31">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-31">§</a>
</div>
<p>By default, a node class has no specific properties.</p>
</div>
<div class="content"><div class='highlight'><pre> astProperties: <span class="hljs-function">-></span> {}</pre></div></div>
</li>
<li id="section-32">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-32">§</a>
</div>
<p>By default, a node class’s AST <code>type</code> is its class name.</p>
</div>
<div class="content"><div class='highlight'><pre> astType: <span class="hljs-function">-></span> @constructor.name</pre></div></div>
</li>
<li id="section-33">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-33">§</a>
</div>
<p>The AST location data is a rearranged version of our Jison location data,
mutated into the structure that the Babel spec uses.</p>
</div>
<div class="content"><div class='highlight'><pre> astLocationData: <span class="hljs-function">-></span>
jisonLocationDataToAstLocationData @locationData</pre></div></div>
</li>
<li id="section-34">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-34">§</a>
</div>
<p>Determines whether an AST node needs an <code>ExpressionStatement</code> wrapper.
Typically matches our <code>isStatement()</code> logic but this allows overriding.</p>
</div>
<div class="content"><div class='highlight'><pre> isStatementAst: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
@isStatement o</pre></div></div>
</li>
<li id="section-35">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-35">§</a>
</div>
<p>Passes each child to a function, breaking when the function returns <code>false</code>.</p>
</div>
<div class="content"><div class='highlight'><pre> eachChild: <span class="hljs-function"><span class="hljs-params">(func)</span> -></span>
<span class="hljs-keyword">return</span> this <span class="hljs-keyword">unless</span> @children
<span class="hljs-keyword">for</span> attr <span class="hljs-keyword">in</span> @children <span class="hljs-keyword">when</span> @[attr]
<span class="hljs-keyword">for</span> child <span class="hljs-keyword">in</span> flatten [@[attr]]
<span class="hljs-keyword">return</span> this <span class="hljs-keyword">if</span> func(child) <span class="hljs-keyword">is</span> <span class="hljs-literal">false</span>
this
traverseChildren: <span class="hljs-function"><span class="hljs-params">(crossScope, func)</span> -></span>
@eachChild (child) ->
recur = func(child)
child.traverseChildren(crossScope, func) <span class="hljs-keyword">unless</span> recur <span class="hljs-keyword">is</span> <span class="hljs-literal">no</span></pre></div></div>
</li>
<li id="section-36">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-36">§</a>
</div>
<p><code>replaceInContext</code> will traverse children looking for a node for which <code>match</code> returns
true. Once found, the matching node will be replaced by the result of calling <code>replacement</code>.</p>
</div>
<div class="content"><div class='highlight'><pre> replaceInContext: <span class="hljs-function"><span class="hljs-params">(match, replacement)</span> -></span>
<span class="hljs-keyword">return</span> <span class="hljs-literal">false</span> <span class="hljs-keyword">unless</span> @children
<span class="hljs-keyword">for</span> attr <span class="hljs-keyword">in</span> @children <span class="hljs-keyword">when</span> children = @[attr]
<span class="hljs-keyword">if</span> <span class="hljs-built_in">Array</span>.isArray children
<span class="hljs-keyword">for</span> child, i <span class="hljs-keyword">in</span> children
<span class="hljs-keyword">if</span> match child
children[i..i] = replacement child, @
<span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>
<span class="hljs-keyword">else</span>
<span class="hljs-keyword">return</span> <span class="hljs-literal">true</span> <span class="hljs-keyword">if</span> child.replaceInContext match, replacement
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> match children
@[attr] = replacement children, @
<span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>
<span class="hljs-keyword">else</span>
<span class="hljs-keyword">return</span> <span class="hljs-literal">true</span> <span class="hljs-keyword">if</span> children.replaceInContext match, replacement
invert: <span class="hljs-function">-></span>
<span class="hljs-keyword">new</span> Op <span class="hljs-string">'!'</span>, this
unwrapAll: <span class="hljs-function">-></span>
node = this
<span class="hljs-keyword">continue</span> <span class="hljs-keyword">until</span> node <span class="hljs-keyword">is</span> node = node.unwrap()
node</pre></div></div>
</li>
<li id="section-37">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-37">§</a>
</div>
<p>Default implementations of the common node properties and methods. Nodes
will override these with custom logic, if needed.</p>
</div>
</li>
<li id="section-38">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-38">§</a>
</div>
<p><code>children</code> are the properties to recurse into when tree walking. The
<code>children</code> list <em>is</em> the structure of the AST. The <code>parent</code> pointer, and
the pointer to the <code>children</code> are how you can traverse the tree.</p>
</div>
<div class="content"><div class='highlight'><pre> children: []</pre></div></div>
</li>
<li id="section-39">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-39">§</a>
</div>
<p><code>isStatement</code> has to do with “everything is an expression”. A few things
can’t be expressions, such as <code>break</code>. Things that <code>isStatement</code> returns
<code>true</code> for are things that can’t be used as expressions. There are some
error messages that come from <code>nodes.coffee</code> due to statements ending up
in expression position.</p>
</div>
<div class="content"><div class='highlight'><pre> isStatement: NO</pre></div></div>
</li>
<li id="section-40">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-40">§</a>
</div>
<p>Track comments that have been compiled into fragments, to avoid outputting
them twice.</p>
</div>
<div class="content"><div class='highlight'><pre> compiledComments: []</pre></div></div>
</li>
<li id="section-41">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-41">§</a>
</div>
<p><code>includeCommentFragments</code> lets <code>compileCommentFragments</code> know whether this node
has special awareness of how to handle comments within its output.</p>
</div>
<div class="content"><div class='highlight'><pre> includeCommentFragments: NO</pre></div></div>
</li>
<li id="section-42">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-42">§</a>
</div>
<p><code>jumps</code> tells you if an expression, or an internal part of an expression,
has a flow control construct (like <code>break</code>, <code>continue</code>, or <code>return</code>)
that jumps out of the normal flow of control and can’t be used as a value.
(Note that <code>throw</code> is not considered a flow control construct.)
This is important because flow control in the middle of an expression
makes no sense; we have to disallow it.</p>
</div>
<div class="content"><div class='highlight'><pre> jumps: NO</pre></div></div>
</li>
<li id="section-43">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-43">§</a>
</div>
<p>If <code>node.shouldCache() is false</code>, it is safe to use <code>node</code> more than once.
Otherwise you need to store the value of <code>node</code> in a variable and output
that variable several times instead. Kind of like this: <code>5</code> need not be
cached. <code>returnFive()</code>, however, could have side effects as a result of
evaluating it more than once, and therefore we need to cache it. The
parameter is named <code>shouldCache</code> rather than <code>mustCache</code> because there are
also cases where we might not need to cache but where we want to, for
example a long expression that may well be idempotent but we want to cache
for brevity.</p>
</div>
<div class="content"><div class='highlight'><pre> shouldCache: YES
isChainable: NO
isAssignable: NO
isNumber: NO
unwrap: THIS
unfoldSoak: NO</pre></div></div>
</li>
<li id="section-44">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-44">§</a>
</div>
<p>Is this node used to assign a certain variable?</p>
</div>
<div class="content"><div class='highlight'><pre> assigns: NO</pre></div></div>
</li>
<li id="section-45">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-45">§</a>
</div>
<p>For this node and all descendents, set the location data to <code>locationData</code>
if the location data is not already set.</p>
</div>
<div class="content"><div class='highlight'><pre> updateLocationDataIfMissing: <span class="hljs-function"><span class="hljs-params">(locationData, force)</span> -></span>
@forceUpdateLocation = <span class="hljs-literal">yes</span> <span class="hljs-keyword">if</span> force
<span class="hljs-keyword">return</span> this <span class="hljs-keyword">if</span> @locationData <span class="hljs-keyword">and</span> <span class="hljs-keyword">not</span> @forceUpdateLocation
<span class="hljs-keyword">delete</span> @forceUpdateLocation
@locationData = locationData
@eachChild (child) ->
child.updateLocationDataIfMissing locationData</pre></div></div>
</li>
<li id="section-46">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-46">§</a>
</div>
<p>Add location data from another node</p>
</div>
<div class="content"><div class='highlight'><pre> withLocationDataFrom: <span class="hljs-function"><span class="hljs-params">({locationData})</span> -></span>
@updateLocationDataIfMissing locationData</pre></div></div>
</li>
<li id="section-47">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-47">§</a>
</div>
<p>Add location data and comments from another node</p>
</div>
<div class="content"><div class='highlight'><pre> withLocationDataAndCommentsFrom: <span class="hljs-function"><span class="hljs-params">(node)</span> -></span>
@withLocationDataFrom node
{comments} = node
@comments = comments <span class="hljs-keyword">if</span> comments?.length
this</pre></div></div>
</li>
<li id="section-48">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-48">§</a>
</div>
<p>Throw a SyntaxError associated with this node’s location.</p>
</div>
<div class="content"><div class='highlight'><pre> error: <span class="hljs-function"><span class="hljs-params">(message)</span> -></span>
throwSyntaxError message, @locationData
makeCode: <span class="hljs-function"><span class="hljs-params">(code)</span> -></span>
<span class="hljs-keyword">new</span> CodeFragment this, code
wrapInParentheses: <span class="hljs-function"><span class="hljs-params">(fragments)</span> -></span>
[@makeCode(<span class="hljs-string">'('</span>), fragments..., @makeCode(<span class="hljs-string">')'</span>)]
wrapInBraces: <span class="hljs-function"><span class="hljs-params">(fragments)</span> -></span>
[@makeCode(<span class="hljs-string">'{'</span>), fragments..., @makeCode(<span class="hljs-string">'}'</span>)]</pre></div></div>
</li>
<li id="section-49">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-49">§</a>
</div>
<p><code>fragmentsList</code> is an array of arrays of fragments. Each array in fragmentsList will be
concatenated together, with <code>joinStr</code> added in between each, to produce a final flat array
of fragments.</p>
</div>
<div class="content"><div class='highlight'><pre> joinFragmentArrays: <span class="hljs-function"><span class="hljs-params">(fragmentsList, joinStr)</span> -></span>
answer = []
<span class="hljs-keyword">for</span> fragments, i <span class="hljs-keyword">in</span> fragmentsList
<span class="hljs-keyword">if</span> i <span class="hljs-keyword">then</span> answer.push @makeCode joinStr
answer = answer.concat fragments
answer</pre></div></div>
</li>
<li id="section-50">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-50">§</a>
</div>
<h3 id="hoisttarget">HoistTarget</h3>
</div>
</li>
<li id="section-51">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-51">§</a>
</div>
<p>A <strong>HoistTargetNode</strong> represents the output location in the node tree for a hoisted node.
See Base#hoist.</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-built_in">exports</span>.HoistTarget = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">HoistTarget</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Base</span></span></pre></div></div>
</li>
<li id="section-52">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-52">§</a>
</div>
<p>Expands hoisted fragments in the given array</p>
</div>
<div class="content"><div class='highlight'><pre> @expand = <span class="hljs-function"><span class="hljs-params">(fragments)</span> -></span>
<span class="hljs-keyword">for</span> fragment, i <span class="hljs-keyword">in</span> fragments <span class="hljs-keyword">by</span> <span class="hljs-number">-1</span> <span class="hljs-keyword">when</span> fragment.fragments
fragments[i..i] = @expand fragment.fragments
fragments
constructor: <span class="hljs-function"><span class="hljs-params">(@source)</span> -></span>
super()</pre></div></div>
</li>
<li id="section-53">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-53">§</a>
</div>
<p>Holds presentational options to apply when the source node is compiled.</p>
</div>
<div class="content"><div class='highlight'><pre> @options = {}</pre></div></div>
</li>
<li id="section-54">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-54">§</a>
</div>
<p>Placeholder fragments to be replaced by the source node’s compilation.</p>
</div>
<div class="content"><div class='highlight'><pre> @targetFragments = { fragments: [] }
isStatement: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
@source.isStatement o</pre></div></div>
</li>
<li id="section-55">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-55">§</a>
</div>
<p>Update the target fragments with the result of compiling the source.
Calls the given compile function with the node and options (overriden with the target
presentational options).</p>
</div>
<div class="content"><div class='highlight'><pre> update: <span class="hljs-function"><span class="hljs-params">(compile, o)</span> -></span>
@targetFragments.fragments = compile.call @source, merge o, @options</pre></div></div>
</li>
<li id="section-56">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-56">§</a>
</div>
<p>Copies the target indent and level, and returns the placeholder fragments</p>
</div>
<div class="content"><div class='highlight'><pre> compileToFragments: <span class="hljs-function"><span class="hljs-params">(o, level)</span> -></span>
@options.indent = o.indent
@options.level = level ? o.level
[ @targetFragments ]
compileNode: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
@compileToFragments o
compileClosure: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
@compileToFragments o</pre></div></div>
</li>
<li id="section-57">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-57">§</a>
</div>
<h3 id="root">Root</h3>
</div>
</li>
<li id="section-58">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-58">§</a>
</div>
<p>The root node of the node tree</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-built_in">exports</span>.Root = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Root</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Base</span></span>
constructor: <span class="hljs-function"><span class="hljs-params">(@body)</span> -></span>
super()
@isAsync = (<span class="hljs-keyword">new</span> Code [], @body).isAsync
children: [<span class="hljs-string">'body'</span>]</pre></div></div>
</li>
<li id="section-59">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-59">§</a>
</div>
<p>Wrap everything in a safety closure, unless requested not to. It would be
better not to generate them in the first place, but for now, clean up
obvious double-parentheses.</p>
</div>
<div class="content"><div class='highlight'><pre> compileNode: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
o.indent = <span class="hljs-keyword">if</span> o.bare <span class="hljs-keyword">then</span> <span class="hljs-string">''</span> <span class="hljs-keyword">else</span> TAB
o.level = LEVEL_TOP
o.compiling = <span class="hljs-literal">yes</span>
@initializeScope o
fragments = @body.compileRoot o
<span class="hljs-keyword">return</span> fragments <span class="hljs-keyword">if</span> o.bare
functionKeyword = <span class="hljs-string">"<span class="hljs-subst">#{<span class="hljs-keyword">if</span> @isAsync <span class="hljs-keyword">then</span> <span class="hljs-string">'async '</span> <span class="hljs-keyword">else</span> <span class="hljs-string">''</span>}</span>function"</span>
[].concat @makeCode(<span class="hljs-string">"(<span class="hljs-subst">#{functionKeyword}</span>() {\n"</span>), fragments, @makeCode(<span class="hljs-string">"\n}).call(this);\n"</span>)
initializeScope: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
o.scope = <span class="hljs-keyword">new</span> Scope <span class="hljs-literal">null</span>, @body, <span class="hljs-literal">null</span>, o.referencedVars ? []</pre></div></div>
</li>
<li id="section-60">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-60">§</a>
</div>
<p>Mark given local variables in the root scope as parameters so they don’t
end up being declared on the root block.</p>
</div>
<div class="content"><div class='highlight'><pre> o.scope.parameter name <span class="hljs-keyword">for</span> name <span class="hljs-keyword">in</span> o.locals <span class="hljs-keyword">or</span> []
commentsAst: <span class="hljs-function">-></span>
@allComments ?=
<span class="hljs-keyword">for</span> commentToken <span class="hljs-keyword">in</span> (@allCommentTokens ? []) <span class="hljs-keyword">when</span> <span class="hljs-keyword">not</span> commentToken.heregex
<span class="hljs-keyword">if</span> commentToken.here
<span class="hljs-keyword">new</span> HereComment commentToken
<span class="hljs-keyword">else</span>
<span class="hljs-keyword">new</span> LineComment commentToken
comment.ast() <span class="hljs-keyword">for</span> comment <span class="hljs-keyword">in</span> @allComments
astNode: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
o.level = LEVEL_TOP
@initializeScope o
super o
astType: <span class="hljs-function">-></span> <span class="hljs-string">'File'</span>
astProperties: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
@body.isRootBlock = <span class="hljs-literal">yes</span>
<span class="hljs-keyword">return</span>
program: <span class="hljs-built_in">Object</span>.assign @body.ast(o), @astLocationData()
comments: @commentsAst()</pre></div></div>
</li>
<li id="section-61">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-61">§</a>
</div>
<h3 id="block">Block</h3>
</div>
</li>
<li id="section-62">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-62">§</a>
</div>
<p>The block is the list of expressions that forms the body of an
indented block of code – the implementation of a function, a clause in an
<code>if</code>, <code>switch</code>, or <code>try</code>, and so on…</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-built_in">exports</span>.Block = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Block</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Base</span></span>
constructor: <span class="hljs-function"><span class="hljs-params">(nodes)</span> -></span>
super()
@expressions = compact flatten nodes <span class="hljs-keyword">or</span> []
children: [<span class="hljs-string">'expressions'</span>]</pre></div></div>
</li>
<li id="section-63">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-63">§</a>
</div>
<p>Tack an expression on to the end of this expression list.</p>
</div>
<div class="content"><div class='highlight'><pre> push: <span class="hljs-function"><span class="hljs-params">(node)</span> -></span>
@expressions.push node
this</pre></div></div>
</li>
<li id="section-64">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-64">§</a>
</div>
<p>Remove and return the last expression of this expression list.</p>
</div>
<div class="content"><div class='highlight'><pre> pop: <span class="hljs-function">-></span>
@expressions.pop()</pre></div></div>
</li>
<li id="section-65">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-65">§</a>
</div>
<p>Add an expression at the beginning of this expression list.</p>
</div>
<div class="content"><div class='highlight'><pre> unshift: <span class="hljs-function"><span class="hljs-params">(node)</span> -></span>
@expressions.unshift node
this</pre></div></div>
</li>
<li id="section-66">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-66">§</a>
</div>
<p>If this Block consists of just a single node, unwrap it by pulling
it back out.</p>
</div>
<div class="content"><div class='highlight'><pre> unwrap: <span class="hljs-function">-></span>
<span class="hljs-keyword">if</span> @expressions.length <span class="hljs-keyword">is</span> <span class="hljs-number">1</span> <span class="hljs-keyword">then</span> @expressions[<span class="hljs-number">0</span>] <span class="hljs-keyword">else</span> this</pre></div></div>
</li>
<li id="section-67">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-67">§</a>
</div>
<p>Is this an empty block of code?</p>
</div>
<div class="content"><div class='highlight'><pre> isEmpty: <span class="hljs-function">-></span>
<span class="hljs-keyword">not</span> @expressions.length
isStatement: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
<span class="hljs-keyword">for</span> exp <span class="hljs-keyword">in</span> @expressions <span class="hljs-keyword">when</span> exp.isStatement o
<span class="hljs-keyword">return</span> <span class="hljs-literal">yes</span>
<span class="hljs-literal">no</span>
jumps: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
<span class="hljs-keyword">for</span> exp <span class="hljs-keyword">in</span> @expressions
<span class="hljs-keyword">return</span> jumpNode <span class="hljs-keyword">if</span> jumpNode = exp.jumps o</pre></div></div>
</li>
<li id="section-68">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-68">§</a>
</div>
<p>A Block node does not return its entire body, rather it
ensures that the final expression is returned.</p>
</div>
<div class="content"><div class='highlight'><pre> makeReturn: <span class="hljs-function"><span class="hljs-params">(results, mark)</span> -></span>
len = @expressions.length
[..., lastExp] = @expressions
lastExp = lastExp?.unwrap() <span class="hljs-keyword">or</span> <span class="hljs-literal">no</span></pre></div></div>
</li>
<li id="section-69">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-69">§</a>
</div>
<p>We also need to check that we’re not returning a JSX tag if there’s an
adjacent one at the same level; JSX doesn’t allow that.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> lastExp <span class="hljs-keyword">and</span> lastExp <span class="hljs-keyword">instanceof</span> Parens <span class="hljs-keyword">and</span> lastExp.body.expressions.length > <span class="hljs-number">1</span>
{body:{expressions}} = lastExp
[..., penult, last] = expressions
penult = penult.unwrap()
last = last.unwrap()
<span class="hljs-keyword">if</span> penult <span class="hljs-keyword">instanceof</span> JSXElement <span class="hljs-keyword">and</span> last <span class="hljs-keyword">instanceof</span> JSXElement
expressions[expressions.length - <span class="hljs-number">1</span>].error <span class="hljs-string">'Adjacent JSX elements must be wrapped in an enclosing tag'</span>
<span class="hljs-keyword">if</span> mark
@expressions[len - <span class="hljs-number">1</span>]?.makeReturn results, mark
<span class="hljs-keyword">return</span>
<span class="hljs-keyword">while</span> len--
expr = @expressions[len]
@expressions[len] = expr.makeReturn results
@expressions.splice(len, <span class="hljs-number">1</span>) <span class="hljs-keyword">if</span> expr <span class="hljs-keyword">instanceof</span> Return <span class="hljs-keyword">and</span> <span class="hljs-keyword">not</span> expr.expression
<span class="hljs-keyword">break</span>
this
compile: <span class="hljs-function"><span class="hljs-params">(o, lvl)</span> -></span>
<span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> Root(this).withLocationDataFrom(this).compile o, lvl <span class="hljs-keyword">unless</span> o.scope
super o, lvl</pre></div></div>
</li>
<li id="section-70">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-70">§</a>
</div>
<p>Compile all expressions within the <strong>Block</strong> body. If we need to return
the result, and it’s an expression, simply return it. If it’s a statement,
ask the statement to do so.</p>
</div>
<div class="content"><div class='highlight'><pre> compileNode: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
@tab = o.indent
top = o.level <span class="hljs-keyword">is</span> LEVEL_TOP
compiledNodes = []
<span class="hljs-keyword">for</span> node, index <span class="hljs-keyword">in</span> @expressions
<span class="hljs-keyword">if</span> node.hoisted</pre></div></div>
</li>
<li id="section-71">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-71">§</a>
</div>
<p>This is a hoisted expression.
We want to compile this and ignore the result.</p>
</div>
<div class="content"><div class='highlight'><pre> node.compileToFragments o
<span class="hljs-keyword">continue</span>
node = (node.unfoldSoak(o) <span class="hljs-keyword">or</span> node)
<span class="hljs-keyword">if</span> node <span class="hljs-keyword">instanceof</span> Block</pre></div></div>
</li>
<li id="section-72">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-72">§</a>
</div>
<p>This is a nested block. We don’t do anything special here like
enclose it in a new scope; we just compile the statements in this
block along with our own.</p>
</div>
<div class="content"><div class='highlight'><pre> compiledNodes.push node.compileNode o
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> top
node.front = <span class="hljs-literal">yes</span>
fragments = node.compileToFragments o
<span class="hljs-keyword">unless</span> node.isStatement o
fragments = indentInitial fragments, @
[..., lastFragment] = fragments
<span class="hljs-keyword">unless</span> lastFragment.code <span class="hljs-keyword">is</span> <span class="hljs-string">''</span> <span class="hljs-keyword">or</span> lastFragment.isComment
fragments.push @makeCode <span class="hljs-string">';'</span>
compiledNodes.push fragments
<span class="hljs-keyword">else</span>
compiledNodes.push node.compileToFragments o, LEVEL_LIST
<span class="hljs-keyword">if</span> top
<span class="hljs-keyword">if</span> @spaced
<span class="hljs-keyword">return</span> [].concat @joinFragmentArrays(compiledNodes, <span class="hljs-string">'\n\n'</span>), @makeCode(<span class="hljs-string">'\n'</span>)
<span class="hljs-keyword">else</span>
<span class="hljs-keyword">return</span> @joinFragmentArrays(compiledNodes, <span class="hljs-string">'\n'</span>)
<span class="hljs-keyword">if</span> compiledNodes.length
answer = @joinFragmentArrays(compiledNodes, <span class="hljs-string">', '</span>)
<span class="hljs-keyword">else</span>
answer = [@makeCode <span class="hljs-string">'void 0'</span>]
<span class="hljs-keyword">if</span> compiledNodes.length > <span class="hljs-number">1</span> <span class="hljs-keyword">and</span> o.level >= LEVEL_LIST <span class="hljs-keyword">then</span> @wrapInParentheses answer <span class="hljs-keyword">else</span> answer
compileRoot: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
@spaced = <span class="hljs-literal">yes</span>
fragments = @compileWithDeclarations o
HoistTarget.expand fragments
@compileComments fragments</pre></div></div>
</li>
<li id="section-73">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-73">§</a>
</div>
<p>Compile the expressions body for the contents of a function, with
declarations of all inner variables pushed up to the top.</p>
</div>
<div class="content"><div class='highlight'><pre> compileWithDeclarations: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
fragments = []
post = []
<span class="hljs-keyword">for</span> exp, i <span class="hljs-keyword">in</span> @expressions
exp = exp.unwrap()
<span class="hljs-keyword">break</span> <span class="hljs-keyword">unless</span> exp <span class="hljs-keyword">instanceof</span> Literal
o = merge(o, level: LEVEL_TOP)
<span class="hljs-keyword">if</span> i
rest = @expressions.splice i, <span class="hljs-number">9e9</span>
[spaced, @spaced] = [@spaced, <span class="hljs-literal">no</span>]
[fragments, @spaced] = [@compileNode(o), spaced]
@expressions = rest
post = @compileNode o
{scope} = o
<span class="hljs-keyword">if</span> scope.expressions <span class="hljs-keyword">is</span> this
declars = o.scope.hasDeclarations()
assigns = scope.hasAssignments
<span class="hljs-keyword">if</span> declars <span class="hljs-keyword">or</span> assigns
fragments.push @makeCode <span class="hljs-string">'\n'</span> <span class="hljs-keyword">if</span> i
fragments.push @makeCode <span class="hljs-string">"<span class="hljs-subst">#{@tab}</span>var "</span>
<span class="hljs-keyword">if</span> declars
declaredVariables = scope.declaredVariables()
<span class="hljs-keyword">for</span> declaredVariable, declaredVariablesIndex <span class="hljs-keyword">in</span> declaredVariables
fragments.push @makeCode declaredVariable
<span class="hljs-keyword">if</span> Object::hasOwnProperty.call o.scope.comments, declaredVariable
fragments.push o.scope.comments[declaredVariable]...
<span class="hljs-keyword">if</span> declaredVariablesIndex <span class="hljs-keyword">isnt</span> declaredVariables.length - <span class="hljs-number">1</span>
fragments.push @makeCode <span class="hljs-string">', '</span>
<span class="hljs-keyword">if</span> assigns
fragments.push @makeCode <span class="hljs-string">",\n<span class="hljs-subst">#{@tab + TAB}</span>"</span> <span class="hljs-keyword">if</span> declars
fragments.push @makeCode scope.assignedVariables().join(<span class="hljs-string">",\n<span class="hljs-subst">#{@tab + TAB}</span>"</span>)
fragments.push @makeCode <span class="hljs-string">";\n<span class="hljs-subst">#{<span class="hljs-keyword">if</span> @spaced <span class="hljs-keyword">then</span> <span class="hljs-string">'\n'</span> <span class="hljs-keyword">else</span> <span class="hljs-string">''</span>}</span>"</span>
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> fragments.length <span class="hljs-keyword">and</span> post.length
fragments.push @makeCode <span class="hljs-string">"\n"</span>
fragments.concat post
compileComments: <span class="hljs-function"><span class="hljs-params">(fragments)</span> -></span>
<span class="hljs-keyword">for</span> fragment, fragmentIndex <span class="hljs-keyword">in</span> fragments</pre></div></div>
</li>
<li id="section-74">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-74">§</a>
</div>
<p>Insert comments into the output at the next or previous newline.
If there are no newlines at which to place comments, create them.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> fragment.precedingComments</pre></div></div>
</li>
<li id="section-75">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-75">§</a>
</div>
<p>Determine the indentation level of the fragment that we are about
to insert comments before, and use that indentation level for our
inserted comments. At this point, the fragments’ <code>code</code> property
is the generated output JavaScript, and CoffeeScript always
generates output indented by two spaces; so all we need to do is
search for a <code>code</code> property that begins with at least two spaces.</p>
</div>
<div class="content"><div class='highlight'><pre> fragmentIndent = <span class="hljs-string">''</span>
<span class="hljs-keyword">for</span> pastFragment <span class="hljs-keyword">in</span> fragments[<span class="hljs-number">0.</span>..(fragmentIndex + <span class="hljs-number">1</span>)] <span class="hljs-keyword">by</span> <span class="hljs-number">-1</span>
indent = <span class="hljs-regexp">/^ {2,}/m</span>.exec pastFragment.code
<span class="hljs-keyword">if</span> indent
fragmentIndent = indent[<span class="hljs-number">0</span>]
<span class="hljs-keyword">break</span>
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> <span class="hljs-string">'\n'</span> <span class="hljs-keyword">in</span> pastFragment.code
<span class="hljs-keyword">break</span>
code = <span class="hljs-string">"\n<span class="hljs-subst">#{fragmentIndent}</span>"</span> + (
<span class="hljs-keyword">for</span> commentFragment <span class="hljs-keyword">in</span> fragment.precedingComments
<span class="hljs-keyword">if</span> commentFragment.isHereComment <span class="hljs-keyword">and</span> commentFragment.multiline
multident commentFragment.code, fragmentIndent, <span class="hljs-literal">no</span>
<span class="hljs-keyword">else</span>
commentFragment.code
).join(<span class="hljs-string">"\n<span class="hljs-subst">#{fragmentIndent}</span>"</span>).replace <span class="hljs-regexp">/^(\s*)$/gm</span>, <span class="hljs-string">''</span>
<span class="hljs-keyword">for</span> pastFragment, pastFragmentIndex <span class="hljs-keyword">in</span> fragments[<span class="hljs-number">0.</span>..(fragmentIndex + <span class="hljs-number">1</span>)] <span class="hljs-keyword">by</span> <span class="hljs-number">-1</span>
newLineIndex = pastFragment.code.lastIndexOf <span class="hljs-string">'\n'</span>
<span class="hljs-keyword">if</span> newLineIndex <span class="hljs-keyword">is</span> <span class="hljs-number">-1</span></pre></div></div>
</li>
<li id="section-76">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-76">§</a>
</div>
<p>Keep searching previous fragments until we can’t go back any
further, either because there are no fragments left or we’ve
discovered that we’re in a code block that is interpolated
inside a string.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> pastFragmentIndex <span class="hljs-keyword">is</span> <span class="hljs-number">0</span>
pastFragment.code = <span class="hljs-string">'\n'</span> + pastFragment.code
newLineIndex = <span class="hljs-number">0</span>
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> pastFragment.isStringWithInterpolations <span class="hljs-keyword">and</span> pastFragment.code <span class="hljs-keyword">is</span> <span class="hljs-string">'{'</span>
code = code[<span class="hljs-number">1.</span>.] + <span class="hljs-string">'\n'</span> <span class="hljs-comment"># Move newline to end.</span>
newLineIndex = <span class="hljs-number">1</span>
<span class="hljs-keyword">else</span>
<span class="hljs-keyword">continue</span>
<span class="hljs-keyword">delete</span> fragment.precedingComments
pastFragment.code = pastFragment.code[<span class="hljs-number">0.</span>..newLineIndex] +
code + pastFragment.code[newLineIndex..]
<span class="hljs-keyword">break</span></pre></div></div>
</li>
<li id="section-77">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-77">§</a>
</div>
<p>Yes, this is awfully similar to the previous <code>if</code> block, but if you
look closely you’ll find lots of tiny differences that make this
confusing if it were abstracted into a function that both blocks share.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> fragment.followingComments</pre></div></div>
</li>
<li id="section-78">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-78">§</a>
</div>
<p>Does the first trailing comment follow at the end of a line of code,
like <code>; // Comment</code>, or does it start a new line after a line of code?</p>
</div>
<div class="content"><div class='highlight'><pre> trail = fragment.followingComments[<span class="hljs-number">0</span>].trail
fragmentIndent = <span class="hljs-string">''</span></pre></div></div>
</li>
<li id="section-79">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-79">§</a>
</div>
<p>Find the indent of the next line of code, if we have any non-trailing
comments to output. We need to first find the next newline, as these
comments will be output after that; and then the indent of the line
that follows the next newline.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">unless</span> trail <span class="hljs-keyword">and</span> fragment.followingComments.length <span class="hljs-keyword">is</span> <span class="hljs-number">1</span>
onNextLine = <span class="hljs-literal">no</span>
<span class="hljs-keyword">for</span> upcomingFragment <span class="hljs-keyword">in</span> fragments[fragmentIndex...]
<span class="hljs-keyword">unless</span> onNextLine
<span class="hljs-keyword">if</span> <span class="hljs-string">'\n'</span> <span class="hljs-keyword">in</span> upcomingFragment.code
onNextLine = <span class="hljs-literal">yes</span>
<span class="hljs-keyword">else</span>
<span class="hljs-keyword">continue</span>
<span class="hljs-keyword">else</span>
indent = <span class="hljs-regexp">/^ {2,}/m</span>.exec upcomingFragment.code
<span class="hljs-keyword">if</span> indent
fragmentIndent = indent[<span class="hljs-number">0</span>]
<span class="hljs-keyword">break</span>
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> <span class="hljs-string">'\n'</span> <span class="hljs-keyword">in</span> upcomingFragment.code
<span class="hljs-keyword">break</span></pre></div></div>
</li>
<li id="section-80">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-80">§</a>
</div>
<p>Is this comment following the indent inserted by bare mode?
If so, there’s no need to indent this further.</p>
</div>
<div class="content"><div class='highlight'><pre> code = <span class="hljs-keyword">if</span> fragmentIndex <span class="hljs-keyword">is</span> <span class="hljs-number">1</span> <span class="hljs-keyword">and</span> <span class="hljs-regexp">/^\s+$/</span>.test fragments[<span class="hljs-number">0</span>].code
<span class="hljs-string">''</span>
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> trail
<span class="hljs-string">' '</span>
<span class="hljs-keyword">else</span>
<span class="hljs-string">"\n<span class="hljs-subst">#{fragmentIndent}</span>"</span></pre></div></div>
</li>
<li id="section-81">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-81">§</a>
</div>
<p>Assemble properly indented comments.</p>
</div>
<div class="content"><div class='highlight'><pre> code += (
<span class="hljs-keyword">for</span> commentFragment <span class="hljs-keyword">in</span> fragment.followingComments
<span class="hljs-keyword">if</span> commentFragment.isHereComment <span class="hljs-keyword">and</span> commentFragment.multiline
multident commentFragment.code, fragmentIndent, <span class="hljs-literal">no</span>
<span class="hljs-keyword">else</span>
commentFragment.code
).join(<span class="hljs-string">"\n<span class="hljs-subst">#{fragmentIndent}</span>"</span>).replace <span class="hljs-regexp">/^(\s*)$/gm</span>, <span class="hljs-string">''</span>
<span class="hljs-keyword">for</span> upcomingFragment, upcomingFragmentIndex <span class="hljs-keyword">in</span> fragments[fragmentIndex...]
newLineIndex = upcomingFragment.code.indexOf <span class="hljs-string">'\n'</span>
<span class="hljs-keyword">if</span> newLineIndex <span class="hljs-keyword">is</span> <span class="hljs-number">-1</span></pre></div></div>
</li>
<li id="section-82">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-82">§</a>
</div>
<p>Keep searching upcoming fragments until we can’t go any
further, either because there are no fragments left or we’ve
discovered that we’re in a code block that is interpolated
inside a string.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> upcomingFragmentIndex <span class="hljs-keyword">is</span> fragments.length - <span class="hljs-number">1</span>
upcomingFragment.code = upcomingFragment.code + <span class="hljs-string">'\n'</span>
newLineIndex = upcomingFragment.code.length
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> upcomingFragment.isStringWithInterpolations <span class="hljs-keyword">and</span> upcomingFragment.code <span class="hljs-keyword">is</span> <span class="hljs-string">'}'</span>
code = <span class="hljs-string">"<span class="hljs-subst">#{code}</span>\n"</span>
newLineIndex = <span class="hljs-number">0</span>
<span class="hljs-keyword">else</span>
<span class="hljs-keyword">continue</span>
<span class="hljs-keyword">delete</span> fragment.followingComments</pre></div></div>
</li>
<li id="section-83">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-83">§</a>
</div>
<p>Avoid inserting extra blank lines.</p>
</div>
<div class="content"><div class='highlight'><pre> code = code.replace <span class="hljs-regexp">/^\n/</span>, <span class="hljs-string">''</span> <span class="hljs-keyword">if</span> upcomingFragment.code <span class="hljs-keyword">is</span> <span class="hljs-string">'\n'</span>
upcomingFragment.code = upcomingFragment.code[<span class="hljs-number">0.</span>..newLineIndex] +
code + upcomingFragment.code[newLineIndex..]
<span class="hljs-keyword">break</span>
fragments</pre></div></div>
</li>
<li id="section-84">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-84">§</a>
</div>
<p>Wrap up the given nodes as a <strong>Block</strong>, unless it already happens
to be one.</p>
</div>
<div class="content"><div class='highlight'><pre> @wrap: <span class="hljs-function"><span class="hljs-params">(nodes)</span> -></span>
<span class="hljs-keyword">return</span> nodes[<span class="hljs-number">0</span>] <span class="hljs-keyword">if</span> nodes.length <span class="hljs-keyword">is</span> <span class="hljs-number">1</span> <span class="hljs-keyword">and</span> nodes[<span class="hljs-number">0</span>] <span class="hljs-keyword">instanceof</span> Block
<span class="hljs-keyword">new</span> Block nodes
astNode: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
<span class="hljs-keyword">if</span> (o.level? <span class="hljs-keyword">and</span> o.level <span class="hljs-keyword">isnt</span> LEVEL_TOP) <span class="hljs-keyword">and</span> @expressions.length
<span class="hljs-keyword">return</span> (<span class="hljs-keyword">new</span> Sequence(@expressions).withLocationDataFrom @).ast o
super o
astType: <span class="hljs-function">-></span>
<span class="hljs-keyword">if</span> @isRootBlock
<span class="hljs-string">'Program'</span>
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> @isClassBody
<span class="hljs-string">'ClassBody'</span>
<span class="hljs-keyword">else</span>
<span class="hljs-string">'BlockStatement'</span>
astProperties: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
checkForDirectives = del o, <span class="hljs-string">'checkForDirectives'</span>
sniffDirectives @expressions, notFinalExpression: checkForDirectives <span class="hljs-keyword">if</span> @isRootBlock <span class="hljs-keyword">or</span> checkForDirectives
directives = []
body = []
<span class="hljs-keyword">for</span> expression <span class="hljs-keyword">in</span> @expressions
expressionAst = expression.ast o</pre></div></div>
</li>
<li id="section-85">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-85">§</a>
</div>
<p>Ignore generated PassthroughLiteral</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> expressionAst?
<span class="hljs-keyword">continue</span>
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> expression <span class="hljs-keyword">instanceof</span> Directive
directives.push expressionAst</pre></div></div>
</li>
<li id="section-86">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-86">§</a>
</div>
<p>If an expression is a statement, it can be added to the body as is.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> expression.isStatementAst o
body.push expressionAst</pre></div></div>
</li>
<li id="section-87">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-87">§</a>
</div>
<p>Otherwise, we need to wrap it in an <code>ExpressionStatement</code> AST node.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">else</span>
body.push <span class="hljs-built_in">Object</span>.assign
type: <span class="hljs-string">'ExpressionStatement'</span>
expression: expressionAst
,
expression.astLocationData()
<span class="hljs-keyword">return</span> {</pre></div></div>
</li>
<li id="section-88">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-88">§</a>
</div>
<p>For now, we’re not including <code>sourceType</code> on the <code>Program</code> AST node.
Its value could be either <code>'script'</code> or <code>'module'</code>, and there’s no way
for CoffeeScript to always know which it should be. The presence of an
<code>import</code> or <code>export</code> statement in source code would imply that it should
be a <code>module</code>, but a project may consist of mostly such files and also
an outlier file that lacks <code>import</code> or <code>export</code> but is still imported
into the project and therefore expects to be treated as a <code>module</code>.
Determining the value of <code>sourceType</code> is essentially the same challenge
posed by determining the parse goal of a JavaScript file, also <code>module</code>
or <code>script</code>, and so if Node figures out a way to do so for <code>.js</code> files
then CoffeeScript can copy Node’s algorithm.</p>
</div>
</li>
<li id="section-89">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-89">§</a>
</div>
<p>sourceType: ‘module’</p>
</div>
<div class="content"><div class='highlight'><pre> body, directives
}
astLocationData: <span class="hljs-function">-></span>
<span class="hljs-keyword">return</span> <span class="hljs-keyword">if</span> @isRootBlock <span class="hljs-keyword">and</span> <span class="hljs-keyword">not</span> @locationData?
super()</pre></div></div>
</li>
<li id="section-90">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-90">§</a>
</div>
<p>A directive e.g. ‘use strict’.
Currently only used during AST generation.</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-built_in">exports</span>.Directive = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Directive</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Base</span></span>
constructor: <span class="hljs-function"><span class="hljs-params">(@value)</span> -></span>
super()
astProperties: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
<span class="hljs-keyword">return</span>
value: <span class="hljs-built_in">Object</span>.assign {},
@value.ast o
type: <span class="hljs-string">'DirectiveLiteral'</span></pre></div></div>
</li>
<li id="section-91">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-91">§</a>
</div>
<h3 id="literal">Literal</h3>
</div>
</li>
<li id="section-92">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-92">§</a>
</div>
<p><code>Literal</code> is a base class for static values that can be passed through
directly into JavaScript without translation, such as: strings, numbers,
<code>true</code>, <code>false</code>, <code>null</code>…</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-built_in">exports</span>.Literal = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Literal</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Base</span></span>
constructor: <span class="hljs-function"><span class="hljs-params">(@value)</span> -></span>
super()
shouldCache: NO
assigns: <span class="hljs-function"><span class="hljs-params">(name)</span> -></span>
name <span class="hljs-keyword">is</span> @value
compileNode: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
[@makeCode @value]
astProperties: <span class="hljs-function">-></span>
<span class="hljs-keyword">return</span>
value: @value
toString: <span class="hljs-function">-></span></pre></div></div>
</li>
<li id="section-93">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-93">§</a>
</div>
<p>This is only intended for debugging.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-string">" <span class="hljs-subst">#{<span class="hljs-keyword">if</span> @isStatement() <span class="hljs-keyword">then</span> super() <span class="hljs-keyword">else</span> @constructor.name}</span>: <span class="hljs-subst">#{@value}</span>"</span>
<span class="hljs-built_in">exports</span>.NumberLiteral = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">NumberLiteral</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Literal</span></span>
constructor: <span class="hljs-function"><span class="hljs-params">(@value, {@parsedValue} = {})</span> -></span>
super()
<span class="hljs-keyword">unless</span> @parsedValue?
<span class="hljs-keyword">if</span> isNumber @value
@parsedValue = @value
@value = <span class="hljs-string">"<span class="hljs-subst">#{@value}</span>"</span>
<span class="hljs-keyword">else</span>
@parsedValue = parseNumber @value
isBigInt: <span class="hljs-function">-></span>
<span class="hljs-regexp">/n$/</span>.test @value
astType: <span class="hljs-function">-></span>
<span class="hljs-keyword">if</span> @isBigInt()
<span class="hljs-string">'BigIntLiteral'</span>
<span class="hljs-keyword">else</span>
<span class="hljs-string">'NumericLiteral'</span>
astProperties: <span class="hljs-function">-></span>
<span class="hljs-keyword">return</span>
value:
<span class="hljs-keyword">if</span> @isBigInt()
@parsedValue.toString()
<span class="hljs-keyword">else</span>
@parsedValue
extra:
rawValue:
<span class="hljs-keyword">if</span> @isBigInt()
@parsedValue.toString()
<span class="hljs-keyword">else</span>
@parsedValue
raw: @value
<span class="hljs-built_in">exports</span>.InfinityLiteral = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">InfinityLiteral</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">NumberLiteral</span></span>
constructor: <span class="hljs-function"><span class="hljs-params">(@value, {@originalValue = <span class="hljs-string">'Infinity'</span>} = {})</span> -></span>
super()
compileNode: <span class="hljs-function">-></span>
[@makeCode <span class="hljs-string">'2e308'</span>]
astNode: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
<span class="hljs-keyword">unless</span> @originalValue <span class="hljs-keyword">is</span> <span class="hljs-string">'Infinity'</span>
<span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> NumberLiteral(@value).withLocationDataFrom(@).ast o
super o
astType: <span class="hljs-function">-></span> <span class="hljs-string">'Identifier'</span>
astProperties: <span class="hljs-function">-></span>
<span class="hljs-keyword">return</span>
name: <span class="hljs-string">'Infinity'</span>
declaration: <span class="hljs-literal">no</span>
<span class="hljs-built_in">exports</span>.NaNLiteral = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">NaNLiteral</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">NumberLiteral</span></span>
constructor: <span class="hljs-function">-></span>
super <span class="hljs-string">'NaN'</span>
compileNode: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
code = [@makeCode <span class="hljs-string">'0/0'</span>]
<span class="hljs-keyword">if</span> o.level >= LEVEL_OP <span class="hljs-keyword">then</span> @wrapInParentheses code <span class="hljs-keyword">else</span> code
astType: <span class="hljs-function">-></span> <span class="hljs-string">'Identifier'</span>
astProperties: <span class="hljs-function">-></span>
<span class="hljs-keyword">return</span>
name: <span class="hljs-string">'NaN'</span>
declaration: <span class="hljs-literal">no</span>
<span class="hljs-built_in">exports</span>.StringLiteral = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">StringLiteral</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Literal</span></span>
constructor: <span class="hljs-function"><span class="hljs-params">(@originalValue, {@quote, @initialChunk, @finalChunk, @indent, @double, @heregex} = {})</span> -></span>
super <span class="hljs-string">''</span>
@quote = <span class="hljs-literal">null</span> <span class="hljs-keyword">if</span> @quote <span class="hljs-keyword">is</span> <span class="hljs-string">'///'</span>
@fromSourceString = @quote?
@quote ?= <span class="hljs-string">'"'</span>
heredoc = @isFromHeredoc()
val = @originalValue
<span class="hljs-keyword">if</span> @heregex
val = val.replace HEREGEX_OMIT, <span class="hljs-string">'$1$2'</span>
val = replaceUnicodeCodePointEscapes val, flags: @heregex.flags
<span class="hljs-keyword">else</span>
val = val.replace STRING_OMIT, <span class="hljs-string">'$1'</span>
val =
<span class="hljs-keyword">unless</span> @fromSourceString
val
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> heredoc
indentRegex = <span class="hljs-regexp">/// \n<span class="hljs-subst">#{@indent}</span> ///</span>g <span class="hljs-keyword">if</span> @indent
val = val.replace indentRegex, <span class="hljs-string">'\n'</span> <span class="hljs-keyword">if</span> indentRegex
val = val.replace LEADING_BLANK_LINE, <span class="hljs-string">''</span> <span class="hljs-keyword">if</span> @initialChunk
val = val.replace TRAILING_BLANK_LINE, <span class="hljs-string">''</span> <span class="hljs-keyword">if</span> @finalChunk
val
<span class="hljs-keyword">else</span>
val.replace SIMPLE_STRING_OMIT, <span class="hljs-function"><span class="hljs-params">(match, offset)</span> =></span>
<span class="hljs-keyword">if</span> (@initialChunk <span class="hljs-keyword">and</span> offset <span class="hljs-keyword">is</span> <span class="hljs-number">0</span>) <span class="hljs-keyword">or</span>
(@finalChunk <span class="hljs-keyword">and</span> offset + match.length <span class="hljs-keyword">is</span> val.length)
<span class="hljs-string">''</span>
<span class="hljs-keyword">else</span>
<span class="hljs-string">' '</span>
@delimiter = @quote.charAt <span class="hljs-number">0</span>
@value = makeDelimitedLiteral val, {
@delimiter
@double
}
@unquotedValueForTemplateLiteral = makeDelimitedLiteral val, {
delimiter: <span class="hljs-string">'`'</span>
@double
escapeNewlines: <span class="hljs-literal">no</span>
includeDelimiters: <span class="hljs-literal">no</span>
convertTrailingNullEscapes: <span class="hljs-literal">yes</span>
}
@unquotedValueForJSX = makeDelimitedLiteral val, {
@double
escapeNewlines: <span class="hljs-literal">no</span>
includeDelimiters: <span class="hljs-literal">no</span>
escapeDelimiter: <span class="hljs-literal">no</span>
}
compileNode: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
<span class="hljs-keyword">return</span> StringWithInterpolations.fromStringLiteral(@).compileNode o <span class="hljs-keyword">if</span> @shouldGenerateTemplateLiteral()
<span class="hljs-keyword">return</span> [@makeCode @unquotedValueForJSX] <span class="hljs-keyword">if</span> @jsx
super o</pre></div></div>
</li>
<li id="section-94">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-94">§</a>
</div>
<p><code>StringLiteral</code>s can represent either entire literal strings
or pieces of text inside of e.g. an interpolated string.
When parsed as the former but needing to be treated as the latter
(e.g. the string part of a tagged template literal), this will return
a copy of the <code>StringLiteral</code> with the quotes trimmed from its location
data (like it would have if parsed as part of an interpolated string).</p>
</div>
<div class="content"><div class='highlight'><pre> withoutQuotesInLocationData: <span class="hljs-function">-></span>
endsWithNewline = @originalValue[<span class="hljs-number">-1.</span>.] <span class="hljs-keyword">is</span> <span class="hljs-string">'\n'</span>
locationData = <span class="hljs-built_in">Object</span>.assign {}, @locationData
locationData.first_column += @quote.length
<span class="hljs-keyword">if</span> endsWithNewline
locationData.last_line -= <span class="hljs-number">1</span>
locationData.last_column =
<span class="hljs-keyword">if</span> locationData.last_line <span class="hljs-keyword">is</span> locationData.first_line
locationData.first_column + @originalValue.length - <span class="hljs-string">'\n'</span>.length
<span class="hljs-keyword">else</span>
@originalValue[...<span class="hljs-number">-1</span>].length - <span class="hljs-string">'\n'</span>.length - @originalValue[...<span class="hljs-number">-1</span>].lastIndexOf(<span class="hljs-string">'\n'</span>)
<span class="hljs-keyword">else</span>
locationData.last_column -= @quote.length
locationData.last_column_exclusive -= @quote.length
locationData.range = [
locationData.range[<span class="hljs-number">0</span>] + @quote.length
locationData.range[<span class="hljs-number">1</span>] - @quote.length
]
copy = <span class="hljs-keyword">new</span> StringLiteral @originalValue, {@quote, @initialChunk, @finalChunk, @indent, @double, @heregex}
copy.locationData = locationData
copy
isFromHeredoc: <span class="hljs-function">-></span>
@quote.length <span class="hljs-keyword">is</span> <span class="hljs-number">3</span>
shouldGenerateTemplateLiteral: <span class="hljs-function">-></span>
@isFromHeredoc()
astNode: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
<span class="hljs-keyword">return</span> StringWithInterpolations.fromStringLiteral(@).ast o <span class="hljs-keyword">if</span> @shouldGenerateTemplateLiteral()
super o
astProperties: <span class="hljs-function">-></span>
<span class="hljs-keyword">return</span>
value: @originalValue
extra:
raw: <span class="hljs-string">"<span class="hljs-subst">#{@delimiter}</span><span class="hljs-subst">#{@originalValue}</span><span class="hljs-subst">#{@delimiter}</span>"</span>
<span class="hljs-built_in">exports</span>.RegexLiteral = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">RegexLiteral</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Literal</span></span>
constructor: <span class="hljs-function"><span class="hljs-params">(value, {@delimiter = <span class="hljs-string">'/'</span>, @heregexCommentTokens = []} = {})</span> -></span>
super <span class="hljs-string">''</span>
heregex = @delimiter <span class="hljs-keyword">is</span> <span class="hljs-string">'///'</span>
endDelimiterIndex = value.lastIndexOf <span class="hljs-string">'/'</span>
@flags = value[endDelimiterIndex + <span class="hljs-number">1.</span>.]
val = @originalValue = value[<span class="hljs-number">1.</span>..endDelimiterIndex]
val = val.replace HEREGEX_OMIT, <span class="hljs-string">'$1$2'</span> <span class="hljs-keyword">if</span> heregex
val = replaceUnicodeCodePointEscapes val, {@flags}
@value = <span class="hljs-string">"<span class="hljs-subst">#{makeDelimitedLiteral val, delimiter: <span class="hljs-string">'/'</span>}</span><span class="hljs-subst">#{@flags}</span>"</span>
REGEX_REGEX: <span class="hljs-regexp">/// ^ / (.*) / \w* $ ///</span>
astType: <span class="hljs-function">-></span> <span class="hljs-string">'RegExpLiteral'</span>
astProperties: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
[, pattern] = @REGEX_REGEX.exec @value
<span class="hljs-keyword">return</span> {
value: <span class="hljs-literal">undefined</span>
pattern, @flags, @delimiter
originalPattern: @originalValue
extra:
raw: @value
originalRaw: <span class="hljs-string">"<span class="hljs-subst">#{@delimiter}</span><span class="hljs-subst">#{@originalValue}</span><span class="hljs-subst">#{@delimiter}</span><span class="hljs-subst">#{@flags}</span>"</span>
rawValue: <span class="hljs-literal">undefined</span>
comments:
<span class="hljs-keyword">for</span> heregexCommentToken <span class="hljs-keyword">in</span> @heregexCommentTokens
<span class="hljs-keyword">if</span> heregexCommentToken.here
<span class="hljs-keyword">new</span> HereComment(heregexCommentToken).ast o
<span class="hljs-keyword">else</span>
<span class="hljs-keyword">new</span> LineComment(heregexCommentToken).ast o
}
<span class="hljs-built_in">exports</span>.PassthroughLiteral = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">PassthroughLiteral</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Literal</span></span>
constructor: <span class="hljs-function"><span class="hljs-params">(@originalValue, {@here, @generated} = {})</span> -></span>
super <span class="hljs-string">''</span>
@value = @originalValue.replace <span class="hljs-regexp">/\\+(`|$)/g</span>, <span class="hljs-function"><span class="hljs-params">(string)</span> -></span></pre></div></div>
</li>
<li id="section-95">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-95">§</a>
</div>
<p><code>string</code> is always a value like ‘`‘, ‘\`‘, ‘\\`‘, etc.
By reducing it to its latter half, we turn ‘`‘ to ‘<code>', '\\\</code>‘ to ‘`‘, etc.</p>
</div>
<div class="content"><div class='highlight'><pre> string[-<span class="hljs-built_in">Math</span>.ceil(string.length / <span class="hljs-number">2</span>)..]
astNode: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
<span class="hljs-keyword">return</span> <span class="hljs-literal">null</span> <span class="hljs-keyword">if</span> @generated
super o
astProperties: <span class="hljs-function">-></span>
<span class="hljs-keyword">return</span> {
value: @originalValue
here: !!@here
}
<span class="hljs-built_in">exports</span>.IdentifierLiteral = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">IdentifierLiteral</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Literal</span></span>
isAssignable: YES
eachName: <span class="hljs-function"><span class="hljs-params">(iterator)</span> -></span>
iterator @
astType: <span class="hljs-function">-></span>
<span class="hljs-keyword">if</span> @jsx
<span class="hljs-string">'JSXIdentifier'</span>
<span class="hljs-keyword">else</span>
<span class="hljs-string">'Identifier'</span>
astProperties: <span class="hljs-function">-></span>
<span class="hljs-keyword">return</span>
name: @value
declaration: !!@isDeclaration
<span class="hljs-built_in">exports</span>.PropertyName = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">PropertyName</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Literal</span></span>
isAssignable: YES
astType: <span class="hljs-function">-></span>
<span class="hljs-keyword">if</span> @jsx
<span class="hljs-string">'JSXIdentifier'</span>
<span class="hljs-keyword">else</span>
<span class="hljs-string">'Identifier'</span>
astProperties: <span class="hljs-function">-></span>
<span class="hljs-keyword">return</span>
name: @value
declaration: <span class="hljs-literal">no</span>
<span class="hljs-built_in">exports</span>.ComputedPropertyName = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ComputedPropertyName</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">PropertyName</span></span>
compileNode: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
[@makeCode(<span class="hljs-string">'['</span>), @value.compileToFragments(o, LEVEL_LIST)..., @makeCode(<span class="hljs-string">']'</span>)]
astNode: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
@value.ast o
<span class="hljs-built_in">exports</span>.StatementLiteral = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">StatementLiteral</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Literal</span></span>
isStatement: YES
makeReturn: THIS
jumps: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
<span class="hljs-keyword">return</span> this <span class="hljs-keyword">if</span> @value <span class="hljs-keyword">is</span> <span class="hljs-string">'break'</span> <span class="hljs-keyword">and</span> <span class="hljs-keyword">not</span> (o?.<span class="hljs-keyword">loop</span> <span class="hljs-keyword">or</span> o?.block)
<span class="hljs-keyword">return</span> this <span class="hljs-keyword">if</span> @value <span class="hljs-keyword">is</span> <span class="hljs-string">'continue'</span> <span class="hljs-keyword">and</span> <span class="hljs-keyword">not</span> o?.<span class="hljs-keyword">loop</span>
compileNode: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
[@makeCode <span class="hljs-string">"<span class="hljs-subst">#{@tab}</span><span class="hljs-subst">#{@value}</span>;"</span>]
astType: <span class="hljs-function">-></span>
<span class="hljs-keyword">switch</span> @value
<span class="hljs-keyword">when</span> <span class="hljs-string">'continue'</span> <span class="hljs-keyword">then</span> <span class="hljs-string">'ContinueStatement'</span>
<span class="hljs-keyword">when</span> <span class="hljs-string">'break'</span> <span class="hljs-keyword">then</span> <span class="hljs-string">'BreakStatement'</span>
<span class="hljs-keyword">when</span> <span class="hljs-string">'debugger'</span> <span class="hljs-keyword">then</span> <span class="hljs-string">'DebuggerStatement'</span>
<span class="hljs-built_in">exports</span>.ThisLiteral = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ThisLiteral</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Literal</span></span>
constructor: <span class="hljs-function"><span class="hljs-params">(value)</span> -></span>
super <span class="hljs-string">'this'</span>
@shorthand = value <span class="hljs-keyword">is</span> <span class="hljs-string">'@'</span>
compileNode: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
code = <span class="hljs-keyword">if</span> o.scope.method?.bound <span class="hljs-keyword">then</span> o.scope.method.context <span class="hljs-keyword">else</span> @value
[@makeCode code]
astType: <span class="hljs-function">-></span> <span class="hljs-string">'ThisExpression'</span>
astProperties: <span class="hljs-function">-></span>
<span class="hljs-keyword">return</span>
shorthand: @shorthand
<span class="hljs-built_in">exports</span>.UndefinedLiteral = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">UndefinedLiteral</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Literal</span></span>
constructor: <span class="hljs-function">-></span>
super <span class="hljs-string">'undefined'</span>
compileNode: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
[@makeCode <span class="hljs-keyword">if</span> o.level >= LEVEL_ACCESS <span class="hljs-keyword">then</span> <span class="hljs-string">'(void 0)'</span> <span class="hljs-keyword">else</span> <span class="hljs-string">'void 0'</span>]
astType: <span class="hljs-function">-></span> <span class="hljs-string">'Identifier'</span>
astProperties: <span class="hljs-function">-></span>
<span class="hljs-keyword">return</span>
name: @value
declaration: <span class="hljs-literal">no</span>
<span class="hljs-built_in">exports</span>.NullLiteral = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">NullLiteral</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Literal</span></span>
constructor: <span class="hljs-function">-></span>
super <span class="hljs-string">'null'</span>
<span class="hljs-built_in">exports</span>.BooleanLiteral = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">BooleanLiteral</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Literal</span></span>
constructor: <span class="hljs-function"><span class="hljs-params">(value, {@originalValue} = {})</span> -></span>
super value
@originalValue ?= @value
astProperties: <span class="hljs-function">-></span>
value: <span class="hljs-keyword">if</span> @value <span class="hljs-keyword">is</span> <span class="hljs-string">'true'</span> <span class="hljs-keyword">then</span> <span class="hljs-literal">yes</span> <span class="hljs-keyword">else</span> <span class="hljs-literal">no</span>
name: @originalValue
<span class="hljs-built_in">exports</span>.DefaultLiteral = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">DefaultLiteral</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Literal</span></span>
astType: <span class="hljs-function">-></span> <span class="hljs-string">'Identifier'</span>
astProperties: <span class="hljs-function">-></span>
<span class="hljs-keyword">return</span>
name: <span class="hljs-string">'default'</span>
declaration: <span class="hljs-literal">no</span></pre></div></div>
</li>
<li id="section-96">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-96">§</a>
</div>
<h3 id="return">Return</h3>
</div>
</li>
<li id="section-97">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-97">§</a>
</div>
<p>A <code>return</code> is a <em>pureStatement</em>—wrapping it in a closure wouldn’t make sense.</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-built_in">exports</span>.Return = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Return</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Base</span></span>
constructor: <span class="hljs-function"><span class="hljs-params">(@expression, {@belongsToFuncDirectiveReturn} = {})</span> -></span>
super()
children: [<span class="hljs-string">'expression'</span>]
isStatement: YES
makeReturn: THIS
jumps: THIS
compileToFragments: <span class="hljs-function"><span class="hljs-params">(o, level)</span> -></span>
expr = @expression?.makeReturn()
<span class="hljs-keyword">if</span> expr <span class="hljs-keyword">and</span> expr <span class="hljs-keyword">not</span> <span class="hljs-keyword">instanceof</span> Return <span class="hljs-keyword">then</span> expr.compileToFragments o, level <span class="hljs-keyword">else</span> super o, level
compileNode: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
answer = []</pre></div></div>
</li>
<li id="section-98">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-98">§</a>
</div>
<p>TODO: If we call <code>expression.compile()</code> here twice, we’ll sometimes
get back different results!</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> @expression
answer = @expression.compileToFragments o, LEVEL_PAREN
unshiftAfterComments answer, @makeCode <span class="hljs-string">"<span class="hljs-subst">#{@tab}</span>return "</span></pre></div></div>
</li>
<li id="section-99">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-99">§</a>
</div>
<p>Since the <code>return</code> got indented by <code>@tab</code>, preceding comments that are
multiline need to be indented.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">for</span> fragment <span class="hljs-keyword">in</span> answer
<span class="hljs-keyword">if</span> fragment.isHereComment <span class="hljs-keyword">and</span> <span class="hljs-string">'\n'</span> <span class="hljs-keyword">in</span> fragment.code
fragment.code = multident fragment.code, @tab
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> fragment.isLineComment
fragment.code = <span class="hljs-string">"<span class="hljs-subst">#{@tab}</span><span class="hljs-subst">#{fragment.code}</span>"</span>
<span class="hljs-keyword">else</span>
<span class="hljs-keyword">break</span>
<span class="hljs-keyword">else</span>
answer.push @makeCode <span class="hljs-string">"<span class="hljs-subst">#{@tab}</span>return"</span>
answer.push @makeCode <span class="hljs-string">';'</span>
answer
checkForPureStatementInExpression: <span class="hljs-function">-></span></pre></div></div>
</li>
<li id="section-100">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-100">§</a>
</div>
<p>don’t flag <code>return</code> from <code>await return</code>/<code>yield return</code> as invalid.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">return</span> <span class="hljs-keyword">if</span> @belongsToFuncDirectiveReturn
super()
astType: <span class="hljs-function">-></span> <span class="hljs-string">'ReturnStatement'</span>
astProperties: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
argument: @expression?.ast(o, LEVEL_PAREN) ? <span class="hljs-literal">null</span></pre></div></div>
</li>
<li id="section-101">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-101">§</a>
</div>
<p>Parent class for <code>YieldReturn</code>/<code>AwaitReturn</code>.</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-built_in">exports</span>.FuncDirectiveReturn = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">FuncDirectiveReturn</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Return</span></span>
constructor: <span class="hljs-function"><span class="hljs-params">(expression, {@returnKeyword})</span> -></span>
super expression
compileNode: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
@checkScope o
super o
checkScope: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
<span class="hljs-keyword">unless</span> o.scope.parent?
@error <span class="hljs-string">"<span class="hljs-subst">#{@keyword}</span> can only occur inside functions"</span>
isStatementAst: NO
astNode: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
@checkScope o
<span class="hljs-keyword">new</span> Op @keyword,
<span class="hljs-keyword">new</span> Return @expression, belongsToFuncDirectiveReturn: <span class="hljs-literal">yes</span>
.withLocationDataFrom(
<span class="hljs-keyword">if</span> @expression?
locationData: mergeLocationData @returnKeyword.locationData, @expression.locationData
<span class="hljs-keyword">else</span>
@returnKeyword
)
.withLocationDataFrom @
.ast o</pre></div></div>
</li>
<li id="section-102">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-102">§</a>
</div>
<p><code>yield return</code> works exactly like <code>return</code>, except that it turns the function
into a generator.</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-built_in">exports</span>.YieldReturn = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">YieldReturn</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">FuncDirectiveReturn</span></span>
keyword: <span class="hljs-string">'yield'</span>
<span class="hljs-built_in">exports</span>.AwaitReturn = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">AwaitReturn</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">FuncDirectiveReturn</span></span>
keyword: <span class="hljs-string">'await'</span></pre></div></div>
</li>
<li id="section-103">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-103">§</a>
</div>
<h3 id="value">Value</h3>
</div>
</li>
<li id="section-104">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-104">§</a>
</div>
<p>A value, variable or literal or parenthesized, indexed or dotted into,
or vanilla.</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-built_in">exports</span>.Value = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Value</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Base</span></span>
constructor: <span class="hljs-function"><span class="hljs-params">(base, props, tag, isDefaultValue = <span class="hljs-literal">no</span>)</span> -></span>
super()
<span class="hljs-keyword">return</span> base <span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> props <span class="hljs-keyword">and</span> base <span class="hljs-keyword">instanceof</span> Value
@base = base
@properties = props <span class="hljs-keyword">or</span> []
@tag = tag
@[tag] = <span class="hljs-literal">yes</span> <span class="hljs-keyword">if</span> tag
@isDefaultValue = isDefaultValue</pre></div></div>
</li>
<li id="section-105">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-105">§</a>
</div>
<p>If this is a <code>@foo =</code> assignment, if there are comments on <code>@</code> move them
to be on <code>foo</code>.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> @base?.comments <span class="hljs-keyword">and</span> @base <span class="hljs-keyword">instanceof</span> ThisLiteral <span class="hljs-keyword">and</span> @properties[<span class="hljs-number">0</span>]?.name?
moveComments @base, @properties[<span class="hljs-number">0</span>].name
children: [<span class="hljs-string">'base'</span>, <span class="hljs-string">'properties'</span>]</pre></div></div>
</li>
<li id="section-106">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-106">§</a>
</div>
<p>Add a property (or <em>properties</em> ) <code>Access</code> to the list.</p>
</div>
<div class="content"><div class='highlight'><pre> add: <span class="hljs-function"><span class="hljs-params">(props)</span> -></span>
@properties = @properties.concat props
@forceUpdateLocation = <span class="hljs-literal">yes</span>
this
hasProperties: <span class="hljs-function">-></span>
@properties.length <span class="hljs-keyword">isnt</span> <span class="hljs-number">0</span>
bareLiteral: <span class="hljs-function"><span class="hljs-params">(type)</span> -></span>
<span class="hljs-keyword">not</span> @properties.length <span class="hljs-keyword">and</span> @base <span class="hljs-keyword">instanceof</span> type</pre></div></div>
</li>
<li id="section-107">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-107">§</a>
</div>
<p>Some boolean checks for the benefit of other nodes.</p>
</div>
<div class="content"><div class='highlight'><pre> isArray : <span class="hljs-function">-></span> @bareLiteral(Arr)
isRange : <span class="hljs-function">-></span> @bareLiteral(Range)
shouldCache : <span class="hljs-function">-></span> @hasProperties() <span class="hljs-keyword">or</span> @base.shouldCache()
isAssignable : <span class="hljs-function"><span class="hljs-params">(opts)</span> -></span> @hasProperties() <span class="hljs-keyword">or</span> @base.isAssignable opts
isNumber : <span class="hljs-function">-></span> @bareLiteral(NumberLiteral)
isString : <span class="hljs-function">-></span> @bareLiteral(StringLiteral)
isRegex : <span class="hljs-function">-></span> @bareLiteral(RegexLiteral)
isUndefined : <span class="hljs-function">-></span> @bareLiteral(UndefinedLiteral)
isNull : <span class="hljs-function">-></span> @bareLiteral(NullLiteral)
isBoolean : <span class="hljs-function">-></span> @bareLiteral(BooleanLiteral)
isAtomic : <span class="hljs-function">-></span>
<span class="hljs-keyword">for</span> node <span class="hljs-keyword">in</span> @properties.concat @base
<span class="hljs-keyword">return</span> <span class="hljs-literal">no</span> <span class="hljs-keyword">if</span> node.soak <span class="hljs-keyword">or</span> node <span class="hljs-keyword">instanceof</span> Call <span class="hljs-keyword">or</span> node <span class="hljs-keyword">instanceof</span> Op <span class="hljs-keyword">and</span> node.operator <span class="hljs-keyword">is</span> <span class="hljs-string">'do'</span>
<span class="hljs-literal">yes</span>
isNotCallable : <span class="hljs-function">-></span> @isNumber() <span class="hljs-keyword">or</span> @isString() <span class="hljs-keyword">or</span> @isRegex() <span class="hljs-keyword">or</span>
@isArray() <span class="hljs-keyword">or</span> @isRange() <span class="hljs-keyword">or</span> @isSplice() <span class="hljs-keyword">or</span> @isObject() <span class="hljs-keyword">or</span>
@isUndefined() <span class="hljs-keyword">or</span> @isNull() <span class="hljs-keyword">or</span> @isBoolean()
isStatement : <span class="hljs-function"><span class="hljs-params">(o)</span> -></span> <span class="hljs-keyword">not</span> @properties.length <span class="hljs-keyword">and</span> @base.isStatement o
isJSXTag : <span class="hljs-function">-></span> @base <span class="hljs-keyword">instanceof</span> JSXTag
assigns : <span class="hljs-function"><span class="hljs-params">(name)</span> -></span> <span class="hljs-keyword">not</span> @properties.length <span class="hljs-keyword">and</span> @base.assigns name
jumps : <span class="hljs-function"><span class="hljs-params">(o)</span> -></span> <span class="hljs-keyword">not</span> @properties.length <span class="hljs-keyword">and</span> @base.jumps o
isObject: <span class="hljs-function"><span class="hljs-params">(onlyGenerated)</span> -></span>
<span class="hljs-keyword">return</span> <span class="hljs-literal">no</span> <span class="hljs-keyword">if</span> @properties.length
(@base <span class="hljs-keyword">instanceof</span> Obj) <span class="hljs-keyword">and</span> (<span class="hljs-keyword">not</span> onlyGenerated <span class="hljs-keyword">or</span> @base.generated)
isElision: <span class="hljs-function">-></span>
<span class="hljs-keyword">return</span> <span class="hljs-literal">no</span> <span class="hljs-keyword">unless</span> @base <span class="hljs-keyword">instanceof</span> Arr
@base.hasElision()
isSplice: <span class="hljs-function">-></span>
[..., lastProperty] = @properties
lastProperty <span class="hljs-keyword">instanceof</span> Slice
looksStatic: <span class="hljs-function"><span class="hljs-params">(className)</span> -></span>
<span class="hljs-keyword">return</span> <span class="hljs-literal">no</span> <span class="hljs-keyword">unless</span> ((thisLiteral = @base) <span class="hljs-keyword">instanceof</span> ThisLiteral <span class="hljs-keyword">or</span> (name = @base).value <span class="hljs-keyword">is</span> className) <span class="hljs-keyword">and</span>
@properties.length <span class="hljs-keyword">is</span> <span class="hljs-number">1</span> <span class="hljs-keyword">and</span> @properties[<span class="hljs-number">0</span>].name?.value <span class="hljs-keyword">isnt</span> <span class="hljs-string">'prototype'</span>
<span class="hljs-keyword">return</span>
staticClassName: thisLiteral ? name</pre></div></div>
</li>
<li id="section-108">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-108">§</a>
</div>
<p>The value can be unwrapped as its inner node, if there are no attached
properties.</p>
</div>
<div class="content"><div class='highlight'><pre> unwrap: <span class="hljs-function">-></span>
<span class="hljs-keyword">if</span> @properties.length <span class="hljs-keyword">then</span> this <span class="hljs-keyword">else</span> @base</pre></div></div>
</li>
<li id="section-109">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-109">§</a>
</div>
<p>A reference has base part (<code>this</code> value) and name part.
We cache them separately for compiling complex expressions.
<code>a()[b()] ?= c</code> -> <code>(_base = a())[_name = b()] ? _base[_name] = c</code></p>
</div>
<div class="content"><div class='highlight'><pre> cacheReference: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
[..., name] = @properties
<span class="hljs-keyword">if</span> @properties.length < <span class="hljs-number">2</span> <span class="hljs-keyword">and</span> <span class="hljs-keyword">not</span> @base.shouldCache() <span class="hljs-keyword">and</span> <span class="hljs-keyword">not</span> name?.shouldCache()
<span class="hljs-keyword">return</span> [this, this] <span class="hljs-comment"># `a` `a.b`</span>
base = <span class="hljs-keyword">new</span> Value @base, @properties[...<span class="hljs-number">-1</span>]
<span class="hljs-keyword">if</span> base.shouldCache() <span class="hljs-comment"># `a().b`</span>
bref = <span class="hljs-keyword">new</span> IdentifierLiteral o.scope.freeVariable <span class="hljs-string">'base'</span>
base = <span class="hljs-keyword">new</span> Value <span class="hljs-keyword">new</span> Parens <span class="hljs-keyword">new</span> Assign bref, base
<span class="hljs-keyword">return</span> [base, bref] <span class="hljs-keyword">unless</span> name <span class="hljs-comment"># `a()`</span>
<span class="hljs-keyword">if</span> name.shouldCache() <span class="hljs-comment"># `a[b()]`</span>
nref = <span class="hljs-keyword">new</span> IdentifierLiteral o.scope.freeVariable <span class="hljs-string">'name'</span>
name = <span class="hljs-keyword">new</span> Index <span class="hljs-keyword">new</span> Assign nref, name.index
nref = <span class="hljs-keyword">new</span> Index nref
[base.add(name), <span class="hljs-keyword">new</span> Value(bref <span class="hljs-keyword">or</span> base.base, [nref <span class="hljs-keyword">or</span> name])]</pre></div></div>
</li>
<li id="section-110">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-110">§</a>
</div>
<p>We compile a value to JavaScript by compiling and joining each property.
Things get much more interesting if the chain of properties has <em>soak</em>
operators <code>?.</code> interspersed. Then we have to take care not to accidentally
evaluate anything twice when building the soak chain.</p>
</div>
<div class="content"><div class='highlight'><pre> compileNode: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
@base.front = @front
props = @properties
<span class="hljs-keyword">if</span> props.length <span class="hljs-keyword">and</span> @base.cached?</pre></div></div>
</li>
<li id="section-111">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-111">§</a>
</div>
<p>Cached fragments enable correct order of the compilation,
and reuse of variables in the scope.
Example:
<code>a(x = 5).b(-> x = 6)</code> should compile in the same order as
<code>a(x = 5); b(-> x = 6)</code>
(see issue #4437, <a href="https://github.com/jashkenas/coffeescript/issues/4437">https://github.com/jashkenas/coffeescript/issues/4437</a>)</p>
</div>
<div class="content"><div class='highlight'><pre> fragments = @base.cached
<span class="hljs-keyword">else</span>
fragments = @base.compileToFragments o, (<span class="hljs-keyword">if</span> props.length <span class="hljs-keyword">then</span> LEVEL_ACCESS <span class="hljs-keyword">else</span> <span class="hljs-literal">null</span>)
<span class="hljs-keyword">if</span> props.length <span class="hljs-keyword">and</span> SIMPLENUM.test fragmentsToText fragments
fragments.push @makeCode <span class="hljs-string">'.'</span>
<span class="hljs-keyword">for</span> prop <span class="hljs-keyword">in</span> props
fragments.push (prop.compileToFragments o)...
fragments</pre></div></div>
</li>
<li id="section-112">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-112">§</a>
</div>
<p>Unfold a soak into an <code>If</code>: <code>a?.b</code> -> <code>a.b if a?</code></p>
</div>
<div class="content"><div class='highlight'><pre> unfoldSoak: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
@unfoldedSoak ?= <span class="hljs-keyword">do</span> =>
ifn = @base.unfoldSoak o
<span class="hljs-keyword">if</span> ifn
ifn.body.properties.push @properties...
<span class="hljs-keyword">return</span> ifn
<span class="hljs-keyword">for</span> prop, i <span class="hljs-keyword">in</span> @properties <span class="hljs-keyword">when</span> prop.soak
prop.soak = <span class="hljs-literal">off</span>
fst = <span class="hljs-keyword">new</span> Value @base, @properties[...i]
snd = <span class="hljs-keyword">new</span> Value @base, @properties[i..]
<span class="hljs-keyword">if</span> fst.shouldCache()
ref = <span class="hljs-keyword">new</span> IdentifierLiteral o.scope.freeVariable <span class="hljs-string">'ref'</span>
fst = <span class="hljs-keyword">new</span> Parens <span class="hljs-keyword">new</span> Assign ref, fst
snd.base = ref
<span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> If <span class="hljs-keyword">new</span> Existence(fst), snd, soak: <span class="hljs-literal">on</span>
<span class="hljs-literal">no</span>
eachName: <span class="hljs-function"><span class="hljs-params">(iterator, {checkAssignability = <span class="hljs-literal">yes</span>} = {})</span> -></span>
<span class="hljs-keyword">if</span> @hasProperties()
iterator @
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> checkAssignability <span class="hljs-keyword">or</span> @base.isAssignable()
@base.eachName iterator
<span class="hljs-keyword">else</span>
@error <span class="hljs-string">'tried to assign to unassignable value'</span></pre></div></div>
</li>
<li id="section-113">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-113">§</a>
</div>
<p>For AST generation, we need an <code>object</code> that’s this <code>Value</code> minus its last
property, if it has properties.</p>
</div>
<div class="content"><div class='highlight'><pre> object: <span class="hljs-function">-></span>
<span class="hljs-keyword">return</span> @ <span class="hljs-keyword">unless</span> @hasProperties()</pre></div></div>
</li>
<li id="section-114">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-114">§</a>
</div>
<p>Get all properties except the last one; for a <code>Value</code> with only one
property, <code>initialProperties</code> is an empty array.</p>
</div>
<div class="content"><div class='highlight'><pre> initialProperties = @properties[<span class="hljs-number">0.</span>..@properties.length - <span class="hljs-number">1</span>]</pre></div></div>
</li>
<li id="section-115">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-115">§</a>
</div>
<p>Create the <code>object</code> that becomes the new “base” for the split-off final
property.</p>
</div>
<div class="content"><div class='highlight'><pre> object = <span class="hljs-keyword">new</span> Value @base, initialProperties, @tag, @isDefaultValue</pre></div></div>
</li>
<li id="section-116">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-116">§</a>
</div>
<p>Add location data to our new node, so that it has correct location data
for source maps or later conversion into AST location data.</p>
</div>
<div class="content"><div class='highlight'><pre> object.locationData =
<span class="hljs-keyword">if</span> initialProperties.length <span class="hljs-keyword">is</span> <span class="hljs-number">0</span></pre></div></div>
</li>
<li id="section-117">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-117">§</a>
</div>
<p>This new <code>Value</code> has only one property, so the location data is just
that of the parent <code>Value</code>’s base.</p>
</div>
<div class="content"><div class='highlight'><pre> @base.locationData
<span class="hljs-keyword">else</span></pre></div></div>
</li>
<li id="section-118">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-118">§</a>
</div>
<p>This new <code>Value</code> has multiple properties, so the location data spans
from the parent <code>Value</code>’s base to the last property that’s included
in this new node (a.k.a. the second-to-last property of the parent).</p>
</div>
<div class="content"><div class='highlight'><pre> mergeLocationData @base.locationData, initialProperties[initialProperties.length - <span class="hljs-number">1</span>].locationData
object
containsSoak: <span class="hljs-function">-></span>
<span class="hljs-keyword">return</span> <span class="hljs-literal">no</span> <span class="hljs-keyword">unless</span> @hasProperties()
<span class="hljs-keyword">for</span> property <span class="hljs-keyword">in</span> @properties <span class="hljs-keyword">when</span> property.soak
<span class="hljs-keyword">return</span> <span class="hljs-literal">yes</span>
<span class="hljs-keyword">return</span> <span class="hljs-literal">yes</span> <span class="hljs-keyword">if</span> @base <span class="hljs-keyword">instanceof</span> Call <span class="hljs-keyword">and</span> @base.soak
<span class="hljs-literal">no</span>
astNode: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span></pre></div></div>
</li>
<li id="section-119">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-119">§</a>
</div>
<p>If the <code>Value</code> has no properties, the AST node is just whatever this
node’s <code>base</code> is.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">return</span> @base.ast o <span class="hljs-keyword">unless</span> @hasProperties()</pre></div></div>
</li>
<li id="section-120">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-120">§</a>
</div>
<p>Otherwise, call <code>Base::ast</code> which in turn calls the <code>astType</code> and
<code>astProperties</code> methods below.</p>
</div>
<div class="content"><div class='highlight'><pre> super o
astType: <span class="hljs-function">-></span>
<span class="hljs-keyword">if</span> @isJSXTag()
<span class="hljs-string">'JSXMemberExpression'</span>
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> @containsSoak()
<span class="hljs-string">'OptionalMemberExpression'</span>
<span class="hljs-keyword">else</span>
<span class="hljs-string">'MemberExpression'</span></pre></div></div>
</li>
<li id="section-121">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-121">§</a>
</div>
<p>If this <code>Value</code> has properties, the <em>last</em> property (e.g. <code>c</code> in <code>a.b.c</code>)
becomes the <code>property</code>, and the preceding properties (e.g. <code>a.b</code>) become
a child <code>Value</code> node assigned to the <code>object</code> property.</p>
</div>
<div class="content"><div class='highlight'><pre> astProperties: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
[..., property] = @properties
property.name.jsx = <span class="hljs-literal">yes</span> <span class="hljs-keyword">if</span> @isJSXTag()
computed = property <span class="hljs-keyword">instanceof</span> Index <span class="hljs-keyword">or</span> property.name?.unwrap() <span class="hljs-keyword">not</span> <span class="hljs-keyword">instanceof</span> PropertyName
<span class="hljs-keyword">return</span> {
object: @object().ast o, LEVEL_ACCESS
property: property.ast o, (LEVEL_PAREN <span class="hljs-keyword">if</span> computed)
computed
optional: !!property.soak
shorthand: !!property.shorthand
}
astLocationData: <span class="hljs-function">-></span>
<span class="hljs-keyword">return</span> super() <span class="hljs-keyword">unless</span> @isJSXTag()</pre></div></div>
</li>
<li id="section-122">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-122">§</a>
</div>
<p>don’t include leading < of JSX tag in location data</p>
</div>
<div class="content"><div class='highlight'><pre> mergeAstLocationData(
jisonLocationDataToAstLocationData(@base.tagNameLocationData),
jisonLocationDataToAstLocationData(@properties[@properties.length - <span class="hljs-number">1</span>].locationData)
)
<span class="hljs-built_in">exports</span>.MetaProperty = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MetaProperty</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Base</span></span>
constructor: <span class="hljs-function"><span class="hljs-params">(@meta, @property)</span> -></span>
super()
children: [<span class="hljs-string">'meta'</span>, <span class="hljs-string">'property'</span>]
checkValid: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
<span class="hljs-keyword">if</span> @meta.value <span class="hljs-keyword">is</span> <span class="hljs-string">'new'</span>
<span class="hljs-keyword">if</span> @property <span class="hljs-keyword">instanceof</span> Access <span class="hljs-keyword">and</span> @property.name.value <span class="hljs-keyword">is</span> <span class="hljs-string">'target'</span>
<span class="hljs-keyword">unless</span> o.scope.parent?
@error <span class="hljs-string">"new.target can only occur inside functions"</span>
<span class="hljs-keyword">else</span>
@error <span class="hljs-string">"the only valid meta property for new is new.target"</span>
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> @meta.value <span class="hljs-keyword">is</span> <span class="hljs-string">'import'</span>
<span class="hljs-keyword">unless</span> @property <span class="hljs-keyword">instanceof</span> Access <span class="hljs-keyword">and</span> @property.name.value <span class="hljs-keyword">is</span> <span class="hljs-string">'meta'</span>
@error <span class="hljs-string">"the only valid meta property for import is import.meta"</span>
compileNode: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
@checkValid o
fragments = []
fragments.push @meta.compileToFragments(o, LEVEL_ACCESS)...
fragments.push @property.compileToFragments(o)...
fragments
astProperties: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
@checkValid o
<span class="hljs-keyword">return</span>
meta: @meta.ast o, LEVEL_ACCESS
property: @property.ast o</pre></div></div>
</li>
<li id="section-123">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-123">§</a>
</div>
<h3 id="herecomment">HereComment</h3>
</div>
</li>
<li id="section-124">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-124">§</a>
</div>
<p>Comment delimited by <code>###</code> (becoming <code>/* */</code>).</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-built_in">exports</span>.HereComment = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">HereComment</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Base</span></span>
constructor: <span class="hljs-function"><span class="hljs-params">({ @content, @newLine, @unshift, @locationData })</span> -></span>
super()
compileNode: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
multiline = <span class="hljs-string">'\n'</span> <span class="hljs-keyword">in</span> @content</pre></div></div>
</li>
<li id="section-125">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-125">§</a>
</div>
<p>Unindent multiline comments. They will be reindented later.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> multiline
indent = <span class="hljs-literal">null</span>
<span class="hljs-keyword">for</span> line <span class="hljs-keyword">in</span> @content.split <span class="hljs-string">'\n'</span>
leadingWhitespace = <span class="hljs-regexp">/^\s*/</span>.exec(line)[<span class="hljs-number">0</span>]
<span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> indent <span class="hljs-keyword">or</span> leadingWhitespace.length < indent.length
indent = leadingWhitespace
@content = @content.replace <span class="hljs-regexp">/// \n <span class="hljs-subst">#{indent}</span> ///</span>g, <span class="hljs-string">'\n'</span> <span class="hljs-keyword">if</span> indent
hasLeadingMarks = <span class="hljs-regexp">/\n\s*[#|\*]/</span>.test @content
@content = @content.replace <span class="hljs-regexp">/^([ \t]*)#(?=\s)/gm</span>, <span class="hljs-string">' *'</span> <span class="hljs-keyword">if</span> hasLeadingMarks
@content = <span class="hljs-string">"/*<span class="hljs-subst">#{@content}</span><span class="hljs-subst">#{<span class="hljs-keyword">if</span> hasLeadingMarks <span class="hljs-keyword">then</span> <span class="hljs-string">' '</span> <span class="hljs-keyword">else</span> <span class="hljs-string">''</span>}</span>*/"</span>
fragment = @makeCode @content
fragment.newLine = @newLine
fragment.unshift = @unshift
fragment.multiline = multiline</pre></div></div>
</li>
<li id="section-126">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-126">§</a>
</div>
<p>Don’t rely on <code>fragment.type</code>, which can break when the compiler is minified.</p>
</div>
<div class="content"><div class='highlight'><pre> fragment.isComment = fragment.isHereComment = <span class="hljs-literal">yes</span>
fragment
astType: <span class="hljs-function">-></span> <span class="hljs-string">'CommentBlock'</span>
astProperties: <span class="hljs-function">-></span>
<span class="hljs-keyword">return</span>
value: @content</pre></div></div>
</li>
<li id="section-127">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-127">§</a>
</div>
<h3 id="linecomment">LineComment</h3>
</div>
</li>
<li id="section-128">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-128">§</a>
</div>
<p>Comment running from <code>#</code> to the end of a line (becoming <code>//</code>).</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-built_in">exports</span>.LineComment = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">LineComment</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Base</span></span>
constructor: <span class="hljs-function"><span class="hljs-params">({ @content, @newLine, @unshift, @locationData, @precededByBlankLine })</span> -></span>
super()
compileNode: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
fragment = @makeCode(<span class="hljs-keyword">if</span> <span class="hljs-regexp">/^\s*$/</span>.test @content <span class="hljs-keyword">then</span> <span class="hljs-string">''</span> <span class="hljs-keyword">else</span> <span class="hljs-string">"<span class="hljs-subst">#{<span class="hljs-keyword">if</span> @precededByBlankLine <span class="hljs-keyword">then</span> <span class="hljs-string">"\n<span class="hljs-subst">#{o.indent}</span>"</span> <span class="hljs-keyword">else</span> <span class="hljs-string">''</span>}</span>//<span class="hljs-subst">#{@content}</span>"</span>)
fragment.newLine = @newLine
fragment.unshift = @unshift
fragment.trail = <span class="hljs-keyword">not</span> @newLine <span class="hljs-keyword">and</span> <span class="hljs-keyword">not</span> @unshift</pre></div></div>
</li>
<li id="section-129">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-129">§</a>
</div>
<p>Don’t rely on <code>fragment.type</code>, which can break when the compiler is minified.</p>
</div>
<div class="content"><div class='highlight'><pre> fragment.isComment = fragment.isLineComment = <span class="hljs-literal">yes</span>
fragment
astType: <span class="hljs-function">-></span> <span class="hljs-string">'CommentLine'</span>
astProperties: <span class="hljs-function">-></span>
<span class="hljs-keyword">return</span>
value: @content</pre></div></div>
</li>
<li id="section-130">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-130">§</a>
</div>
<h3 id="jsx">JSX</h3>
</div>
<div class="content"><div class='highlight'><pre>
<span class="hljs-built_in">exports</span>.JSXIdentifier = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">JSXIdentifier</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">IdentifierLiteral</span></span>
astType: <span class="hljs-function">-></span> <span class="hljs-string">'JSXIdentifier'</span>
<span class="hljs-built_in">exports</span>.JSXTag = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">JSXTag</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">JSXIdentifier</span></span>
constructor: (value, {
@tagNameLocationData
@closingTagOpeningBracketLocationData
@closingTagSlashLocationData
@closingTagNameLocationData
@closingTagClosingBracketLocationData
}) ->
super value
astProperties: <span class="hljs-function">-></span>
<span class="hljs-keyword">return</span>
name: @value
<span class="hljs-built_in">exports</span>.JSXExpressionContainer = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">JSXExpressionContainer</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Base</span></span>
constructor: <span class="hljs-function"><span class="hljs-params">(@expression, {locationData} = {})</span> -></span>
super()
@expression.jsxAttribute = <span class="hljs-literal">yes</span>
@locationData = locationData ? @expression.locationData
children: [<span class="hljs-string">'expression'</span>]
compileNode: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
@expression.compileNode(o)
astProperties: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
<span class="hljs-keyword">return</span>
expression: astAsBlockIfNeeded @expression, o
<span class="hljs-built_in">exports</span>.JSXEmptyExpression = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">JSXEmptyExpression</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Base</span></span>
<span class="hljs-built_in">exports</span>.JSXText = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">JSXText</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Base</span></span>
constructor: <span class="hljs-function"><span class="hljs-params">(stringLiteral)</span> -></span>
super()
@value = stringLiteral.unquotedValueForJSX
@locationData = stringLiteral.locationData
astProperties: <span class="hljs-function">-></span>
<span class="hljs-keyword">return</span> {
@value
extra:
raw: @value
}
<span class="hljs-built_in">exports</span>.JSXAttribute = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">JSXAttribute</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Base</span></span>
constructor: <span class="hljs-function"><span class="hljs-params">({@name, value})</span> -></span>
super()
@value =
<span class="hljs-keyword">if</span> value?
value = value.base
<span class="hljs-keyword">if</span> value <span class="hljs-keyword">instanceof</span> StringLiteral <span class="hljs-keyword">and</span> <span class="hljs-keyword">not</span> value.shouldGenerateTemplateLiteral()
value
<span class="hljs-keyword">else</span>
<span class="hljs-keyword">new</span> JSXExpressionContainer value
<span class="hljs-keyword">else</span>
<span class="hljs-literal">null</span>
@value?.comments = value.comments
children: [<span class="hljs-string">'name'</span>, <span class="hljs-string">'value'</span>]
compileNode: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
compiledName = @name.compileToFragments o, LEVEL_LIST
<span class="hljs-keyword">return</span> compiledName <span class="hljs-keyword">unless</span> @value?
val = @value.compileToFragments o, LEVEL_LIST
compiledName.concat @makeCode(<span class="hljs-string">'='</span>), val
astProperties: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
name = @name
<span class="hljs-keyword">if</span> <span class="hljs-string">':'</span> <span class="hljs-keyword">in</span> name.value
name = <span class="hljs-keyword">new</span> JSXNamespacedName name
<span class="hljs-keyword">return</span>
name: name.ast o
value: @value?.ast(o) ? <span class="hljs-literal">null</span>
<span class="hljs-built_in">exports</span>.JSXAttributes = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">JSXAttributes</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Base</span></span>
constructor: <span class="hljs-function"><span class="hljs-params">(arr)</span> -></span>
super()
@attributes = []
<span class="hljs-keyword">for</span> object <span class="hljs-keyword">in</span> arr.objects
@checkValidAttribute object
{base} = object
<span class="hljs-keyword">if</span> base <span class="hljs-keyword">instanceof</span> IdentifierLiteral</pre></div></div>
</li>
<li id="section-131">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-131">§</a>
</div>
<p>attribute with no value eg disabled</p>
</div>
<div class="content"><div class='highlight'><pre> attribute = <span class="hljs-keyword">new</span> JSXAttribute name: <span class="hljs-keyword">new</span> JSXIdentifier(base.value).withLocationDataAndCommentsFrom base
attribute.locationData = base.locationData
@attributes.push attribute
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> base.generated</pre></div></div>
</li>
<li id="section-132">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-132">§</a>
</div>
<p>object spread attribute eg {…props}</p>
</div>
<div class="content"><div class='highlight'><pre> attribute = base.properties[<span class="hljs-number">0</span>]
attribute.jsx = <span class="hljs-literal">yes</span>
attribute.locationData = base.locationData
@attributes.push attribute
<span class="hljs-keyword">else</span></pre></div></div>
</li>
<li id="section-133">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-133">§</a>
</div>
<p>Obj containing attributes with values eg a=”b” c={d}</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">for</span> property <span class="hljs-keyword">in</span> base.properties
{variable, value} = property
attribute = <span class="hljs-keyword">new</span> JSXAttribute {
name: <span class="hljs-keyword">new</span> JSXIdentifier(variable.base.value).withLocationDataAndCommentsFrom variable.base
value
}
attribute.locationData = property.locationData
@attributes.push attribute
@locationData = arr.locationData
children: [<span class="hljs-string">'attributes'</span>]</pre></div></div>
</li>
<li id="section-134">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-134">§</a>
</div>
<p>Catch invalid attributes: <div {a:”b”, props} {props} “value” /></p>
</div>
<div class="content"><div class='highlight'><pre> checkValidAttribute: <span class="hljs-function"><span class="hljs-params">(object)</span> -></span>
{base: attribute} = object
properties = attribute?.properties <span class="hljs-keyword">or</span> []
<span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> (attribute <span class="hljs-keyword">instanceof</span> Obj <span class="hljs-keyword">or</span> attribute <span class="hljs-keyword">instanceof</span> IdentifierLiteral) <span class="hljs-keyword">or</span> (attribute <span class="hljs-keyword">instanceof</span> Obj <span class="hljs-keyword">and</span> <span class="hljs-keyword">not</span> attribute.generated <span class="hljs-keyword">and</span> (properties.length > <span class="hljs-number">1</span> <span class="hljs-keyword">or</span> <span class="hljs-keyword">not</span> (properties[<span class="hljs-number">0</span>] <span class="hljs-keyword">instanceof</span> Splat)))
object.error <span class="hljs-string">"""
Unexpected token. Allowed JSX attributes are: id="val", src={source}, {props...} or attribute.
"""</span>
compileNode: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
fragments = []
<span class="hljs-keyword">for</span> attribute <span class="hljs-keyword">in</span> @attributes
fragments.push @makeCode <span class="hljs-string">' '</span>
fragments.push attribute.compileToFragments(o, LEVEL_TOP)...
fragments
astNode: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
attribute.ast(o) <span class="hljs-keyword">for</span> attribute <span class="hljs-keyword">in</span> @attributes
<span class="hljs-built_in">exports</span>.JSXNamespacedName = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">JSXNamespacedName</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Base</span></span>
constructor: <span class="hljs-function"><span class="hljs-params">(tag)</span> -></span>
super()
[namespace, name] = tag.value.split <span class="hljs-string">':'</span>
@namespace = <span class="hljs-keyword">new</span> JSXIdentifier(namespace).withLocationDataFrom locationData: extractSameLineLocationDataFirst(namespace.length) tag.locationData
@name = <span class="hljs-keyword">new</span> JSXIdentifier(name ).withLocationDataFrom locationData: extractSameLineLocationDataLast(name.length ) tag.locationData
@locationData = tag.locationData
children: [<span class="hljs-string">'namespace'</span>, <span class="hljs-string">'name'</span>]
astProperties: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
<span class="hljs-keyword">return</span>
namespace: @namespace.ast o
name: @name.ast o</pre></div></div>
</li>
<li id="section-135">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-135">§</a>
</div>
<p>Node for a JSX element</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-built_in">exports</span>.JSXElement = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">JSXElement</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Base</span></span>
constructor: <span class="hljs-function"><span class="hljs-params">({@tagName, @attributes, @content})</span> -></span>
super()
children: [<span class="hljs-string">'tagName'</span>, <span class="hljs-string">'attributes'</span>, <span class="hljs-string">'content'</span>]
compileNode: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
@content?.base.jsx = <span class="hljs-literal">yes</span>
fragments = [@makeCode(<span class="hljs-string">'<'</span>)]
fragments.push (tag = @tagName.compileToFragments(o, LEVEL_ACCESS))...
fragments.push @attributes.compileToFragments(o)...
<span class="hljs-keyword">if</span> @content
fragments.push @makeCode(<span class="hljs-string">'>'</span>)
fragments.push @content.compileNode(o, LEVEL_LIST)...
fragments.push [@makeCode(<span class="hljs-string">'</'</span>), tag..., @makeCode(<span class="hljs-string">'>'</span>)]...
<span class="hljs-keyword">else</span>
fragments.push @makeCode(<span class="hljs-string">' />'</span>)
fragments
isFragment: <span class="hljs-function">-></span>
!@tagName.base.value.length
astNode: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span></pre></div></div>
</li>
<li id="section-136">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-136">§</a>
</div>
<p>The location data spanning the opening element < … > is captured by
the generated Arr which contains the element’s attributes</p>
</div>
<div class="content"><div class='highlight'><pre> @openingElementLocationData = jisonLocationDataToAstLocationData @attributes.locationData
tagName = @tagName.base
tagName.locationData = tagName.tagNameLocationData
<span class="hljs-keyword">if</span> @content?
@closingElementLocationData = mergeAstLocationData(
jisonLocationDataToAstLocationData tagName.closingTagOpeningBracketLocationData
jisonLocationDataToAstLocationData tagName.closingTagClosingBracketLocationData
)
super o
astType: <span class="hljs-function">-></span>
<span class="hljs-keyword">if</span> @isFragment()
<span class="hljs-string">'JSXFragment'</span>
<span class="hljs-keyword">else</span>
<span class="hljs-string">'JSXElement'</span>
elementAstProperties: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
<span class="hljs-function"> <span class="hljs-title">tagNameAst</span> = =></span>
tag = @tagName.unwrap()
<span class="hljs-keyword">if</span> tag?.value <span class="hljs-keyword">and</span> <span class="hljs-string">':'</span> <span class="hljs-keyword">in</span> tag.value
tag = <span class="hljs-keyword">new</span> JSXNamespacedName tag
tag.ast o
openingElement = <span class="hljs-built_in">Object</span>.assign {
type: <span class="hljs-string">'JSXOpeningElement'</span>
name: tagNameAst()
selfClosing: <span class="hljs-keyword">not</span> @closingElementLocationData?
attributes: @attributes.ast o
}, @openingElementLocationData
closingElement = <span class="hljs-literal">null</span>
<span class="hljs-keyword">if</span> @closingElementLocationData?
closingElement = <span class="hljs-built_in">Object</span>.assign {
type: <span class="hljs-string">'JSXClosingElement'</span>
name: <span class="hljs-built_in">Object</span>.assign(
tagNameAst(),
jisonLocationDataToAstLocationData @tagName.base.closingTagNameLocationData
)
}, @closingElementLocationData
<span class="hljs-keyword">if</span> closingElement.name.type <span class="hljs-keyword">in</span> [<span class="hljs-string">'JSXMemberExpression'</span>, <span class="hljs-string">'JSXNamespacedName'</span>]
rangeDiff = closingElement.range[<span class="hljs-number">0</span>] - openingElement.range[<span class="hljs-number">0</span>] + <span class="hljs-string">'/'</span>.length
columnDiff = closingElement.loc.start.column - openingElement.loc.start.column + <span class="hljs-string">'/'</span>.length
<span class="hljs-function"> <span class="hljs-title">shiftAstLocationData</span> = <span class="hljs-params">(node)</span> =></span>
node.range = [
node.range[<span class="hljs-number">0</span>] + rangeDiff
node.range[<span class="hljs-number">1</span>] + rangeDiff
]
node.start += rangeDiff
node.end += rangeDiff
node.loc.start =
line: @closingElementLocationData.loc.start.line
column: node.loc.start.column + columnDiff
node.loc.end =
line: @closingElementLocationData.loc.start.line
column: node.loc.end.column + columnDiff
<span class="hljs-keyword">if</span> closingElement.name.type <span class="hljs-keyword">is</span> <span class="hljs-string">'JSXMemberExpression'</span>
currentExpr = closingElement.name
<span class="hljs-keyword">while</span> currentExpr.type <span class="hljs-keyword">is</span> <span class="hljs-string">'JSXMemberExpression'</span>
shiftAstLocationData currentExpr <span class="hljs-keyword">unless</span> currentExpr <span class="hljs-keyword">is</span> closingElement.name
shiftAstLocationData currentExpr.property
currentExpr = currentExpr.object
shiftAstLocationData currentExpr
<span class="hljs-keyword">else</span> <span class="hljs-comment"># JSXNamespacedName</span>
shiftAstLocationData closingElement.name.namespace
shiftAstLocationData closingElement.name.name
{openingElement, closingElement}
fragmentAstProperties: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
openingFragment = <span class="hljs-built_in">Object</span>.assign {
type: <span class="hljs-string">'JSXOpeningFragment'</span>
}, @openingElementLocationData
closingFragment = <span class="hljs-built_in">Object</span>.assign {
type: <span class="hljs-string">'JSXClosingFragment'</span>
}, @closingElementLocationData
{openingFragment, closingFragment}
contentAst: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
<span class="hljs-keyword">return</span> [] <span class="hljs-keyword">unless</span> @content <span class="hljs-keyword">and</span> <span class="hljs-keyword">not</span> @content.base.isEmpty?()
content = @content.unwrapAll()
children =
<span class="hljs-keyword">if</span> content <span class="hljs-keyword">instanceof</span> StringLiteral
[<span class="hljs-keyword">new</span> JSXText content]
<span class="hljs-keyword">else</span> <span class="hljs-comment"># StringWithInterpolations</span>
<span class="hljs-keyword">for</span> element <span class="hljs-keyword">in</span> @content.unwrapAll().extractElements o, includeInterpolationWrappers: <span class="hljs-literal">yes</span>, isJsx: <span class="hljs-literal">yes</span>
<span class="hljs-keyword">if</span> element <span class="hljs-keyword">instanceof</span> StringLiteral
<span class="hljs-keyword">new</span> JSXText element
<span class="hljs-keyword">else</span> <span class="hljs-comment"># Interpolation</span>
{expression} = element
<span class="hljs-keyword">unless</span> expression?
emptyExpression = <span class="hljs-keyword">new</span> JSXEmptyExpression()
emptyExpression.locationData = emptyExpressionLocationData {
interpolationNode: element
openingBrace: <span class="hljs-string">'{'</span>
closingBrace: <span class="hljs-string">'}'</span>
}
<span class="hljs-keyword">new</span> JSXExpressionContainer emptyExpression, locationData: element.locationData
<span class="hljs-keyword">else</span>
unwrapped = expression.unwrapAll()
<span class="hljs-keyword">if</span> unwrapped <span class="hljs-keyword">instanceof</span> JSXElement <span class="hljs-keyword">and</span></pre></div></div>
</li>
<li id="section-137">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-137">§</a>
</div>
<p>distinguish <code><a><b /></a></code> from <code><a>{<b />}</a></code></p>
</div>
<div class="content"><div class='highlight'><pre> unwrapped.locationData.range[<span class="hljs-number">0</span>] <span class="hljs-keyword">is</span> element.locationData.range[<span class="hljs-number">0</span>]
unwrapped
<span class="hljs-keyword">else</span>
<span class="hljs-keyword">new</span> JSXExpressionContainer unwrapped, locationData: element.locationData
child.ast(o) <span class="hljs-keyword">for</span> child <span class="hljs-keyword">in</span> children <span class="hljs-keyword">when</span> <span class="hljs-keyword">not</span> (child <span class="hljs-keyword">instanceof</span> JSXText <span class="hljs-keyword">and</span> child.value.length <span class="hljs-keyword">is</span> <span class="hljs-number">0</span>)
astProperties: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
<span class="hljs-built_in">Object</span>.assign(
<span class="hljs-keyword">if</span> @isFragment()
@fragmentAstProperties o
<span class="hljs-keyword">else</span>
@elementAstProperties o
,
children: @contentAst o
)
astLocationData: <span class="hljs-function">-></span>
<span class="hljs-keyword">if</span> @closingElementLocationData?
mergeAstLocationData @openingElementLocationData, @closingElementLocationData
<span class="hljs-keyword">else</span>
@openingElementLocationData</pre></div></div>
</li>
<li id="section-138">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-138">§</a>
</div>
<h3 id="call">Call</h3>
</div>
</li>
<li id="section-139">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-139">§</a>
</div>
<p>Node for a function invocation.</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-built_in">exports</span>.Call = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Call</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Base</span></span>
constructor: <span class="hljs-function"><span class="hljs-params">(@variable, @args = [], @soak, @token)</span> -></span>
super()
@implicit = @args.implicit
@isNew = <span class="hljs-literal">no</span>
<span class="hljs-keyword">if</span> @variable <span class="hljs-keyword">instanceof</span> Value <span class="hljs-keyword">and</span> @variable.isNotCallable()
@variable.error <span class="hljs-string">"literal is not a function"</span>
<span class="hljs-keyword">if</span> @variable.base <span class="hljs-keyword">instanceof</span> JSXTag
<span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> JSXElement(
tagName: @variable
attributes: <span class="hljs-keyword">new</span> JSXAttributes @args[<span class="hljs-number">0</span>].base
content: @args[<span class="hljs-number">1</span>]
)</pre></div></div>
</li>
<li id="section-140">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-140">§</a>
</div>
<p><code>@variable</code> never gets output as a result of this node getting created as
part of <code>RegexWithInterpolations</code>, so for that case move any comments to
the <code>args</code> property that gets passed into <code>RegexWithInterpolations</code> via
the grammar.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> @variable.base?.value <span class="hljs-keyword">is</span> <span class="hljs-string">'RegExp'</span> <span class="hljs-keyword">and</span> @args.length <span class="hljs-keyword">isnt</span> <span class="hljs-number">0</span>
moveComments @variable, @args[<span class="hljs-number">0</span>]
children: [<span class="hljs-string">'variable'</span>, <span class="hljs-string">'args'</span>]</pre></div></div>
</li>
<li id="section-141">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-141">§</a>
</div>
<p>When setting the location, we sometimes need to update the start location to
account for a newly-discovered <code>new</code> operator to the left of us. This
expands the range on the left, but not the right.</p>
</div>
<div class="content"><div class='highlight'><pre> updateLocationDataIfMissing: <span class="hljs-function"><span class="hljs-params">(locationData)</span> -></span>
<span class="hljs-keyword">if</span> @locationData <span class="hljs-keyword">and</span> @needsUpdatedStartLocation
@locationData = <span class="hljs-built_in">Object</span>.assign {},
@locationData,
first_line: locationData.first_line
first_column: locationData.first_column
range: [
locationData.range[<span class="hljs-number">0</span>]
@locationData.range[<span class="hljs-number">1</span>]
]
base = @variable?.base <span class="hljs-keyword">or</span> @variable
<span class="hljs-keyword">if</span> base.needsUpdatedStartLocation
@variable.locationData = <span class="hljs-built_in">Object</span>.assign {},
@variable.locationData,
first_line: locationData.first_line
first_column: locationData.first_column
range: [
locationData.range[<span class="hljs-number">0</span>]
@variable.locationData.range[<span class="hljs-number">1</span>]
]
base.updateLocationDataIfMissing locationData
<span class="hljs-keyword">delete</span> @needsUpdatedStartLocation
super locationData</pre></div></div>
</li>
<li id="section-142">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-142">§</a>
</div>
<p>Tag this invocation as creating a new instance.</p>
</div>
<div class="content"><div class='highlight'><pre> newInstance: <span class="hljs-function">-></span>
base = @variable?.base <span class="hljs-keyword">or</span> @variable
<span class="hljs-keyword">if</span> base <span class="hljs-keyword">instanceof</span> Call <span class="hljs-keyword">and</span> <span class="hljs-keyword">not</span> base.isNew
base.newInstance()
<span class="hljs-keyword">else</span>
@isNew = <span class="hljs-literal">true</span>
@needsUpdatedStartLocation = <span class="hljs-literal">true</span>
this</pre></div></div>
</li>
<li id="section-143">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-143">§</a>
</div>
<p>Soaked chained invocations unfold into if/else ternary structures.</p>
</div>
<div class="content"><div class='highlight'><pre> unfoldSoak: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
<span class="hljs-keyword">if</span> @soak
<span class="hljs-keyword">if</span> @variable <span class="hljs-keyword">instanceof</span> Super
left = <span class="hljs-keyword">new</span> Literal @variable.compile o
rite = <span class="hljs-keyword">new</span> Value left
@variable.error <span class="hljs-string">"Unsupported reference to 'super'"</span> <span class="hljs-keyword">unless</span> @variable.accessor?
<span class="hljs-keyword">else</span>
<span class="hljs-keyword">return</span> ifn <span class="hljs-keyword">if</span> ifn = unfoldSoak o, this, <span class="hljs-string">'variable'</span>
[left, rite] = <span class="hljs-keyword">new</span> Value(@variable).cacheReference o
rite = <span class="hljs-keyword">new</span> Call rite, @args
rite.isNew = @isNew
left = <span class="hljs-keyword">new</span> Literal <span class="hljs-string">"typeof <span class="hljs-subst">#{ left.compile o }</span> === \"function\""</span>
<span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> If left, <span class="hljs-keyword">new</span> Value(rite), soak: <span class="hljs-literal">yes</span>
call = this
list = []
<span class="hljs-keyword">loop</span>
<span class="hljs-keyword">if</span> call.variable <span class="hljs-keyword">instanceof</span> Call
list.push call
call = call.variable
<span class="hljs-keyword">continue</span>
<span class="hljs-keyword">break</span> <span class="hljs-keyword">unless</span> call.variable <span class="hljs-keyword">instanceof</span> Value
list.push call
<span class="hljs-keyword">break</span> <span class="hljs-keyword">unless</span> (call = call.variable.base) <span class="hljs-keyword">instanceof</span> Call
<span class="hljs-keyword">for</span> call <span class="hljs-keyword">in</span> list.reverse()
<span class="hljs-keyword">if</span> ifn
<span class="hljs-keyword">if</span> call.variable <span class="hljs-keyword">instanceof</span> Call
call.variable = ifn
<span class="hljs-keyword">else</span>
call.variable.base = ifn
ifn = unfoldSoak o, call, <span class="hljs-string">'variable'</span>
ifn</pre></div></div>
</li>
<li id="section-144">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-144">§</a>
</div>
<p>Compile a vanilla function call.</p>
</div>
<div class="content"><div class='highlight'><pre> compileNode: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
@checkForNewSuper()
@variable?.front = @front
compiledArgs = []</pre></div></div>
</li>
<li id="section-145">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-145">§</a>
</div>
<p>If variable is <code>Accessor</code> fragments are cached and used later
in <code>Value::compileNode</code> to ensure correct order of the compilation,
and reuse of variables in the scope.
Example:
<code>a(x = 5).b(-> x = 6)</code> should compile in the same order as
<code>a(x = 5); b(-> x = 6)</code>
(see issue #4437, <a href="https://github.com/jashkenas/coffeescript/issues/4437">https://github.com/jashkenas/coffeescript/issues/4437</a>)</p>
</div>
<div class="content"><div class='highlight'><pre> varAccess = @variable?.properties?[<span class="hljs-number">0</span>] <span class="hljs-keyword">instanceof</span> Access
argCode = (arg <span class="hljs-keyword">for</span> arg <span class="hljs-keyword">in</span> (@args || []) <span class="hljs-keyword">when</span> arg <span class="hljs-keyword">instanceof</span> Code)
<span class="hljs-keyword">if</span> argCode.length > <span class="hljs-number">0</span> <span class="hljs-keyword">and</span> varAccess <span class="hljs-keyword">and</span> <span class="hljs-keyword">not</span> @variable.base.cached
[cache] = @variable.base.cache o, LEVEL_ACCESS, <span class="hljs-function">-></span> <span class="hljs-literal">no</span>
@variable.base.cached = cache
<span class="hljs-keyword">for</span> arg, argIndex <span class="hljs-keyword">in</span> @args
<span class="hljs-keyword">if</span> argIndex <span class="hljs-keyword">then</span> compiledArgs.push @makeCode <span class="hljs-string">", "</span>
compiledArgs.push (arg.compileToFragments o, LEVEL_LIST)...
fragments = []
<span class="hljs-keyword">if</span> @isNew
fragments.push @makeCode <span class="hljs-string">'new '</span>
fragments.push @variable.compileToFragments(o, LEVEL_ACCESS)...
fragments.push @makeCode(<span class="hljs-string">'('</span>), compiledArgs..., @makeCode(<span class="hljs-string">')'</span>)
fragments
checkForNewSuper: <span class="hljs-function">-></span>
<span class="hljs-keyword">if</span> @isNew
@variable.error <span class="hljs-string">"Unsupported reference to 'super'"</span> <span class="hljs-keyword">if</span> @variable <span class="hljs-keyword">instanceof</span> Super
containsSoak: <span class="hljs-function">-></span>
<span class="hljs-keyword">return</span> <span class="hljs-literal">yes</span> <span class="hljs-keyword">if</span> @soak
<span class="hljs-keyword">return</span> <span class="hljs-literal">yes</span> <span class="hljs-keyword">if</span> @variable?.containsSoak?()
<span class="hljs-literal">no</span>
astNode: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
<span class="hljs-keyword">if</span> @soak <span class="hljs-keyword">and</span> @variable <span class="hljs-keyword">instanceof</span> Super <span class="hljs-keyword">and</span> o.scope.namedMethod()?.ctor
@variable.error <span class="hljs-string">"Unsupported reference to 'super'"</span>
@checkForNewSuper()
super o
astType: <span class="hljs-function">-></span>
<span class="hljs-keyword">if</span> @isNew
<span class="hljs-string">'NewExpression'</span>
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> @containsSoak()
<span class="hljs-string">'OptionalCallExpression'</span>
<span class="hljs-keyword">else</span>
<span class="hljs-string">'CallExpression'</span>
astProperties: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
<span class="hljs-keyword">return</span>
callee: @variable.ast o, LEVEL_ACCESS
arguments: arg.ast(o, LEVEL_LIST) <span class="hljs-keyword">for</span> arg <span class="hljs-keyword">in</span> @args
optional: !!@soak
implicit: !!@implicit</pre></div></div>
</li>
<li id="section-146">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-146">§</a>
</div>
<h3 id="super">Super</h3>
</div>
</li>
<li id="section-147">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-147">§</a>
</div>
<p>Takes care of converting <code>super()</code> calls into calls against the prototype’s
function of the same name.
When <code>expressions</code> are set the call will be compiled in such a way that the
expressions are evaluated without altering the return value of the <code>SuperCall</code>
expression.</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-built_in">exports</span>.SuperCall = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">SuperCall</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Call</span></span>
children: Call::children.concat [<span class="hljs-string">'expressions'</span>]
isStatement: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
@expressions?.length <span class="hljs-keyword">and</span> o.level <span class="hljs-keyword">is</span> LEVEL_TOP
compileNode: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
<span class="hljs-keyword">return</span> super o <span class="hljs-keyword">unless</span> @expressions?.length
superCall = <span class="hljs-keyword">new</span> Literal fragmentsToText super o
replacement = <span class="hljs-keyword">new</span> Block @expressions.slice()
<span class="hljs-keyword">if</span> o.level > LEVEL_TOP</pre></div></div>
</li>
<li id="section-148">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-148">§</a>
</div>
<p>If we might be in an expression we need to cache and return the result</p>
</div>
<div class="content"><div class='highlight'><pre> [superCall, ref] = superCall.cache o, <span class="hljs-literal">null</span>, YES
replacement.push ref
replacement.unshift superCall
replacement.compileToFragments o, <span class="hljs-keyword">if</span> o.level <span class="hljs-keyword">is</span> LEVEL_TOP <span class="hljs-keyword">then</span> o.level <span class="hljs-keyword">else</span> LEVEL_LIST
<span class="hljs-built_in">exports</span>.Super = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Super</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Base</span></span>
constructor: <span class="hljs-function"><span class="hljs-params">(@accessor, @superLiteral)</span> -></span>
super()
children: [<span class="hljs-string">'accessor'</span>]
compileNode: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
@checkInInstanceMethod o
method = o.scope.namedMethod()
<span class="hljs-keyword">unless</span> method.ctor? <span class="hljs-keyword">or</span> @accessor?
{name, variable} = method
<span class="hljs-keyword">if</span> name.shouldCache() <span class="hljs-keyword">or</span> (name <span class="hljs-keyword">instanceof</span> Index <span class="hljs-keyword">and</span> name.index.isAssignable())
nref = <span class="hljs-keyword">new</span> IdentifierLiteral o.scope.parent.freeVariable <span class="hljs-string">'name'</span>
name.index = <span class="hljs-keyword">new</span> Assign nref, name.index
@accessor = <span class="hljs-keyword">if</span> nref? <span class="hljs-keyword">then</span> <span class="hljs-keyword">new</span> Index nref <span class="hljs-keyword">else</span> name
<span class="hljs-keyword">if</span> @accessor?.name?.comments</pre></div></div>
</li>
<li id="section-149">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-149">§</a>
</div>
<p>A <code>super()</code> call gets compiled to e.g. <code>super.method()</code>, which means
the <code>method</code> property name gets compiled for the first time here, and
again when the <code>method:</code> property of the class gets compiled. Since
this compilation happens first, comments attached to <code>method:</code> would
get incorrectly output near <code>super.method()</code>, when we want them to
get output on the second pass when <code>method:</code> is output. So set them
aside during this compilation pass, and put them back on the object so
that they’re there for the later compilation.</p>
</div>
<div class="content"><div class='highlight'><pre> salvagedComments = @accessor.name.comments
<span class="hljs-keyword">delete</span> @accessor.name.comments
fragments = (<span class="hljs-keyword">new</span> Value (<span class="hljs-keyword">new</span> Literal <span class="hljs-string">'super'</span>), <span class="hljs-keyword">if</span> @accessor <span class="hljs-keyword">then</span> [ @accessor ] <span class="hljs-keyword">else</span> [])
.compileToFragments o
attachCommentsToNode salvagedComments, @accessor.name <span class="hljs-keyword">if</span> salvagedComments
fragments
checkInInstanceMethod: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
method = o.scope.namedMethod()
@error <span class="hljs-string">'cannot use super outside of an instance method'</span> <span class="hljs-keyword">unless</span> method?.isMethod
astNode: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
@checkInInstanceMethod o
<span class="hljs-keyword">if</span> @accessor?
<span class="hljs-keyword">return</span> (
<span class="hljs-keyword">new</span> Value(
<span class="hljs-keyword">new</span> Super().withLocationDataFrom (@superLiteral ? @)
[@accessor]
).withLocationDataFrom @
).ast o
super o</pre></div></div>
</li>
<li id="section-150">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-150">§</a>
</div>
<h3 id="regexwithinterpolations">RegexWithInterpolations</h3>
</div>
</li>
<li id="section-151">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-151">§</a>
</div>
<p>Regexes with interpolations are in fact just a variation of a <code>Call</code> (a
<code>RegExp()</code> call to be precise) with a <code>StringWithInterpolations</code> inside.</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-built_in">exports</span>.RegexWithInterpolations = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">RegexWithInterpolations</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Base</span></span>
constructor: <span class="hljs-function"><span class="hljs-params">(@call, {@heregexCommentTokens = []} = {})</span> -></span>
super()
children: [<span class="hljs-string">'call'</span>]
compileNode: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
@call.compileNode o
astType: <span class="hljs-function">-></span> <span class="hljs-string">'InterpolatedRegExpLiteral'</span>
astProperties: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
interpolatedPattern: @call.args[<span class="hljs-number">0</span>].ast o
flags: @call.args[<span class="hljs-number">1</span>]?.unwrap().originalValue ? <span class="hljs-string">''</span>
comments:
<span class="hljs-keyword">for</span> heregexCommentToken <span class="hljs-keyword">in</span> @heregexCommentTokens
<span class="hljs-keyword">if</span> heregexCommentToken.here
<span class="hljs-keyword">new</span> HereComment(heregexCommentToken).ast o
<span class="hljs-keyword">else</span>
<span class="hljs-keyword">new</span> LineComment(heregexCommentToken).ast o</pre></div></div>
</li>
<li id="section-152">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-152">§</a>
</div>
<h3 id="taggedtemplatecall">TaggedTemplateCall</h3>
</div>
<div class="content"><div class='highlight'><pre>
<span class="hljs-built_in">exports</span>.TaggedTemplateCall = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">TaggedTemplateCall</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Call</span></span>
constructor: <span class="hljs-function"><span class="hljs-params">(variable, arg, soak)</span> -></span>
arg = StringWithInterpolations.fromStringLiteral arg <span class="hljs-keyword">if</span> arg <span class="hljs-keyword">instanceof</span> StringLiteral
super variable, [ arg ], soak
compileNode: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
@variable.compileToFragments(o, LEVEL_ACCESS).concat @args[<span class="hljs-number">0</span>].compileToFragments(o, LEVEL_LIST)
astType: <span class="hljs-function">-></span> <span class="hljs-string">'TaggedTemplateExpression'</span>
astProperties: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
<span class="hljs-keyword">return</span>
tag: @variable.ast o, LEVEL_ACCESS
quasi: @args[<span class="hljs-number">0</span>].ast o, LEVEL_LIST</pre></div></div>
</li>
<li id="section-153">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-153">§</a>
</div>
<h3 id="extends">Extends</h3>
</div>
</li>
<li id="section-154">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-154">§</a>
</div>
<p>Node to extend an object’s prototype with an ancestor object.
After <code>goog.inherits</code> from the
<a href="https://github.com/google/closure-library/blob/master/closure/goog/base.js">Closure Library</a>.</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-built_in">exports</span>.Extends = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Extends</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Base</span></span>
constructor: <span class="hljs-function"><span class="hljs-params">(@child, @parent)</span> -></span>
super()
children: [<span class="hljs-string">'child'</span>, <span class="hljs-string">'parent'</span>]</pre></div></div>
</li>
<li id="section-155">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-155">§</a>
</div>
<p>Hooks one constructor into another’s prototype chain.</p>
</div>
<div class="content"><div class='highlight'><pre> compileToFragments: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
<span class="hljs-keyword">new</span> Call(<span class="hljs-keyword">new</span> Value(<span class="hljs-keyword">new</span> Literal utility <span class="hljs-string">'extend'</span>, o), [@child, @parent]).compileToFragments o</pre></div></div>
</li>
<li id="section-156">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-156">§</a>
</div>
<h3 id="access">Access</h3>
</div>
</li>
<li id="section-157">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-157">§</a>
</div>
<p>A <code>.</code> access into a property of a value, or the <code>::</code> shorthand for
an access into the object’s prototype.</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-built_in">exports</span>.Access = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Access</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Base</span></span>
constructor: <span class="hljs-function"><span class="hljs-params">(@name, {@soak, @shorthand} = {})</span> -></span>
super()
children: [<span class="hljs-string">'name'</span>]
compileToFragments: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
name = @name.compileToFragments o
node = @name.unwrap()
<span class="hljs-keyword">if</span> node <span class="hljs-keyword">instanceof</span> PropertyName
[@makeCode(<span class="hljs-string">'.'</span>), name...]
<span class="hljs-keyword">else</span>
[@makeCode(<span class="hljs-string">'['</span>), name..., @makeCode(<span class="hljs-string">']'</span>)]
shouldCache: NO
astNode: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span></pre></div></div>
</li>
<li id="section-158">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-158">§</a>
</div>
<p>Babel doesn’t have an AST node for <code>Access</code>, but rather just includes
this Access node’s child <code>name</code> Identifier node as the <code>property</code> of
the <code>MemberExpression</code> node.</p>
</div>
<div class="content"><div class='highlight'><pre> @name.ast o</pre></div></div>
</li>
<li id="section-159">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-159">§</a>
</div>
<h3 id="index">Index</h3>
</div>
</li>
<li id="section-160">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-160">§</a>
</div>
<p>A <code>[ ... ]</code> indexed access into an array or object.</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-built_in">exports</span>.Index = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Index</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Base</span></span>
constructor: <span class="hljs-function"><span class="hljs-params">(@index)</span> -></span>
super()
children: [<span class="hljs-string">'index'</span>]
compileToFragments: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
[].concat @makeCode(<span class="hljs-string">"["</span>), @index.compileToFragments(o, LEVEL_PAREN), @makeCode(<span class="hljs-string">"]"</span>)
shouldCache: <span class="hljs-function">-></span>
@index.shouldCache()
astNode: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span></pre></div></div>
</li>
<li id="section-161">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-161">§</a>
</div>
<p>Babel doesn’t have an AST node for <code>Index</code>, but rather just includes
this Index node’s child <code>index</code> Identifier node as the <code>property</code> of
the <code>MemberExpression</code> node. The fact that the <code>MemberExpression</code>’s
<code>property</code> is an Index means that <code>computed</code> is <code>true</code> for the
<code>MemberExpression</code>.</p>
</div>
<div class="content"><div class='highlight'><pre> @index.ast o</pre></div></div>
</li>
<li id="section-162">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-162">§</a>
</div>
<h3 id="range">Range</h3>
</div>
</li>
<li id="section-163">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-163">§</a>
</div>
<p>A range literal. Ranges can be used to extract portions (slices) of arrays,
to specify a range for comprehensions, or as a value, to be expanded into the
corresponding array of integers at runtime.</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-built_in">exports</span>.Range = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Range</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Base</span></span>
children: [<span class="hljs-string">'from'</span>, <span class="hljs-string">'to'</span>]
constructor: <span class="hljs-function"><span class="hljs-params">(@from, @to, tag)</span> -></span>
super()
@exclusive = tag <span class="hljs-keyword">is</span> <span class="hljs-string">'exclusive'</span>
@equals = <span class="hljs-keyword">if</span> @exclusive <span class="hljs-keyword">then</span> <span class="hljs-string">''</span> <span class="hljs-keyword">else</span> <span class="hljs-string">'='</span></pre></div></div>
</li>
<li id="section-164">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-164">§</a>
</div>
<p>Compiles the range’s source variables – where it starts and where it ends.
But only if they need to be cached to avoid double evaluation.</p>
</div>
<div class="content"><div class='highlight'><pre> compileVariables: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
o = merge o, top: <span class="hljs-literal">true</span>
shouldCache = del o, <span class="hljs-string">'shouldCache'</span>
[@fromC, @fromVar] = @cacheToCodeFragments @from.cache o, LEVEL_LIST, shouldCache
[@toC, @toVar] = @cacheToCodeFragments @to.cache o, LEVEL_LIST, shouldCache
[@step, @stepVar] = @cacheToCodeFragments step.cache o, LEVEL_LIST, shouldCache <span class="hljs-keyword">if</span> step = del o, <span class="hljs-string">'step'</span>
@fromNum = <span class="hljs-keyword">if</span> @from.isNumber() <span class="hljs-keyword">then</span> parseNumber @fromVar <span class="hljs-keyword">else</span> <span class="hljs-literal">null</span>
@toNum = <span class="hljs-keyword">if</span> @to.isNumber() <span class="hljs-keyword">then</span> parseNumber @toVar <span class="hljs-keyword">else</span> <span class="hljs-literal">null</span>
@stepNum = <span class="hljs-keyword">if</span> step?.isNumber() <span class="hljs-keyword">then</span> parseNumber @stepVar <span class="hljs-keyword">else</span> <span class="hljs-literal">null</span></pre></div></div>
</li>
<li id="section-165">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-165">§</a>
</div>
<p>When compiled normally, the range returns the contents of the <em>for loop</em>
needed to iterate over the values in the range. Used by comprehensions.</p>
</div>
<div class="content"><div class='highlight'><pre> compileNode: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
@compileVariables o <span class="hljs-keyword">unless</span> @fromVar
<span class="hljs-keyword">return</span> @compileArray(o) <span class="hljs-keyword">unless</span> o.index</pre></div></div>
</li>
<li id="section-166">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-166">§</a>
</div>
<p>Set up endpoints.</p>
</div>
<div class="content"><div class='highlight'><pre> known = @fromNum? <span class="hljs-keyword">and</span> @toNum?
idx = del o, <span class="hljs-string">'index'</span>
idxName = del o, <span class="hljs-string">'name'</span>
namedIndex = idxName <span class="hljs-keyword">and</span> idxName <span class="hljs-keyword">isnt</span> idx
varPart =
<span class="hljs-keyword">if</span> known <span class="hljs-keyword">and</span> <span class="hljs-keyword">not</span> namedIndex
<span class="hljs-string">"var <span class="hljs-subst">#{idx}</span> = <span class="hljs-subst">#{@fromC}</span>"</span>
<span class="hljs-keyword">else</span>
<span class="hljs-string">"<span class="hljs-subst">#{idx}</span> = <span class="hljs-subst">#{@fromC}</span>"</span>
varPart += <span class="hljs-string">", <span class="hljs-subst">#{@toC}</span>"</span> <span class="hljs-keyword">if</span> @toC <span class="hljs-keyword">isnt</span> @toVar
varPart += <span class="hljs-string">", <span class="hljs-subst">#{@step}</span>"</span> <span class="hljs-keyword">if</span> @step <span class="hljs-keyword">isnt</span> @stepVar
[lt, gt] = [<span class="hljs-string">"<span class="hljs-subst">#{idx}</span> <<span class="hljs-subst">#{@equals}</span>"</span>, <span class="hljs-string">"<span class="hljs-subst">#{idx}</span> ><span class="hljs-subst">#{@equals}</span>"</span>]</pre></div></div>
</li>
<li id="section-167">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-167">§</a>
</div>
<p>Generate the condition.</p>
</div>
<div class="content"><div class='highlight'><pre> [<span class="hljs-keyword">from</span>, to] = [@fromNum, @toNum]</pre></div></div>
</li>
<li id="section-168">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-168">§</a>
</div>
<p>Always check if the <code>step</code> isn’t zero to avoid the infinite loop.</p>
</div>
<div class="content"><div class='highlight'><pre> stepNotZero = <span class="hljs-string">"<span class="hljs-subst">#{ @stepNum ? @stepVar }</span> !== 0"</span>
stepCond = <span class="hljs-string">"<span class="hljs-subst">#{ @stepNum ? @stepVar }</span> > 0"</span>
lowerBound = <span class="hljs-string">"<span class="hljs-subst">#{lt}</span> <span class="hljs-subst">#{ <span class="hljs-keyword">if</span> known <span class="hljs-keyword">then</span> to <span class="hljs-keyword">else</span> @toVar }</span>"</span>
upperBound = <span class="hljs-string">"<span class="hljs-subst">#{gt}</span> <span class="hljs-subst">#{ <span class="hljs-keyword">if</span> known <span class="hljs-keyword">then</span> to <span class="hljs-keyword">else</span> @toVar }</span>"</span>
condPart =
<span class="hljs-keyword">if</span> @step?
<span class="hljs-keyword">if</span> @stepNum? <span class="hljs-keyword">and</span> @stepNum <span class="hljs-keyword">isnt</span> <span class="hljs-number">0</span>
<span class="hljs-keyword">if</span> @stepNum > <span class="hljs-number">0</span> <span class="hljs-keyword">then</span> <span class="hljs-string">"<span class="hljs-subst">#{lowerBound}</span>"</span> <span class="hljs-keyword">else</span> <span class="hljs-string">"<span class="hljs-subst">#{upperBound}</span>"</span>
<span class="hljs-keyword">else</span>
<span class="hljs-string">"<span class="hljs-subst">#{stepNotZero}</span> && (<span class="hljs-subst">#{stepCond}</span> ? <span class="hljs-subst">#{lowerBound}</span> : <span class="hljs-subst">#{upperBound}</span>)"</span>
<span class="hljs-keyword">else</span>
<span class="hljs-keyword">if</span> known
<span class="hljs-string">"<span class="hljs-subst">#{ <span class="hljs-keyword">if</span> <span class="hljs-keyword">from</span> <= to <span class="hljs-keyword">then</span> lt <span class="hljs-keyword">else</span> gt }</span> <span class="hljs-subst">#{to}</span>"</span>
<span class="hljs-keyword">else</span>
<span class="hljs-string">"(<span class="hljs-subst">#{@fromVar}</span> <= <span class="hljs-subst">#{@toVar}</span> ? <span class="hljs-subst">#{lowerBound}</span> : <span class="hljs-subst">#{upperBound}</span>)"</span>
cond = <span class="hljs-keyword">if</span> @stepVar <span class="hljs-keyword">then</span> <span class="hljs-string">"<span class="hljs-subst">#{@stepVar}</span> > 0"</span> <span class="hljs-keyword">else</span> <span class="hljs-string">"<span class="hljs-subst">#{@fromVar}</span> <= <span class="hljs-subst">#{@toVar}</span>"</span></pre></div></div>
</li>
<li id="section-169">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-169">§</a>
</div>
<p>Generate the step.</p>
</div>
<div class="content"><div class='highlight'><pre> stepPart = <span class="hljs-keyword">if</span> @stepVar
<span class="hljs-string">"<span class="hljs-subst">#{idx}</span> += <span class="hljs-subst">#{@stepVar}</span>"</span>
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> known
<span class="hljs-keyword">if</span> namedIndex
<span class="hljs-keyword">if</span> <span class="hljs-keyword">from</span> <= to <span class="hljs-keyword">then</span> <span class="hljs-string">"++<span class="hljs-subst">#{idx}</span>"</span> <span class="hljs-keyword">else</span> <span class="hljs-string">"--<span class="hljs-subst">#{idx}</span>"</span>
<span class="hljs-keyword">else</span>
<span class="hljs-keyword">if</span> <span class="hljs-keyword">from</span> <= to <span class="hljs-keyword">then</span> <span class="hljs-string">"<span class="hljs-subst">#{idx}</span>++"</span> <span class="hljs-keyword">else</span> <span class="hljs-string">"<span class="hljs-subst">#{idx}</span>--"</span>
<span class="hljs-keyword">else</span>
<span class="hljs-keyword">if</span> namedIndex
<span class="hljs-string">"<span class="hljs-subst">#{cond}</span> ? ++<span class="hljs-subst">#{idx}</span> : --<span class="hljs-subst">#{idx}</span>"</span>
<span class="hljs-keyword">else</span>
<span class="hljs-string">"<span class="hljs-subst">#{cond}</span> ? <span class="hljs-subst">#{idx}</span>++ : <span class="hljs-subst">#{idx}</span>--"</span>
varPart = <span class="hljs-string">"<span class="hljs-subst">#{idxName}</span> = <span class="hljs-subst">#{varPart}</span>"</span> <span class="hljs-keyword">if</span> namedIndex
stepPart = <span class="hljs-string">"<span class="hljs-subst">#{idxName}</span> = <span class="hljs-subst">#{stepPart}</span>"</span> <span class="hljs-keyword">if</span> namedIndex</pre></div></div>
</li>
<li id="section-170">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-170">§</a>
</div>
<p>The final loop body.</p>
</div>
<div class="content"><div class='highlight'><pre> [@makeCode <span class="hljs-string">"<span class="hljs-subst">#{varPart}</span>; <span class="hljs-subst">#{condPart}</span>; <span class="hljs-subst">#{stepPart}</span>"</span>]</pre></div></div>
</li>
<li id="section-171">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-171">§</a>
</div>
<p>When used as a value, expand the range into the equivalent array.</p>
</div>
<div class="content"><div class='highlight'><pre> compileArray: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
known = @fromNum? <span class="hljs-keyword">and</span> @toNum?
<span class="hljs-keyword">if</span> known <span class="hljs-keyword">and</span> <span class="hljs-built_in">Math</span>.abs(@fromNum - @toNum) <= <span class="hljs-number">20</span>
range = [@fromNum..@toNum]
range.pop() <span class="hljs-keyword">if</span> @exclusive
<span class="hljs-keyword">return</span> [@makeCode <span class="hljs-string">"[<span class="hljs-subst">#{ range.join(<span class="hljs-string">', '</span>) }</span>]"</span>]
idt = @tab + TAB
i = o.scope.freeVariable <span class="hljs-string">'i'</span>, single: <span class="hljs-literal">true</span>, reserve: <span class="hljs-literal">no</span>
result = o.scope.freeVariable <span class="hljs-string">'results'</span>, reserve: <span class="hljs-literal">no</span>
pre = <span class="hljs-string">"\n<span class="hljs-subst">#{idt}</span>var <span class="hljs-subst">#{result}</span> = [];"</span>
<span class="hljs-keyword">if</span> known
o.index = i
body = fragmentsToText @compileNode o
<span class="hljs-keyword">else</span>
vars = <span class="hljs-string">"<span class="hljs-subst">#{i}</span> = <span class="hljs-subst">#{@fromC}</span>"</span> + <span class="hljs-keyword">if</span> @toC <span class="hljs-keyword">isnt</span> @toVar <span class="hljs-keyword">then</span> <span class="hljs-string">", <span class="hljs-subst">#{@toC}</span>"</span> <span class="hljs-keyword">else</span> <span class="hljs-string">''</span>
cond = <span class="hljs-string">"<span class="hljs-subst">#{@fromVar}</span> <= <span class="hljs-subst">#{@toVar}</span>"</span>
body = <span class="hljs-string">"var <span class="hljs-subst">#{vars}</span>; <span class="hljs-subst">#{cond}</span> ? <span class="hljs-subst">#{i}</span> <<span class="hljs-subst">#{@equals}</span> <span class="hljs-subst">#{@toVar}</span> : <span class="hljs-subst">#{i}</span> ><span class="hljs-subst">#{@equals}</span> <span class="hljs-subst">#{@toVar}</span>; <span class="hljs-subst">#{cond}</span> ? <span class="hljs-subst">#{i}</span>++ : <span class="hljs-subst">#{i}</span>--"</span>
post = <span class="hljs-string">"{ <span class="hljs-subst">#{result}</span>.push(<span class="hljs-subst">#{i}</span>); }\n<span class="hljs-subst">#{idt}</span>return <span class="hljs-subst">#{result}</span>;\n<span class="hljs-subst">#{o.indent}</span>"</span>
<span class="hljs-function"> <span class="hljs-title">hasArgs</span> = <span class="hljs-params">(node)</span> -></span> node?.contains isLiteralArguments
args = <span class="hljs-string">', arguments'</span> <span class="hljs-keyword">if</span> hasArgs(@from) <span class="hljs-keyword">or</span> hasArgs(@to)
[@makeCode <span class="hljs-string">"(function() {<span class="hljs-subst">#{pre}</span>\n<span class="hljs-subst">#{idt}</span>for (<span class="hljs-subst">#{body}</span>)<span class="hljs-subst">#{post}</span>}).apply(this<span class="hljs-subst">#{args ? <span class="hljs-string">''</span>}</span>)"</span>]
astProperties: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
<span class="hljs-keyword">return</span> {
from: @from?.ast(o) ? <span class="hljs-literal">null</span>
to: @to?.ast(o) ? <span class="hljs-literal">null</span>
@exclusive
}</pre></div></div>
</li>
<li id="section-172">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-172">§</a>
</div>
<h3 id="slice">Slice</h3>
</div>
</li>
<li id="section-173">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-173">§</a>
</div>
<p>An array slice literal. Unlike JavaScript’s <code>Array#slice</code>, the second parameter
specifies the index of the end of the slice, just as the first parameter
is the index of the beginning.</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-built_in">exports</span>.Slice = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Slice</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Base</span></span>
children: [<span class="hljs-string">'range'</span>]
constructor: <span class="hljs-function"><span class="hljs-params">(@range)</span> -></span>
super()</pre></div></div>
</li>
<li id="section-174">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-174">§</a>
</div>
<p>We have to be careful when trying to slice through the end of the array,
<code>9e9</code> is used because not all implementations respect <code>undefined</code> or <code>1/0</code>.
<code>9e9</code> should be safe because <code>9e9</code> > <code>2**32</code>, the max array length.</p>
</div>
<div class="content"><div class='highlight'><pre> compileNode: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
{to, <span class="hljs-keyword">from</span>} = @range</pre></div></div>
</li>
<li id="section-175">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-175">§</a>
</div>
<p>Handle an expression in the property access, e.g. <code>a[!b in c..]</code>.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> <span class="hljs-keyword">from</span>?.shouldCache()
<span class="hljs-keyword">from</span> = <span class="hljs-keyword">new</span> Value <span class="hljs-keyword">new</span> Parens <span class="hljs-keyword">from</span>
<span class="hljs-keyword">if</span> to?.shouldCache()
to = <span class="hljs-keyword">new</span> Value <span class="hljs-keyword">new</span> Parens to
fromCompiled = <span class="hljs-keyword">from</span>?.compileToFragments(o, LEVEL_PAREN) <span class="hljs-keyword">or</span> [@makeCode <span class="hljs-string">'0'</span>]
<span class="hljs-keyword">if</span> to
compiled = to.compileToFragments o, LEVEL_PAREN
compiledText = fragmentsToText compiled
<span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> (<span class="hljs-keyword">not</span> @range.exclusive <span class="hljs-keyword">and</span> +compiledText <span class="hljs-keyword">is</span> <span class="hljs-number">-1</span>)
toStr = <span class="hljs-string">', '</span> + <span class="hljs-keyword">if</span> @range.exclusive
compiledText
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> to.isNumber()
<span class="hljs-string">"<span class="hljs-subst">#{+compiledText + <span class="hljs-number">1</span>}</span>"</span>
<span class="hljs-keyword">else</span>
compiled = to.compileToFragments o, LEVEL_ACCESS
<span class="hljs-string">"+<span class="hljs-subst">#{fragmentsToText compiled}</span> + 1 || 9e9"</span>
[@makeCode <span class="hljs-string">".slice(<span class="hljs-subst">#{ fragmentsToText fromCompiled }</span><span class="hljs-subst">#{ toStr <span class="hljs-keyword">or</span> <span class="hljs-string">''</span> }</span>)"</span>]
astNode: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
@range.ast o</pre></div></div>
</li>
<li id="section-176">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-176">§</a>
</div>
<h3 id="obj">Obj</h3>
</div>
</li>
<li id="section-177">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-177">§</a>
</div>
<p>An object literal, nothing fancy.</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-built_in">exports</span>.Obj = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Obj</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Base</span></span>
constructor: <span class="hljs-function"><span class="hljs-params">(props, @generated = <span class="hljs-literal">no</span>)</span> -></span>
super()
@objects = @properties = props <span class="hljs-keyword">or</span> []
children: [<span class="hljs-string">'properties'</span>]
isAssignable: <span class="hljs-function"><span class="hljs-params">(opts)</span> -></span>
<span class="hljs-keyword">for</span> prop <span class="hljs-keyword">in</span> @properties</pre></div></div>
</li>
<li id="section-178">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-178">§</a>
</div>
<p>Check for reserved words.</p>
</div>
<div class="content"><div class='highlight'><pre> message = isUnassignable prop.unwrapAll().value
prop.error message <span class="hljs-keyword">if</span> message
prop = prop.value <span class="hljs-keyword">if</span> prop <span class="hljs-keyword">instanceof</span> Assign <span class="hljs-keyword">and</span>
prop.context <span class="hljs-keyword">is</span> <span class="hljs-string">'object'</span> <span class="hljs-keyword">and</span>
prop.value?.base <span class="hljs-keyword">not</span> <span class="hljs-keyword">instanceof</span> Arr
<span class="hljs-keyword">return</span> <span class="hljs-literal">no</span> <span class="hljs-keyword">unless</span> prop.isAssignable opts
<span class="hljs-literal">yes</span>
shouldCache: <span class="hljs-function">-></span>
<span class="hljs-keyword">not</span> @isAssignable()</pre></div></div>
</li>
<li id="section-179">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-179">§</a>
</div>
<p>Check if object contains splat.</p>
</div>
<div class="content"><div class='highlight'><pre> hasSplat: <span class="hljs-function">-></span>
<span class="hljs-keyword">return</span> <span class="hljs-literal">yes</span> <span class="hljs-keyword">for</span> prop <span class="hljs-keyword">in</span> @properties <span class="hljs-keyword">when</span> prop <span class="hljs-keyword">instanceof</span> Splat
<span class="hljs-literal">no</span></pre></div></div>
</li>
<li id="section-180">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-180">§</a>
</div>
<p>Move rest property to the end of the list.
<code>{a, rest..., b} = obj</code> -> <code>{a, b, rest...} = obj</code>
<code>foo = ({a, rest..., b}) -></code> -> <code>foo = {a, b, rest...}) -></code></p>
</div>
<div class="content"><div class='highlight'><pre> reorderProperties: <span class="hljs-function">-></span>
props = @properties
splatProps = @getAndCheckSplatProps()
splatProp = props.splice splatProps[<span class="hljs-number">0</span>], <span class="hljs-number">1</span>
@objects = @properties = [].concat props, splatProp
compileNode: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
@reorderProperties() <span class="hljs-keyword">if</span> @hasSplat() <span class="hljs-keyword">and</span> @lhs
props = @properties
<span class="hljs-keyword">if</span> @generated
<span class="hljs-keyword">for</span> node <span class="hljs-keyword">in</span> props <span class="hljs-keyword">when</span> node <span class="hljs-keyword">instanceof</span> Value
node.error <span class="hljs-string">'cannot have an implicit value in an implicit object'</span>
idt = o.indent += TAB
lastNode = @lastNode @properties</pre></div></div>
</li>
<li id="section-181">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-181">§</a>
</div>
<p>If this object is the left-hand side of an assignment, all its children
are too.</p>
</div>
<div class="content"><div class='highlight'><pre> @propagateLhs()
isCompact = <span class="hljs-literal">yes</span>
<span class="hljs-keyword">for</span> prop <span class="hljs-keyword">in</span> @properties
<span class="hljs-keyword">if</span> prop <span class="hljs-keyword">instanceof</span> Assign <span class="hljs-keyword">and</span> prop.context <span class="hljs-keyword">is</span> <span class="hljs-string">'object'</span>
isCompact = <span class="hljs-literal">no</span>
answer = []
answer.push @makeCode <span class="hljs-keyword">if</span> isCompact <span class="hljs-keyword">then</span> <span class="hljs-string">''</span> <span class="hljs-keyword">else</span> <span class="hljs-string">'\n'</span>
<span class="hljs-keyword">for</span> prop, i <span class="hljs-keyword">in</span> props
join = <span class="hljs-keyword">if</span> i <span class="hljs-keyword">is</span> props.length - <span class="hljs-number">1</span>
<span class="hljs-string">''</span>
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> isCompact
<span class="hljs-string">', '</span>
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> prop <span class="hljs-keyword">is</span> lastNode
<span class="hljs-string">'\n'</span>
<span class="hljs-keyword">else</span>
<span class="hljs-string">',\n'</span>
indent = <span class="hljs-keyword">if</span> isCompact <span class="hljs-keyword">then</span> <span class="hljs-string">''</span> <span class="hljs-keyword">else</span> idt
key = <span class="hljs-keyword">if</span> prop <span class="hljs-keyword">instanceof</span> Assign <span class="hljs-keyword">and</span> prop.context <span class="hljs-keyword">is</span> <span class="hljs-string">'object'</span>
prop.variable
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> prop <span class="hljs-keyword">instanceof</span> Assign
prop.operatorToken.error <span class="hljs-string">"unexpected <span class="hljs-subst">#{prop.operatorToken.value}</span>"</span> <span class="hljs-keyword">unless</span> @lhs
prop.variable
<span class="hljs-keyword">else</span>
prop
<span class="hljs-keyword">if</span> key <span class="hljs-keyword">instanceof</span> Value <span class="hljs-keyword">and</span> key.hasProperties()
key.error <span class="hljs-string">'invalid object key'</span> <span class="hljs-keyword">if</span> prop.context <span class="hljs-keyword">is</span> <span class="hljs-string">'object'</span> <span class="hljs-keyword">or</span> <span class="hljs-keyword">not</span> key.this
key = key.properties[<span class="hljs-number">0</span>].name
prop = <span class="hljs-keyword">new</span> Assign key, prop, <span class="hljs-string">'object'</span>
<span class="hljs-keyword">if</span> key <span class="hljs-keyword">is</span> prop
<span class="hljs-keyword">if</span> prop.shouldCache()
[key, value] = prop.base.cache o
key = <span class="hljs-keyword">new</span> PropertyName key.value <span class="hljs-keyword">if</span> key <span class="hljs-keyword">instanceof</span> IdentifierLiteral
prop = <span class="hljs-keyword">new</span> Assign key, value, <span class="hljs-string">'object'</span>
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> key <span class="hljs-keyword">instanceof</span> Value <span class="hljs-keyword">and</span> key.base <span class="hljs-keyword">instanceof</span> ComputedPropertyName</pre></div></div>
</li>
<li id="section-182">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-182">§</a>
</div>
<p><code>{ [foo()] }</code> output as <code>{ [ref = foo()]: ref }</code>.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> prop.base.value.shouldCache()
[key, value] = prop.base.value.cache o
key = <span class="hljs-keyword">new</span> ComputedPropertyName key.value <span class="hljs-keyword">if</span> key <span class="hljs-keyword">instanceof</span> IdentifierLiteral
prop = <span class="hljs-keyword">new</span> Assign key, value, <span class="hljs-string">'object'</span>
<span class="hljs-keyword">else</span></pre></div></div>
</li>
<li id="section-183">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-183">§</a>
</div>
<p><code>{ [expression] }</code> output as <code>{ [expression]: expression }</code>.</p>
</div>
<div class="content"><div class='highlight'><pre> prop = <span class="hljs-keyword">new</span> Assign key, prop.base.value, <span class="hljs-string">'object'</span>
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> prop.bareLiteral?(IdentifierLiteral) <span class="hljs-keyword">and</span> prop <span class="hljs-keyword">not</span> <span class="hljs-keyword">instanceof</span> Splat
prop = <span class="hljs-keyword">new</span> Assign prop, prop, <span class="hljs-string">'object'</span>
<span class="hljs-keyword">if</span> indent <span class="hljs-keyword">then</span> answer.push @makeCode indent
answer.push prop.compileToFragments(o, LEVEL_TOP)...
<span class="hljs-keyword">if</span> join <span class="hljs-keyword">then</span> answer.push @makeCode join
answer.push @makeCode <span class="hljs-keyword">if</span> isCompact <span class="hljs-keyword">then</span> <span class="hljs-string">''</span> <span class="hljs-keyword">else</span> <span class="hljs-string">"\n<span class="hljs-subst">#{@tab}</span>"</span>
answer = @wrapInBraces answer
<span class="hljs-keyword">if</span> @front <span class="hljs-keyword">then</span> @wrapInParentheses answer <span class="hljs-keyword">else</span> answer
getAndCheckSplatProps: <span class="hljs-function">-></span>
<span class="hljs-keyword">return</span> <span class="hljs-keyword">unless</span> @hasSplat() <span class="hljs-keyword">and</span> @lhs
props = @properties
splatProps = (i <span class="hljs-keyword">for</span> prop, i <span class="hljs-keyword">in</span> props <span class="hljs-keyword">when</span> prop <span class="hljs-keyword">instanceof</span> Splat)
props[splatProps[<span class="hljs-number">1</span>]].error <span class="hljs-string">"multiple spread elements are disallowed"</span> <span class="hljs-keyword">if</span> splatProps?.length > <span class="hljs-number">1</span>
splatProps
assigns: <span class="hljs-function"><span class="hljs-params">(name)</span> -></span>
<span class="hljs-keyword">for</span> prop <span class="hljs-keyword">in</span> @properties <span class="hljs-keyword">when</span> prop.assigns name <span class="hljs-keyword">then</span> <span class="hljs-keyword">return</span> <span class="hljs-literal">yes</span>
<span class="hljs-literal">no</span>
eachName: <span class="hljs-function"><span class="hljs-params">(iterator)</span> -></span>
<span class="hljs-keyword">for</span> prop <span class="hljs-keyword">in</span> @properties
prop = prop.value <span class="hljs-keyword">if</span> prop <span class="hljs-keyword">instanceof</span> Assign <span class="hljs-keyword">and</span> prop.context <span class="hljs-keyword">is</span> <span class="hljs-string">'object'</span>
prop = prop.unwrapAll()
prop.eachName iterator <span class="hljs-keyword">if</span> prop.eachName?</pre></div></div>
</li>
<li id="section-184">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-184">§</a>
</div>
<p>Convert “bare” properties to <code>ObjectProperty</code>s (or <code>Splat</code>s).</p>
</div>
<div class="content"><div class='highlight'><pre> expandProperty: <span class="hljs-function"><span class="hljs-params">(property)</span> -></span>
{variable, context, operatorToken} = property
key = <span class="hljs-keyword">if</span> property <span class="hljs-keyword">instanceof</span> Assign <span class="hljs-keyword">and</span> context <span class="hljs-keyword">is</span> <span class="hljs-string">'object'</span>
variable
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> property <span class="hljs-keyword">instanceof</span> Assign
operatorToken.error <span class="hljs-string">"unexpected <span class="hljs-subst">#{operatorToken.value}</span>"</span> <span class="hljs-keyword">unless</span> @lhs
variable
<span class="hljs-keyword">else</span>
property
<span class="hljs-keyword">if</span> key <span class="hljs-keyword">instanceof</span> Value <span class="hljs-keyword">and</span> key.hasProperties()
key.error <span class="hljs-string">'invalid object key'</span> <span class="hljs-keyword">unless</span> context <span class="hljs-keyword">isnt</span> <span class="hljs-string">'object'</span> <span class="hljs-keyword">and</span> key.this
<span class="hljs-keyword">if</span> property <span class="hljs-keyword">instanceof</span> Assign
<span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> ObjectProperty fromAssign: property
<span class="hljs-keyword">else</span>
<span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> ObjectProperty key: property
<span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> ObjectProperty(fromAssign: property) <span class="hljs-keyword">unless</span> key <span class="hljs-keyword">is</span> property
<span class="hljs-keyword">return</span> property <span class="hljs-keyword">if</span> property <span class="hljs-keyword">instanceof</span> Splat
<span class="hljs-keyword">new</span> ObjectProperty key: property
expandProperties: <span class="hljs-function">-></span>
@expandProperty(property) <span class="hljs-keyword">for</span> property <span class="hljs-keyword">in</span> @properties
propagateLhs: <span class="hljs-function"><span class="hljs-params">(setLhs)</span> -></span>
@lhs = <span class="hljs-literal">yes</span> <span class="hljs-keyword">if</span> setLhs
<span class="hljs-keyword">return</span> <span class="hljs-keyword">unless</span> @lhs
<span class="hljs-keyword">for</span> property <span class="hljs-keyword">in</span> @properties
<span class="hljs-keyword">if</span> property <span class="hljs-keyword">instanceof</span> Assign <span class="hljs-keyword">and</span> property.context <span class="hljs-keyword">is</span> <span class="hljs-string">'object'</span>
{value} = property
unwrappedValue = value.unwrapAll()
<span class="hljs-keyword">if</span> unwrappedValue <span class="hljs-keyword">instanceof</span> Arr <span class="hljs-keyword">or</span> unwrappedValue <span class="hljs-keyword">instanceof</span> Obj
unwrappedValue.propagateLhs <span class="hljs-literal">yes</span>
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> unwrappedValue <span class="hljs-keyword">instanceof</span> Assign
unwrappedValue.nestedLhs = <span class="hljs-literal">yes</span>
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> property <span class="hljs-keyword">instanceof</span> Assign</pre></div></div>
</li>
<li id="section-185">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-185">§</a>
</div>
<p>Shorthand property with default, e.g. <code>{a = 1} = b</code>.</p>
</div>
<div class="content"><div class='highlight'><pre> property.nestedLhs = <span class="hljs-literal">yes</span>
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> property <span class="hljs-keyword">instanceof</span> Splat
property.propagateLhs <span class="hljs-literal">yes</span>
astNode: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
@getAndCheckSplatProps()
super o
astType: <span class="hljs-function">-></span>
<span class="hljs-keyword">if</span> @lhs
<span class="hljs-string">'ObjectPattern'</span>
<span class="hljs-keyword">else</span>
<span class="hljs-string">'ObjectExpression'</span>
astProperties: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
<span class="hljs-keyword">return</span>
implicit: !!@generated
properties:
property.ast(o) <span class="hljs-keyword">for</span> property <span class="hljs-keyword">in</span> @expandProperties()
<span class="hljs-built_in">exports</span>.ObjectProperty = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ObjectProperty</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Base</span></span>
constructor: <span class="hljs-function"><span class="hljs-params">({key, fromAssign})</span> -></span>
super()
<span class="hljs-keyword">if</span> fromAssign
{variable: @key, value, context} = fromAssign
<span class="hljs-keyword">if</span> context <span class="hljs-keyword">is</span> <span class="hljs-string">'object'</span></pre></div></div>
</li>
<li id="section-186">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-186">§</a>
</div>
<p>All non-shorthand properties (i.e. includes <code>:</code>).</p>
</div>
<div class="content"><div class='highlight'><pre> @value = value
<span class="hljs-keyword">else</span></pre></div></div>
</li>
<li id="section-187">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-187">§</a>
</div>
<p>Left-hand-side shorthand with default e.g. <code>{a = 1} = b</code>.</p>
</div>
<div class="content"><div class='highlight'><pre> @value = fromAssign
@shorthand = <span class="hljs-literal">yes</span>
@locationData = fromAssign.locationData
<span class="hljs-keyword">else</span></pre></div></div>
</li>
<li id="section-188">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-188">§</a>
</div>
<p>Shorthand without default e.g. <code>{a}</code> or <code>{@a}</code> or <code>{[a]}</code>.</p>
</div>
<div class="content"><div class='highlight'><pre> @key = key
@shorthand = <span class="hljs-literal">yes</span>
@locationData = key.locationData
astProperties: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
isComputedPropertyName = (@key <span class="hljs-keyword">instanceof</span> Value <span class="hljs-keyword">and</span> @key.base <span class="hljs-keyword">instanceof</span> ComputedPropertyName) <span class="hljs-keyword">or</span> @key.unwrap() <span class="hljs-keyword">instanceof</span> StringWithInterpolations
keyAst = @key.ast o, LEVEL_LIST
<span class="hljs-keyword">return</span>
key:
<span class="hljs-keyword">if</span> keyAst?.declaration
<span class="hljs-built_in">Object</span>.assign {}, keyAst, declaration: <span class="hljs-literal">no</span>
<span class="hljs-keyword">else</span>
keyAst
value: @value?.ast(o, LEVEL_LIST) ? keyAst
shorthand: !!@shorthand
computed: !!isComputedPropertyName
method: <span class="hljs-literal">no</span></pre></div></div>
</li>
<li id="section-189">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-189">§</a>
</div>
<h3 id="arr">Arr</h3>
</div>
</li>
<li id="section-190">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-190">§</a>
</div>
<p>An array literal.</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-built_in">exports</span>.Arr = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Arr</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Base</span></span>
constructor: <span class="hljs-function"><span class="hljs-params">(objs, @lhs = <span class="hljs-literal">no</span>)</span> -></span>
super()
@objects = objs <span class="hljs-keyword">or</span> []
@propagateLhs()
children: [<span class="hljs-string">'objects'</span>]
hasElision: <span class="hljs-function">-></span>
<span class="hljs-keyword">return</span> <span class="hljs-literal">yes</span> <span class="hljs-keyword">for</span> obj <span class="hljs-keyword">in</span> @objects <span class="hljs-keyword">when</span> obj <span class="hljs-keyword">instanceof</span> Elision
<span class="hljs-literal">no</span>
isAssignable: <span class="hljs-function"><span class="hljs-params">(opts)</span> -></span>
{allowExpansion, allowNontrailingSplat, allowEmptyArray = <span class="hljs-literal">no</span>} = opts ? {}
<span class="hljs-keyword">return</span> allowEmptyArray <span class="hljs-keyword">unless</span> @objects.length
<span class="hljs-keyword">for</span> obj, i <span class="hljs-keyword">in</span> @objects
<span class="hljs-keyword">return</span> <span class="hljs-literal">no</span> <span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> allowNontrailingSplat <span class="hljs-keyword">and</span> obj <span class="hljs-keyword">instanceof</span> Splat <span class="hljs-keyword">and</span> i + <span class="hljs-number">1</span> <span class="hljs-keyword">isnt</span> @objects.length
<span class="hljs-keyword">return</span> <span class="hljs-literal">no</span> <span class="hljs-keyword">unless</span> (allowExpansion <span class="hljs-keyword">and</span> obj <span class="hljs-keyword">instanceof</span> Expansion) <span class="hljs-keyword">or</span> (obj.isAssignable(opts) <span class="hljs-keyword">and</span> (<span class="hljs-keyword">not</span> obj.isAtomic <span class="hljs-keyword">or</span> obj.isAtomic()))
<span class="hljs-literal">yes</span>
shouldCache: <span class="hljs-function">-></span>
<span class="hljs-keyword">not</span> @isAssignable()
compileNode: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
<span class="hljs-keyword">return</span> [@makeCode <span class="hljs-string">'[]'</span>] <span class="hljs-keyword">unless</span> @objects.length
o.indent += TAB
<span class="hljs-function"> <span class="hljs-title">fragmentIsElision</span> = <span class="hljs-params">([ fragment ])</span> -></span>
fragment.type <span class="hljs-keyword">is</span> <span class="hljs-string">'Elision'</span> <span class="hljs-keyword">and</span> fragment.code.trim() <span class="hljs-keyword">is</span> <span class="hljs-string">','</span></pre></div></div>
</li>
<li id="section-191">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-191">§</a>
</div>
<p>Detect if <code>Elision</code>s at the beginning of the array are processed (e.g. [, , , a]).</p>
</div>
<div class="content"><div class='highlight'><pre> passedElision = <span class="hljs-literal">no</span>
answer = []
<span class="hljs-keyword">for</span> obj, objIndex <span class="hljs-keyword">in</span> @objects
unwrappedObj = obj.unwrapAll()</pre></div></div>
</li>
<li id="section-192">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-192">§</a>
</div>
<p>Let <code>compileCommentFragments</code> know to intersperse block comments
into the fragments created when compiling this array.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> unwrappedObj.comments <span class="hljs-keyword">and</span>
unwrappedObj.comments.filter(<span class="hljs-function"><span class="hljs-params">(comment)</span> -></span> <span class="hljs-keyword">not</span> comment.here).length <span class="hljs-keyword">is</span> <span class="hljs-number">0</span>
unwrappedObj.includeCommentFragments = YES
compiledObjs = (obj.compileToFragments o, LEVEL_LIST <span class="hljs-keyword">for</span> obj <span class="hljs-keyword">in</span> @objects)
olen = compiledObjs.length</pre></div></div>
</li>
<li id="section-193">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-193">§</a>
</div>
<p>If <code>compiledObjs</code> includes newlines, we will output this as a multiline
array (i.e. with a newline and indentation after the <code>[</code>). If an element
contains line comments, that should also trigger multiline output since
by definition line comments will introduce newlines into our output.
The exception is if only the first element has line comments; in that
case, output as the compact form if we otherwise would have, so that the
first element’s line comments get output before or after the array.</p>
</div>
<div class="content"><div class='highlight'><pre> includesLineCommentsOnNonFirstElement = <span class="hljs-literal">no</span>
<span class="hljs-keyword">for</span> fragments, index <span class="hljs-keyword">in</span> compiledObjs
<span class="hljs-keyword">for</span> fragment <span class="hljs-keyword">in</span> fragments
<span class="hljs-keyword">if</span> fragment.isHereComment
fragment.code = fragment.code.trim()
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> index <span class="hljs-keyword">isnt</span> <span class="hljs-number">0</span> <span class="hljs-keyword">and</span> includesLineCommentsOnNonFirstElement <span class="hljs-keyword">is</span> <span class="hljs-literal">no</span> <span class="hljs-keyword">and</span> hasLineComments fragment
includesLineCommentsOnNonFirstElement = <span class="hljs-literal">yes</span></pre></div></div>
</li>
<li id="section-194">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-194">§</a>
</div>
<p>Add ‘, ‘ if all <code>Elisions</code> from the beginning of the array are processed (e.g. [, , , a]) and
element isn’t <code>Elision</code> or last element is <code>Elision</code> (e.g. [a,,b,,])</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> index <span class="hljs-keyword">isnt</span> <span class="hljs-number">0</span> <span class="hljs-keyword">and</span> passedElision <span class="hljs-keyword">and</span> (<span class="hljs-keyword">not</span> fragmentIsElision(fragments) <span class="hljs-keyword">or</span> index <span class="hljs-keyword">is</span> olen - <span class="hljs-number">1</span>)
answer.push @makeCode <span class="hljs-string">', '</span>
passedElision = passedElision <span class="hljs-keyword">or</span> <span class="hljs-keyword">not</span> fragmentIsElision fragments
answer.push fragments...
<span class="hljs-keyword">if</span> includesLineCommentsOnNonFirstElement <span class="hljs-keyword">or</span> <span class="hljs-string">'\n'</span> <span class="hljs-keyword">in</span> fragmentsToText(answer)
<span class="hljs-keyword">for</span> fragment, fragmentIndex <span class="hljs-keyword">in</span> answer
<span class="hljs-keyword">if</span> fragment.isHereComment
fragment.code = <span class="hljs-string">"<span class="hljs-subst">#{multident(fragment.code, o.indent, <span class="hljs-literal">no</span>)}</span>\n<span class="hljs-subst">#{o.indent}</span>"</span>
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> fragment.code <span class="hljs-keyword">is</span> <span class="hljs-string">', '</span> <span class="hljs-keyword">and</span> <span class="hljs-keyword">not</span> fragment?.isElision <span class="hljs-keyword">and</span> fragment.type <span class="hljs-keyword">not</span> <span class="hljs-keyword">in</span> [<span class="hljs-string">'StringLiteral'</span>, <span class="hljs-string">'StringWithInterpolations'</span>]
fragment.code = <span class="hljs-string">",\n<span class="hljs-subst">#{o.indent}</span>"</span>
answer.unshift @makeCode <span class="hljs-string">"[\n<span class="hljs-subst">#{o.indent}</span>"</span>
answer.push @makeCode <span class="hljs-string">"\n<span class="hljs-subst">#{@tab}</span>]"</span>
<span class="hljs-keyword">else</span>
<span class="hljs-keyword">for</span> fragment <span class="hljs-keyword">in</span> answer <span class="hljs-keyword">when</span> fragment.isHereComment
fragment.code = <span class="hljs-string">"<span class="hljs-subst">#{fragment.code}</span> "</span>
answer.unshift @makeCode <span class="hljs-string">'['</span>
answer.push @makeCode <span class="hljs-string">']'</span>
answer
assigns: <span class="hljs-function"><span class="hljs-params">(name)</span> -></span>
<span class="hljs-keyword">for</span> obj <span class="hljs-keyword">in</span> @objects <span class="hljs-keyword">when</span> obj.assigns name <span class="hljs-keyword">then</span> <span class="hljs-keyword">return</span> <span class="hljs-literal">yes</span>
<span class="hljs-literal">no</span>
eachName: <span class="hljs-function"><span class="hljs-params">(iterator)</span> -></span>
<span class="hljs-keyword">for</span> obj <span class="hljs-keyword">in</span> @objects
obj = obj.unwrapAll()
obj.eachName iterator</pre></div></div>
</li>
<li id="section-195">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-195">§</a>
</div>
<p>If this array is the left-hand side of an assignment, all its children
are too.</p>
</div>
<div class="content"><div class='highlight'><pre> propagateLhs: <span class="hljs-function"><span class="hljs-params">(setLhs)</span> -></span>
@lhs = <span class="hljs-literal">yes</span> <span class="hljs-keyword">if</span> setLhs
<span class="hljs-keyword">return</span> <span class="hljs-keyword">unless</span> @lhs
<span class="hljs-keyword">for</span> object <span class="hljs-keyword">in</span> @objects
object.lhs = <span class="hljs-literal">yes</span> <span class="hljs-keyword">if</span> object <span class="hljs-keyword">instanceof</span> Splat <span class="hljs-keyword">or</span> object <span class="hljs-keyword">instanceof</span> Expansion
unwrappedObject = object.unwrapAll()
<span class="hljs-keyword">if</span> unwrappedObject <span class="hljs-keyword">instanceof</span> Arr <span class="hljs-keyword">or</span> unwrappedObject <span class="hljs-keyword">instanceof</span> Obj
unwrappedObject.propagateLhs <span class="hljs-literal">yes</span>
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> unwrappedObject <span class="hljs-keyword">instanceof</span> Assign
unwrappedObject.nestedLhs = <span class="hljs-literal">yes</span>
astType: <span class="hljs-function">-></span>
<span class="hljs-keyword">if</span> @lhs
<span class="hljs-string">'ArrayPattern'</span>
<span class="hljs-keyword">else</span>
<span class="hljs-string">'ArrayExpression'</span>
astProperties: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
<span class="hljs-keyword">return</span>
elements:
object.ast(o, LEVEL_LIST) <span class="hljs-keyword">for</span> object <span class="hljs-keyword">in</span> @objects</pre></div></div>
</li>
<li id="section-196">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-196">§</a>
</div>
<h3 id="class">Class</h3>
</div>
</li>
<li id="section-197">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-197">§</a>
</div>
<p>The CoffeeScript class definition.
Initialize a <strong>Class</strong> with its name, an optional superclass, and a body.</p>
</div>
<div class="content"><div class='highlight'><pre>
<span class="hljs-built_in">exports</span>.Class = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Class</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Base</span></span>
children: [<span class="hljs-string">'variable'</span>, <span class="hljs-string">'parent'</span>, <span class="hljs-string">'body'</span>]
constructor: <span class="hljs-function"><span class="hljs-params">(@variable, @parent, @body)</span> -></span>
super()
<span class="hljs-keyword">unless</span> @body?
@body = <span class="hljs-keyword">new</span> Block
@hasGeneratedBody = <span class="hljs-literal">yes</span>
compileNode: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
@name = @determineName()
executableBody = @walkBody o</pre></div></div>
</li>
<li id="section-198">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-198">§</a>
</div>
<p>Special handling to allow <code>class expr.A extends A</code> declarations</p>
</div>
<div class="content"><div class='highlight'><pre> parentName = @parent.base.value <span class="hljs-keyword">if</span> @parent <span class="hljs-keyword">instanceof</span> Value <span class="hljs-keyword">and</span> <span class="hljs-keyword">not</span> @parent.hasProperties()
@hasNameClash = @name? <span class="hljs-keyword">and</span> @name <span class="hljs-keyword">is</span> parentName
node = @
<span class="hljs-keyword">if</span> executableBody <span class="hljs-keyword">or</span> @hasNameClash
node = <span class="hljs-keyword">new</span> ExecutableClassBody node, executableBody
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> @name? <span class="hljs-keyword">and</span> o.level <span class="hljs-keyword">is</span> LEVEL_TOP</pre></div></div>
</li>
<li id="section-199">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-199">§</a>
</div>
<p>Anonymous classes are only valid in expressions</p>
</div>
<div class="content"><div class='highlight'><pre> node = <span class="hljs-keyword">new</span> Parens node
<span class="hljs-keyword">if</span> @boundMethods.length <span class="hljs-keyword">and</span> @parent
@variable ?= <span class="hljs-keyword">new</span> IdentifierLiteral o.scope.freeVariable <span class="hljs-string">'_class'</span>
[@variable, @variableRef] = @variable.cache o <span class="hljs-keyword">unless</span> @variableRef?
<span class="hljs-keyword">if</span> @variable
node = <span class="hljs-keyword">new</span> Assign @variable, node, <span class="hljs-literal">null</span>, { @moduleDeclaration }
@compileNode = @compileClassDeclaration
<span class="hljs-keyword">try</span>
<span class="hljs-keyword">return</span> node.compileToFragments o
<span class="hljs-keyword">finally</span>
<span class="hljs-keyword">delete</span> @compileNode
compileClassDeclaration: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
@ctor ?= @makeDefaultConstructor() <span class="hljs-keyword">if</span> @externalCtor <span class="hljs-keyword">or</span> @boundMethods.length
@ctor?.noReturn = <span class="hljs-literal">true</span>
@proxyBoundMethods() <span class="hljs-keyword">if</span> @boundMethods.length
o.indent += TAB
result = []
result.push @makeCode <span class="hljs-string">"class "</span>
result.push @makeCode @name <span class="hljs-keyword">if</span> @name
@compileCommentFragments o, @variable, result <span class="hljs-keyword">if</span> @variable?.comments?
result.push @makeCode <span class="hljs-string">' '</span> <span class="hljs-keyword">if</span> @name
result.push @makeCode(<span class="hljs-string">'extends '</span>), @parent.compileToFragments(o)..., @makeCode <span class="hljs-string">' '</span> <span class="hljs-keyword">if</span> @parent
result.push @makeCode <span class="hljs-string">'{'</span>
<span class="hljs-keyword">unless</span> @body.isEmpty()
@body.spaced = <span class="hljs-literal">true</span>
result.push @makeCode <span class="hljs-string">'\n'</span>
result.push @body.compileToFragments(o, LEVEL_TOP)...
result.push @makeCode <span class="hljs-string">"\n<span class="hljs-subst">#{@tab}</span>"</span>
result.push @makeCode <span class="hljs-string">'}'</span>
result</pre></div></div>
</li>
<li id="section-200">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-200">§</a>
</div>
<p>Figure out the appropriate name for this class</p>
</div>
<div class="content"><div class='highlight'><pre> determineName: <span class="hljs-function">-></span>
<span class="hljs-keyword">return</span> <span class="hljs-literal">null</span> <span class="hljs-keyword">unless</span> @variable
[..., tail] = @variable.properties
node = <span class="hljs-keyword">if</span> tail
tail <span class="hljs-keyword">instanceof</span> Access <span class="hljs-keyword">and</span> tail.name
<span class="hljs-keyword">else</span>
@variable.base
<span class="hljs-keyword">unless</span> node <span class="hljs-keyword">instanceof</span> IdentifierLiteral <span class="hljs-keyword">or</span> node <span class="hljs-keyword">instanceof</span> PropertyName
<span class="hljs-keyword">return</span> <span class="hljs-literal">null</span>
name = node.value
<span class="hljs-keyword">unless</span> tail
message = isUnassignable name
@variable.error message <span class="hljs-keyword">if</span> message
<span class="hljs-keyword">if</span> name <span class="hljs-keyword">in</span> JS_FORBIDDEN <span class="hljs-keyword">then</span> <span class="hljs-string">"_<span class="hljs-subst">#{name}</span>"</span> <span class="hljs-keyword">else</span> name
walkBody: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
@ctor = <span class="hljs-literal">null</span>
@boundMethods = []
executableBody = <span class="hljs-literal">null</span>
initializer = []
{ expressions } = @body
i = <span class="hljs-number">0</span>
<span class="hljs-keyword">for</span> expression <span class="hljs-keyword">in</span> expressions.slice()
<span class="hljs-keyword">if</span> expression <span class="hljs-keyword">instanceof</span> Value <span class="hljs-keyword">and</span> expression.isObject <span class="hljs-literal">true</span>
{ properties } = expression.base
exprs = []
end = <span class="hljs-number">0</span>
start = <span class="hljs-number">0</span>
<span class="hljs-function"> <span class="hljs-title">pushSlice</span> = -></span> exprs.push <span class="hljs-keyword">new</span> Value <span class="hljs-keyword">new</span> Obj properties[start...end], <span class="hljs-literal">true</span> <span class="hljs-keyword">if</span> end > start
<span class="hljs-keyword">while</span> assign = properties[end]
<span class="hljs-keyword">if</span> initializerExpression = @addInitializerExpression assign, o
pushSlice()
exprs.push initializerExpression
initializer.push initializerExpression
start = end + <span class="hljs-number">1</span>
end++
pushSlice()
expressions[i..i] = exprs
i += exprs.length
<span class="hljs-keyword">else</span>
<span class="hljs-keyword">if</span> initializerExpression = @addInitializerExpression expression, o
initializer.push initializerExpression
expressions[i] = initializerExpression
i += <span class="hljs-number">1</span>
<span class="hljs-keyword">for</span> method <span class="hljs-keyword">in</span> initializer <span class="hljs-keyword">when</span> method <span class="hljs-keyword">instanceof</span> Code
<span class="hljs-keyword">if</span> method.ctor
method.error <span class="hljs-string">'Cannot define more than one constructor in a class'</span> <span class="hljs-keyword">if</span> @ctor
@ctor = method
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> method.isStatic <span class="hljs-keyword">and</span> method.bound
method.context = @name
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> method.bound
@boundMethods.push method
<span class="hljs-keyword">return</span> <span class="hljs-keyword">unless</span> o.compiling
<span class="hljs-keyword">if</span> initializer.length <span class="hljs-keyword">isnt</span> expressions.length
@body.expressions = (expression.hoist() <span class="hljs-keyword">for</span> expression <span class="hljs-keyword">in</span> initializer)
<span class="hljs-keyword">new</span> Block expressions</pre></div></div>
</li>
<li id="section-201">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-201">§</a>
</div>
<p>Add an expression to the class initializer</p>
<p>This is the key method for determining whether an expression in a class
body should appear in the initializer or the executable body. If the given
<code>node</code> is valid in a class body the method will return a (new, modified,
or identical) node for inclusion in the class initializer, otherwise
nothing will be returned and the node will appear in the executable body.</p>
<p>At time of writing, only methods (instance and static) are valid in ES
class initializers. As new ES class features (such as class fields) reach
Stage 4, this method will need to be updated to support them. We
additionally allow <code>PassthroughLiteral</code>s (backticked expressions) in the
initializer as an escape hatch for ES features that are not implemented
(e.g. getters and setters defined via the <code>get</code> and <code>set</code> keywords as
opposed to the <code>Object.defineProperty</code> method).</p>
</div>
<div class="content"><div class='highlight'><pre> addInitializerExpression: <span class="hljs-function"><span class="hljs-params">(node, o)</span> -></span>
<span class="hljs-keyword">if</span> node.unwrapAll() <span class="hljs-keyword">instanceof</span> PassthroughLiteral
node
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> @validInitializerMethod node
@addInitializerMethod node
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> o.compiling <span class="hljs-keyword">and</span> @validClassProperty node
@addClassProperty node
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> o.compiling <span class="hljs-keyword">and</span> @validClassPrototypeProperty node
@addClassPrototypeProperty node
<span class="hljs-keyword">else</span>
<span class="hljs-literal">null</span></pre></div></div>
</li>
<li id="section-202">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-202">§</a>
</div>
<p>Checks if the given node is a valid ES class initializer method.</p>
</div>
<div class="content"><div class='highlight'><pre> validInitializerMethod: <span class="hljs-function"><span class="hljs-params">(node)</span> -></span>
<span class="hljs-keyword">return</span> <span class="hljs-literal">no</span> <span class="hljs-keyword">unless</span> node <span class="hljs-keyword">instanceof</span> Assign <span class="hljs-keyword">and</span> node.value <span class="hljs-keyword">instanceof</span> Code
<span class="hljs-keyword">return</span> <span class="hljs-literal">yes</span> <span class="hljs-keyword">if</span> node.context <span class="hljs-keyword">is</span> <span class="hljs-string">'object'</span> <span class="hljs-keyword">and</span> <span class="hljs-keyword">not</span> node.variable.hasProperties()
<span class="hljs-keyword">return</span> node.variable.looksStatic(@name) <span class="hljs-keyword">and</span> (@name <span class="hljs-keyword">or</span> <span class="hljs-keyword">not</span> node.value.bound)</pre></div></div>
</li>
<li id="section-203">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-203">§</a>
</div>
<p>Returns a configured class initializer method</p>
</div>
<div class="content"><div class='highlight'><pre> addInitializerMethod: <span class="hljs-function"><span class="hljs-params">(assign)</span> -></span>
{ variable, value: method, operatorToken } = assign
method.isMethod = <span class="hljs-literal">yes</span>
method.isStatic = variable.looksStatic @name
<span class="hljs-keyword">if</span> method.isStatic
method.name = variable.properties[<span class="hljs-number">0</span>]
<span class="hljs-keyword">else</span>
methodName = variable.base
method.name = <span class="hljs-keyword">new</span> (<span class="hljs-keyword">if</span> methodName.shouldCache() <span class="hljs-keyword">then</span> Index <span class="hljs-keyword">else</span> Access) methodName
method.name.updateLocationDataIfMissing methodName.locationData
isConstructor =
<span class="hljs-keyword">if</span> methodName <span class="hljs-keyword">instanceof</span> StringLiteral
methodName.originalValue <span class="hljs-keyword">is</span> <span class="hljs-string">'constructor'</span>
<span class="hljs-keyword">else</span>
methodName.value <span class="hljs-keyword">is</span> <span class="hljs-string">'constructor'</span>
method.ctor = (<span class="hljs-keyword">if</span> @parent <span class="hljs-keyword">then</span> <span class="hljs-string">'derived'</span> <span class="hljs-keyword">else</span> <span class="hljs-string">'base'</span>) <span class="hljs-keyword">if</span> isConstructor
method.error <span class="hljs-string">'Cannot define a constructor as a bound (fat arrow) function'</span> <span class="hljs-keyword">if</span> method.bound <span class="hljs-keyword">and</span> method.ctor
method.operatorToken = operatorToken
method
validClassProperty: <span class="hljs-function"><span class="hljs-params">(node)</span> -></span>
<span class="hljs-keyword">return</span> <span class="hljs-literal">no</span> <span class="hljs-keyword">unless</span> node <span class="hljs-keyword">instanceof</span> Assign
<span class="hljs-keyword">return</span> node.variable.looksStatic @name
addClassProperty: <span class="hljs-function"><span class="hljs-params">(assign)</span> -></span>
{variable, value, operatorToken} = assign
{staticClassName} = variable.looksStatic @name
<span class="hljs-keyword">new</span> ClassProperty({
name: variable.properties[<span class="hljs-number">0</span>]
isStatic: <span class="hljs-literal">yes</span>
staticClassName
value
operatorToken
}).withLocationDataFrom assign
validClassPrototypeProperty: <span class="hljs-function"><span class="hljs-params">(node)</span> -></span>
<span class="hljs-keyword">return</span> <span class="hljs-literal">no</span> <span class="hljs-keyword">unless</span> node <span class="hljs-keyword">instanceof</span> Assign
node.context <span class="hljs-keyword">is</span> <span class="hljs-string">'object'</span> <span class="hljs-keyword">and</span> <span class="hljs-keyword">not</span> node.variable.hasProperties()
addClassPrototypeProperty: <span class="hljs-function"><span class="hljs-params">(assign)</span> -></span>
{variable, value} = assign
<span class="hljs-keyword">new</span> ClassPrototypeProperty({
name: variable.base
value
}).withLocationDataFrom assign
makeDefaultConstructor: <span class="hljs-function">-></span>
ctor = @addInitializerMethod <span class="hljs-keyword">new</span> Assign (<span class="hljs-keyword">new</span> Value <span class="hljs-keyword">new</span> PropertyName <span class="hljs-string">'constructor'</span>), <span class="hljs-keyword">new</span> Code
@body.unshift ctor
<span class="hljs-keyword">if</span> @parent
ctor.body.push <span class="hljs-keyword">new</span> SuperCall <span class="hljs-keyword">new</span> Super, [<span class="hljs-keyword">new</span> Splat <span class="hljs-keyword">new</span> IdentifierLiteral <span class="hljs-string">'arguments'</span>]
<span class="hljs-keyword">if</span> @externalCtor
applyCtor = <span class="hljs-keyword">new</span> Value @externalCtor, [ <span class="hljs-keyword">new</span> Access <span class="hljs-keyword">new</span> PropertyName <span class="hljs-string">'apply'</span> ]
applyArgs = [ <span class="hljs-keyword">new</span> ThisLiteral, <span class="hljs-keyword">new</span> IdentifierLiteral <span class="hljs-string">'arguments'</span> ]
ctor.body.push <span class="hljs-keyword">new</span> Call applyCtor, applyArgs
ctor.body.makeReturn()
ctor
proxyBoundMethods: <span class="hljs-function">-></span>
@ctor.thisAssignments = <span class="hljs-keyword">for</span> method <span class="hljs-keyword">in</span> @boundMethods
method.classVariable = @variableRef <span class="hljs-keyword">if</span> @parent
name = <span class="hljs-keyword">new</span> Value(<span class="hljs-keyword">new</span> ThisLiteral, [ method.name ])
<span class="hljs-keyword">new</span> Assign name, <span class="hljs-keyword">new</span> Call(<span class="hljs-keyword">new</span> Value(name, [<span class="hljs-keyword">new</span> Access <span class="hljs-keyword">new</span> PropertyName <span class="hljs-string">'bind'</span>]), [<span class="hljs-keyword">new</span> ThisLiteral])
<span class="hljs-literal">null</span>
declareName: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
<span class="hljs-keyword">return</span> <span class="hljs-keyword">unless</span> (name = @variable?.unwrap()) <span class="hljs-keyword">instanceof</span> IdentifierLiteral
alreadyDeclared = o.scope.find name.value
name.isDeclaration = <span class="hljs-keyword">not</span> alreadyDeclared
isStatementAst: <span class="hljs-function">-></span> <span class="hljs-literal">yes</span>
astNode: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
<span class="hljs-keyword">if</span> jumpNode = @body.jumps()
jumpNode.error <span class="hljs-string">'Class bodies cannot contain pure statements'</span>
<span class="hljs-keyword">if</span> argumentsNode = @body.contains isLiteralArguments
argumentsNode.error <span class="hljs-string">"Class bodies shouldn't reference arguments"</span>
@declareName o
@name = @determineName()
@body.isClassBody = <span class="hljs-literal">yes</span>
@body.locationData = zeroWidthLocationDataFromEndLocation @locationData <span class="hljs-keyword">if</span> @hasGeneratedBody
@walkBody o
sniffDirectives @body.expressions
@ctor?.noReturn = <span class="hljs-literal">yes</span>
super o
astType: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
<span class="hljs-keyword">if</span> o.level <span class="hljs-keyword">is</span> LEVEL_TOP
<span class="hljs-string">'ClassDeclaration'</span>
<span class="hljs-keyword">else</span>
<span class="hljs-string">'ClassExpression'</span>
astProperties: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
<span class="hljs-keyword">return</span>
id: @variable?.ast(o) ? <span class="hljs-literal">null</span>
superClass: @parent?.ast(o, LEVEL_PAREN) ? <span class="hljs-literal">null</span>
body: @body.ast o, LEVEL_TOP
<span class="hljs-built_in">exports</span>.ExecutableClassBody = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ExecutableClassBody</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Base</span></span>
children: [ <span class="hljs-string">'class'</span>, <span class="hljs-string">'body'</span> ]
defaultClassVariableName: <span class="hljs-string">'_Class'</span>
constructor: <span class="hljs-function"><span class="hljs-params">(@class, @body = <span class="hljs-keyword">new</span> Block)</span> -></span>
super()
compileNode: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
<span class="hljs-keyword">if</span> jumpNode = @body.jumps()
jumpNode.error <span class="hljs-string">'Class bodies cannot contain pure statements'</span>
<span class="hljs-keyword">if</span> argumentsNode = @body.contains isLiteralArguments
argumentsNode.error <span class="hljs-string">"Class bodies shouldn't reference arguments"</span>
params = []
args = [<span class="hljs-keyword">new</span> ThisLiteral]
wrapper = <span class="hljs-keyword">new</span> Code params, @body
klass = <span class="hljs-keyword">new</span> Parens <span class="hljs-keyword">new</span> Call (<span class="hljs-keyword">new</span> Value wrapper, [<span class="hljs-keyword">new</span> Access <span class="hljs-keyword">new</span> PropertyName <span class="hljs-string">'call'</span>]), args
@body.spaced = <span class="hljs-literal">true</span>
o.classScope = wrapper.makeScope o.scope
@name = @class.name ? o.classScope.freeVariable @defaultClassVariableName
ident = <span class="hljs-keyword">new</span> IdentifierLiteral @name
directives = @walkBody()
@setContext()
<span class="hljs-keyword">if</span> @class.hasNameClash
parent = <span class="hljs-keyword">new</span> IdentifierLiteral o.classScope.freeVariable <span class="hljs-string">'superClass'</span>
wrapper.params.push <span class="hljs-keyword">new</span> Param parent
args.push @class.parent
@class.parent = parent
<span class="hljs-keyword">if</span> @externalCtor
externalCtor = <span class="hljs-keyword">new</span> IdentifierLiteral o.classScope.freeVariable <span class="hljs-string">'ctor'</span>, reserve: <span class="hljs-literal">no</span>
@class.externalCtor = externalCtor
@externalCtor.variable.base = externalCtor
<span class="hljs-keyword">if</span> @name <span class="hljs-keyword">isnt</span> @class.name
@body.expressions.unshift <span class="hljs-keyword">new</span> Assign (<span class="hljs-keyword">new</span> IdentifierLiteral @name), @class
<span class="hljs-keyword">else</span>
@body.expressions.unshift @class
@body.expressions.unshift directives...
@body.push ident
klass.compileToFragments o</pre></div></div>
</li>
<li id="section-204">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-204">§</a>
</div>
<p>Traverse the class’s children and:</p>
<ul>
<li>Hoist valid ES properties into <code>@properties</code></li>
<li>Hoist static assignments into <code>@properties</code></li>
<li>Convert invalid ES properties into class or prototype assignments</li>
</ul>
</div>
<div class="content"><div class='highlight'><pre> walkBody: <span class="hljs-function">-></span>
directives = []
index = <span class="hljs-number">0</span>
<span class="hljs-keyword">while</span> expr = @body.expressions[index]
<span class="hljs-keyword">break</span> <span class="hljs-keyword">unless</span> expr <span class="hljs-keyword">instanceof</span> Value <span class="hljs-keyword">and</span> expr.isString()
<span class="hljs-keyword">if</span> expr.hoisted
index++
<span class="hljs-keyword">else</span>
directives.push @body.expressions.splice(index, <span class="hljs-number">1</span>)...
@traverseChildren <span class="hljs-literal">false</span>, <span class="hljs-function"><span class="hljs-params">(child)</span> =></span>
<span class="hljs-keyword">return</span> <span class="hljs-literal">false</span> <span class="hljs-keyword">if</span> child <span class="hljs-keyword">instanceof</span> Class <span class="hljs-keyword">or</span> child <span class="hljs-keyword">instanceof</span> HoistTarget
cont = <span class="hljs-literal">true</span>
<span class="hljs-keyword">if</span> child <span class="hljs-keyword">instanceof</span> Block
<span class="hljs-keyword">for</span> node, i <span class="hljs-keyword">in</span> child.expressions
<span class="hljs-keyword">if</span> node <span class="hljs-keyword">instanceof</span> Value <span class="hljs-keyword">and</span> node.isObject(<span class="hljs-literal">true</span>)
cont = <span class="hljs-literal">false</span>
child.expressions[i] = @addProperties node.base.properties
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> node <span class="hljs-keyword">instanceof</span> Assign <span class="hljs-keyword">and</span> node.variable.looksStatic @name
node.value.isStatic = <span class="hljs-literal">yes</span>
child.expressions = flatten child.expressions
cont
directives
setContext: <span class="hljs-function">-></span>
@body.traverseChildren <span class="hljs-literal">false</span>, <span class="hljs-function"><span class="hljs-params">(node)</span> =></span>
<span class="hljs-keyword">if</span> node <span class="hljs-keyword">instanceof</span> ThisLiteral
node.value = @name
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> node <span class="hljs-keyword">instanceof</span> Code <span class="hljs-keyword">and</span> node.bound <span class="hljs-keyword">and</span> (node.isStatic <span class="hljs-keyword">or</span> <span class="hljs-keyword">not</span> node.name)
node.context = @name</pre></div></div>
</li>
<li id="section-205">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-205">§</a>
</div>
<p>Make class/prototype assignments for invalid ES properties</p>
</div>
<div class="content"><div class='highlight'><pre> addProperties: <span class="hljs-function"><span class="hljs-params">(assigns)</span> -></span>
result = <span class="hljs-keyword">for</span> assign <span class="hljs-keyword">in</span> assigns
variable = assign.variable
base = variable?.base
value = assign.value
<span class="hljs-keyword">delete</span> assign.context
<span class="hljs-keyword">if</span> base.value <span class="hljs-keyword">is</span> <span class="hljs-string">'constructor'</span>
<span class="hljs-keyword">if</span> value <span class="hljs-keyword">instanceof</span> Code
base.error <span class="hljs-string">'constructors must be defined at the top level of a class body'</span></pre></div></div>
</li>
<li id="section-206">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-206">§</a>
</div>
<p>The class scope is not available yet, so return the assignment to update later</p>
</div>
<div class="content"><div class='highlight'><pre> assign = @externalCtor = <span class="hljs-keyword">new</span> Assign <span class="hljs-keyword">new</span> Value, value
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> assign.variable.this
name =
<span class="hljs-keyword">if</span> base <span class="hljs-keyword">instanceof</span> ComputedPropertyName
<span class="hljs-keyword">new</span> Index base.value
<span class="hljs-keyword">else</span>
<span class="hljs-keyword">new</span> (<span class="hljs-keyword">if</span> base.shouldCache() <span class="hljs-keyword">then</span> Index <span class="hljs-keyword">else</span> Access) base
prototype = <span class="hljs-keyword">new</span> Access <span class="hljs-keyword">new</span> PropertyName <span class="hljs-string">'prototype'</span>
variable = <span class="hljs-keyword">new</span> Value <span class="hljs-keyword">new</span> ThisLiteral(), [ prototype, name ]
assign.variable = variable
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> assign.value <span class="hljs-keyword">instanceof</span> Code
assign.value.isStatic = <span class="hljs-literal">true</span>
assign
compact result
<span class="hljs-built_in">exports</span>.ClassProperty = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ClassProperty</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Base</span></span>
constructor: <span class="hljs-function"><span class="hljs-params">({@name, @isStatic, @staticClassName, @value, @operatorToken})</span> -></span>
super()
children: [<span class="hljs-string">'name'</span>, <span class="hljs-string">'value'</span>, <span class="hljs-string">'staticClassName'</span>]
isStatement: YES
astProperties: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
<span class="hljs-keyword">return</span>
key: @name.ast o, LEVEL_LIST
value: @value.ast o, LEVEL_LIST
static: !!@isStatic
computed: @name <span class="hljs-keyword">instanceof</span> Index <span class="hljs-keyword">or</span> @name <span class="hljs-keyword">instanceof</span> ComputedPropertyName
operator: @operatorToken?.value ? <span class="hljs-string">'='</span>
staticClassName: @staticClassName?.ast(o) ? <span class="hljs-literal">null</span>
<span class="hljs-built_in">exports</span>.ClassPrototypeProperty = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ClassPrototypeProperty</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Base</span></span>
constructor: <span class="hljs-function"><span class="hljs-params">({@name, @value})</span> -></span>
super()
children: [<span class="hljs-string">'name'</span>, <span class="hljs-string">'value'</span>]
isStatement: YES
astProperties: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
<span class="hljs-keyword">return</span>
key: @name.ast o, LEVEL_LIST
value: @value.ast o, LEVEL_LIST
computed: @name <span class="hljs-keyword">instanceof</span> ComputedPropertyName <span class="hljs-keyword">or</span> @name <span class="hljs-keyword">instanceof</span> StringWithInterpolations</pre></div></div>
</li>
<li id="section-207">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-207">§</a>
</div>
<h3 id="import-and-export">Import and Export</h3>
</div>
<div class="content"><div class='highlight'><pre>
<span class="hljs-built_in">exports</span>.ModuleDeclaration = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ModuleDeclaration</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Base</span></span>
constructor: <span class="hljs-function"><span class="hljs-params">(@clause, @source, @assertions)</span> -></span>
super()
@checkSource()
children: [<span class="hljs-string">'clause'</span>, <span class="hljs-string">'source'</span>, <span class="hljs-string">'assertions'</span>]
isStatement: YES
jumps: THIS
makeReturn: THIS
checkSource: <span class="hljs-function">-></span>
<span class="hljs-keyword">if</span> @source? <span class="hljs-keyword">and</span> @source <span class="hljs-keyword">instanceof</span> StringWithInterpolations
@source.error <span class="hljs-string">'the name of the module to be imported from must be an uninterpolated string'</span>
checkScope: <span class="hljs-function"><span class="hljs-params">(o, moduleDeclarationType)</span> -></span></pre></div></div>
</li>
<li id="section-208">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-208">§</a>
</div>
<p>TODO: would be appropriate to flag this error during AST generation (as
well as when compiling to JS). But <code>o.indent</code> isn’t tracked during AST
generation, and there doesn’t seem to be a current alternative way to track
whether we’re at the “program top-level”.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> o.indent.length <span class="hljs-keyword">isnt</span> <span class="hljs-number">0</span>
@error <span class="hljs-string">"<span class="hljs-subst">#{moduleDeclarationType}</span> statements must be at top-level scope"</span>
astAssertions: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
<span class="hljs-keyword">if</span> @assertions?.properties?
@assertions.properties.map (assertion) =>
{ start, end, loc, left, right } = assertion.ast(o)
{ type: <span class="hljs-string">'ImportAttribute'</span>, start, end, loc, key: left, value: right }
<span class="hljs-keyword">else</span>
[]
<span class="hljs-built_in">exports</span>.ImportDeclaration = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ImportDeclaration</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">ModuleDeclaration</span></span>
compileNode: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
@checkScope o, <span class="hljs-string">'import'</span>
o.importedSymbols = []
code = []
code.push @makeCode <span class="hljs-string">"<span class="hljs-subst">#{@tab}</span>import "</span>
code.push @clause.compileNode(o)... <span class="hljs-keyword">if</span> @clause?
<span class="hljs-keyword">if</span> @source?.value?
code.push @makeCode <span class="hljs-string">' from '</span> <span class="hljs-keyword">unless</span> @clause <span class="hljs-keyword">is</span> <span class="hljs-literal">null</span>
code.push @makeCode @source.value
<span class="hljs-keyword">if</span> @assertions?
code.push @makeCode <span class="hljs-string">' assert '</span>
code.push @assertions.compileToFragments(o)...
code.push @makeCode <span class="hljs-string">';'</span>
code
astNode: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
o.importedSymbols = []
super o
astProperties: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
ret =
specifiers: @clause?.ast(o) ? []
source: @source.ast o
assertions: @astAssertions(o)
ret.importKind = <span class="hljs-string">'value'</span> <span class="hljs-keyword">if</span> @clause
ret
<span class="hljs-built_in">exports</span>.ImportClause = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ImportClause</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Base</span></span>
constructor: <span class="hljs-function"><span class="hljs-params">(@defaultBinding, @namedImports)</span> -></span>
super()
children: [<span class="hljs-string">'defaultBinding'</span>, <span class="hljs-string">'namedImports'</span>]
compileNode: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
code = []
<span class="hljs-keyword">if</span> @defaultBinding?
code.push @defaultBinding.compileNode(o)...
code.push @makeCode <span class="hljs-string">', '</span> <span class="hljs-keyword">if</span> @namedImports?
<span class="hljs-keyword">if</span> @namedImports?
code.push @namedImports.compileNode(o)...
code
astNode: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span></pre></div></div>
</li>
<li id="section-209">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-209">§</a>
</div>
<p>The AST for <code>ImportClause</code> is the non-nested list of import specifiers
that will be the <code>specifiers</code> property of an <code>ImportDeclaration</code> AST</p>
</div>
<div class="content"><div class='highlight'><pre> compact flatten [
@defaultBinding?.ast o
@namedImports?.ast o
]
<span class="hljs-built_in">exports</span>.ExportDeclaration = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ExportDeclaration</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">ModuleDeclaration</span></span>
compileNode: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
@checkScope o, <span class="hljs-string">'export'</span>
@checkForAnonymousClassExport()
code = []
code.push @makeCode <span class="hljs-string">"<span class="hljs-subst">#{@tab}</span>export "</span>
code.push @makeCode <span class="hljs-string">'default '</span> <span class="hljs-keyword">if</span> @ <span class="hljs-keyword">instanceof</span> ExportDefaultDeclaration
<span class="hljs-keyword">if</span> @ <span class="hljs-keyword">not</span> <span class="hljs-keyword">instanceof</span> ExportDefaultDeclaration <span class="hljs-keyword">and</span>
(@clause <span class="hljs-keyword">instanceof</span> Assign <span class="hljs-keyword">or</span> @clause <span class="hljs-keyword">instanceof</span> Class)
code.push @makeCode <span class="hljs-string">'var '</span>
@clause.moduleDeclaration = <span class="hljs-string">'export'</span>
<span class="hljs-keyword">if</span> @clause.body? <span class="hljs-keyword">and</span> @clause.body <span class="hljs-keyword">instanceof</span> Block
code = code.concat @clause.compileToFragments o, LEVEL_TOP
<span class="hljs-keyword">else</span>
code = code.concat @clause.compileNode o
<span class="hljs-keyword">if</span> @source?.value?
code.push @makeCode <span class="hljs-string">" from <span class="hljs-subst">#{@source.value}</span>"</span>
<span class="hljs-keyword">if</span> @assertions?
code.push @makeCode <span class="hljs-string">' assert '</span>
code.push @assertions.compileToFragments(o)...
code.push @makeCode <span class="hljs-string">';'</span>
code</pre></div></div>
</li>
<li id="section-210">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-210">§</a>
</div>
<p>Prevent exporting an anonymous class; all exported members must be named</p>
</div>
<div class="content"><div class='highlight'><pre> checkForAnonymousClassExport: <span class="hljs-function">-></span>
<span class="hljs-keyword">if</span> @ <span class="hljs-keyword">not</span> <span class="hljs-keyword">instanceof</span> ExportDefaultDeclaration <span class="hljs-keyword">and</span> @clause <span class="hljs-keyword">instanceof</span> Class <span class="hljs-keyword">and</span> <span class="hljs-keyword">not</span> @clause.variable
@clause.error <span class="hljs-string">'anonymous classes cannot be exported'</span>
astNode: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
@checkForAnonymousClassExport()
super o
<span class="hljs-built_in">exports</span>.ExportNamedDeclaration = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ExportNamedDeclaration</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">ExportDeclaration</span></span>
astProperties: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
ret =
source: @source?.ast(o) ? <span class="hljs-literal">null</span>
assertions: @astAssertions(o)
exportKind: <span class="hljs-string">'value'</span>
clauseAst = @clause.ast o
<span class="hljs-keyword">if</span> @clause <span class="hljs-keyword">instanceof</span> ExportSpecifierList
ret.specifiers = clauseAst
ret.declaration = <span class="hljs-literal">null</span>
<span class="hljs-keyword">else</span>
ret.specifiers = []
ret.declaration = clauseAst
ret
<span class="hljs-built_in">exports</span>.ExportDefaultDeclaration = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ExportDefaultDeclaration</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">ExportDeclaration</span></span>
astProperties: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
<span class="hljs-keyword">return</span>
declaration: @clause.ast o
assertions: @astAssertions(o)
<span class="hljs-built_in">exports</span>.ExportAllDeclaration = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ExportAllDeclaration</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">ExportDeclaration</span></span>
astProperties: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
<span class="hljs-keyword">return</span>
source: @source.ast o
assertions: @astAssertions(o)
exportKind: <span class="hljs-string">'value'</span>
<span class="hljs-built_in">exports</span>.ModuleSpecifierList = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ModuleSpecifierList</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Base</span></span>
constructor: <span class="hljs-function"><span class="hljs-params">(@specifiers)</span> -></span>
super()
children: [<span class="hljs-string">'specifiers'</span>]
compileNode: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
code = []
o.indent += TAB
compiledList = (specifier.compileToFragments o, LEVEL_LIST <span class="hljs-keyword">for</span> specifier <span class="hljs-keyword">in</span> @specifiers)
<span class="hljs-keyword">if</span> @specifiers.length <span class="hljs-keyword">isnt</span> <span class="hljs-number">0</span>
code.push @makeCode <span class="hljs-string">"{\n<span class="hljs-subst">#{o.indent}</span>"</span>
<span class="hljs-keyword">for</span> fragments, index <span class="hljs-keyword">in</span> compiledList
code.push @makeCode(<span class="hljs-string">",\n<span class="hljs-subst">#{o.indent}</span>"</span>) <span class="hljs-keyword">if</span> index
code.push fragments...
code.push @makeCode <span class="hljs-string">"\n}"</span>
<span class="hljs-keyword">else</span>
code.push @makeCode <span class="hljs-string">'{}'</span>
code
astNode: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
specifier.ast(o) <span class="hljs-keyword">for</span> specifier <span class="hljs-keyword">in</span> @specifiers
<span class="hljs-built_in">exports</span>.ImportSpecifierList = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ImportSpecifierList</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">ModuleSpecifierList</span></span>
<span class="hljs-built_in">exports</span>.ExportSpecifierList = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ExportSpecifierList</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">ModuleSpecifierList</span></span>
<span class="hljs-built_in">exports</span>.ModuleSpecifier = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ModuleSpecifier</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Base</span></span>
constructor: <span class="hljs-function"><span class="hljs-params">(@original, @alias, @moduleDeclarationType)</span> -></span>
super()
<span class="hljs-keyword">if</span> @original.comments <span class="hljs-keyword">or</span> @alias?.comments
@comments = []
@comments.push @original.comments... <span class="hljs-keyword">if</span> @original.comments
@comments.push @alias.comments... <span class="hljs-keyword">if</span> @alias?.comments</pre></div></div>
</li>
<li id="section-211">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-211">§</a>
</div>
<p>The name of the variable entering the local scope</p>
</div>
<div class="content"><div class='highlight'><pre> @identifier = <span class="hljs-keyword">if</span> @alias? <span class="hljs-keyword">then</span> @alias.value <span class="hljs-keyword">else</span> @original.value
children: [<span class="hljs-string">'original'</span>, <span class="hljs-string">'alias'</span>]
compileNode: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
@addIdentifierToScope o
code = []
code.push @makeCode @original.value
code.push @makeCode <span class="hljs-string">" as <span class="hljs-subst">#{@alias.value}</span>"</span> <span class="hljs-keyword">if</span> @alias?
code
addIdentifierToScope: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
o.scope.find @identifier, @moduleDeclarationType
astNode: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
@addIdentifierToScope o
super o
<span class="hljs-built_in">exports</span>.ImportSpecifier = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ImportSpecifier</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">ModuleSpecifier</span></span>
constructor: <span class="hljs-function"><span class="hljs-params">(imported, local)</span> -></span>
super imported, local, <span class="hljs-string">'import'</span>
addIdentifierToScope: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span></pre></div></div>
</li>
<li id="section-212">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-212">§</a>
</div>
<p>Per the spec, symbols can’t be imported multiple times
(e.g. <code>import { foo, foo } from 'lib'</code> is invalid)</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> @identifier <span class="hljs-keyword">in</span> o.importedSymbols <span class="hljs-keyword">or</span> o.scope.check(@identifier)
@error <span class="hljs-string">"'<span class="hljs-subst">#{@identifier}</span>' has already been declared"</span>
<span class="hljs-keyword">else</span>
o.importedSymbols.push @identifier
super o
astProperties: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
originalAst = @original.ast o
<span class="hljs-keyword">return</span>
imported: originalAst
local: @alias?.ast(o) ? originalAst
importKind: <span class="hljs-literal">null</span>
<span class="hljs-built_in">exports</span>.ImportDefaultSpecifier = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ImportDefaultSpecifier</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">ImportSpecifier</span></span>
astProperties: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
<span class="hljs-keyword">return</span>
local: @original.ast o
<span class="hljs-built_in">exports</span>.ImportNamespaceSpecifier = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ImportNamespaceSpecifier</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">ImportSpecifier</span></span>
astProperties: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
<span class="hljs-keyword">return</span>
local: @alias.ast o
<span class="hljs-built_in">exports</span>.ExportSpecifier = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ExportSpecifier</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">ModuleSpecifier</span></span>
constructor: <span class="hljs-function"><span class="hljs-params">(local, exported)</span> -></span>
super local, exported, <span class="hljs-string">'export'</span>
astProperties: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
originalAst = @original.ast o
<span class="hljs-keyword">return</span>
local: originalAst
exported: @alias?.ast(o) ? originalAst
<span class="hljs-built_in">exports</span>.DynamicImport = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">DynamicImport</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Base</span></span>
compileNode: <span class="hljs-function">-></span>
[@makeCode <span class="hljs-string">'import'</span>]
astType: <span class="hljs-function">-></span> <span class="hljs-string">'Import'</span>
<span class="hljs-built_in">exports</span>.DynamicImportCall = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">DynamicImportCall</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Call</span></span>
compileNode: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
@checkArguments()
super o
checkArguments: <span class="hljs-function">-></span>
<span class="hljs-keyword">unless</span> <span class="hljs-number">1</span> <= @args.length <= <span class="hljs-number">2</span>
@error <span class="hljs-string">'import() accepts either one or two arguments'</span>
astNode: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
@checkArguments()
super o</pre></div></div>
</li>
<li id="section-213">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-213">§</a>
</div>
<h3 id="assign">Assign</h3>
</div>
</li>
<li id="section-214">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-214">§</a>
</div>
<p>The <strong>Assign</strong> is used to assign a local variable to value, or to set the
property of an object – including within object literals.</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-built_in">exports</span>.Assign = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Assign</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Base</span></span>
constructor: <span class="hljs-function"><span class="hljs-params">(@variable, @value, @context, options = {})</span> -></span>
super()
{@param, @subpattern, @operatorToken, @moduleDeclaration, @originalContext = @context} = options
@propagateLhs()
children: [<span class="hljs-string">'variable'</span>, <span class="hljs-string">'value'</span>]
isAssignable: YES
isStatement: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
o?.level <span class="hljs-keyword">is</span> LEVEL_TOP <span class="hljs-keyword">and</span> @context? <span class="hljs-keyword">and</span> (@moduleDeclaration <span class="hljs-keyword">or</span> <span class="hljs-string">"?"</span> <span class="hljs-keyword">in</span> @context)
checkNameAssignability: <span class="hljs-function"><span class="hljs-params">(o, varBase)</span> -></span>
<span class="hljs-keyword">if</span> o.scope.type(varBase.value) <span class="hljs-keyword">is</span> <span class="hljs-string">'import'</span>
varBase.error <span class="hljs-string">"'<span class="hljs-subst">#{varBase.value}</span>' is read-only"</span>
assigns: <span class="hljs-function"><span class="hljs-params">(name)</span> -></span>
@[<span class="hljs-keyword">if</span> @context <span class="hljs-keyword">is</span> <span class="hljs-string">'object'</span> <span class="hljs-keyword">then</span> <span class="hljs-string">'value'</span> <span class="hljs-keyword">else</span> <span class="hljs-string">'variable'</span>].assigns name
unfoldSoak: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
unfoldSoak o, this, <span class="hljs-string">'variable'</span>
addScopeVariables: (o, {</pre></div></div>
</li>
<li id="section-215">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-215">§</a>
</div>
<p>During AST generation, we need to allow assignment to these constructs
that are considered “unassignable” during compile-to-JS, while still
flagging things like <code>[null] = b</code>.</p>
</div>
<div class="content"><div class='highlight'><pre> allowAssignmentToExpansion = <span class="hljs-literal">no</span>,
allowAssignmentToNontrailingSplat = <span class="hljs-literal">no</span>,
allowAssignmentToEmptyArray = <span class="hljs-literal">no</span>,
allowAssignmentToComplexSplat = <span class="hljs-literal">no</span>
} = {}) ->
<span class="hljs-keyword">return</span> <span class="hljs-keyword">unless</span> <span class="hljs-keyword">not</span> @context <span class="hljs-keyword">or</span> @context <span class="hljs-keyword">is</span> <span class="hljs-string">'**='</span>
varBase = @variable.unwrapAll()
<span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> varBase.isAssignable {
allowExpansion: allowAssignmentToExpansion
allowNontrailingSplat: allowAssignmentToNontrailingSplat
allowEmptyArray: allowAssignmentToEmptyArray
allowComplexSplat: allowAssignmentToComplexSplat
}
@variable.error <span class="hljs-string">"'<span class="hljs-subst">#{@variable.compile o}</span>' can't be assigned"</span>
varBase.eachName (name) =>
<span class="hljs-keyword">return</span> <span class="hljs-keyword">if</span> name.hasProperties?()
message = isUnassignable name.value
name.error message <span class="hljs-keyword">if</span> message</pre></div></div>
</li>
<li id="section-216">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-216">§</a>
</div>
<p><code>moduleDeclaration</code> can be <code>'import'</code> or <code>'export'</code>.</p>
</div>
<div class="content"><div class='highlight'><pre> @checkNameAssignability o, name
<span class="hljs-keyword">if</span> @moduleDeclaration
o.scope.add name.value, @moduleDeclaration
name.isDeclaration = <span class="hljs-literal">yes</span>
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> @param
o.scope.add name.value,
<span class="hljs-keyword">if</span> @param <span class="hljs-keyword">is</span> <span class="hljs-string">'alwaysDeclare'</span>
<span class="hljs-string">'var'</span>
<span class="hljs-keyword">else</span>
<span class="hljs-string">'param'</span>
<span class="hljs-keyword">else</span>
alreadyDeclared = o.scope.find name.value
name.isDeclaration ?= <span class="hljs-keyword">not</span> alreadyDeclared</pre></div></div>
</li>
<li id="section-217">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-217">§</a>
</div>
<p>If this assignment identifier has one or more herecomments
attached, output them as part of the declarations line (unless
other herecomments are already staged there) for compatibility
with Flow typing. Don’t do this if this assignment is for a
class, e.g. <code>ClassName = class ClassName {</code>, as Flow requires
the comment to be between the class name and the <code>{</code>.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> name.comments <span class="hljs-keyword">and</span> <span class="hljs-keyword">not</span> o.scope.comments[name.value] <span class="hljs-keyword">and</span>
@value <span class="hljs-keyword">not</span> <span class="hljs-keyword">instanceof</span> Class <span class="hljs-keyword">and</span>
name.comments.every(<span class="hljs-function"><span class="hljs-params">(comment)</span> -></span> comment.here <span class="hljs-keyword">and</span> <span class="hljs-keyword">not</span> comment.multiline)
commentsNode = <span class="hljs-keyword">new</span> IdentifierLiteral name.value
commentsNode.comments = name.comments
commentFragments = []
@compileCommentFragments o, commentsNode, commentFragments
o.scope.comments[name.value] = commentFragments</pre></div></div>
</li>
<li id="section-218">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-218">§</a>
</div>
<p>Compile an assignment, delegating to <code>compileDestructuring</code> or
<code>compileSplice</code> if appropriate. Keep track of the name of the base object
we’ve been assigned to, for correct internal references. If the variable
has not been seen yet within the current scope, declare it.</p>
</div>
<div class="content"><div class='highlight'><pre> compileNode: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
isValue = @variable <span class="hljs-keyword">instanceof</span> Value
<span class="hljs-keyword">if</span> isValue</pre></div></div>
</li>
<li id="section-219">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-219">§</a>
</div>
<p>If <code>@variable</code> is an array or an object, we’re destructuring;
if it’s also <code>isAssignable()</code>, the destructuring syntax is supported
in ES and we can output it as is; otherwise we <code>@compileDestructuring</code>
and convert this ES-unsupported destructuring into acceptable output.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> @variable.isArray() <span class="hljs-keyword">or</span> @variable.isObject()
<span class="hljs-keyword">unless</span> @variable.isAssignable()
<span class="hljs-keyword">if</span> @variable.isObject() <span class="hljs-keyword">and</span> @variable.base.hasSplat()
<span class="hljs-keyword">return</span> @compileObjectDestruct o
<span class="hljs-keyword">else</span>
<span class="hljs-keyword">return</span> @compileDestructuring o
<span class="hljs-keyword">return</span> @compileSplice o <span class="hljs-keyword">if</span> @variable.isSplice()
<span class="hljs-keyword">return</span> @compileConditional o <span class="hljs-keyword">if</span> @isConditional()
<span class="hljs-keyword">return</span> @compileSpecialMath o <span class="hljs-keyword">if</span> @context <span class="hljs-keyword">in</span> [<span class="hljs-string">'//='</span>, <span class="hljs-string">'%%='</span>]
@addScopeVariables o
<span class="hljs-keyword">if</span> @value <span class="hljs-keyword">instanceof</span> Code
<span class="hljs-keyword">if</span> @value.isStatic
@value.name = @variable.properties[<span class="hljs-number">0</span>]
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> @variable.properties?.length >= <span class="hljs-number">2</span>
[properties..., prototype, name] = @variable.properties
@value.name = name <span class="hljs-keyword">if</span> prototype.name?.value <span class="hljs-keyword">is</span> <span class="hljs-string">'prototype'</span>
val = @value.compileToFragments o, LEVEL_LIST
compiledName = @variable.compileToFragments o, LEVEL_LIST
<span class="hljs-keyword">if</span> @context <span class="hljs-keyword">is</span> <span class="hljs-string">'object'</span>
<span class="hljs-keyword">if</span> @variable.shouldCache()
compiledName.unshift @makeCode <span class="hljs-string">'['</span>
compiledName.push @makeCode <span class="hljs-string">']'</span>
<span class="hljs-keyword">return</span> compiledName.concat @makeCode(<span class="hljs-string">': '</span>), val
answer = compiledName.concat @makeCode(<span class="hljs-string">" <span class="hljs-subst">#{ @context <span class="hljs-keyword">or</span> <span class="hljs-string">'='</span> }</span> "</span>), val</pre></div></div>
</li>
<li id="section-220">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-220">§</a>
</div>
<p>Per <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#Assignment_without_declaration">https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#Assignment_without_declaration</a>,
if we’re destructuring without declaring, the destructuring assignment must be wrapped in parentheses.
The assignment is wrapped in parentheses if ‘o.level’ has lower precedence than LEVEL_LIST (3)
(i.e. LEVEL_COND (4), LEVEL_OP (5) or LEVEL_ACCESS (6)), or if we’re destructuring object, e.g. {a,b} = obj.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> o.level > LEVEL_LIST <span class="hljs-keyword">or</span> isValue <span class="hljs-keyword">and</span> @variable.base <span class="hljs-keyword">instanceof</span> Obj <span class="hljs-keyword">and</span> <span class="hljs-keyword">not</span> @nestedLhs <span class="hljs-keyword">and</span> <span class="hljs-keyword">not</span> (@param <span class="hljs-keyword">is</span> <span class="hljs-literal">yes</span>)
@wrapInParentheses answer
<span class="hljs-keyword">else</span>
answer</pre></div></div>
</li>
<li id="section-221">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-221">§</a>
</div>
<p>Object rest property is not assignable: <code>{{a}...}</code></p>
</div>
<div class="content"><div class='highlight'><pre> compileObjectDestruct: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
@variable.base.reorderProperties()
{properties: props} = @variable.base
[..., splat] = props
splatProp = splat.name
assigns = []
refVal = <span class="hljs-keyword">new</span> Value <span class="hljs-keyword">new</span> IdentifierLiteral o.scope.freeVariable <span class="hljs-string">'ref'</span>
props.splice <span class="hljs-number">-1</span>, <span class="hljs-number">1</span>, <span class="hljs-keyword">new</span> Splat refVal
assigns.push <span class="hljs-keyword">new</span> Assign(<span class="hljs-keyword">new</span> Value(<span class="hljs-keyword">new</span> Obj props), @value).compileToFragments o, LEVEL_LIST
assigns.push <span class="hljs-keyword">new</span> Assign(<span class="hljs-keyword">new</span> Value(splatProp), refVal).compileToFragments o, LEVEL_LIST
@joinFragmentArrays assigns, <span class="hljs-string">', '</span></pre></div></div>
</li>
<li id="section-222">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-222">§</a>
</div>
<p>Brief implementation of recursive pattern matching, when assigning array or
object literals to a value. Peeks at their properties to assign inner names.</p>
</div>
<div class="content"><div class='highlight'><pre> compileDestructuring: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
top = o.level <span class="hljs-keyword">is</span> LEVEL_TOP
{value} = this
{objects} = @variable.base
olen = objects.length</pre></div></div>
</li>
<li id="section-223">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-223">§</a>
</div>
<p>Special-case for <code>{} = a</code> and <code>[] = a</code> (empty patterns).
Compile to simply <code>a</code>.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> olen <span class="hljs-keyword">is</span> <span class="hljs-number">0</span>
code = value.compileToFragments o
<span class="hljs-keyword">return</span> <span class="hljs-keyword">if</span> o.level >= LEVEL_OP <span class="hljs-keyword">then</span> @wrapInParentheses code <span class="hljs-keyword">else</span> code
[obj] = objects
@disallowLoneExpansion()
{splats, expans, splatsAndExpans} = @getAndCheckSplatsAndExpansions()
isSplat = splats?.length > <span class="hljs-number">0</span>
isExpans = expans?.length > <span class="hljs-number">0</span>
vvar = value.compileToFragments o, LEVEL_LIST
vvarText = fragmentsToText vvar
assigns = []
<span class="hljs-function"> <span class="hljs-title">pushAssign</span> = <span class="hljs-params">(variable, val)</span> =></span>
assigns.push <span class="hljs-keyword">new</span> Assign(variable, val, <span class="hljs-literal">null</span>, param: @param, subpattern: <span class="hljs-literal">yes</span>).compileToFragments o, LEVEL_LIST
<span class="hljs-keyword">if</span> isSplat
splatVar = objects[splats[<span class="hljs-number">0</span>]].name.unwrap()
<span class="hljs-keyword">if</span> splatVar <span class="hljs-keyword">instanceof</span> Arr <span class="hljs-keyword">or</span> splatVar <span class="hljs-keyword">instanceof</span> Obj
splatVarRef = <span class="hljs-keyword">new</span> IdentifierLiteral o.scope.freeVariable <span class="hljs-string">'ref'</span>
objects[splats[<span class="hljs-number">0</span>]].name = splatVarRef
<span class="hljs-function"> <span class="hljs-title">splatVarAssign</span> = -></span> pushAssign <span class="hljs-keyword">new</span> Value(splatVar), splatVarRef</pre></div></div>
</li>
<li id="section-224">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-224">§</a>
</div>
<p>At this point, there are several things to destructure. So the <code>fn()</code> in
<code>{a, b} = fn()</code> must be cached, for example. Make vvar into a simple
variable if it isn’t already.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> value.unwrap() <span class="hljs-keyword">not</span> <span class="hljs-keyword">instanceof</span> IdentifierLiteral <span class="hljs-keyword">or</span> @variable.assigns(vvarText)
ref = o.scope.freeVariable <span class="hljs-string">'ref'</span>
assigns.push [@makeCode(ref + <span class="hljs-string">' = '</span>), vvar...]
vvar = [@makeCode ref]
vvarText = ref
<span class="hljs-function">
<span class="hljs-title">slicer</span> = <span class="hljs-params">(type)</span> -></span> (vvar, start, end = <span class="hljs-literal">no</span>) ->
vvar = <span class="hljs-keyword">new</span> IdentifierLiteral vvar <span class="hljs-keyword">unless</span> vvar <span class="hljs-keyword">instanceof</span> Value
args = [vvar, <span class="hljs-keyword">new</span> NumberLiteral(start)]
args.push <span class="hljs-keyword">new</span> NumberLiteral end <span class="hljs-keyword">if</span> end
slice = <span class="hljs-keyword">new</span> Value (<span class="hljs-keyword">new</span> IdentifierLiteral utility type, o), [<span class="hljs-keyword">new</span> Access <span class="hljs-keyword">new</span> PropertyName <span class="hljs-string">'call'</span>]
<span class="hljs-keyword">new</span> Value <span class="hljs-keyword">new</span> Call slice, args</pre></div></div>
</li>
<li id="section-225">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-225">§</a>
</div>
<p>Helper which outputs <code>[].slice</code> code.</p>
</div>
<div class="content"><div class='highlight'><pre> compSlice = slicer <span class="hljs-string">"slice"</span></pre></div></div>
</li>
<li id="section-226">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-226">§</a>
</div>
<p>Helper which outputs <code>[].splice</code> code.</p>
</div>
<div class="content"><div class='highlight'><pre> compSplice = slicer <span class="hljs-string">"splice"</span></pre></div></div>
</li>
<li id="section-227">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-227">§</a>
</div>
<p>Check if <code>objects</code> array contains any instance of <code>Assign</code>, e.g. {a:1}.</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-function"> <span class="hljs-title">hasObjAssigns</span> = <span class="hljs-params">(objs)</span> -></span>
(i <span class="hljs-keyword">for</span> obj, i <span class="hljs-keyword">in</span> objs <span class="hljs-keyword">when</span> obj <span class="hljs-keyword">instanceof</span> Assign <span class="hljs-keyword">and</span> obj.context <span class="hljs-keyword">is</span> <span class="hljs-string">'object'</span>)</pre></div></div>
</li>
<li id="section-228">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-228">§</a>
</div>
<p>Check if <code>objects</code> array contains any unassignable object.</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-function"> <span class="hljs-title">objIsUnassignable</span> = <span class="hljs-params">(objs)</span> -></span>
<span class="hljs-keyword">return</span> <span class="hljs-literal">yes</span> <span class="hljs-keyword">for</span> obj <span class="hljs-keyword">in</span> objs <span class="hljs-keyword">when</span> <span class="hljs-keyword">not</span> obj.isAssignable()
<span class="hljs-literal">no</span></pre></div></div>
</li>
<li id="section-229">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-229">§</a>
</div>
<p><code>objects</code> are complex when there is object assign ({a:1}),
unassignable object, or just a single node.</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-function"> <span class="hljs-title">complexObjects</span> = <span class="hljs-params">(objs)</span> -></span>
hasObjAssigns(objs).length <span class="hljs-keyword">or</span> objIsUnassignable(objs) <span class="hljs-keyword">or</span> olen <span class="hljs-keyword">is</span> <span class="hljs-number">1</span></pre></div></div>
</li>
<li id="section-230">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-230">§</a>
</div>
<p>“Complex” <code>objects</code> are processed in a loop.
Examples: [a, b, {c, r…}, d], [a, …, {b, r…}, c, d]</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-function"> <span class="hljs-title">loopObjects</span> = <span class="hljs-params">(objs, vvar, vvarTxt)</span> =></span>
<span class="hljs-keyword">for</span> obj, i <span class="hljs-keyword">in</span> objs</pre></div></div>
</li>
<li id="section-231">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-231">§</a>
</div>
<p><code>Elision</code> can be skipped.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">continue</span> <span class="hljs-keyword">if</span> obj <span class="hljs-keyword">instanceof</span> Elision</pre></div></div>
</li>
<li id="section-232">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-232">§</a>
</div>
<p>If <code>obj</code> is {a: 1}</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> obj <span class="hljs-keyword">instanceof</span> Assign <span class="hljs-keyword">and</span> obj.context <span class="hljs-keyword">is</span> <span class="hljs-string">'object'</span>
{variable: {base: idx}, value: vvar} = obj
{variable: vvar} = vvar <span class="hljs-keyword">if</span> vvar <span class="hljs-keyword">instanceof</span> Assign
idx =
<span class="hljs-keyword">if</span> vvar.this
vvar.properties[<span class="hljs-number">0</span>].name
<span class="hljs-keyword">else</span>
<span class="hljs-keyword">new</span> PropertyName vvar.unwrap().value
acc = idx.unwrap() <span class="hljs-keyword">instanceof</span> PropertyName
vval = <span class="hljs-keyword">new</span> Value value, [<span class="hljs-keyword">new</span> (<span class="hljs-keyword">if</span> acc <span class="hljs-keyword">then</span> Access <span class="hljs-keyword">else</span> Index) idx]
<span class="hljs-keyword">else</span></pre></div></div>
</li>
<li id="section-233">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-233">§</a>
</div>
<p><code>obj</code> is [a…], {a…} or a</p>
</div>
<div class="content"><div class='highlight'><pre> vvar = <span class="hljs-keyword">switch</span>
<span class="hljs-keyword">when</span> obj <span class="hljs-keyword">instanceof</span> Splat <span class="hljs-keyword">then</span> <span class="hljs-keyword">new</span> Value obj.name
<span class="hljs-keyword">else</span> obj
vval = <span class="hljs-keyword">switch</span>
<span class="hljs-keyword">when</span> obj <span class="hljs-keyword">instanceof</span> Splat <span class="hljs-keyword">then</span> compSlice(vvarTxt, i)
<span class="hljs-keyword">else</span> <span class="hljs-keyword">new</span> Value <span class="hljs-keyword">new</span> Literal(vvarTxt), [<span class="hljs-keyword">new</span> Index <span class="hljs-keyword">new</span> NumberLiteral i]
message = isUnassignable vvar.unwrap().value
vvar.error message <span class="hljs-keyword">if</span> message
pushAssign vvar, vval</pre></div></div>
</li>
<li id="section-234">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-234">§</a>
</div>
<p>“Simple” <code>objects</code> can be split and compiled to arrays, [a, b, c] = arr, [a, b, c…] = arr</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-function"> <span class="hljs-title">assignObjects</span> = <span class="hljs-params">(objs, vvar, vvarTxt)</span> =></span>
vvar = <span class="hljs-keyword">new</span> Value <span class="hljs-keyword">new</span> Arr(objs, <span class="hljs-literal">yes</span>)
vval = <span class="hljs-keyword">if</span> vvarTxt <span class="hljs-keyword">instanceof</span> Value <span class="hljs-keyword">then</span> vvarTxt <span class="hljs-keyword">else</span> <span class="hljs-keyword">new</span> Value <span class="hljs-keyword">new</span> Literal(vvarTxt)
pushAssign vvar, vval
<span class="hljs-function">
<span class="hljs-title">processObjects</span> = <span class="hljs-params">(objs, vvar, vvarTxt)</span> -></span>
<span class="hljs-keyword">if</span> complexObjects objs
loopObjects objs, vvar, vvarTxt
<span class="hljs-keyword">else</span>
assignObjects objs, vvar, vvarTxt</pre></div></div>
</li>
<li id="section-235">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-235">§</a>
</div>
<p>In case there is <code>Splat</code> or <code>Expansion</code> in <code>objects</code>,
we can split array in two simple subarrays.
<code>Splat</code> [a, b, c…, d, e] can be split into [a, b, c…] and [d, e].
<code>Expansion</code> [a, b, …, c, d] can be split into [a, b] and [c, d].
Examples:
a) <code>Splat</code>
CS: [a, b, c…, d, e] = arr
JS: [a, b, …c] = arr, [d, e] = splice.call(c, -2)
b) <code>Expansion</code>
CS: [a, b, …, d, e] = arr
JS: [a, b] = arr, [d, e] = slice.call(arr, -2)</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> splatsAndExpans.length
expIdx = splatsAndExpans[<span class="hljs-number">0</span>]
leftObjs = objects.slice <span class="hljs-number">0</span>, expIdx + (<span class="hljs-keyword">if</span> isSplat <span class="hljs-keyword">then</span> <span class="hljs-number">1</span> <span class="hljs-keyword">else</span> <span class="hljs-number">0</span>)
rightObjs = objects.slice expIdx + <span class="hljs-number">1</span>
processObjects leftObjs, vvar, vvarText <span class="hljs-keyword">if</span> leftObjs.length <span class="hljs-keyword">isnt</span> <span class="hljs-number">0</span>
<span class="hljs-keyword">if</span> rightObjs.length <span class="hljs-keyword">isnt</span> <span class="hljs-number">0</span></pre></div></div>
</li>
<li id="section-236">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-236">§</a>
</div>
<p>Slice or splice <code>objects</code>.</p>
</div>
<div class="content"><div class='highlight'><pre> refExp = <span class="hljs-keyword">switch</span>
<span class="hljs-keyword">when</span> isSplat <span class="hljs-keyword">then</span> compSplice <span class="hljs-keyword">new</span> Value(objects[expIdx].name), rightObjs.length * <span class="hljs-number">-1</span>
<span class="hljs-keyword">when</span> isExpans <span class="hljs-keyword">then</span> compSlice vvarText, rightObjs.length * <span class="hljs-number">-1</span>
<span class="hljs-keyword">if</span> complexObjects rightObjs
restVar = refExp
refExp = o.scope.freeVariable <span class="hljs-string">'ref'</span>
assigns.push [@makeCode(refExp + <span class="hljs-string">' = '</span>), restVar.compileToFragments(o, LEVEL_LIST)...]
processObjects rightObjs, vvar, refExp
<span class="hljs-keyword">else</span></pre></div></div>
</li>
<li id="section-237">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-237">§</a>
</div>
<p>There is no <code>Splat</code> or <code>Expansion</code> in <code>objects</code>.</p>
</div>
<div class="content"><div class='highlight'><pre> processObjects objects, vvar, vvarText
splatVarAssign?()
assigns.push vvar <span class="hljs-keyword">unless</span> top <span class="hljs-keyword">or</span> @subpattern
fragments = @joinFragmentArrays assigns, <span class="hljs-string">', '</span>
<span class="hljs-keyword">if</span> o.level < LEVEL_LIST <span class="hljs-keyword">then</span> fragments <span class="hljs-keyword">else</span> @wrapInParentheses fragments</pre></div></div>
</li>
<li id="section-238">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-238">§</a>
</div>
<p>Disallow <code>[...] = a</code> for some reason. (Could be equivalent to <code>[] = a</code>?)</p>
</div>
<div class="content"><div class='highlight'><pre> disallowLoneExpansion: <span class="hljs-function">-></span>
<span class="hljs-keyword">return</span> <span class="hljs-keyword">unless</span> @variable.base <span class="hljs-keyword">instanceof</span> Arr
{objects} = @variable.base
<span class="hljs-keyword">return</span> <span class="hljs-keyword">unless</span> objects?.length <span class="hljs-keyword">is</span> <span class="hljs-number">1</span>
[loneObject] = objects
<span class="hljs-keyword">if</span> loneObject <span class="hljs-keyword">instanceof</span> Expansion
loneObject.error <span class="hljs-string">'Destructuring assignment has no target'</span></pre></div></div>
</li>
<li id="section-239">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-239">§</a>
</div>
<p>Show error if there is more than one <code>Splat</code>, or <code>Expansion</code>.
Examples: [a, b, c…, d, e, f…], [a, b, …, c, d, …], [a, b, …, c, d, e…]</p>
</div>
<div class="content"><div class='highlight'><pre> getAndCheckSplatsAndExpansions: <span class="hljs-function">-></span>
<span class="hljs-keyword">return</span> {splats: [], expans: [], splatsAndExpans: []} <span class="hljs-keyword">unless</span> @variable.base <span class="hljs-keyword">instanceof</span> Arr
{objects} = @variable.base</pre></div></div>
</li>
<li id="section-240">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-240">§</a>
</div>
<p>Count all <code>Splats</code>: [a, b, c…, d, e]</p>
</div>
<div class="content"><div class='highlight'><pre> splats = (i <span class="hljs-keyword">for</span> obj, i <span class="hljs-keyword">in</span> objects <span class="hljs-keyword">when</span> obj <span class="hljs-keyword">instanceof</span> Splat)</pre></div></div>
</li>
<li id="section-241">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-241">§</a>
</div>
<p>Count all <code>Expansions</code>: [a, b, …, c, d]</p>
</div>
<div class="content"><div class='highlight'><pre> expans = (i <span class="hljs-keyword">for</span> obj, i <span class="hljs-keyword">in</span> objects <span class="hljs-keyword">when</span> obj <span class="hljs-keyword">instanceof</span> Expansion)</pre></div></div>
</li>
<li id="section-242">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-242">§</a>
</div>
<p>Combine splats and expansions.</p>
</div>
<div class="content"><div class='highlight'><pre> splatsAndExpans = [splats..., expans...]
<span class="hljs-keyword">if</span> splatsAndExpans.length > <span class="hljs-number">1</span></pre></div></div>
</li>
<li id="section-243">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-243">§</a>
</div>
<p>Sort ‘splatsAndExpans’ so we can show error at first disallowed token.</p>
</div>
<div class="content"><div class='highlight'><pre> objects[splatsAndExpans.sort()[<span class="hljs-number">1</span>]].error <span class="hljs-string">"multiple splats/expansions are disallowed in an assignment"</span>
{splats, expans, splatsAndExpans}</pre></div></div>
</li>
<li id="section-244">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-244">§</a>
</div>
<p>When compiling a conditional assignment, take care to ensure that the
operands are only evaluated once, even though we have to reference them
more than once.</p>
</div>
<div class="content"><div class='highlight'><pre> compileConditional: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
[left, right] = @variable.cacheReference o</pre></div></div>
</li>
<li id="section-245">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-245">§</a>
</div>
<p>Disallow conditional assignment of undefined variables.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> left.properties.length <span class="hljs-keyword">and</span> left.base <span class="hljs-keyword">instanceof</span> Literal <span class="hljs-keyword">and</span>
left.base <span class="hljs-keyword">not</span> <span class="hljs-keyword">instanceof</span> ThisLiteral <span class="hljs-keyword">and</span> <span class="hljs-keyword">not</span> o.scope.check left.base.value
@throwUnassignableConditionalError left.base.value
<span class="hljs-keyword">if</span> <span class="hljs-string">"?"</span> <span class="hljs-keyword">in</span> @context
o.isExistentialEquals = <span class="hljs-literal">true</span>
<span class="hljs-keyword">new</span> If(<span class="hljs-keyword">new</span> Existence(left), right, type: <span class="hljs-string">'if'</span>).addElse(<span class="hljs-keyword">new</span> Assign(right, @value, <span class="hljs-string">'='</span>)).compileToFragments o
<span class="hljs-keyword">else</span>
fragments = <span class="hljs-keyword">new</span> Op(@context[...<span class="hljs-number">-1</span>], left, <span class="hljs-keyword">new</span> Assign(right, @value, <span class="hljs-string">'='</span>)).compileToFragments o
<span class="hljs-keyword">if</span> o.level <= LEVEL_LIST <span class="hljs-keyword">then</span> fragments <span class="hljs-keyword">else</span> @wrapInParentheses fragments</pre></div></div>
</li>
<li id="section-246">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-246">§</a>
</div>
<p>Convert special math assignment operators like <code>a //= b</code> to the equivalent
extended form <code>a = a ** b</code> and then compiles that.</p>
</div>
<div class="content"><div class='highlight'><pre> compileSpecialMath: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
[left, right] = @variable.cacheReference o
<span class="hljs-keyword">new</span> Assign(left, <span class="hljs-keyword">new</span> Op(@context[...<span class="hljs-number">-1</span>], right, @value)).compileToFragments o</pre></div></div>
</li>
<li id="section-247">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-247">§</a>
</div>
<p>Compile the assignment from an array splice literal, using JavaScript’s
<code>Array#splice</code> method.</p>
</div>
<div class="content"><div class='highlight'><pre> compileSplice: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
{range: {<span class="hljs-keyword">from</span>, to, exclusive}} = @variable.properties.pop()
unwrappedVar = @variable.unwrapAll()
<span class="hljs-keyword">if</span> unwrappedVar.comments
moveComments unwrappedVar, @
<span class="hljs-keyword">delete</span> @variable.comments
name = @variable.compile o
<span class="hljs-keyword">if</span> <span class="hljs-keyword">from</span>
[fromDecl, fromRef] = @cacheToCodeFragments <span class="hljs-keyword">from</span>.cache o, LEVEL_OP
<span class="hljs-keyword">else</span>
fromDecl = fromRef = <span class="hljs-string">'0'</span>
<span class="hljs-keyword">if</span> to
<span class="hljs-keyword">if</span> <span class="hljs-keyword">from</span>?.isNumber() <span class="hljs-keyword">and</span> to.isNumber()
to = to.compile(o) - fromRef
to += <span class="hljs-number">1</span> <span class="hljs-keyword">unless</span> exclusive
<span class="hljs-keyword">else</span>
to = to.compile(o, LEVEL_ACCESS) + <span class="hljs-string">' - '</span> + fromRef
to += <span class="hljs-string">' + 1'</span> <span class="hljs-keyword">unless</span> exclusive
<span class="hljs-keyword">else</span>
to = <span class="hljs-string">"9e9"</span>
[valDef, valRef] = @value.cache o, LEVEL_LIST
answer = [].concat @makeCode(<span class="hljs-string">"<span class="hljs-subst">#{utility <span class="hljs-string">'splice'</span>, o}</span>.apply(<span class="hljs-subst">#{name}</span>, [<span class="hljs-subst">#{fromDecl}</span>, <span class="hljs-subst">#{to}</span>].concat("</span>), valDef, @makeCode(<span class="hljs-string">")), "</span>), valRef
<span class="hljs-keyword">if</span> o.level > LEVEL_TOP <span class="hljs-keyword">then</span> @wrapInParentheses answer <span class="hljs-keyword">else</span> answer
eachName: <span class="hljs-function"><span class="hljs-params">(iterator)</span> -></span>
@variable.unwrapAll().eachName iterator
isDefaultAssignment: <span class="hljs-function">-></span> @param <span class="hljs-keyword">or</span> @nestedLhs
propagateLhs: <span class="hljs-function">-></span>
<span class="hljs-keyword">return</span> <span class="hljs-keyword">unless</span> @variable?.isArray?() <span class="hljs-keyword">or</span> @variable?.isObject?()</pre></div></div>
</li>
<li id="section-248">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-248">§</a>
</div>
<p>This is the left-hand side of an assignment; let <code>Arr</code> and <code>Obj</code>
know that, so that those nodes know that they’re assignable as
destructured variables.</p>
</div>
<div class="content"><div class='highlight'><pre> @variable.base.propagateLhs <span class="hljs-literal">yes</span>
throwUnassignableConditionalError: <span class="hljs-function"><span class="hljs-params">(name)</span> -></span>
@variable.error <span class="hljs-string">"the variable \"<span class="hljs-subst">#{name}</span>\" can't be assigned with <span class="hljs-subst">#{@context}</span> because it has not been declared before"</span>
isConditional: <span class="hljs-function">-></span>
@context <span class="hljs-keyword">in</span> [<span class="hljs-string">'||='</span>, <span class="hljs-string">'&&='</span>, <span class="hljs-string">'?='</span>]
isStatementAst: NO
astNode: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
@disallowLoneExpansion()
@getAndCheckSplatsAndExpansions()
<span class="hljs-keyword">if</span> @isConditional()
variable = @variable.unwrap()
<span class="hljs-keyword">if</span> variable <span class="hljs-keyword">instanceof</span> IdentifierLiteral <span class="hljs-keyword">and</span> <span class="hljs-keyword">not</span> o.scope.check variable.value
@throwUnassignableConditionalError variable.value
@addScopeVariables o, allowAssignmentToExpansion: <span class="hljs-literal">yes</span>, allowAssignmentToNontrailingSplat: <span class="hljs-literal">yes</span>, allowAssignmentToEmptyArray: <span class="hljs-literal">yes</span>, allowAssignmentToComplexSplat: <span class="hljs-literal">yes</span>
super o
astType: <span class="hljs-function">-></span>
<span class="hljs-keyword">if</span> @isDefaultAssignment()
<span class="hljs-string">'AssignmentPattern'</span>
<span class="hljs-keyword">else</span>
<span class="hljs-string">'AssignmentExpression'</span>
astProperties: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
ret =
right: @value.ast o, LEVEL_LIST
left: @variable.ast o, LEVEL_LIST
<span class="hljs-keyword">unless</span> @isDefaultAssignment()
ret.operator = @originalContext ? <span class="hljs-string">'='</span>
ret</pre></div></div>
</li>
<li id="section-249">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-249">§</a>
</div>
<h3 id="funcglyph">FuncGlyph</h3>
</div>
<div class="content"><div class='highlight'><pre>
<span class="hljs-built_in">exports</span>.FuncGlyph = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">FuncGlyph</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Base</span></span>
constructor: <span class="hljs-function"><span class="hljs-params">(@glyph)</span> -></span>
super()</pre></div></div>
</li>
<li id="section-250">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-250">§</a>
</div>
<h3 id="code">Code</h3>
</div>
</li>
<li id="section-251">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-251">§</a>
</div>
<p>A function definition. This is the only node that creates a new Scope.
When for the purposes of walking the contents of a function body, the Code
has no <em>children</em> – they’re within the inner scope.</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-built_in">exports</span>.Code = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Code</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Base</span></span>
constructor: <span class="hljs-function"><span class="hljs-params">(params, body, @funcGlyph, @paramStart)</span> -></span>
super()
@params = params <span class="hljs-keyword">or</span> []
@body = body <span class="hljs-keyword">or</span> <span class="hljs-keyword">new</span> Block
@bound = @funcGlyph?.glyph <span class="hljs-keyword">is</span> <span class="hljs-string">'=>'</span>
@isGenerator = <span class="hljs-literal">no</span>
@isAsync = <span class="hljs-literal">no</span>
@isMethod = <span class="hljs-literal">no</span>
@body.traverseChildren <span class="hljs-literal">no</span>, <span class="hljs-function"><span class="hljs-params">(node)</span> =></span>
<span class="hljs-keyword">if</span> (node <span class="hljs-keyword">instanceof</span> Op <span class="hljs-keyword">and</span> node.isYield()) <span class="hljs-keyword">or</span> node <span class="hljs-keyword">instanceof</span> YieldReturn
@isGenerator = <span class="hljs-literal">yes</span>
<span class="hljs-keyword">if</span> (node <span class="hljs-keyword">instanceof</span> Op <span class="hljs-keyword">and</span> node.isAwait()) <span class="hljs-keyword">or</span> node <span class="hljs-keyword">instanceof</span> AwaitReturn
@isAsync = <span class="hljs-literal">yes</span>
<span class="hljs-keyword">if</span> node <span class="hljs-keyword">instanceof</span> For <span class="hljs-keyword">and</span> node.isAwait()
@isAsync = <span class="hljs-literal">yes</span>
@propagateLhs()
children: [<span class="hljs-string">'params'</span>, <span class="hljs-string">'body'</span>]
isStatement: <span class="hljs-function">-></span> @isMethod
jumps: NO
makeScope: <span class="hljs-function"><span class="hljs-params">(parentScope)</span> -></span> <span class="hljs-keyword">new</span> Scope parentScope, @body, this</pre></div></div>
</li>
<li id="section-252">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-252">§</a>
</div>
<p>Compilation creates a new scope unless explicitly asked to share with the
outer scope. Handles splat parameters in the parameter list by setting
such parameters to be the final parameter in the function definition, as
required per the ES2015 spec. If the CoffeeScript function definition had
parameters after the splat, they are declared via expressions in the
function body.</p>
</div>
<div class="content"><div class='highlight'><pre> compileNode: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
@checkForAsyncOrGeneratorConstructor()
<span class="hljs-keyword">if</span> @bound
@context = o.scope.method.context <span class="hljs-keyword">if</span> o.scope.method?.bound
@context = <span class="hljs-string">'this'</span> <span class="hljs-keyword">unless</span> @context
@updateOptions o
params = []
exprs = []
thisAssignments = @thisAssignments?.slice() ? []
paramsAfterSplat = []
haveSplatParam = <span class="hljs-literal">no</span>
haveBodyParam = <span class="hljs-literal">no</span>
@checkForDuplicateParams()
@disallowLoneExpansionAndMultipleSplats()</pre></div></div>
</li>
<li id="section-253">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-253">§</a>
</div>
<p>Separate <code>this</code> assignments.</p>
</div>
<div class="content"><div class='highlight'><pre> @eachParamName (name, node, param, obj) ->
<span class="hljs-keyword">if</span> node.this
name = node.properties[<span class="hljs-number">0</span>].name.value
name = <span class="hljs-string">"_<span class="hljs-subst">#{name}</span>"</span> <span class="hljs-keyword">if</span> name <span class="hljs-keyword">in</span> JS_FORBIDDEN
target = <span class="hljs-keyword">new</span> IdentifierLiteral o.scope.freeVariable name, reserve: <span class="hljs-literal">no</span></pre></div></div>
</li>
<li id="section-254">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-254">§</a>
</div>
<p><code>Param</code> is object destructuring with a default value: ({@prop = 1}) ->
In a case when the variable name is already reserved, we have to assign
a new variable name to the destructured variable: ({prop:prop1 = 1}) -></p>
</div>
<div class="content"><div class='highlight'><pre> replacement =
<span class="hljs-keyword">if</span> param.name <span class="hljs-keyword">instanceof</span> Obj <span class="hljs-keyword">and</span> obj <span class="hljs-keyword">instanceof</span> Assign <span class="hljs-keyword">and</span>
obj.operatorToken.value <span class="hljs-keyword">is</span> <span class="hljs-string">'='</span>
<span class="hljs-keyword">new</span> Assign (<span class="hljs-keyword">new</span> IdentifierLiteral name), target, <span class="hljs-string">'object'</span> <span class="hljs-comment">#, operatorToken: new Literal ':'</span>
<span class="hljs-keyword">else</span>
target
param.renameParam node, replacement
thisAssignments.push <span class="hljs-keyword">new</span> Assign node, target</pre></div></div>
</li>
<li id="section-255">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-255">§</a>
</div>
<p>Parse the parameters, adding them to the list of parameters to put in the
function definition; and dealing with splats or expansions, including
adding expressions to the function body to declare all parameter
variables that would have been after the splat/expansion parameter.
If we encounter a parameter that needs to be declared in the function
body for any reason, for example it’s destructured with <code>this</code>, also
declare and assign all subsequent parameters in the function body so that
any non-idempotent parameters are evaluated in the correct order.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">for</span> param, i <span class="hljs-keyword">in</span> @params</pre></div></div>
</li>
<li id="section-256">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-256">§</a>
</div>
<p>Was <code>...</code> used with this parameter? Splat/expansion parameters cannot
have default values, so we need not worry about that.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> param.splat <span class="hljs-keyword">or</span> param <span class="hljs-keyword">instanceof</span> Expansion
haveSplatParam = <span class="hljs-literal">yes</span>
<span class="hljs-keyword">if</span> param.splat
<span class="hljs-keyword">if</span> param.name <span class="hljs-keyword">instanceof</span> Arr <span class="hljs-keyword">or</span> param.name <span class="hljs-keyword">instanceof</span> Obj</pre></div></div>
</li>
<li id="section-257">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-257">§</a>
</div>
<p>Splat arrays are treated oddly by ES; deal with them the legacy
way in the function body. TODO: Should this be handled in the
function parameter list, and if so, how?</p>
</div>
<div class="content"><div class='highlight'><pre> splatParamName = o.scope.freeVariable <span class="hljs-string">'arg'</span>
params.push ref = <span class="hljs-keyword">new</span> Value <span class="hljs-keyword">new</span> IdentifierLiteral splatParamName
exprs.push <span class="hljs-keyword">new</span> Assign <span class="hljs-keyword">new</span> Value(param.name), ref
<span class="hljs-keyword">else</span>
params.push ref = param.asReference o
splatParamName = fragmentsToText ref.compileNodeWithoutComments o
<span class="hljs-keyword">if</span> param.shouldCache()
exprs.push <span class="hljs-keyword">new</span> Assign <span class="hljs-keyword">new</span> Value(param.name), ref
<span class="hljs-keyword">else</span> <span class="hljs-comment"># `param` is an Expansion</span>
splatParamName = o.scope.freeVariable <span class="hljs-string">'args'</span>
params.push <span class="hljs-keyword">new</span> Value <span class="hljs-keyword">new</span> IdentifierLiteral splatParamName
o.scope.parameter splatParamName</pre></div></div>
</li>
<li id="section-258">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-258">§</a>
</div>
<p>Parse all other parameters; if a splat paramater has not yet been
encountered, add these other parameters to the list to be output in
the function definition.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">else</span>
<span class="hljs-keyword">if</span> param.shouldCache() <span class="hljs-keyword">or</span> haveBodyParam
param.assignedInBody = <span class="hljs-literal">yes</span>
haveBodyParam = <span class="hljs-literal">yes</span></pre></div></div>
</li>
<li id="section-259">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-259">§</a>
</div>
<p>This parameter cannot be declared or assigned in the parameter
list. So put a reference in the parameter list and add a statement
to the function body assigning it, e.g.
<code>(arg) => { var a = arg.a; }</code>, with a default value if it has one.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> param.value?
condition = <span class="hljs-keyword">new</span> Op <span class="hljs-string">'==='</span>, param, <span class="hljs-keyword">new</span> UndefinedLiteral
ifTrue = <span class="hljs-keyword">new</span> Assign <span class="hljs-keyword">new</span> Value(param.name), param.value
exprs.push <span class="hljs-keyword">new</span> If condition, ifTrue
<span class="hljs-keyword">else</span>
exprs.push <span class="hljs-keyword">new</span> Assign <span class="hljs-keyword">new</span> Value(param.name), param.asReference(o), <span class="hljs-literal">null</span>, param: <span class="hljs-string">'alwaysDeclare'</span></pre></div></div>
</li>
<li id="section-260">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-260">§</a>
</div>
<p>If this parameter comes before the splat or expansion, it will go
in the function definition parameter list.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">unless</span> haveSplatParam</pre></div></div>
</li>
<li id="section-261">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-261">§</a>
</div>
<p>If this parameter has a default value, and it hasn’t already been
set by the <code>shouldCache()</code> block above, define it as a statement in
the function body. This parameter comes after the splat parameter,
so we can’t define its default value in the parameter list.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> param.shouldCache()
ref = param.asReference o
<span class="hljs-keyword">else</span>
<span class="hljs-keyword">if</span> param.value? <span class="hljs-keyword">and</span> <span class="hljs-keyword">not</span> param.assignedInBody
ref = <span class="hljs-keyword">new</span> Assign <span class="hljs-keyword">new</span> Value(param.name), param.value, <span class="hljs-literal">null</span>, param: <span class="hljs-literal">yes</span>
<span class="hljs-keyword">else</span>
ref = param</pre></div></div>
</li>
<li id="section-262">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-262">§</a>
</div>
<p>Add this parameter’s reference(s) to the function scope.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> param.name <span class="hljs-keyword">instanceof</span> Arr <span class="hljs-keyword">or</span> param.name <span class="hljs-keyword">instanceof</span> Obj</pre></div></div>
</li>
<li id="section-263">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-263">§</a>
</div>
<p>This parameter is destructured.</p>
</div>
<div class="content"><div class='highlight'><pre> param.name.lhs = <span class="hljs-literal">yes</span>
<span class="hljs-keyword">unless</span> param.shouldCache()
param.name.eachName (prop) ->
o.scope.parameter prop.value
<span class="hljs-keyword">else</span></pre></div></div>
</li>
<li id="section-264">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-264">§</a>
</div>
<p>This compilation of the parameter is only to get its name to add
to the scope name tracking; since the compilation output here
isn’t kept for eventual output, don’t include comments in this
compilation, so that they get output the “real” time this param
is compiled.</p>
</div>
<div class="content"><div class='highlight'><pre> paramToAddToScope = <span class="hljs-keyword">if</span> param.value? <span class="hljs-keyword">then</span> param <span class="hljs-keyword">else</span> ref
o.scope.parameter fragmentsToText paramToAddToScope.compileToFragmentsWithoutComments o
params.push ref
<span class="hljs-keyword">else</span>
paramsAfterSplat.push param</pre></div></div>
</li>
<li id="section-265">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-265">§</a>
</div>
<p>If this parameter had a default value, since it’s no longer in the
function parameter list we need to assign its default value
(if necessary) as an expression in the body.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> param.value? <span class="hljs-keyword">and</span> <span class="hljs-keyword">not</span> param.shouldCache()
condition = <span class="hljs-keyword">new</span> Op <span class="hljs-string">'==='</span>, param, <span class="hljs-keyword">new</span> UndefinedLiteral
ifTrue = <span class="hljs-keyword">new</span> Assign <span class="hljs-keyword">new</span> Value(param.name), param.value
exprs.push <span class="hljs-keyword">new</span> If condition, ifTrue</pre></div></div>
</li>
<li id="section-266">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-266">§</a>
</div>
<p>Add this parameter to the scope, since it wouldn’t have been added
yet since it was skipped earlier.</p>
</div>
<div class="content"><div class='highlight'><pre> o.scope.add param.name.value, <span class="hljs-string">'var'</span>, <span class="hljs-literal">yes</span> <span class="hljs-keyword">if</span> param.name?.value?</pre></div></div>
</li>
<li id="section-267">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-267">§</a>
</div>
<p>If there were parameters after the splat or expansion parameter, those
parameters need to be assigned in the body of the function.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> paramsAfterSplat.length <span class="hljs-keyword">isnt</span> <span class="hljs-number">0</span></pre></div></div>
</li>
<li id="section-268">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-268">§</a>
</div>
<p>Create a destructured assignment, e.g. <code>[a, b, c] = [args..., b, c]</code></p>
</div>
<div class="content"><div class='highlight'><pre> exprs.unshift <span class="hljs-keyword">new</span> Assign <span class="hljs-keyword">new</span> Value(
<span class="hljs-keyword">new</span> Arr [<span class="hljs-keyword">new</span> Splat(<span class="hljs-keyword">new</span> IdentifierLiteral(splatParamName)), (param.asReference o <span class="hljs-keyword">for</span> param <span class="hljs-keyword">in</span> paramsAfterSplat)...]
), <span class="hljs-keyword">new</span> Value <span class="hljs-keyword">new</span> IdentifierLiteral splatParamName</pre></div></div>
</li>
<li id="section-269">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-269">§</a>
</div>
<p>Add new expressions to the function body</p>
</div>
<div class="content"><div class='highlight'><pre> wasEmpty = @body.isEmpty()
@disallowSuperInParamDefaults()
@checkSuperCallsInConstructorBody()
@body.expressions.unshift thisAssignments... <span class="hljs-keyword">unless</span> @expandCtorSuper thisAssignments
@body.expressions.unshift exprs...
<span class="hljs-keyword">if</span> @isMethod <span class="hljs-keyword">and</span> @bound <span class="hljs-keyword">and</span> <span class="hljs-keyword">not</span> @isStatic <span class="hljs-keyword">and</span> @classVariable
boundMethodCheck = <span class="hljs-keyword">new</span> Value <span class="hljs-keyword">new</span> Literal utility <span class="hljs-string">'boundMethodCheck'</span>, o
@body.expressions.unshift <span class="hljs-keyword">new</span> Call(boundMethodCheck, [<span class="hljs-keyword">new</span> Value(<span class="hljs-keyword">new</span> ThisLiteral), @classVariable])
@body.makeReturn() <span class="hljs-keyword">unless</span> wasEmpty <span class="hljs-keyword">or</span> @noReturn</pre></div></div>
</li>
<li id="section-270">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-270">§</a>
</div>
<p>JavaScript doesn’t allow bound (<code>=></code>) functions to also be generators.
This is usually caught via <code>Op::compileContinuation</code>, but double-check:</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> @bound <span class="hljs-keyword">and</span> @isGenerator
yieldNode = @body.contains (node) -> node <span class="hljs-keyword">instanceof</span> Op <span class="hljs-keyword">and</span> node.operator <span class="hljs-keyword">is</span> <span class="hljs-string">'yield'</span>
(yieldNode <span class="hljs-keyword">or</span> @).error <span class="hljs-string">'yield cannot occur inside bound (fat arrow) functions'</span></pre></div></div>
</li>
<li id="section-271">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-271">§</a>
</div>
<p>Assemble the output</p>
</div>
<div class="content"><div class='highlight'><pre> modifiers = []
modifiers.push <span class="hljs-string">'static'</span> <span class="hljs-keyword">if</span> @isMethod <span class="hljs-keyword">and</span> @isStatic
modifiers.push <span class="hljs-string">'async'</span> <span class="hljs-keyword">if</span> @isAsync
<span class="hljs-keyword">unless</span> @isMethod <span class="hljs-keyword">or</span> @bound
modifiers.push <span class="hljs-string">"function<span class="hljs-subst">#{<span class="hljs-keyword">if</span> @isGenerator <span class="hljs-keyword">then</span> <span class="hljs-string">'*'</span> <span class="hljs-keyword">else</span> <span class="hljs-string">''</span>}</span>"</span>
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> @isGenerator
modifiers.push <span class="hljs-string">'*'</span>
signature = [@makeCode <span class="hljs-string">'('</span>]</pre></div></div>
</li>
<li id="section-272">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-272">§</a>
</div>
<p>Block comments between a function name and <code>(</code> get output between
<code>function</code> and <code>(</code>.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> @paramStart?.comments?
@compileCommentFragments o, @paramStart, signature
<span class="hljs-keyword">for</span> param, i <span class="hljs-keyword">in</span> params
signature.push @makeCode <span class="hljs-string">', '</span> <span class="hljs-keyword">if</span> i <span class="hljs-keyword">isnt</span> <span class="hljs-number">0</span>
signature.push @makeCode <span class="hljs-string">'...'</span> <span class="hljs-keyword">if</span> haveSplatParam <span class="hljs-keyword">and</span> i <span class="hljs-keyword">is</span> params.length - <span class="hljs-number">1</span></pre></div></div>
</li>
<li id="section-273">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-273">§</a>
</div>
<p>Compile this parameter, but if any generated variables get created
(e.g. <code>ref</code>), shift those into the parent scope since we can’t put a
<code>var</code> line inside a function parameter list.</p>
</div>
<div class="content"><div class='highlight'><pre> scopeVariablesCount = o.scope.variables.length
signature.push param.compileToFragments(o, LEVEL_PAREN)...
<span class="hljs-keyword">if</span> scopeVariablesCount <span class="hljs-keyword">isnt</span> o.scope.variables.length
generatedVariables = o.scope.variables.splice scopeVariablesCount
o.scope.parent.variables.push generatedVariables...
signature.push @makeCode <span class="hljs-string">')'</span></pre></div></div>
</li>
<li id="section-274">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-274">§</a>
</div>
<p>Block comments between <code>)</code> and <code>-></code>/<code>=></code> get output between <code>)</code> and <code>{</code>.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> @funcGlyph?.comments?
comment.unshift = <span class="hljs-literal">no</span> <span class="hljs-keyword">for</span> comment <span class="hljs-keyword">in</span> @funcGlyph.comments
@compileCommentFragments o, @funcGlyph, signature
body = @body.compileWithDeclarations o <span class="hljs-keyword">unless</span> @body.isEmpty()</pre></div></div>
</li>
<li id="section-275">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-275">§</a>
</div>
<p>We need to compile the body before method names to ensure <code>super</code>
references are handled.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> @isMethod
[methodScope, o.scope] = [o.scope, o.scope.parent]
name = @name.compileToFragments o
name.shift() <span class="hljs-keyword">if</span> name[<span class="hljs-number">0</span>].code <span class="hljs-keyword">is</span> <span class="hljs-string">'.'</span>
o.scope = methodScope
answer = @joinFragmentArrays (@makeCode m <span class="hljs-keyword">for</span> m <span class="hljs-keyword">in</span> modifiers), <span class="hljs-string">' '</span>
answer.push @makeCode <span class="hljs-string">' '</span> <span class="hljs-keyword">if</span> modifiers.length <span class="hljs-keyword">and</span> name
answer.push name... <span class="hljs-keyword">if</span> name
answer.push signature...
answer.push @makeCode <span class="hljs-string">' =>'</span> <span class="hljs-keyword">if</span> @bound <span class="hljs-keyword">and</span> <span class="hljs-keyword">not</span> @isMethod
answer.push @makeCode <span class="hljs-string">' {'</span>
answer.push @makeCode(<span class="hljs-string">'\n'</span>), body..., @makeCode(<span class="hljs-string">"\n<span class="hljs-subst">#{@tab}</span>"</span>) <span class="hljs-keyword">if</span> body?.length
answer.push @makeCode <span class="hljs-string">'}'</span>
<span class="hljs-keyword">return</span> indentInitial answer, @ <span class="hljs-keyword">if</span> @isMethod
<span class="hljs-keyword">if</span> @front <span class="hljs-keyword">or</span> (o.level >= LEVEL_ACCESS) <span class="hljs-keyword">then</span> @wrapInParentheses answer <span class="hljs-keyword">else</span> answer
updateOptions: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
o.scope = del(o, <span class="hljs-string">'classScope'</span>) <span class="hljs-keyword">or</span> @makeScope o.scope
o.scope.shared = del(o, <span class="hljs-string">'sharedScope'</span>)
o.indent += TAB
<span class="hljs-keyword">delete</span> o.bare
<span class="hljs-keyword">delete</span> o.isExistentialEquals
checkForDuplicateParams: <span class="hljs-function">-></span>
paramNames = []
@eachParamName (name, node, param) ->
node.error <span class="hljs-string">"multiple parameters named '<span class="hljs-subst">#{name}</span>'"</span> <span class="hljs-keyword">if</span> name <span class="hljs-keyword">in</span> paramNames
paramNames.push name
eachParamName: <span class="hljs-function"><span class="hljs-params">(iterator)</span> -></span>
param.eachName iterator <span class="hljs-keyword">for</span> param <span class="hljs-keyword">in</span> @params</pre></div></div>
</li>
<li id="section-276">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-276">§</a>
</div>
<p>Short-circuit <code>traverseChildren</code> method to prevent it from crossing scope
boundaries unless <code>crossScope</code> is <code>true</code>.</p>
</div>
<div class="content"><div class='highlight'><pre> traverseChildren: <span class="hljs-function"><span class="hljs-params">(crossScope, func)</span> -></span>
super(crossScope, func) <span class="hljs-keyword">if</span> crossScope</pre></div></div>
</li>
<li id="section-277">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-277">§</a>
</div>
<p>Short-circuit <code>replaceInContext</code> method to prevent it from crossing context boundaries. Bound
functions have the same context.</p>
</div>
<div class="content"><div class='highlight'><pre> replaceInContext: <span class="hljs-function"><span class="hljs-params">(child, replacement)</span> -></span>
<span class="hljs-keyword">if</span> @bound
super child, replacement
<span class="hljs-keyword">else</span>
<span class="hljs-literal">false</span>
disallowSuperInParamDefaults: <span class="hljs-function"><span class="hljs-params">({forAst} = {})</span> -></span>
<span class="hljs-keyword">return</span> <span class="hljs-literal">false</span> <span class="hljs-keyword">unless</span> @ctor
@eachSuperCall Block.wrap(@params), <span class="hljs-function"><span class="hljs-params">(superCall)</span> -></span>
superCall.error <span class="hljs-string">"'super' is not allowed in constructor parameter defaults"</span>
, checkForThisBeforeSuper: <span class="hljs-keyword">not</span> forAst
checkSuperCallsInConstructorBody: <span class="hljs-function">-></span>
<span class="hljs-keyword">return</span> <span class="hljs-literal">false</span> <span class="hljs-keyword">unless</span> @ctor
seenSuper = @eachSuperCall @body, <span class="hljs-function"><span class="hljs-params">(superCall)</span> =></span>
superCall.error <span class="hljs-string">"'super' is only allowed in derived class constructors"</span> <span class="hljs-keyword">if</span> @ctor <span class="hljs-keyword">is</span> <span class="hljs-string">'base'</span>
seenSuper
flagThisParamInDerivedClassConstructorWithoutCallingSuper: <span class="hljs-function"><span class="hljs-params">(param)</span> -></span>
param.error <span class="hljs-string">"Can't use @params in derived class constructors without calling super"</span>
checkForAsyncOrGeneratorConstructor: <span class="hljs-function">-></span>
<span class="hljs-keyword">if</span> @ctor
@name.error <span class="hljs-string">'Class constructor may not be async'</span> <span class="hljs-keyword">if</span> @isAsync
@name.error <span class="hljs-string">'Class constructor may not be a generator'</span> <span class="hljs-keyword">if</span> @isGenerator
disallowLoneExpansionAndMultipleSplats: <span class="hljs-function">-></span>
seenSplatParam = <span class="hljs-literal">no</span>
<span class="hljs-keyword">for</span> param <span class="hljs-keyword">in</span> @params</pre></div></div>
</li>
<li id="section-278">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-278">§</a>
</div>
<p>Was <code>...</code> used with this parameter? (Only one such parameter is allowed
per function.)</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> param.splat <span class="hljs-keyword">or</span> param <span class="hljs-keyword">instanceof</span> Expansion
<span class="hljs-keyword">if</span> seenSplatParam
param.error <span class="hljs-string">'only one splat or expansion parameter is allowed per function definition'</span>
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> param <span class="hljs-keyword">instanceof</span> Expansion <span class="hljs-keyword">and</span> @params.length <span class="hljs-keyword">is</span> <span class="hljs-number">1</span>
param.error <span class="hljs-string">'an expansion parameter cannot be the only parameter in a function definition'</span>
seenSplatParam = <span class="hljs-literal">yes</span>
expandCtorSuper: <span class="hljs-function"><span class="hljs-params">(thisAssignments)</span> -></span>
<span class="hljs-keyword">return</span> <span class="hljs-literal">false</span> <span class="hljs-keyword">unless</span> @ctor
seenSuper = @eachSuperCall @body, <span class="hljs-function"><span class="hljs-params">(superCall)</span> =></span>
superCall.expressions = thisAssignments
haveThisParam = thisAssignments.length <span class="hljs-keyword">and</span> thisAssignments.length <span class="hljs-keyword">isnt</span> @thisAssignments?.length
<span class="hljs-keyword">if</span> @ctor <span class="hljs-keyword">is</span> <span class="hljs-string">'derived'</span> <span class="hljs-keyword">and</span> <span class="hljs-keyword">not</span> seenSuper <span class="hljs-keyword">and</span> haveThisParam
param = thisAssignments[<span class="hljs-number">0</span>].variable
@flagThisParamInDerivedClassConstructorWithoutCallingSuper param
seenSuper</pre></div></div>
</li>
<li id="section-279">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-279">§</a>
</div>
<p>Find all super calls in the given context node;
returns <code>true</code> if <code>iterator</code> is called.</p>
</div>
<div class="content"><div class='highlight'><pre> eachSuperCall: <span class="hljs-function"><span class="hljs-params">(context, iterator, {checkForThisBeforeSuper = <span class="hljs-literal">yes</span>} = {})</span> -></span>
seenSuper = <span class="hljs-literal">no</span>
context.traverseChildren <span class="hljs-literal">yes</span>, <span class="hljs-function"><span class="hljs-params">(child)</span> =></span>
<span class="hljs-keyword">if</span> child <span class="hljs-keyword">instanceof</span> SuperCall</pre></div></div>
</li>
<li id="section-280">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-280">§</a>
</div>
<p><code>super</code> in a constructor (the only <code>super</code> without an accessor)
cannot be given an argument with a reference to <code>this</code>, as that would
be referencing <code>this</code> before calling <code>super</code>.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">unless</span> child.variable.accessor
childArgs = child.args.filter (arg) ->
arg <span class="hljs-keyword">not</span> <span class="hljs-keyword">instanceof</span> Class <span class="hljs-keyword">and</span> (arg <span class="hljs-keyword">not</span> <span class="hljs-keyword">instanceof</span> Code <span class="hljs-keyword">or</span> arg.bound)
Block.wrap(childArgs).traverseChildren <span class="hljs-literal">yes</span>, <span class="hljs-function"><span class="hljs-params">(node)</span> =></span>
node.error <span class="hljs-string">"Can't call super with @params in derived class constructors"</span> <span class="hljs-keyword">if</span> node.this
seenSuper = <span class="hljs-literal">yes</span>
iterator child
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> checkForThisBeforeSuper <span class="hljs-keyword">and</span> child <span class="hljs-keyword">instanceof</span> ThisLiteral <span class="hljs-keyword">and</span> @ctor <span class="hljs-keyword">is</span> <span class="hljs-string">'derived'</span> <span class="hljs-keyword">and</span> <span class="hljs-keyword">not</span> seenSuper
child.error <span class="hljs-string">"Can't reference 'this' before calling super in derived class constructors"</span></pre></div></div>
</li>
<li id="section-281">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-281">§</a>
</div>
<p><code>super</code> has the same target in bound (arrow) functions, so check them too</p>
</div>
<div class="content"><div class='highlight'><pre> child <span class="hljs-keyword">not</span> <span class="hljs-keyword">instanceof</span> SuperCall <span class="hljs-keyword">and</span> (child <span class="hljs-keyword">not</span> <span class="hljs-keyword">instanceof</span> Code <span class="hljs-keyword">or</span> child.bound)
seenSuper
propagateLhs: <span class="hljs-function">-></span>
<span class="hljs-keyword">for</span> param <span class="hljs-keyword">in</span> @params
{name} = param
<span class="hljs-keyword">if</span> name <span class="hljs-keyword">instanceof</span> Arr <span class="hljs-keyword">or</span> name <span class="hljs-keyword">instanceof</span> Obj
name.propagateLhs <span class="hljs-literal">yes</span>
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> param <span class="hljs-keyword">instanceof</span> Expansion
param.lhs = <span class="hljs-literal">yes</span>
astAddParamsToScope: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
@eachParamName (name) ->
o.scope.add name, <span class="hljs-string">'param'</span>
astNode: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
@updateOptions o
@checkForAsyncOrGeneratorConstructor()
@checkForDuplicateParams()
@disallowSuperInParamDefaults forAst: <span class="hljs-literal">yes</span>
@disallowLoneExpansionAndMultipleSplats()
seenSuper = @checkSuperCallsInConstructorBody()
<span class="hljs-keyword">if</span> @ctor <span class="hljs-keyword">is</span> <span class="hljs-string">'derived'</span> <span class="hljs-keyword">and</span> <span class="hljs-keyword">not</span> seenSuper
@eachParamName (name, node) =>
<span class="hljs-keyword">if</span> node.this
@flagThisParamInDerivedClassConstructorWithoutCallingSuper node
@astAddParamsToScope o
@body.makeReturn <span class="hljs-literal">null</span>, <span class="hljs-literal">yes</span> <span class="hljs-keyword">unless</span> @body.isEmpty() <span class="hljs-keyword">or</span> @noReturn
super o
astType: <span class="hljs-function">-></span>
<span class="hljs-keyword">if</span> @isMethod
<span class="hljs-string">'ClassMethod'</span>
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> @bound
<span class="hljs-string">'ArrowFunctionExpression'</span>
<span class="hljs-keyword">else</span>
<span class="hljs-string">'FunctionExpression'</span>
paramForAst: <span class="hljs-function"><span class="hljs-params">(param)</span> -></span>
<span class="hljs-keyword">return</span> param <span class="hljs-keyword">if</span> param <span class="hljs-keyword">instanceof</span> Expansion
{name, value, splat} = param
<span class="hljs-keyword">if</span> splat
<span class="hljs-keyword">new</span> Splat name, lhs: <span class="hljs-literal">yes</span>, postfix: splat.postfix
.withLocationDataFrom param
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> value?
<span class="hljs-keyword">new</span> Assign name, value, <span class="hljs-literal">null</span>, param: <span class="hljs-literal">yes</span>
.withLocationDataFrom locationData: mergeLocationData name.locationData, value.locationData
<span class="hljs-keyword">else</span>
name
methodAstProperties: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
<span class="hljs-function"> <span class="hljs-title">getIsComputed</span> = =></span>
<span class="hljs-keyword">return</span> <span class="hljs-literal">yes</span> <span class="hljs-keyword">if</span> @name <span class="hljs-keyword">instanceof</span> Index
<span class="hljs-keyword">return</span> <span class="hljs-literal">yes</span> <span class="hljs-keyword">if</span> @name <span class="hljs-keyword">instanceof</span> ComputedPropertyName
<span class="hljs-keyword">return</span> <span class="hljs-literal">yes</span> <span class="hljs-keyword">if</span> @name.name <span class="hljs-keyword">instanceof</span> ComputedPropertyName
<span class="hljs-literal">no</span>
<span class="hljs-keyword">return</span>
static: !!@isStatic
key: @name.ast o
computed: getIsComputed()
kind:
<span class="hljs-keyword">if</span> @ctor
<span class="hljs-string">'constructor'</span>
<span class="hljs-keyword">else</span>
<span class="hljs-string">'method'</span>
operator: @operatorToken?.value ? <span class="hljs-string">'='</span>
staticClassName: @isStatic.staticClassName?.ast(o) ? <span class="hljs-literal">null</span>
bound: !!@bound
astProperties: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
<span class="hljs-keyword">return</span> <span class="hljs-built_in">Object</span>.assign
params: @paramForAst(param).ast(o) <span class="hljs-keyword">for</span> param <span class="hljs-keyword">in</span> @params
body: @body.ast (<span class="hljs-built_in">Object</span>.assign {}, o, checkForDirectives: <span class="hljs-literal">yes</span>), LEVEL_TOP
generator: !!@isGenerator
async: !!@isAsync</pre></div></div>
</li>
<li id="section-282">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-282">§</a>
</div>
<p>We never generate named functions, so specify <code>id</code> as <code>null</code>, which
matches the Babel AST for anonymous function expressions/arrow functions</p>
</div>
<div class="content"><div class='highlight'><pre> id: <span class="hljs-literal">null</span>
hasIndentedBody: @body.locationData.first_line > @funcGlyph?.locationData.first_line
,
<span class="hljs-keyword">if</span> @isMethod <span class="hljs-keyword">then</span> @methodAstProperties o <span class="hljs-keyword">else</span> {}
astLocationData: <span class="hljs-function">-></span>
functionLocationData = super()
<span class="hljs-keyword">return</span> functionLocationData <span class="hljs-keyword">unless</span> @isMethod
astLocationData = mergeAstLocationData @name.astLocationData(), functionLocationData
<span class="hljs-keyword">if</span> @isStatic.staticClassName?
astLocationData = mergeAstLocationData @isStatic.staticClassName.astLocationData(), astLocationData
astLocationData</pre></div></div>
</li>
<li id="section-283">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-283">§</a>
</div>
<h3 id="param">Param</h3>
</div>
</li>
<li id="section-284">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-284">§</a>
</div>
<p>A parameter in a function definition. Beyond a typical JavaScript parameter,
these parameters can also attach themselves to the context of the function,
as well as be a splat, gathering up a group of parameters into an array.</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-built_in">exports</span>.Param = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Param</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Base</span></span>
constructor: <span class="hljs-function"><span class="hljs-params">(@name, @value, @splat)</span> -></span>
super()
message = isUnassignable @name.unwrapAll().value
@name.error message <span class="hljs-keyword">if</span> message
<span class="hljs-keyword">if</span> @name <span class="hljs-keyword">instanceof</span> Obj <span class="hljs-keyword">and</span> @name.generated
token = @name.objects[<span class="hljs-number">0</span>].operatorToken
token.error <span class="hljs-string">"unexpected <span class="hljs-subst">#{token.value}</span>"</span>
children: [<span class="hljs-string">'name'</span>, <span class="hljs-string">'value'</span>]
compileToFragments: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
@name.compileToFragments o, LEVEL_LIST
compileToFragmentsWithoutComments: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
@name.compileToFragmentsWithoutComments o, LEVEL_LIST
asReference: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
<span class="hljs-keyword">return</span> @reference <span class="hljs-keyword">if</span> @reference
node = @name
<span class="hljs-keyword">if</span> node.this
name = node.properties[<span class="hljs-number">0</span>].name.value
name = <span class="hljs-string">"_<span class="hljs-subst">#{name}</span>"</span> <span class="hljs-keyword">if</span> name <span class="hljs-keyword">in</span> JS_FORBIDDEN
node = <span class="hljs-keyword">new</span> IdentifierLiteral o.scope.freeVariable name
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> node.shouldCache()
node = <span class="hljs-keyword">new</span> IdentifierLiteral o.scope.freeVariable <span class="hljs-string">'arg'</span>
node = <span class="hljs-keyword">new</span> Value node
node.updateLocationDataIfMissing @locationData
@reference = node
shouldCache: <span class="hljs-function">-></span>
@name.shouldCache()</pre></div></div>
</li>
<li id="section-285">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-285">§</a>
</div>
<p>Iterates the name or names of a <code>Param</code>.
In a sense, a destructured parameter represents multiple JS parameters. This
method allows to iterate them all.
The <code>iterator</code> function will be called as <code>iterator(name, node)</code> where
<code>name</code> is the name of the parameter and <code>node</code> is the AST node corresponding
to that name.</p>
</div>
<div class="content"><div class='highlight'><pre> eachName: <span class="hljs-function"><span class="hljs-params">(iterator, name = @name)</span> -></span>
<span class="hljs-function"> <span class="hljs-title">checkAssignabilityOfLiteral</span> = <span class="hljs-params">(literal)</span> -></span>
message = isUnassignable literal.value
<span class="hljs-keyword">if</span> message
literal.error message
<span class="hljs-keyword">unless</span> literal.isAssignable()
literal.error <span class="hljs-string">"'<span class="hljs-subst">#{literal.value}</span>' can't be assigned"</span>
<span class="hljs-function">
<span class="hljs-title">atParam</span> = <span class="hljs-params">(obj, originalObj = <span class="hljs-literal">null</span>)</span> =></span> iterator <span class="hljs-string">"@<span class="hljs-subst">#{obj.properties[<span class="hljs-number">0</span>].name.value}</span>"</span>, obj, @, originalObj
<span class="hljs-keyword">if</span> name <span class="hljs-keyword">instanceof</span> Call
name.error <span class="hljs-string">"Function invocation can't be assigned"</span></pre></div></div>
</li>
<li id="section-286">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-286">§</a>
</div>
<ul>
<li>simple literals <code>foo</code></li>
</ul>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> name <span class="hljs-keyword">instanceof</span> Literal
checkAssignabilityOfLiteral name
<span class="hljs-keyword">return</span> iterator name.value, name, @</pre></div></div>
</li>
<li id="section-287">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-287">§</a>
</div>
<ul>
<li>at-params <code>@foo</code></li>
</ul>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">return</span> atParam name <span class="hljs-keyword">if</span> name <span class="hljs-keyword">instanceof</span> Value
<span class="hljs-keyword">for</span> obj <span class="hljs-keyword">in</span> name.objects ? []</pre></div></div>
</li>
<li id="section-288">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-288">§</a>
</div>
<p>Save original obj.</p>
</div>
<div class="content"><div class='highlight'><pre> nObj = obj</pre></div></div>
</li>
<li id="section-289">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-289">§</a>
</div>
<ul>
<li>destructured parameter with default value</li>
</ul>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> obj <span class="hljs-keyword">instanceof</span> Assign <span class="hljs-keyword">and</span> <span class="hljs-keyword">not</span> obj.context?
obj = obj.variable</pre></div></div>
</li>
<li id="section-290">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-290">§</a>
</div>
<ul>
<li>assignments within destructured parameters <code>{foo:bar}</code></li>
</ul>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> obj <span class="hljs-keyword">instanceof</span> Assign</pre></div></div>
</li>
<li id="section-291">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-291">§</a>
</div>
<p>… possibly with a default value</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> obj.value <span class="hljs-keyword">instanceof</span> Assign
obj = obj.value.variable
<span class="hljs-keyword">else</span>
obj = obj.value
@eachName iterator, obj.unwrap()</pre></div></div>
</li>
<li id="section-292">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-292">§</a>
</div>
<ul>
<li>splats within destructured parameters <code>[xs...]</code></li>
</ul>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> obj <span class="hljs-keyword">instanceof</span> Splat
node = obj.name.unwrap()
iterator node.value, node, @
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> obj <span class="hljs-keyword">instanceof</span> Value</pre></div></div>
</li>
<li id="section-293">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-293">§</a>
</div>
<ul>
<li>destructured parameters within destructured parameters <code>[{a}]</code></li>
</ul>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> obj.isArray() <span class="hljs-keyword">or</span> obj.isObject()
@eachName iterator, obj.base</pre></div></div>
</li>
<li id="section-294">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-294">§</a>
</div>
<ul>
<li>at-params within destructured parameters <code>{@foo}</code></li>
</ul>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> obj.this
atParam obj, nObj</pre></div></div>
</li>
<li id="section-295">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-295">§</a>
</div>
<ul>
<li>simple destructured parameters {foo}</li>
</ul>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">else</span>
checkAssignabilityOfLiteral obj.base
iterator obj.base.value, obj.base, @
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> obj <span class="hljs-keyword">instanceof</span> Elision
obj
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> obj <span class="hljs-keyword">not</span> <span class="hljs-keyword">instanceof</span> Expansion
obj.error <span class="hljs-string">"illegal parameter <span class="hljs-subst">#{obj.compile()}</span>"</span>
<span class="hljs-keyword">return</span></pre></div></div>
</li>
<li id="section-296">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-296">§</a>
</div>
<p>Rename a param by replacing the given AST node for a name with a new node.
This needs to ensure that the the source for object destructuring does not change.</p>
</div>
<div class="content"><div class='highlight'><pre> renameParam: <span class="hljs-function"><span class="hljs-params">(node, newNode)</span> -></span>
<span class="hljs-function"> <span class="hljs-title">isNode</span> = <span class="hljs-params">(candidate)</span> -></span> candidate <span class="hljs-keyword">is</span> node
<span class="hljs-function"> <span class="hljs-title">replacement</span> = <span class="hljs-params">(node, parent)</span> =></span>
<span class="hljs-keyword">if</span> parent <span class="hljs-keyword">instanceof</span> Obj
key = node
key = node.properties[<span class="hljs-number">0</span>].name <span class="hljs-keyword">if</span> node.this</pre></div></div>
</li>
<li id="section-297">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-297">§</a>
</div>
<p>No need to assign a new variable for the destructured variable if the variable isn’t reserved.
Examples:
<code>({@foo}) -></code> should compile to <code>({foo}) { this.foo = foo}</code>
<code>foo = 1; ({@foo}) -></code> should compile to <code>foo = 1; ({foo:foo1}) { this.foo = foo1 }</code></p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> node.this <span class="hljs-keyword">and</span> key.value <span class="hljs-keyword">is</span> newNode.value
<span class="hljs-keyword">new</span> Value newNode
<span class="hljs-keyword">else</span>
<span class="hljs-keyword">new</span> Assign <span class="hljs-keyword">new</span> Value(key), newNode, <span class="hljs-string">'object'</span>
<span class="hljs-keyword">else</span>
newNode
@replaceInContext isNode, replacement</pre></div></div>
</li>
<li id="section-298">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-298">§</a>
</div>
<h3 id="splat">Splat</h3>
</div>
</li>
<li id="section-299">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-299">§</a>
</div>
<p>A splat, either as a parameter to a function, an argument to a call,
or as part of a destructuring assignment.</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-built_in">exports</span>.Splat = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Splat</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Base</span></span>
constructor: <span class="hljs-function"><span class="hljs-params">(name, {@lhs, @postfix = <span class="hljs-literal">true</span>} = {})</span> -></span>
super()
@name = <span class="hljs-keyword">if</span> name.compile <span class="hljs-keyword">then</span> name <span class="hljs-keyword">else</span> <span class="hljs-keyword">new</span> Literal name
children: [<span class="hljs-string">'name'</span>]
shouldCache: <span class="hljs-function">-></span> <span class="hljs-literal">no</span>
isAssignable: <span class="hljs-function"><span class="hljs-params">({allowComplexSplat = <span class="hljs-literal">no</span>} = {})</span>-></span>
<span class="hljs-keyword">return</span> allowComplexSplat <span class="hljs-keyword">if</span> @name <span class="hljs-keyword">instanceof</span> Obj <span class="hljs-keyword">or</span> @name <span class="hljs-keyword">instanceof</span> Parens
@name.isAssignable() <span class="hljs-keyword">and</span> (<span class="hljs-keyword">not</span> @name.isAtomic <span class="hljs-keyword">or</span> @name.isAtomic())
assigns: <span class="hljs-function"><span class="hljs-params">(name)</span> -></span>
@name.assigns name
compileNode: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
compiledSplat = [@makeCode(<span class="hljs-string">'...'</span>), @name.compileToFragments(o, LEVEL_OP)...]
<span class="hljs-keyword">return</span> compiledSplat <span class="hljs-keyword">unless</span> @jsx
<span class="hljs-keyword">return</span> [@makeCode(<span class="hljs-string">'{'</span>), compiledSplat..., @makeCode(<span class="hljs-string">'}'</span>)]
unwrap: <span class="hljs-function">-></span> @name
propagateLhs: <span class="hljs-function"><span class="hljs-params">(setLhs)</span> -></span>
@lhs = <span class="hljs-literal">yes</span> <span class="hljs-keyword">if</span> setLhs
<span class="hljs-keyword">return</span> <span class="hljs-keyword">unless</span> @lhs
@name.propagateLhs? <span class="hljs-literal">yes</span>
astType: <span class="hljs-function">-></span>
<span class="hljs-keyword">if</span> @jsx
<span class="hljs-string">'JSXSpreadAttribute'</span>
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> @lhs
<span class="hljs-string">'RestElement'</span>
<span class="hljs-keyword">else</span>
<span class="hljs-string">'SpreadElement'</span>
astProperties: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span> {
argument: @name.ast o, LEVEL_OP
@postfix
}</pre></div></div>
</li>
<li id="section-300">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-300">§</a>
</div>
<h3 id="expansion">Expansion</h3>
</div>
</li>
<li id="section-301">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-301">§</a>
</div>
<p>Used to skip values inside an array destructuring (pattern matching) or
parameter list.</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-built_in">exports</span>.Expansion = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Expansion</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Base</span></span>
shouldCache: NO
compileNode: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
@throwLhsError()
asReference: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
this
eachName: <span class="hljs-function"><span class="hljs-params">(iterator)</span> -></span>
throwLhsError: <span class="hljs-function">-></span>
@error <span class="hljs-string">'Expansion must be used inside a destructuring assignment or parameter list'</span>
astNode: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
<span class="hljs-keyword">unless</span> @lhs
@throwLhsError()
super o
astType: <span class="hljs-function">-></span> <span class="hljs-string">'RestElement'</span>
astProperties: <span class="hljs-function">-></span>
<span class="hljs-keyword">return</span>
argument: <span class="hljs-literal">null</span></pre></div></div>
</li>
<li id="section-302">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-302">§</a>
</div>
<h3 id="elision">Elision</h3>
</div>
</li>
<li id="section-303">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-303">§</a>
</div>
<p>Array elision element (for example, [,a, , , b, , c, ,]).</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-built_in">exports</span>.Elision = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Elision</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Base</span></span>
isAssignable: YES
shouldCache: NO
compileToFragments: <span class="hljs-function"><span class="hljs-params">(o, level)</span> -></span>
fragment = super o, level
fragment.isElision = <span class="hljs-literal">yes</span>
fragment
compileNode: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
[@makeCode <span class="hljs-string">', '</span>]
asReference: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
this
eachName: <span class="hljs-function"><span class="hljs-params">(iterator)</span> -></span>
astNode: <span class="hljs-function">-></span>
<span class="hljs-literal">null</span></pre></div></div>
</li>
<li id="section-304">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-304">§</a>
</div>
<h3 id="while">While</h3>
</div>
</li>
<li id="section-305">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-305">§</a>
</div>
<p>A while loop, the only sort of low-level loop exposed by CoffeeScript. From
it, all other loops can be manufactured. Useful in cases where you need more
flexibility or more speed than a comprehension can provide.</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-built_in">exports</span>.While = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">While</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Base</span></span>
constructor: <span class="hljs-function"><span class="hljs-params">(@condition, {invert: @inverted, @guard, @isLoop} = {})</span> -></span>
super()
children: [<span class="hljs-string">'condition'</span>, <span class="hljs-string">'guard'</span>, <span class="hljs-string">'body'</span>]
isStatement: YES
makeReturn: <span class="hljs-function"><span class="hljs-params">(results, mark)</span> -></span>
<span class="hljs-keyword">return</span> super(results, mark) <span class="hljs-keyword">if</span> results
@returns = <span class="hljs-keyword">not</span> @jumps()
<span class="hljs-keyword">if</span> mark
@body.makeReturn(results, mark) <span class="hljs-keyword">if</span> @returns
<span class="hljs-keyword">return</span>
this
addBody: <span class="hljs-function"><span class="hljs-params">(@body)</span> -></span>
this
jumps: <span class="hljs-function">-></span>
{expressions} = @body
<span class="hljs-keyword">return</span> <span class="hljs-literal">no</span> <span class="hljs-keyword">unless</span> expressions.length
<span class="hljs-keyword">for</span> node <span class="hljs-keyword">in</span> expressions
<span class="hljs-keyword">return</span> jumpNode <span class="hljs-keyword">if</span> jumpNode = node.jumps loop: <span class="hljs-literal">yes</span>
<span class="hljs-literal">no</span></pre></div></div>
</li>
<li id="section-306">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-306">§</a>
</div>
<p>The main difference from a JavaScript <em>while</em> is that the CoffeeScript
<em>while</em> can be used as a part of a larger expression – while loops may
return an array containing the computed result of each iteration.</p>
</div>
<div class="content"><div class='highlight'><pre> compileNode: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
o.indent += TAB
set = <span class="hljs-string">''</span>
{body} = this
<span class="hljs-keyword">if</span> body.isEmpty()
body = @makeCode <span class="hljs-string">''</span>
<span class="hljs-keyword">else</span>
<span class="hljs-keyword">if</span> @returns
body.makeReturn rvar = o.scope.freeVariable <span class="hljs-string">'results'</span>
set = <span class="hljs-string">"<span class="hljs-subst">#{@tab}</span><span class="hljs-subst">#{rvar}</span> = [];\n"</span>
<span class="hljs-keyword">if</span> @guard
<span class="hljs-keyword">if</span> body.expressions.length > <span class="hljs-number">1</span>
body.expressions.unshift <span class="hljs-keyword">new</span> If (<span class="hljs-keyword">new</span> Parens @guard).invert(), <span class="hljs-keyword">new</span> StatementLiteral <span class="hljs-string">"continue"</span>
<span class="hljs-keyword">else</span>
body = Block.wrap [<span class="hljs-keyword">new</span> If @guard, body] <span class="hljs-keyword">if</span> @guard
body = [].concat @makeCode(<span class="hljs-string">"\n"</span>), (body.compileToFragments o, LEVEL_TOP), @makeCode(<span class="hljs-string">"\n<span class="hljs-subst">#{@tab}</span>"</span>)
answer = [].concat @makeCode(set + @tab + <span class="hljs-string">"while ("</span>), @processedCondition().compileToFragments(o, LEVEL_PAREN),
@makeCode(<span class="hljs-string">") {"</span>), body, @makeCode(<span class="hljs-string">"}"</span>)
<span class="hljs-keyword">if</span> @returns
answer.push @makeCode <span class="hljs-string">"\n<span class="hljs-subst">#{@tab}</span>return <span class="hljs-subst">#{rvar}</span>;"</span>
answer
processedCondition: <span class="hljs-function">-></span>
@processedConditionCache ?= <span class="hljs-keyword">if</span> @inverted <span class="hljs-keyword">then</span> @condition.invert() <span class="hljs-keyword">else</span> @condition
astType: <span class="hljs-function">-></span> <span class="hljs-string">'WhileStatement'</span>
astProperties: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
<span class="hljs-keyword">return</span>
test: @condition.ast o, LEVEL_PAREN
body: @body.ast o, LEVEL_TOP
guard: @guard?.ast(o) ? <span class="hljs-literal">null</span>
inverted: !!@inverted
postfix: !!@postfix
loop: !!@isLoop</pre></div></div>
</li>
<li id="section-307">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-307">§</a>
</div>
<h3 id="op">Op</h3>
</div>
</li>
<li id="section-308">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-308">§</a>
</div>
<p>Simple Arithmetic and logical operations. Performs some conversion from
CoffeeScript operations into their JavaScript equivalents.</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-built_in">exports</span>.Op = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Op</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Base</span></span>
constructor: <span class="hljs-function"><span class="hljs-params">(op, first, second, flip, {@invertOperator, @originalOperator = op} = {})</span> -></span>
super()
<span class="hljs-keyword">if</span> op <span class="hljs-keyword">is</span> <span class="hljs-string">'new'</span>
<span class="hljs-keyword">if</span> ((firstCall = unwrapped = first.unwrap()) <span class="hljs-keyword">instanceof</span> Call <span class="hljs-keyword">or</span> (firstCall = unwrapped.base) <span class="hljs-keyword">instanceof</span> Call) <span class="hljs-keyword">and</span> <span class="hljs-keyword">not</span> firstCall.<span class="hljs-keyword">do</span> <span class="hljs-keyword">and</span> <span class="hljs-keyword">not</span> firstCall.isNew
<span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> Value firstCall.newInstance(), <span class="hljs-keyword">if</span> firstCall <span class="hljs-keyword">is</span> unwrapped <span class="hljs-keyword">then</span> [] <span class="hljs-keyword">else</span> unwrapped.properties
first = <span class="hljs-keyword">new</span> Parens first <span class="hljs-keyword">unless</span> first <span class="hljs-keyword">instanceof</span> Parens <span class="hljs-keyword">or</span> first.unwrap() <span class="hljs-keyword">instanceof</span> IdentifierLiteral <span class="hljs-keyword">or</span> first.hasProperties?()
call = <span class="hljs-keyword">new</span> Call first, []
call.locationData = @locationData
call.isNew = <span class="hljs-literal">yes</span>
<span class="hljs-keyword">return</span> call
@operator = CONVERSIONS[op] <span class="hljs-keyword">or</span> op
@first = first
@second = second
@flip = !!flip
<span class="hljs-keyword">if</span> @operator <span class="hljs-keyword">in</span> [<span class="hljs-string">'--'</span>, <span class="hljs-string">'++'</span>]
message = isUnassignable @first.unwrapAll().value
@first.error message <span class="hljs-keyword">if</span> message
<span class="hljs-keyword">return</span> this</pre></div></div>
</li>
<li id="section-309">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-309">§</a>
</div>
<p>The map of conversions from CoffeeScript to JavaScript symbols.</p>
</div>
<div class="content"><div class='highlight'><pre> CONVERSIONS =
<span class="hljs-string">'=='</span>: <span class="hljs-string">'==='</span>
<span class="hljs-string">'!='</span>: <span class="hljs-string">'!=='</span>
<span class="hljs-string">'of'</span>: <span class="hljs-string">'in'</span>
<span class="hljs-string">'yieldfrom'</span>: <span class="hljs-string">'yield*'</span></pre></div></div>
</li>
<li id="section-310">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-310">§</a>
</div>
<p>The map of invertible operators.</p>
</div>
<div class="content"><div class='highlight'><pre> INVERSIONS =
<span class="hljs-string">'!=='</span>: <span class="hljs-string">'==='</span>
<span class="hljs-string">'==='</span>: <span class="hljs-string">'!=='</span>
children: [<span class="hljs-string">'first'</span>, <span class="hljs-string">'second'</span>]
isNumber: <span class="hljs-function">-></span>
@isUnary() <span class="hljs-keyword">and</span> @operator <span class="hljs-keyword">in</span> [<span class="hljs-string">'+'</span>, <span class="hljs-string">'-'</span>] <span class="hljs-keyword">and</span>
@first <span class="hljs-keyword">instanceof</span> Value <span class="hljs-keyword">and</span> @first.isNumber()
isAwait: <span class="hljs-function">-></span>
@operator <span class="hljs-keyword">is</span> <span class="hljs-string">'await'</span>
isYield: <span class="hljs-function">-></span>
@operator <span class="hljs-keyword">in</span> [<span class="hljs-string">'yield'</span>, <span class="hljs-string">'yield*'</span>]
isUnary: <span class="hljs-function">-></span>
<span class="hljs-keyword">not</span> @second
shouldCache: <span class="hljs-function">-></span>
<span class="hljs-keyword">not</span> @isNumber()</pre></div></div>
</li>
<li id="section-311">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-311">§</a>
</div>
<p>Am I capable of
<a href="https://docs.python.org/3/reference/expressions.html#not-in">Python-style comparison chaining</a>?</p>
</div>
<div class="content"><div class='highlight'><pre> isChainable: <span class="hljs-function">-></span>
@operator <span class="hljs-keyword">in</span> [<span class="hljs-string">'<'</span>, <span class="hljs-string">'>'</span>, <span class="hljs-string">'>='</span>, <span class="hljs-string">'<='</span>, <span class="hljs-string">'==='</span>, <span class="hljs-string">'!=='</span>]
isChain: <span class="hljs-function">-></span>
@isChainable() <span class="hljs-keyword">and</span> @first.isChainable()
invert: <span class="hljs-function">-></span>
<span class="hljs-keyword">if</span> @isInOperator()
@invertOperator = <span class="hljs-string">'!'</span>
<span class="hljs-keyword">return</span> @
<span class="hljs-keyword">if</span> @isChain()
allInvertable = <span class="hljs-literal">yes</span>
curr = this
<span class="hljs-keyword">while</span> curr <span class="hljs-keyword">and</span> curr.operator
allInvertable <span class="hljs-keyword">and</span>= (curr.operator <span class="hljs-keyword">of</span> INVERSIONS)
curr = curr.first
<span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> Parens(this).invert() <span class="hljs-keyword">unless</span> allInvertable
curr = this
<span class="hljs-keyword">while</span> curr <span class="hljs-keyword">and</span> curr.operator
curr.invert = !curr.invert
curr.operator = INVERSIONS[curr.operator]
curr = curr.first
this
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> op = INVERSIONS[@operator]
@operator = op
<span class="hljs-keyword">if</span> @first.unwrap() <span class="hljs-keyword">instanceof</span> Op
@first.invert()
this
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> @second
<span class="hljs-keyword">new</span> Parens(this).invert()
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> @operator <span class="hljs-keyword">is</span> <span class="hljs-string">'!'</span> <span class="hljs-keyword">and</span> (fst = @first.unwrap()) <span class="hljs-keyword">instanceof</span> Op <span class="hljs-keyword">and</span>
fst.operator <span class="hljs-keyword">in</span> [<span class="hljs-string">'!'</span>, <span class="hljs-string">'in'</span>, <span class="hljs-string">'instanceof'</span>]
fst
<span class="hljs-keyword">else</span>
<span class="hljs-keyword">new</span> Op <span class="hljs-string">'!'</span>, this
unfoldSoak: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
@operator <span class="hljs-keyword">in</span> [<span class="hljs-string">'++'</span>, <span class="hljs-string">'--'</span>, <span class="hljs-string">'delete'</span>] <span class="hljs-keyword">and</span> unfoldSoak o, this, <span class="hljs-string">'first'</span>
generateDo: <span class="hljs-function"><span class="hljs-params">(exp)</span> -></span>
passedParams = []
func = <span class="hljs-keyword">if</span> exp <span class="hljs-keyword">instanceof</span> Assign <span class="hljs-keyword">and</span> (ref = exp.value.unwrap()) <span class="hljs-keyword">instanceof</span> Code
ref
<span class="hljs-keyword">else</span>
exp
<span class="hljs-keyword">for</span> param <span class="hljs-keyword">in</span> func.params <span class="hljs-keyword">or</span> []
<span class="hljs-keyword">if</span> param.value
passedParams.push param.value
<span class="hljs-keyword">delete</span> param.value
<span class="hljs-keyword">else</span>
passedParams.push param
call = <span class="hljs-keyword">new</span> Call exp, passedParams
call.<span class="hljs-keyword">do</span> = <span class="hljs-literal">yes</span>
call
isInOperator: <span class="hljs-function">-></span>
@originalOperator <span class="hljs-keyword">is</span> <span class="hljs-string">'in'</span>
compileNode: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
<span class="hljs-keyword">if</span> @isInOperator()
inNode = <span class="hljs-keyword">new</span> In @first, @second
<span class="hljs-keyword">return</span> (<span class="hljs-keyword">if</span> @invertOperator <span class="hljs-keyword">then</span> inNode.invert() <span class="hljs-keyword">else</span> inNode).compileNode o
<span class="hljs-keyword">if</span> @invertOperator
@invertOperator = <span class="hljs-literal">null</span>
<span class="hljs-keyword">return</span> @invert().compileNode(o)
<span class="hljs-keyword">return</span> Op::generateDo(@first).compileNode o <span class="hljs-keyword">if</span> @operator <span class="hljs-keyword">is</span> <span class="hljs-string">'do'</span>
isChain = @isChain()</pre></div></div>
</li>
<li id="section-312">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-312">§</a>
</div>
<p>In chains, there’s no need to wrap bare obj literals in parens,
as the chained expression is wrapped.</p>
</div>
<div class="content"><div class='highlight'><pre> @first.front = @front <span class="hljs-keyword">unless</span> isChain
@checkDeleteOperand o
<span class="hljs-keyword">return</span> @compileContinuation o <span class="hljs-keyword">if</span> @isYield() <span class="hljs-keyword">or</span> @isAwait()
<span class="hljs-keyword">return</span> @compileUnary o <span class="hljs-keyword">if</span> @isUnary()
<span class="hljs-keyword">return</span> @compileChain o <span class="hljs-keyword">if</span> isChain
<span class="hljs-keyword">switch</span> @operator
<span class="hljs-keyword">when</span> <span class="hljs-string">'?'</span> <span class="hljs-keyword">then</span> @compileExistence o, @second.isDefaultValue
<span class="hljs-keyword">when</span> <span class="hljs-string">'//'</span> <span class="hljs-keyword">then</span> @compileFloorDivision o
<span class="hljs-keyword">when</span> <span class="hljs-string">'%%'</span> <span class="hljs-keyword">then</span> @compileModulo o
<span class="hljs-keyword">else</span>
lhs = @first.compileToFragments o, LEVEL_OP
rhs = @second.compileToFragments o, LEVEL_OP
answer = [].concat lhs, @makeCode(<span class="hljs-string">" <span class="hljs-subst">#{@operator}</span> "</span>), rhs
<span class="hljs-keyword">if</span> o.level <= LEVEL_OP <span class="hljs-keyword">then</span> answer <span class="hljs-keyword">else</span> @wrapInParentheses answer</pre></div></div>
</li>
<li id="section-313">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-313">§</a>
</div>
<p>Mimic Python’s chained comparisons when multiple comparison operators are
used sequentially. For example:</p>
<pre><code>bin/coffee -e <span class="hljs-string">'console.log 50 < 65 > 10'</span>
<span class="hljs-literal">true</span>
</code></pre>
</div>
<div class="content"><div class='highlight'><pre> compileChain: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
[@first.second, shared] = @first.second.cache o
fst = @first.compileToFragments o, LEVEL_OP
fragments = fst.concat @makeCode(<span class="hljs-string">" <span class="hljs-subst">#{<span class="hljs-keyword">if</span> @invert <span class="hljs-keyword">then</span> <span class="hljs-string">'&&'</span> <span class="hljs-keyword">else</span> <span class="hljs-string">'||'</span>}</span> "</span>),
(shared.compileToFragments o), @makeCode(<span class="hljs-string">" <span class="hljs-subst">#{@operator}</span> "</span>), (@second.compileToFragments o, LEVEL_OP)
@wrapInParentheses fragments</pre></div></div>
</li>
<li id="section-314">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-314">§</a>
</div>
<p>Keep reference to the left expression, unless this an existential assignment</p>
</div>
<div class="content"><div class='highlight'><pre> compileExistence: <span class="hljs-function"><span class="hljs-params">(o, checkOnlyUndefined)</span> -></span>
<span class="hljs-keyword">if</span> @first.shouldCache()
ref = <span class="hljs-keyword">new</span> IdentifierLiteral o.scope.freeVariable <span class="hljs-string">'ref'</span>
fst = <span class="hljs-keyword">new</span> Parens <span class="hljs-keyword">new</span> Assign ref, @first
<span class="hljs-keyword">else</span>
fst = @first
ref = fst
<span class="hljs-keyword">new</span> If(<span class="hljs-keyword">new</span> Existence(fst, checkOnlyUndefined), ref, type: <span class="hljs-string">'if'</span>).addElse(@second).compileToFragments o</pre></div></div>
</li>
<li id="section-315">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-315">§</a>
</div>
<p>Compile a unary <strong>Op</strong>.</p>
</div>
<div class="content"><div class='highlight'><pre> compileUnary: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
parts = []
op = @operator
parts.push [@makeCode op]
<span class="hljs-keyword">if</span> op <span class="hljs-keyword">is</span> <span class="hljs-string">'!'</span> <span class="hljs-keyword">and</span> @first <span class="hljs-keyword">instanceof</span> Existence
@first.negated = <span class="hljs-keyword">not</span> @first.negated
<span class="hljs-keyword">return</span> @first.compileToFragments o
<span class="hljs-keyword">if</span> o.level >= LEVEL_ACCESS
<span class="hljs-keyword">return</span> (<span class="hljs-keyword">new</span> Parens this).compileToFragments o
plusMinus = op <span class="hljs-keyword">in</span> [<span class="hljs-string">'+'</span>, <span class="hljs-string">'-'</span>]
parts.push [@makeCode(<span class="hljs-string">' '</span>)] <span class="hljs-keyword">if</span> op <span class="hljs-keyword">in</span> [<span class="hljs-string">'typeof'</span>, <span class="hljs-string">'delete'</span>] <span class="hljs-keyword">or</span>
plusMinus <span class="hljs-keyword">and</span> @first <span class="hljs-keyword">instanceof</span> Op <span class="hljs-keyword">and</span> @first.operator <span class="hljs-keyword">is</span> op
<span class="hljs-keyword">if</span> plusMinus <span class="hljs-keyword">and</span> @first <span class="hljs-keyword">instanceof</span> Op
@first = <span class="hljs-keyword">new</span> Parens @first
parts.push @first.compileToFragments o, LEVEL_OP
parts.reverse() <span class="hljs-keyword">if</span> @flip
@joinFragmentArrays parts, <span class="hljs-string">''</span>
compileContinuation: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
parts = []
op = @operator
@checkContinuation o <span class="hljs-keyword">unless</span> @isAwait()
<span class="hljs-keyword">if</span> <span class="hljs-string">'expression'</span> <span class="hljs-keyword">in</span> <span class="hljs-built_in">Object</span>.keys(@first) <span class="hljs-keyword">and</span> <span class="hljs-keyword">not</span> (@first <span class="hljs-keyword">instanceof</span> Throw)
parts.push @first.expression.compileToFragments o, LEVEL_OP <span class="hljs-keyword">if</span> @first.expression?
<span class="hljs-keyword">else</span>
parts.push [@makeCode <span class="hljs-string">"("</span>] <span class="hljs-keyword">if</span> o.level >= LEVEL_PAREN
parts.push [@makeCode op]
parts.push [@makeCode <span class="hljs-string">" "</span>] <span class="hljs-keyword">if</span> @first.base?.value <span class="hljs-keyword">isnt</span> <span class="hljs-string">''</span>
parts.push @first.compileToFragments o, LEVEL_OP
parts.push [@makeCode <span class="hljs-string">")"</span>] <span class="hljs-keyword">if</span> o.level >= LEVEL_PAREN
@joinFragmentArrays parts, <span class="hljs-string">''</span>
checkContinuation: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
<span class="hljs-keyword">unless</span> o.scope.parent?
@error <span class="hljs-string">"<span class="hljs-subst">#{@operator}</span> can only occur inside functions"</span>
<span class="hljs-keyword">if</span> o.scope.method?.bound <span class="hljs-keyword">and</span> o.scope.method.isGenerator
@error <span class="hljs-string">'yield cannot occur inside bound (fat arrow) functions'</span>
compileFloorDivision: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
floor = <span class="hljs-keyword">new</span> Value <span class="hljs-keyword">new</span> IdentifierLiteral(<span class="hljs-string">'Math'</span>), [<span class="hljs-keyword">new</span> Access <span class="hljs-keyword">new</span> PropertyName <span class="hljs-string">'floor'</span>]
second = <span class="hljs-keyword">if</span> @second.shouldCache() <span class="hljs-keyword">then</span> <span class="hljs-keyword">new</span> Parens @second <span class="hljs-keyword">else</span> @second
div = <span class="hljs-keyword">new</span> Op <span class="hljs-string">'/'</span>, @first, second
<span class="hljs-keyword">new</span> Call(floor, [div]).compileToFragments o
compileModulo: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
mod = <span class="hljs-keyword">new</span> Value <span class="hljs-keyword">new</span> Literal utility <span class="hljs-string">'modulo'</span>, o
<span class="hljs-keyword">new</span> Call(mod, [@first, @second]).compileToFragments o
toString: <span class="hljs-function"><span class="hljs-params">(idt)</span> -></span>
super idt, @constructor.name + <span class="hljs-string">' '</span> + @operator
checkDeleteOperand: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
<span class="hljs-keyword">if</span> @operator <span class="hljs-keyword">is</span> <span class="hljs-string">'delete'</span> <span class="hljs-keyword">and</span> o.scope.check(@first.unwrapAll().value)
@error <span class="hljs-string">'delete operand may not be argument or var'</span>
astNode: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
@checkContinuation o <span class="hljs-keyword">if</span> @isYield()
@checkDeleteOperand o
super o
astType: <span class="hljs-function">-></span>
<span class="hljs-keyword">return</span> <span class="hljs-string">'AwaitExpression'</span> <span class="hljs-keyword">if</span> @isAwait()
<span class="hljs-keyword">return</span> <span class="hljs-string">'YieldExpression'</span> <span class="hljs-keyword">if</span> @isYield()
<span class="hljs-keyword">return</span> <span class="hljs-string">'ChainedComparison'</span> <span class="hljs-keyword">if</span> @isChain()
<span class="hljs-keyword">switch</span> @operator
<span class="hljs-keyword">when</span> <span class="hljs-string">'||'</span>, <span class="hljs-string">'&&'</span>, <span class="hljs-string">'?'</span> <span class="hljs-keyword">then</span> <span class="hljs-string">'LogicalExpression'</span>
<span class="hljs-keyword">when</span> <span class="hljs-string">'++'</span>, <span class="hljs-string">'--'</span> <span class="hljs-keyword">then</span> <span class="hljs-string">'UpdateExpression'</span>
<span class="hljs-keyword">else</span>
<span class="hljs-keyword">if</span> @isUnary() <span class="hljs-keyword">then</span> <span class="hljs-string">'UnaryExpression'</span>
<span class="hljs-keyword">else</span> <span class="hljs-string">'BinaryExpression'</span>
operatorAst: <span class="hljs-function">-></span>
<span class="hljs-string">"<span class="hljs-subst">#{<span class="hljs-keyword">if</span> @invertOperator <span class="hljs-keyword">then</span> <span class="hljs-string">"<span class="hljs-subst">#{@invertOperator}</span> "</span> <span class="hljs-keyword">else</span> <span class="hljs-string">''</span>}</span><span class="hljs-subst">#{@originalOperator}</span>"</span>
chainAstProperties: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
operators = [@operatorAst()]
operands = [@second]
currentOp = @first
<span class="hljs-keyword">loop</span>
operators.unshift currentOp.operatorAst()
operands.unshift currentOp.second
currentOp = currentOp.first
<span class="hljs-keyword">unless</span> currentOp.isChainable()
operands.unshift currentOp
<span class="hljs-keyword">break</span>
<span class="hljs-keyword">return</span> {
operators
operands: (operand.ast(o, LEVEL_OP) <span class="hljs-keyword">for</span> operand <span class="hljs-keyword">in</span> operands)
}
astProperties: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
<span class="hljs-keyword">return</span> @chainAstProperties(o) <span class="hljs-keyword">if</span> @isChain()
firstAst = @first.ast o, LEVEL_OP
secondAst = @second?.ast o, LEVEL_OP
operatorAst = @operatorAst()
<span class="hljs-keyword">switch</span>
<span class="hljs-keyword">when</span> @isUnary()
argument =
<span class="hljs-keyword">if</span> @isYield() <span class="hljs-keyword">and</span> @first.unwrap().value <span class="hljs-keyword">is</span> <span class="hljs-string">''</span>
<span class="hljs-literal">null</span>
<span class="hljs-keyword">else</span>
firstAst
<span class="hljs-keyword">return</span> {argument} <span class="hljs-keyword">if</span> @isAwait()
<span class="hljs-keyword">return</span> {
argument
delegate: @operator <span class="hljs-keyword">is</span> <span class="hljs-string">'yield*'</span>
} <span class="hljs-keyword">if</span> @isYield()
<span class="hljs-keyword">return</span> {
argument
operator: operatorAst
prefix: !@flip
}
<span class="hljs-keyword">else</span>
<span class="hljs-keyword">return</span>
left: firstAst
right: secondAst
operator: operatorAst</pre></div></div>
</li>
<li id="section-316">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-316">§</a>
</div>
<h3 id="in">In</h3>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-built_in">exports</span>.In = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">In</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Base</span></span>
constructor: <span class="hljs-function"><span class="hljs-params">(@object, @array)</span> -></span>
super()
children: [<span class="hljs-string">'object'</span>, <span class="hljs-string">'array'</span>]
invert: NEGATE
compileNode: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
<span class="hljs-keyword">if</span> @array <span class="hljs-keyword">instanceof</span> Value <span class="hljs-keyword">and</span> @array.isArray() <span class="hljs-keyword">and</span> @array.base.objects.length
<span class="hljs-keyword">for</span> obj <span class="hljs-keyword">in</span> @array.base.objects <span class="hljs-keyword">when</span> obj <span class="hljs-keyword">instanceof</span> Splat
hasSplat = <span class="hljs-literal">yes</span>
<span class="hljs-keyword">break</span></pre></div></div>
</li>
<li id="section-317">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-317">§</a>
</div>
<p><code>compileOrTest</code> only if we have an array literal with no splats</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">return</span> @compileOrTest o <span class="hljs-keyword">unless</span> hasSplat
@compileLoopTest o
compileOrTest: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
[sub, ref] = @object.cache o, LEVEL_OP
[cmp, cnj] = <span class="hljs-keyword">if</span> @negated <span class="hljs-keyword">then</span> [<span class="hljs-string">' !== '</span>, <span class="hljs-string">' && '</span>] <span class="hljs-keyword">else</span> [<span class="hljs-string">' === '</span>, <span class="hljs-string">' || '</span>]
tests = []
<span class="hljs-keyword">for</span> item, i <span class="hljs-keyword">in</span> @array.base.objects
<span class="hljs-keyword">if</span> i <span class="hljs-keyword">then</span> tests.push @makeCode cnj
tests = tests.concat (<span class="hljs-keyword">if</span> i <span class="hljs-keyword">then</span> ref <span class="hljs-keyword">else</span> sub), @makeCode(cmp), item.compileToFragments(o, LEVEL_ACCESS)
<span class="hljs-keyword">if</span> o.level < LEVEL_OP <span class="hljs-keyword">then</span> tests <span class="hljs-keyword">else</span> @wrapInParentheses tests
compileLoopTest: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
[sub, ref] = @object.cache o, LEVEL_LIST
fragments = [].concat @makeCode(utility(<span class="hljs-string">'indexOf'</span>, o) + <span class="hljs-string">".call("</span>), @array.compileToFragments(o, LEVEL_LIST),
@makeCode(<span class="hljs-string">", "</span>), ref, @makeCode(<span class="hljs-string">") "</span> + <span class="hljs-keyword">if</span> @negated <span class="hljs-keyword">then</span> <span class="hljs-string">'< 0'</span> <span class="hljs-keyword">else</span> <span class="hljs-string">'>= 0'</span>)
<span class="hljs-keyword">return</span> fragments <span class="hljs-keyword">if</span> fragmentsToText(sub) <span class="hljs-keyword">is</span> fragmentsToText(ref)
fragments = sub.concat @makeCode(<span class="hljs-string">', '</span>), fragments
<span class="hljs-keyword">if</span> o.level < LEVEL_LIST <span class="hljs-keyword">then</span> fragments <span class="hljs-keyword">else</span> @wrapInParentheses fragments
toString: <span class="hljs-function"><span class="hljs-params">(idt)</span> -></span>
super idt, @constructor.name + <span class="hljs-keyword">if</span> @negated <span class="hljs-keyword">then</span> <span class="hljs-string">'!'</span> <span class="hljs-keyword">else</span> <span class="hljs-string">''</span></pre></div></div>
</li>
<li id="section-318">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-318">§</a>
</div>
<h3 id="try">Try</h3>
</div>
</li>
<li id="section-319">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-319">§</a>
</div>
<p>A classic <em>try/catch/finally</em> block.</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-built_in">exports</span>.Try = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Try</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Base</span></span>
constructor: <span class="hljs-function"><span class="hljs-params">(@attempt, @catch, @ensure, @finallyTag)</span> -></span>
super()
children: [<span class="hljs-string">'attempt'</span>, <span class="hljs-string">'catch'</span>, <span class="hljs-string">'ensure'</span>]
isStatement: YES
jumps: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span> @attempt.jumps(o) <span class="hljs-keyword">or</span> @catch?.jumps(o)
makeReturn: <span class="hljs-function"><span class="hljs-params">(results, mark)</span> -></span>
<span class="hljs-keyword">if</span> mark
@attempt?.makeReturn results, mark
@catch?.makeReturn results, mark
<span class="hljs-keyword">return</span>
@attempt = @attempt.makeReturn results <span class="hljs-keyword">if</span> @attempt
@catch = @catch .makeReturn results <span class="hljs-keyword">if</span> @catch
this</pre></div></div>
</li>
<li id="section-320">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-320">§</a>
</div>
<p>Compilation is more or less as you would expect – the <em>finally</em> clause
is optional, the <em>catch</em> is not.</p>
</div>
<div class="content"><div class='highlight'><pre> compileNode: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
originalIndent = o.indent
o.indent += TAB
tryPart = @attempt.compileToFragments o, LEVEL_TOP
catchPart = <span class="hljs-keyword">if</span> @catch
@catch.compileToFragments merge(o, indent: originalIndent), LEVEL_TOP
<span class="hljs-keyword">else</span> <span class="hljs-keyword">unless</span> @ensure <span class="hljs-keyword">or</span> @catch
generatedErrorVariableName = o.scope.freeVariable <span class="hljs-string">'error'</span>, reserve: <span class="hljs-literal">no</span>
[@makeCode(<span class="hljs-string">" catch (<span class="hljs-subst">#{generatedErrorVariableName}</span>) {}"</span>)]
<span class="hljs-keyword">else</span>
[]
ensurePart = <span class="hljs-keyword">if</span> @ensure <span class="hljs-keyword">then</span> ([].concat @makeCode(<span class="hljs-string">" finally {\n"</span>), @ensure.compileToFragments(o, LEVEL_TOP),
@makeCode(<span class="hljs-string">"\n<span class="hljs-subst">#{@tab}</span>}"</span>)) <span class="hljs-keyword">else</span> []
[].concat @makeCode(<span class="hljs-string">"<span class="hljs-subst">#{@tab}</span>try {\n"</span>),
tryPart,
@makeCode(<span class="hljs-string">"\n<span class="hljs-subst">#{@tab}</span>}"</span>), catchPart, ensurePart
astType: <span class="hljs-function">-></span> <span class="hljs-string">'TryStatement'</span>
astProperties: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
<span class="hljs-keyword">return</span>
block: @attempt.ast o, LEVEL_TOP
handler: @catch?.ast(o) ? <span class="hljs-literal">null</span>
finalizer:
<span class="hljs-keyword">if</span> @ensure?
<span class="hljs-built_in">Object</span>.assign @ensure.ast(o, LEVEL_TOP),</pre></div></div>
</li>
<li id="section-321">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-321">§</a>
</div>
<p>Include <code>finally</code> keyword in location data.</p>
</div>
<div class="content"><div class='highlight'><pre> mergeAstLocationData(
jisonLocationDataToAstLocationData(@finallyTag.locationData),
@ensure.astLocationData()
)
<span class="hljs-keyword">else</span>
<span class="hljs-literal">null</span>
<span class="hljs-built_in">exports</span>.Catch = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Catch</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Base</span></span>
constructor: <span class="hljs-function"><span class="hljs-params">(@recovery, @errorVariable)</span> -></span>
super()
@errorVariable?.unwrap().propagateLhs? <span class="hljs-literal">yes</span>
children: [<span class="hljs-string">'recovery'</span>, <span class="hljs-string">'errorVariable'</span>]
isStatement: YES
jumps: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span> @recovery.jumps o
makeReturn: <span class="hljs-function"><span class="hljs-params">(results, mark)</span> -></span>
ret = @recovery.makeReturn results, mark
<span class="hljs-keyword">return</span> <span class="hljs-keyword">if</span> mark
@recovery = ret
this
compileNode: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
o.indent += TAB
generatedErrorVariableName = o.scope.freeVariable <span class="hljs-string">'error'</span>, reserve: <span class="hljs-literal">no</span>
placeholder = <span class="hljs-keyword">new</span> IdentifierLiteral generatedErrorVariableName
@checkUnassignable()
<span class="hljs-keyword">if</span> @errorVariable
@recovery.unshift <span class="hljs-keyword">new</span> Assign @errorVariable, placeholder
[].concat @makeCode(<span class="hljs-string">" catch ("</span>), placeholder.compileToFragments(o), @makeCode(<span class="hljs-string">") {\n"</span>),
@recovery.compileToFragments(o, LEVEL_TOP), @makeCode(<span class="hljs-string">"\n<span class="hljs-subst">#{@tab}</span>}"</span>)
checkUnassignable: <span class="hljs-function">-></span>
<span class="hljs-keyword">if</span> @errorVariable
message = isUnassignable @errorVariable.unwrapAll().value
@errorVariable.error message <span class="hljs-keyword">if</span> message
astNode: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
@checkUnassignable()
@errorVariable?.eachName (name) ->
alreadyDeclared = o.scope.find name.value
name.isDeclaration = <span class="hljs-keyword">not</span> alreadyDeclared
super o
astType: <span class="hljs-function">-></span> <span class="hljs-string">'CatchClause'</span>
astProperties: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
<span class="hljs-keyword">return</span>
param: @errorVariable?.ast(o) ? <span class="hljs-literal">null</span>
body: @recovery.ast o, LEVEL_TOP</pre></div></div>
</li>
<li id="section-322">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-322">§</a>
</div>
<h3 id="throw">Throw</h3>
</div>
</li>
<li id="section-323">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-323">§</a>
</div>
<p>Simple node to throw an exception.</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-built_in">exports</span>.Throw = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Throw</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Base</span></span>
constructor: <span class="hljs-function"><span class="hljs-params">(@expression)</span> -></span>
super()
children: [<span class="hljs-string">'expression'</span>]
isStatement: YES
jumps: NO</pre></div></div>
</li>
<li id="section-324">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-324">§</a>
</div>
<p>A <strong>Throw</strong> is already a return, of sorts…</p>
</div>
<div class="content"><div class='highlight'><pre> makeReturn: THIS
compileNode: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
fragments = @expression.compileToFragments o, LEVEL_LIST
unshiftAfterComments fragments, @makeCode <span class="hljs-string">'throw '</span>
fragments.unshift @makeCode @tab
fragments.push @makeCode <span class="hljs-string">';'</span>
fragments
astType: <span class="hljs-function">-></span> <span class="hljs-string">'ThrowStatement'</span>
astProperties: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
<span class="hljs-keyword">return</span>
argument: @expression.ast o, LEVEL_LIST</pre></div></div>
</li>
<li id="section-325">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-325">§</a>
</div>
<h3 id="existence">Existence</h3>
</div>
</li>
<li id="section-326">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-326">§</a>
</div>
<p>Checks a variable for existence – not <code>null</code> and not <code>undefined</code>. This is
similar to <code>.nil?</code> in Ruby, and avoids having to consult a JavaScript truth
table. Optionally only check if a variable is not <code>undefined</code>.</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-built_in">exports</span>.Existence = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Existence</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Base</span></span>
constructor: <span class="hljs-function"><span class="hljs-params">(@expression, onlyNotUndefined = <span class="hljs-literal">no</span>)</span> -></span>
super()
@comparisonTarget = <span class="hljs-keyword">if</span> onlyNotUndefined <span class="hljs-keyword">then</span> <span class="hljs-string">'undefined'</span> <span class="hljs-keyword">else</span> <span class="hljs-string">'null'</span>
salvagedComments = []
@expression.traverseChildren <span class="hljs-literal">yes</span>, <span class="hljs-function"><span class="hljs-params">(child)</span> -></span>
<span class="hljs-keyword">if</span> child.comments
<span class="hljs-keyword">for</span> comment <span class="hljs-keyword">in</span> child.comments
salvagedComments.push comment <span class="hljs-keyword">unless</span> comment <span class="hljs-keyword">in</span> salvagedComments
<span class="hljs-keyword">delete</span> child.comments
attachCommentsToNode salvagedComments, @
moveComments @expression, @
children: [<span class="hljs-string">'expression'</span>]
invert: NEGATE
compileNode: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
@expression.front = @front
code = @expression.compile o, LEVEL_OP
<span class="hljs-keyword">if</span> @expression.unwrap() <span class="hljs-keyword">instanceof</span> IdentifierLiteral <span class="hljs-keyword">and</span> <span class="hljs-keyword">not</span> o.scope.check code
[cmp, cnj] = <span class="hljs-keyword">if</span> @negated <span class="hljs-keyword">then</span> [<span class="hljs-string">'==='</span>, <span class="hljs-string">'||'</span>] <span class="hljs-keyword">else</span> [<span class="hljs-string">'!=='</span>, <span class="hljs-string">'&&'</span>]
code = <span class="hljs-string">"typeof <span class="hljs-subst">#{code}</span> <span class="hljs-subst">#{cmp}</span> \"undefined\""</span> + <span class="hljs-keyword">if</span> @comparisonTarget <span class="hljs-keyword">isnt</span> <span class="hljs-string">'undefined'</span> <span class="hljs-keyword">then</span> <span class="hljs-string">" <span class="hljs-subst">#{cnj}</span> <span class="hljs-subst">#{code}</span> <span class="hljs-subst">#{cmp}</span> <span class="hljs-subst">#{@comparisonTarget}</span>"</span> <span class="hljs-keyword">else</span> <span class="hljs-string">''</span>
<span class="hljs-keyword">else</span></pre></div></div>
</li>
<li id="section-327">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-327">§</a>
</div>
<p>We explicity want to use loose equality (<code>==</code>) when comparing against <code>null</code>,
so that an existence check roughly corresponds to a check for truthiness.
Do <em>not</em> change this to <code>===</code> for <code>null</code>, as this will break mountains of
existing code. When comparing only against <code>undefined</code>, however, we want to
use <code>===</code> because this use case is for parity with ES2015+ default values,
which only get assigned when the variable is <code>undefined</code> (but not <code>null</code>).</p>
</div>
<div class="content"><div class='highlight'><pre> cmp = <span class="hljs-keyword">if</span> @comparisonTarget <span class="hljs-keyword">is</span> <span class="hljs-string">'null'</span>
<span class="hljs-keyword">if</span> @negated <span class="hljs-keyword">then</span> <span class="hljs-string">'=='</span> <span class="hljs-keyword">else</span> <span class="hljs-string">'!='</span>
<span class="hljs-keyword">else</span> <span class="hljs-comment"># `undefined`</span>
<span class="hljs-keyword">if</span> @negated <span class="hljs-keyword">then</span> <span class="hljs-string">'==='</span> <span class="hljs-keyword">else</span> <span class="hljs-string">'!=='</span>
code = <span class="hljs-string">"<span class="hljs-subst">#{code}</span> <span class="hljs-subst">#{cmp}</span> <span class="hljs-subst">#{@comparisonTarget}</span>"</span>
[@makeCode(<span class="hljs-keyword">if</span> o.level <= LEVEL_COND <span class="hljs-keyword">then</span> code <span class="hljs-keyword">else</span> <span class="hljs-string">"(<span class="hljs-subst">#{code}</span>)"</span>)]
astType: <span class="hljs-function">-></span> <span class="hljs-string">'UnaryExpression'</span>
astProperties: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
<span class="hljs-keyword">return</span>
argument: @expression.ast o
operator: <span class="hljs-string">'?'</span>
prefix: <span class="hljs-literal">no</span></pre></div></div>
</li>
<li id="section-328">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-328">§</a>
</div>
<h3 id="parens">Parens</h3>
</div>
</li>
<li id="section-329">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-329">§</a>
</div>
<p>An extra set of parentheses, specified explicitly in the source. At one time
we tried to clean up the results by detecting and removing redundant
parentheses, but no longer – you can put in as many as you please.</p>
<p>Parentheses are a good way to force any statement to become an expression.</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-built_in">exports</span>.Parens = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Parens</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Base</span></span>
constructor: <span class="hljs-function"><span class="hljs-params">(@body)</span> -></span>
super()
children: [<span class="hljs-string">'body'</span>]
unwrap: <span class="hljs-function">-></span> @body
shouldCache: <span class="hljs-function">-></span> @body.shouldCache()
compileNode: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
expr = @body.unwrap()</pre></div></div>
</li>
<li id="section-330">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-330">§</a>
</div>
<p>If these parentheses are wrapping an <code>IdentifierLiteral</code> followed by a
block comment, output the parentheses (or put another way, don’t optimize
away these redundant parentheses). This is because Flow requires
parentheses in certain circumstances to distinguish identifiers followed
by comment-based type annotations from JavaScript labels.</p>
</div>
<div class="content"><div class='highlight'><pre> shouldWrapComment = expr.comments?.some(
<span class="hljs-function"><span class="hljs-params">(comment)</span> -></span> comment.here <span class="hljs-keyword">and</span> <span class="hljs-keyword">not</span> comment.unshift <span class="hljs-keyword">and</span> <span class="hljs-keyword">not</span> comment.newLine)
<span class="hljs-keyword">if</span> expr <span class="hljs-keyword">instanceof</span> Value <span class="hljs-keyword">and</span> expr.isAtomic() <span class="hljs-keyword">and</span> <span class="hljs-keyword">not</span> @jsxAttribute <span class="hljs-keyword">and</span> <span class="hljs-keyword">not</span> shouldWrapComment
expr.front = @front
<span class="hljs-keyword">return</span> expr.compileToFragments o
fragments = expr.compileToFragments o, LEVEL_PAREN
bare = o.level < LEVEL_OP <span class="hljs-keyword">and</span> <span class="hljs-keyword">not</span> shouldWrapComment <span class="hljs-keyword">and</span> (
expr <span class="hljs-keyword">instanceof</span> Op <span class="hljs-keyword">and</span> <span class="hljs-keyword">not</span> expr.isInOperator() <span class="hljs-keyword">or</span> expr.unwrap() <span class="hljs-keyword">instanceof</span> Call <span class="hljs-keyword">or</span>
(expr <span class="hljs-keyword">instanceof</span> For <span class="hljs-keyword">and</span> expr.returns)
) <span class="hljs-keyword">and</span> (o.level < LEVEL_COND <span class="hljs-keyword">or</span> fragments.length <= <span class="hljs-number">3</span>)
<span class="hljs-keyword">return</span> @wrapInBraces fragments <span class="hljs-keyword">if</span> @jsxAttribute
<span class="hljs-keyword">if</span> bare <span class="hljs-keyword">then</span> fragments <span class="hljs-keyword">else</span> @wrapInParentheses fragments
astNode: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span> @body.unwrap().ast o, LEVEL_PAREN</pre></div></div>
</li>
<li id="section-331">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-331">§</a>
</div>
<h3 id="stringwithinterpolations">StringWithInterpolations</h3>
</div>
<div class="content"><div class='highlight'><pre>
<span class="hljs-built_in">exports</span>.StringWithInterpolations = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">StringWithInterpolations</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Base</span></span>
constructor: <span class="hljs-function"><span class="hljs-params">(@body, {@quote, @startQuote, @jsxAttribute} = {})</span> -></span>
super()
@fromStringLiteral: <span class="hljs-function"><span class="hljs-params">(stringLiteral)</span> -></span>
updatedString = stringLiteral.withoutQuotesInLocationData()
updatedStringValue = <span class="hljs-keyword">new</span> Value(updatedString).withLocationDataFrom updatedString
<span class="hljs-keyword">new</span> StringWithInterpolations Block.wrap([updatedStringValue]), quote: stringLiteral.quote, jsxAttribute: stringLiteral.jsxAttribute
.withLocationDataFrom stringLiteral
children: [<span class="hljs-string">'body'</span>]</pre></div></div>
</li>
<li id="section-332">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-332">§</a>
</div>
<p><code>unwrap</code> returns <code>this</code> to stop ancestor nodes reaching in to grab @body,
and using @body.compileNode. <code>StringWithInterpolations.compileNode</code> is
<em>the</em> custom logic to output interpolated strings as code.</p>
</div>
<div class="content"><div class='highlight'><pre> unwrap: <span class="hljs-function">-></span> this
shouldCache: <span class="hljs-function">-></span> @body.shouldCache()
extractElements: <span class="hljs-function"><span class="hljs-params">(o, {includeInterpolationWrappers, isJsx} = {})</span> -></span></pre></div></div>
</li>
<li id="section-333">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-333">§</a>
</div>
<p>Assumes that <code>expr</code> is <code>Block</code></p>
</div>
<div class="content"><div class='highlight'><pre> expr = @body.unwrap()
elements = []
salvagedComments = []
expr.traverseChildren <span class="hljs-literal">no</span>, <span class="hljs-function"><span class="hljs-params">(node)</span> =></span>
<span class="hljs-keyword">if</span> node <span class="hljs-keyword">instanceof</span> StringLiteral
<span class="hljs-keyword">if</span> node.comments
salvagedComments.push node.comments...
<span class="hljs-keyword">delete</span> node.comments
elements.push node
<span class="hljs-keyword">return</span> <span class="hljs-literal">yes</span>
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> node <span class="hljs-keyword">instanceof</span> Interpolation
<span class="hljs-keyword">if</span> salvagedComments.length <span class="hljs-keyword">isnt</span> <span class="hljs-number">0</span>
<span class="hljs-keyword">for</span> comment <span class="hljs-keyword">in</span> salvagedComments
comment.unshift = <span class="hljs-literal">yes</span>
comment.newLine = <span class="hljs-literal">yes</span>
attachCommentsToNode salvagedComments, node
<span class="hljs-keyword">if</span> (unwrapped = node.expression?.unwrapAll()) <span class="hljs-keyword">instanceof</span> PassthroughLiteral <span class="hljs-keyword">and</span> unwrapped.generated <span class="hljs-keyword">and</span> <span class="hljs-keyword">not</span> (isJsx <span class="hljs-keyword">and</span> o.compiling)
<span class="hljs-keyword">if</span> o.compiling
commentPlaceholder = <span class="hljs-keyword">new</span> StringLiteral(<span class="hljs-string">''</span>).withLocationDataFrom node
commentPlaceholder.comments = unwrapped.comments
(commentPlaceholder.comments ?= []).push node.comments... <span class="hljs-keyword">if</span> node.comments
elements.push <span class="hljs-keyword">new</span> Value commentPlaceholder
<span class="hljs-keyword">else</span>
empty = <span class="hljs-keyword">new</span> Interpolation().withLocationDataFrom node
empty.comments = node.comments
elements.push empty
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> node.expression <span class="hljs-keyword">or</span> includeInterpolationWrappers
(node.expression?.comments ?= []).push node.comments... <span class="hljs-keyword">if</span> node.comments
elements.push <span class="hljs-keyword">if</span> includeInterpolationWrappers <span class="hljs-keyword">then</span> node <span class="hljs-keyword">else</span> node.expression
<span class="hljs-keyword">return</span> <span class="hljs-literal">no</span>
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> node.comments</pre></div></div>
</li>
<li id="section-334">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-334">§</a>
</div>
<p>This node is getting discarded, but salvage its comments.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> elements.length <span class="hljs-keyword">isnt</span> <span class="hljs-number">0</span> <span class="hljs-keyword">and</span> elements[elements.length - <span class="hljs-number">1</span>] <span class="hljs-keyword">not</span> <span class="hljs-keyword">instanceof</span> StringLiteral
<span class="hljs-keyword">for</span> comment <span class="hljs-keyword">in</span> node.comments
comment.unshift = <span class="hljs-literal">no</span>
comment.newLine = <span class="hljs-literal">yes</span>
attachCommentsToNode node.comments, elements[elements.length - <span class="hljs-number">1</span>]
<span class="hljs-keyword">else</span>
salvagedComments.push node.comments...
<span class="hljs-keyword">delete</span> node.comments
<span class="hljs-keyword">return</span> <span class="hljs-literal">yes</span>
elements
compileNode: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
@comments ?= @startQuote?.comments
<span class="hljs-keyword">if</span> @jsxAttribute
wrapped = <span class="hljs-keyword">new</span> Parens <span class="hljs-keyword">new</span> StringWithInterpolations @body
wrapped.jsxAttribute = <span class="hljs-literal">yes</span>
<span class="hljs-keyword">return</span> wrapped.compileNode o
elements = @extractElements o, isJsx: @jsx
fragments = []
fragments.push @makeCode <span class="hljs-string">'`'</span> <span class="hljs-keyword">unless</span> @jsx
<span class="hljs-keyword">for</span> element <span class="hljs-keyword">in</span> elements
<span class="hljs-keyword">if</span> element <span class="hljs-keyword">instanceof</span> StringLiteral
unquotedElementValue = <span class="hljs-keyword">if</span> @jsx <span class="hljs-keyword">then</span> element.unquotedValueForJSX <span class="hljs-keyword">else</span> element.unquotedValueForTemplateLiteral
fragments.push @makeCode unquotedElementValue
<span class="hljs-keyword">else</span>
fragments.push @makeCode <span class="hljs-string">'$'</span> <span class="hljs-keyword">unless</span> @jsx
code = element.compileToFragments(o, LEVEL_PAREN)
<span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> @isNestedTag(element) <span class="hljs-keyword">or</span>
code.some(<span class="hljs-function"><span class="hljs-params">(fragment)</span> -></span> fragment.comments?.some(<span class="hljs-function"><span class="hljs-params">(comment)</span> -></span> comment.here <span class="hljs-keyword">is</span> <span class="hljs-literal">no</span>))
code = @wrapInBraces code</pre></div></div>
</li>
<li id="section-335">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-335">§</a>
</div>
<p>Flag the <code>{</code> and <code>}</code> fragments as having been generated by this
<code>StringWithInterpolations</code> node, so that <code>compileComments</code> knows
to treat them as bounds. But the braces are unnecessary if all of
the enclosed comments are <code>/* */</code> comments. Don’t trust
<code>fragment.type</code>, which can report minified variable names when
this compiler is minified.</p>
</div>
<div class="content"><div class='highlight'><pre> code[<span class="hljs-number">0</span>].isStringWithInterpolations = <span class="hljs-literal">yes</span>
code[code.length - <span class="hljs-number">1</span>].isStringWithInterpolations = <span class="hljs-literal">yes</span>
fragments.push code...
fragments.push @makeCode <span class="hljs-string">'`'</span> <span class="hljs-keyword">unless</span> @jsx
fragments
isNestedTag: <span class="hljs-function"><span class="hljs-params">(element)</span> -></span>
call = element.unwrapAll?()
@jsx <span class="hljs-keyword">and</span> call <span class="hljs-keyword">instanceof</span> JSXElement
astType: <span class="hljs-function">-></span> <span class="hljs-string">'TemplateLiteral'</span>
astProperties: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
elements = @extractElements o, includeInterpolationWrappers: <span class="hljs-literal">yes</span>
[..., last] = elements
quasis = []
expressions = []
<span class="hljs-keyword">for</span> element, index <span class="hljs-keyword">in</span> elements
<span class="hljs-keyword">if</span> element <span class="hljs-keyword">instanceof</span> StringLiteral
quasis.push <span class="hljs-keyword">new</span> TemplateElement(
element.originalValue
tail: element <span class="hljs-keyword">is</span> last
).withLocationDataFrom(element).ast o
<span class="hljs-keyword">else</span> <span class="hljs-comment"># Interpolation</span>
{expression} = element
node =
<span class="hljs-keyword">unless</span> expression?
emptyInterpolation = <span class="hljs-keyword">new</span> EmptyInterpolation()
emptyInterpolation.locationData = emptyExpressionLocationData {
interpolationNode: element
openingBrace: <span class="hljs-string">'#{'</span>
closingBrace: <span class="hljs-string">'}'</span>
}
emptyInterpolation
<span class="hljs-keyword">else</span>
expression.unwrapAll()
expressions.push astAsBlockIfNeeded node, o
{expressions, quasis, @quote}
<span class="hljs-built_in">exports</span>.TemplateElement = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">TemplateElement</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Base</span></span>
constructor: <span class="hljs-function"><span class="hljs-params">(@value, {@tail} = {})</span> -></span>
super()
astProperties: <span class="hljs-function">-></span>
<span class="hljs-keyword">return</span>
value:
raw: @value
tail: !!@tail
<span class="hljs-built_in">exports</span>.Interpolation = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Interpolation</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Base</span></span>
constructor: <span class="hljs-function"><span class="hljs-params">(@expression)</span> -></span>
super()
children: [<span class="hljs-string">'expression'</span>]</pre></div></div>
</li>
<li id="section-336">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-336">§</a>
</div>
<p>Represents the contents of an empty interpolation (e.g. <code>#{}</code>).
Only used during AST generation.</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-built_in">exports</span>.EmptyInterpolation = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">EmptyInterpolation</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Base</span></span>
constructor: <span class="hljs-function">-></span>
super()</pre></div></div>
</li>
<li id="section-337">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-337">§</a>
</div>
<h3 id="for">For</h3>
</div>
</li>
<li id="section-338">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-338">§</a>
</div>
<p>CoffeeScript’s replacement for the <em>for</em> loop is our array and object
comprehensions, that compile into <em>for</em> loops here. They also act as an
expression, able to return the result of each filtered iteration.</p>
<p>Unlike Python array comprehensions, they can be multi-line, and you can pass
the current index of the loop as a second parameter. Unlike Ruby blocks,
you can map and filter in a single pass.</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-built_in">exports</span>.For = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">For</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">While</span></span>
constructor: <span class="hljs-function"><span class="hljs-params">(body, source)</span> -></span>
super()
@addBody body
@addSource source
children: [<span class="hljs-string">'body'</span>, <span class="hljs-string">'source'</span>, <span class="hljs-string">'guard'</span>, <span class="hljs-string">'step'</span>]
isAwait: <span class="hljs-function">-></span> @await ? <span class="hljs-literal">no</span>
addBody: <span class="hljs-function"><span class="hljs-params">(body)</span> -></span>
@body = Block.wrap [body]
{expressions} = @body
<span class="hljs-keyword">if</span> expressions.length
@body.locationData ?= mergeLocationData expressions[<span class="hljs-number">0</span>].locationData, expressions[expressions.length - <span class="hljs-number">1</span>].locationData
this
addSource: <span class="hljs-function"><span class="hljs-params">(source)</span> -></span>
{@source = <span class="hljs-literal">no</span>} = source
attribs = [<span class="hljs-string">"name"</span>, <span class="hljs-string">"index"</span>, <span class="hljs-string">"guard"</span>, <span class="hljs-string">"step"</span>, <span class="hljs-string">"own"</span>, <span class="hljs-string">"ownTag"</span>, <span class="hljs-string">"await"</span>, <span class="hljs-string">"awaitTag"</span>, <span class="hljs-string">"object"</span>, <span class="hljs-string">"from"</span>]
@[attr] = source[attr] ? @[attr] <span class="hljs-keyword">for</span> attr <span class="hljs-keyword">in</span> attribs
<span class="hljs-keyword">return</span> this <span class="hljs-keyword">unless</span> @source
@index.error <span class="hljs-string">'cannot use index with for-from'</span> <span class="hljs-keyword">if</span> @from <span class="hljs-keyword">and</span> @index
@ownTag.error <span class="hljs-string">"cannot use own with for-<span class="hljs-subst">#{<span class="hljs-keyword">if</span> @from <span class="hljs-keyword">then</span> <span class="hljs-string">'from'</span> <span class="hljs-keyword">else</span> <span class="hljs-string">'in'</span>}</span>"</span> <span class="hljs-keyword">if</span> @own <span class="hljs-keyword">and</span> <span class="hljs-keyword">not</span> @object
[@name, @index] = [@index, @name] <span class="hljs-keyword">if</span> @object
@index.error <span class="hljs-string">'index cannot be a pattern matching expression'</span> <span class="hljs-keyword">if</span> @index?.isArray?() <span class="hljs-keyword">or</span> @index?.isObject?()
@awaitTag.error <span class="hljs-string">'await must be used with for-from'</span> <span class="hljs-keyword">if</span> @await <span class="hljs-keyword">and</span> <span class="hljs-keyword">not</span> @from
@range = @source <span class="hljs-keyword">instanceof</span> Value <span class="hljs-keyword">and</span> @source.base <span class="hljs-keyword">instanceof</span> Range <span class="hljs-keyword">and</span> <span class="hljs-keyword">not</span> @source.properties.length <span class="hljs-keyword">and</span> <span class="hljs-keyword">not</span> @from
@pattern = @name <span class="hljs-keyword">instanceof</span> Value
@name.unwrap().propagateLhs?(<span class="hljs-literal">yes</span>) <span class="hljs-keyword">if</span> @pattern
@index.error <span class="hljs-string">'indexes do not apply to range loops'</span> <span class="hljs-keyword">if</span> @range <span class="hljs-keyword">and</span> @index
@name.error <span class="hljs-string">'cannot pattern match over range loops'</span> <span class="hljs-keyword">if</span> @range <span class="hljs-keyword">and</span> @pattern
@returns = <span class="hljs-literal">no</span></pre></div></div>
</li>
<li id="section-339">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-339">§</a>
</div>
<p>Move up any comments in the “<code>for</code> line”, i.e. the line of code with <code>for</code>,
from any child nodes of that line up to the <code>for</code> node itself so that these
comments get output, and get output above the <code>for</code> loop.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">for</span> attribute <span class="hljs-keyword">in</span> [<span class="hljs-string">'source'</span>, <span class="hljs-string">'guard'</span>, <span class="hljs-string">'step'</span>, <span class="hljs-string">'name'</span>, <span class="hljs-string">'index'</span>] <span class="hljs-keyword">when</span> @[attribute]
@[attribute].traverseChildren <span class="hljs-literal">yes</span>, <span class="hljs-function"><span class="hljs-params">(node)</span> =></span>
<span class="hljs-keyword">if</span> node.comments</pre></div></div>
</li>
<li id="section-340">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-340">§</a>
</div>
<p>These comments are buried pretty deeply, so if they happen to be
trailing comments the line they trail will be unrecognizable when
we’re done compiling this <code>for</code> loop; so just shift them up to
output above the <code>for</code> line.</p>
</div>
<div class="content"><div class='highlight'><pre> comment.newLine = comment.unshift = <span class="hljs-literal">yes</span> <span class="hljs-keyword">for</span> comment <span class="hljs-keyword">in</span> node.comments
moveComments node, @[attribute]
moveComments @[attribute], @
this</pre></div></div>
</li>
<li id="section-341">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-341">§</a>
</div>
<p>Welcome to the hairiest method in all of CoffeeScript. Handles the inner
loop, filtering, stepping, and result saving for array, object, and range
comprehensions. Some of the generated code can be shared in common, and
some cannot.</p>
</div>
<div class="content"><div class='highlight'><pre> compileNode: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
body = Block.wrap [@body]
[..., last] = body.expressions
@returns = <span class="hljs-literal">no</span> <span class="hljs-keyword">if</span> last?.jumps() <span class="hljs-keyword">instanceof</span> Return
source = <span class="hljs-keyword">if</span> @range <span class="hljs-keyword">then</span> @source.base <span class="hljs-keyword">else</span> @source
scope = o.scope
name = @name <span class="hljs-keyword">and</span> (@name.compile o, LEVEL_LIST) <span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> @pattern
index = @index <span class="hljs-keyword">and</span> (@index.compile o, LEVEL_LIST)
scope.find(name) <span class="hljs-keyword">if</span> name <span class="hljs-keyword">and</span> <span class="hljs-keyword">not</span> @pattern
scope.find(index) <span class="hljs-keyword">if</span> index <span class="hljs-keyword">and</span> @index <span class="hljs-keyword">not</span> <span class="hljs-keyword">instanceof</span> Value
rvar = scope.freeVariable <span class="hljs-string">'results'</span> <span class="hljs-keyword">if</span> @returns
<span class="hljs-keyword">if</span> @from
ivar = scope.freeVariable <span class="hljs-string">'x'</span>, single: <span class="hljs-literal">true</span> <span class="hljs-keyword">if</span> @pattern
<span class="hljs-keyword">else</span>
ivar = (@object <span class="hljs-keyword">and</span> index) <span class="hljs-keyword">or</span> scope.freeVariable <span class="hljs-string">'i'</span>, single: <span class="hljs-literal">true</span>
kvar = ((@range <span class="hljs-keyword">or</span> @from) <span class="hljs-keyword">and</span> name) <span class="hljs-keyword">or</span> index <span class="hljs-keyword">or</span> ivar
kvarAssign = <span class="hljs-keyword">if</span> kvar <span class="hljs-keyword">isnt</span> ivar <span class="hljs-keyword">then</span> <span class="hljs-string">"<span class="hljs-subst">#{kvar}</span> = "</span> <span class="hljs-keyword">else</span> <span class="hljs-string">""</span>
<span class="hljs-keyword">if</span> @step <span class="hljs-keyword">and</span> <span class="hljs-keyword">not</span> @range
[step, stepVar] = @cacheToCodeFragments @step.cache o, LEVEL_LIST, shouldCacheOrIsAssignable
stepNum = parseNumber stepVar <span class="hljs-keyword">if</span> @step.isNumber()
name = ivar <span class="hljs-keyword">if</span> @pattern
varPart = <span class="hljs-string">''</span>
guardPart = <span class="hljs-string">''</span>
defPart = <span class="hljs-string">''</span>
idt1 = @tab + TAB
<span class="hljs-keyword">if</span> @range
forPartFragments = source.compileToFragments merge o,
{index: ivar, name, @step, shouldCache: shouldCacheOrIsAssignable}
<span class="hljs-keyword">else</span>
svar = @source.compile o, LEVEL_LIST
<span class="hljs-keyword">if</span> (name <span class="hljs-keyword">or</span> @own) <span class="hljs-keyword">and</span> @source.unwrap() <span class="hljs-keyword">not</span> <span class="hljs-keyword">instanceof</span> IdentifierLiteral
defPart += <span class="hljs-string">"<span class="hljs-subst">#{@tab}</span><span class="hljs-subst">#{ref = scope.freeVariable <span class="hljs-string">'ref'</span>}</span> = <span class="hljs-subst">#{svar}</span>;\n"</span>
svar = ref
<span class="hljs-keyword">if</span> name <span class="hljs-keyword">and</span> <span class="hljs-keyword">not</span> @pattern <span class="hljs-keyword">and</span> <span class="hljs-keyword">not</span> @from
namePart = <span class="hljs-string">"<span class="hljs-subst">#{name}</span> = <span class="hljs-subst">#{svar}</span>[<span class="hljs-subst">#{kvar}</span>]"</span>
<span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> @object <span class="hljs-keyword">and</span> <span class="hljs-keyword">not</span> @from
defPart += <span class="hljs-string">"<span class="hljs-subst">#{@tab}</span><span class="hljs-subst">#{step}</span>;\n"</span> <span class="hljs-keyword">if</span> step <span class="hljs-keyword">isnt</span> stepVar
down = stepNum < <span class="hljs-number">0</span>
lvar = scope.freeVariable <span class="hljs-string">'len'</span> <span class="hljs-keyword">unless</span> @step <span class="hljs-keyword">and</span> stepNum? <span class="hljs-keyword">and</span> down
declare = <span class="hljs-string">"<span class="hljs-subst">#{kvarAssign}</span><span class="hljs-subst">#{ivar}</span> = 0, <span class="hljs-subst">#{lvar}</span> = <span class="hljs-subst">#{svar}</span>.length"</span>
declareDown = <span class="hljs-string">"<span class="hljs-subst">#{kvarAssign}</span><span class="hljs-subst">#{ivar}</span> = <span class="hljs-subst">#{svar}</span>.length - 1"</span>
compare = <span class="hljs-string">"<span class="hljs-subst">#{ivar}</span> < <span class="hljs-subst">#{lvar}</span>"</span>
compareDown = <span class="hljs-string">"<span class="hljs-subst">#{ivar}</span> >= 0"</span>
<span class="hljs-keyword">if</span> @step
<span class="hljs-keyword">if</span> stepNum?
<span class="hljs-keyword">if</span> down
compare = compareDown
declare = declareDown
<span class="hljs-keyword">else</span>
compare = <span class="hljs-string">"<span class="hljs-subst">#{stepVar}</span> > 0 ? <span class="hljs-subst">#{compare}</span> : <span class="hljs-subst">#{compareDown}</span>"</span>
declare = <span class="hljs-string">"(<span class="hljs-subst">#{stepVar}</span> > 0 ? (<span class="hljs-subst">#{declare}</span>) : <span class="hljs-subst">#{declareDown}</span>)"</span>
increment = <span class="hljs-string">"<span class="hljs-subst">#{ivar}</span> += <span class="hljs-subst">#{stepVar}</span>"</span>
<span class="hljs-keyword">else</span>
increment = <span class="hljs-string">"<span class="hljs-subst">#{<span class="hljs-keyword">if</span> kvar <span class="hljs-keyword">isnt</span> ivar <span class="hljs-keyword">then</span> <span class="hljs-string">"++<span class="hljs-subst">#{ivar}</span>"</span> <span class="hljs-keyword">else</span> <span class="hljs-string">"<span class="hljs-subst">#{ivar}</span>++"</span>}</span>"</span>
forPartFragments = [@makeCode(<span class="hljs-string">"<span class="hljs-subst">#{declare}</span>; <span class="hljs-subst">#{compare}</span>; <span class="hljs-subst">#{kvarAssign}</span><span class="hljs-subst">#{increment}</span>"</span>)]
<span class="hljs-keyword">if</span> @returns
resultPart = <span class="hljs-string">"<span class="hljs-subst">#{@tab}</span><span class="hljs-subst">#{rvar}</span> = [];\n"</span>
returnResult = <span class="hljs-string">"\n<span class="hljs-subst">#{@tab}</span>return <span class="hljs-subst">#{rvar}</span>;"</span>
body.makeReturn rvar
<span class="hljs-keyword">if</span> @guard
<span class="hljs-keyword">if</span> body.expressions.length > <span class="hljs-number">1</span>
body.expressions.unshift <span class="hljs-keyword">new</span> If (<span class="hljs-keyword">new</span> Parens @guard).invert(), <span class="hljs-keyword">new</span> StatementLiteral <span class="hljs-string">"continue"</span>
<span class="hljs-keyword">else</span>
body = Block.wrap [<span class="hljs-keyword">new</span> If @guard, body] <span class="hljs-keyword">if</span> @guard
<span class="hljs-keyword">if</span> @pattern
body.expressions.unshift <span class="hljs-keyword">new</span> Assign @name, <span class="hljs-keyword">if</span> @from <span class="hljs-keyword">then</span> <span class="hljs-keyword">new</span> IdentifierLiteral kvar <span class="hljs-keyword">else</span> <span class="hljs-keyword">new</span> Literal <span class="hljs-string">"<span class="hljs-subst">#{svar}</span>[<span class="hljs-subst">#{kvar}</span>]"</span>
varPart = <span class="hljs-string">"\n<span class="hljs-subst">#{idt1}</span><span class="hljs-subst">#{namePart}</span>;"</span> <span class="hljs-keyword">if</span> namePart
<span class="hljs-keyword">if</span> @object
forPartFragments = [@makeCode(<span class="hljs-string">"<span class="hljs-subst">#{kvar}</span> in <span class="hljs-subst">#{svar}</span>"</span>)]
guardPart = <span class="hljs-string">"\n<span class="hljs-subst">#{idt1}</span>if (!<span class="hljs-subst">#{utility <span class="hljs-string">'hasProp'</span>, o}</span>.call(<span class="hljs-subst">#{svar}</span>, <span class="hljs-subst">#{kvar}</span>)) continue;"</span> <span class="hljs-keyword">if</span> @own
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> @from
<span class="hljs-keyword">if</span> @await
forPartFragments = <span class="hljs-keyword">new</span> Op <span class="hljs-string">'await'</span>, <span class="hljs-keyword">new</span> Parens <span class="hljs-keyword">new</span> Literal <span class="hljs-string">"<span class="hljs-subst">#{kvar}</span> of <span class="hljs-subst">#{svar}</span>"</span>
forPartFragments = forPartFragments.compileToFragments o, LEVEL_TOP
<span class="hljs-keyword">else</span>
forPartFragments = [@makeCode(<span class="hljs-string">"<span class="hljs-subst">#{kvar}</span> of <span class="hljs-subst">#{svar}</span>"</span>)]
bodyFragments = body.compileToFragments merge(o, indent: idt1), LEVEL_TOP
<span class="hljs-keyword">if</span> bodyFragments <span class="hljs-keyword">and</span> bodyFragments.length > <span class="hljs-number">0</span>
bodyFragments = [].concat @makeCode(<span class="hljs-string">'\n'</span>), bodyFragments, @makeCode(<span class="hljs-string">'\n'</span>)
fragments = [@makeCode(defPart)]
fragments.push @makeCode(resultPart) <span class="hljs-keyword">if</span> resultPart
forCode = <span class="hljs-keyword">if</span> @await <span class="hljs-keyword">then</span> <span class="hljs-string">'for '</span> <span class="hljs-keyword">else</span> <span class="hljs-string">'for ('</span>
forClose = <span class="hljs-keyword">if</span> @await <span class="hljs-keyword">then</span> <span class="hljs-string">''</span> <span class="hljs-keyword">else</span> <span class="hljs-string">')'</span>
fragments = fragments.concat @makeCode(@tab), @makeCode( forCode),
forPartFragments, @makeCode(<span class="hljs-string">"<span class="hljs-subst">#{forClose}</span> {<span class="hljs-subst">#{guardPart}</span><span class="hljs-subst">#{varPart}</span>"</span>), bodyFragments,
@makeCode(@tab), @makeCode(<span class="hljs-string">'}'</span>)
fragments.push @makeCode(returnResult) <span class="hljs-keyword">if</span> returnResult
fragments
astNode: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
<span class="hljs-function"> <span class="hljs-title">addToScope</span> = <span class="hljs-params">(name)</span> -></span>
alreadyDeclared = o.scope.find name.value
name.isDeclaration = <span class="hljs-keyword">not</span> alreadyDeclared
@name?.eachName addToScope, checkAssignability: <span class="hljs-literal">no</span>
@index?.eachName addToScope, checkAssignability: <span class="hljs-literal">no</span>
super o
astType: <span class="hljs-function">-></span> <span class="hljs-string">'For'</span>
astProperties: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
<span class="hljs-keyword">return</span>
source: @source?.ast o
body: @body.ast o, LEVEL_TOP
guard: @guard?.ast(o) ? <span class="hljs-literal">null</span>
name: @name?.ast(o) ? <span class="hljs-literal">null</span>
index: @index?.ast(o) ? <span class="hljs-literal">null</span>
step: @step?.ast(o) ? <span class="hljs-literal">null</span>
postfix: !!@postfix
own: !!@own
await: !!@await
style: <span class="hljs-keyword">switch</span>
<span class="hljs-keyword">when</span> @from <span class="hljs-keyword">then</span> <span class="hljs-string">'from'</span>
<span class="hljs-keyword">when</span> @object <span class="hljs-keyword">then</span> <span class="hljs-string">'of'</span>
<span class="hljs-keyword">when</span> @name <span class="hljs-keyword">then</span> <span class="hljs-string">'in'</span>
<span class="hljs-keyword">else</span> <span class="hljs-string">'range'</span></pre></div></div>
</li>
<li id="section-342">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-342">§</a>
</div>
<h3 id="switch">Switch</h3>
</div>
</li>
<li id="section-343">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-343">§</a>
</div>
<p>A JavaScript <em>switch</em> statement. Converts into a returnable expression on-demand.</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-built_in">exports</span>.Switch = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Switch</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Base</span></span>
constructor: <span class="hljs-function"><span class="hljs-params">(@subject, @cases, @otherwise)</span> -></span>
super()
children: [<span class="hljs-string">'subject'</span>, <span class="hljs-string">'cases'</span>, <span class="hljs-string">'otherwise'</span>]
isStatement: YES
jumps: <span class="hljs-function"><span class="hljs-params">(o = {block: <span class="hljs-literal">yes</span>})</span> -></span>
<span class="hljs-keyword">for</span> {block} <span class="hljs-keyword">in</span> @cases
<span class="hljs-keyword">return</span> jumpNode <span class="hljs-keyword">if</span> jumpNode = block.jumps o
@otherwise?.jumps o
makeReturn: <span class="hljs-function"><span class="hljs-params">(results, mark)</span> -></span>
block.makeReturn(results, mark) <span class="hljs-keyword">for</span> {block} <span class="hljs-keyword">in</span> @cases
@otherwise <span class="hljs-keyword">or</span>= <span class="hljs-keyword">new</span> Block [<span class="hljs-keyword">new</span> Literal <span class="hljs-string">'void 0'</span>] <span class="hljs-keyword">if</span> results
@otherwise?.makeReturn results, mark
this
compileNode: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
idt1 = o.indent + TAB
idt2 = o.indent = idt1 + TAB
fragments = [].concat @makeCode(@tab + <span class="hljs-string">"switch ("</span>),
(<span class="hljs-keyword">if</span> @subject <span class="hljs-keyword">then</span> @subject.compileToFragments(o, LEVEL_PAREN) <span class="hljs-keyword">else</span> @makeCode <span class="hljs-string">"false"</span>),
@makeCode(<span class="hljs-string">") {\n"</span>)
<span class="hljs-keyword">for</span> {conditions, block}, i <span class="hljs-keyword">in</span> @cases
<span class="hljs-keyword">for</span> cond <span class="hljs-keyword">in</span> flatten [conditions]
cond = cond.invert() <span class="hljs-keyword">unless</span> @subject
fragments = fragments.concat @makeCode(idt1 + <span class="hljs-string">"case "</span>), cond.compileToFragments(o, LEVEL_PAREN), @makeCode(<span class="hljs-string">":\n"</span>)
fragments = fragments.concat body, @makeCode(<span class="hljs-string">'\n'</span>) <span class="hljs-keyword">if</span> (body = block.compileToFragments o, LEVEL_TOP).length > <span class="hljs-number">0</span>
<span class="hljs-keyword">break</span> <span class="hljs-keyword">if</span> i <span class="hljs-keyword">is</span> @cases.length - <span class="hljs-number">1</span> <span class="hljs-keyword">and</span> <span class="hljs-keyword">not</span> @otherwise
expr = @lastNode block.expressions
<span class="hljs-keyword">continue</span> <span class="hljs-keyword">if</span> expr <span class="hljs-keyword">instanceof</span> Return <span class="hljs-keyword">or</span> expr <span class="hljs-keyword">instanceof</span> Throw <span class="hljs-keyword">or</span> (expr <span class="hljs-keyword">instanceof</span> Literal <span class="hljs-keyword">and</span> expr.jumps() <span class="hljs-keyword">and</span> expr.value <span class="hljs-keyword">isnt</span> <span class="hljs-string">'debugger'</span>)
fragments.push cond.makeCode(idt2 + <span class="hljs-string">'break;\n'</span>)
<span class="hljs-keyword">if</span> @otherwise <span class="hljs-keyword">and</span> @otherwise.expressions.length
fragments.push @makeCode(idt1 + <span class="hljs-string">"default:\n"</span>), (@otherwise.compileToFragments o, LEVEL_TOP)..., @makeCode(<span class="hljs-string">"\n"</span>)
fragments.push @makeCode @tab + <span class="hljs-string">'}'</span>
fragments
astType: <span class="hljs-function">-></span> <span class="hljs-string">'SwitchStatement'</span>
casesAst: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
cases = []
<span class="hljs-keyword">for</span> kase, caseIndex <span class="hljs-keyword">in</span> @cases
{conditions: tests, block: consequent} = kase
tests = flatten [tests]
lastTestIndex = tests.length - <span class="hljs-number">1</span>
<span class="hljs-keyword">for</span> test, testIndex <span class="hljs-keyword">in</span> tests
testConsequent =
<span class="hljs-keyword">if</span> testIndex <span class="hljs-keyword">is</span> lastTestIndex
consequent
<span class="hljs-keyword">else</span>
<span class="hljs-literal">null</span>
caseLocationData = test.locationData
caseLocationData = mergeLocationData caseLocationData, testConsequent.expressions[testConsequent.expressions.length - <span class="hljs-number">1</span>].locationData <span class="hljs-keyword">if</span> testConsequent?.expressions.length
caseLocationData = mergeLocationData caseLocationData, kase.locationData, justLeading: <span class="hljs-literal">yes</span> <span class="hljs-keyword">if</span> testIndex <span class="hljs-keyword">is</span> <span class="hljs-number">0</span>
caseLocationData = mergeLocationData caseLocationData, kase.locationData, justEnding: <span class="hljs-literal">yes</span> <span class="hljs-keyword">if</span> testIndex <span class="hljs-keyword">is</span> lastTestIndex
cases.push <span class="hljs-keyword">new</span> SwitchCase(test, testConsequent, trailing: testIndex <span class="hljs-keyword">is</span> lastTestIndex).withLocationDataFrom locationData: caseLocationData
<span class="hljs-keyword">if</span> @otherwise?.expressions.length
cases.push <span class="hljs-keyword">new</span> SwitchCase(<span class="hljs-literal">null</span>, @otherwise).withLocationDataFrom @otherwise
kase.ast(o) <span class="hljs-keyword">for</span> kase <span class="hljs-keyword">in</span> cases
astProperties: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
<span class="hljs-keyword">return</span>
discriminant: @subject?.ast(o, LEVEL_PAREN) ? <span class="hljs-literal">null</span>
cases: @casesAst o
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">SwitchCase</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Base</span></span>
constructor: <span class="hljs-function"><span class="hljs-params">(@test, @block, {@trailing} = {})</span> -></span>
super()
children: [<span class="hljs-string">'test'</span>, <span class="hljs-string">'block'</span>]
astProperties: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
<span class="hljs-keyword">return</span>
test: @test?.ast(o, LEVEL_PAREN) ? <span class="hljs-literal">null</span>
consequent: @block?.ast(o, LEVEL_TOP).body ? []
trailing: !!@trailing
<span class="hljs-built_in">exports</span>.SwitchWhen = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">SwitchWhen</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Base</span></span>
constructor: <span class="hljs-function"><span class="hljs-params">(@conditions, @block)</span> -></span>
super()
children: [<span class="hljs-string">'conditions'</span>, <span class="hljs-string">'block'</span>]</pre></div></div>
</li>
<li id="section-344">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-344">§</a>
</div>
<h3 id="if">If</h3>
</div>
</li>
<li id="section-345">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-345">§</a>
</div>
<p><em>If/else</em> statements. Acts as an expression by pushing down requested returns
to the last line of each clause.</p>
<p>Single-expression <strong>Ifs</strong> are compiled into conditional operators if possible,
because ternaries are already proper expressions, and don’t need conversion.</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-built_in">exports</span>.If = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">If</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Base</span></span>
constructor: <span class="hljs-function"><span class="hljs-params">(@condition, @body, options = {})</span> -></span>
super()
@elseBody = <span class="hljs-literal">null</span>
@isChain = <span class="hljs-literal">false</span>
{@soak, @postfix, @type} = options
moveComments @condition, @ <span class="hljs-keyword">if</span> @condition.comments
children: [<span class="hljs-string">'condition'</span>, <span class="hljs-string">'body'</span>, <span class="hljs-string">'elseBody'</span>]
bodyNode: <span class="hljs-function">-></span> @body?.unwrap()
elseBodyNode: <span class="hljs-function">-></span> @elseBody?.unwrap()</pre></div></div>
</li>
<li id="section-346">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-346">§</a>
</div>
<p>Rewrite a chain of <strong>Ifs</strong> to add a default case as the final <em>else</em>.</p>
</div>
<div class="content"><div class='highlight'><pre> addElse: <span class="hljs-function"><span class="hljs-params">(elseBody)</span> -></span>
<span class="hljs-keyword">if</span> @isChain
@elseBodyNode().addElse elseBody
@locationData = mergeLocationData @locationData, @elseBodyNode().locationData
<span class="hljs-keyword">else</span>
@isChain = elseBody <span class="hljs-keyword">instanceof</span> If
@elseBody = @ensureBlock elseBody
@elseBody.updateLocationDataIfMissing elseBody.locationData
@locationData = mergeLocationData @locationData, @elseBody.locationData <span class="hljs-keyword">if</span> @locationData? <span class="hljs-keyword">and</span> @elseBody.locationData?
this</pre></div></div>
</li>
<li id="section-347">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-347">§</a>
</div>
<p>The <strong>If</strong> only compiles into a statement if either of its bodies needs
to be a statement. Otherwise a conditional operator is safe.</p>
</div>
<div class="content"><div class='highlight'><pre> isStatement: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
o?.level <span class="hljs-keyword">is</span> LEVEL_TOP <span class="hljs-keyword">or</span>
@bodyNode().isStatement(o) <span class="hljs-keyword">or</span> @elseBodyNode()?.isStatement(o)
jumps: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span> @body.jumps(o) <span class="hljs-keyword">or</span> @elseBody?.jumps(o)
compileNode: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
<span class="hljs-keyword">if</span> @isStatement o <span class="hljs-keyword">then</span> @compileStatement o <span class="hljs-keyword">else</span> @compileExpression o
makeReturn: <span class="hljs-function"><span class="hljs-params">(results, mark)</span> -></span>
<span class="hljs-keyword">if</span> mark
@body?.makeReturn results, mark
@elseBody?.makeReturn results, mark
<span class="hljs-keyword">return</span>
@elseBody <span class="hljs-keyword">or</span>= <span class="hljs-keyword">new</span> Block [<span class="hljs-keyword">new</span> Literal <span class="hljs-string">'void 0'</span>] <span class="hljs-keyword">if</span> results
@body <span class="hljs-keyword">and</span>= <span class="hljs-keyword">new</span> Block [@body.makeReturn results]
@elseBody <span class="hljs-keyword">and</span>= <span class="hljs-keyword">new</span> Block [@elseBody.makeReturn results]
this
ensureBlock: <span class="hljs-function"><span class="hljs-params">(node)</span> -></span>
<span class="hljs-keyword">if</span> node <span class="hljs-keyword">instanceof</span> Block <span class="hljs-keyword">then</span> node <span class="hljs-keyword">else</span> <span class="hljs-keyword">new</span> Block [node]</pre></div></div>
</li>
<li id="section-348">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-348">§</a>
</div>
<p>Compile the <code>If</code> as a regular <em>if-else</em> statement. Flattened chains
force inner <em>else</em> bodies into statement form.</p>
</div>
<div class="content"><div class='highlight'><pre> compileStatement: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
child = del o, <span class="hljs-string">'chainChild'</span>
exeq = del o, <span class="hljs-string">'isExistentialEquals'</span>
<span class="hljs-keyword">if</span> exeq
<span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> If(@processedCondition().invert(), @elseBodyNode(), type: <span class="hljs-string">'if'</span>).compileToFragments o
indent = o.indent + TAB
cond = @processedCondition().compileToFragments o, LEVEL_PAREN
body = @ensureBlock(@body).compileToFragments merge o, {indent}
ifPart = [].concat @makeCode(<span class="hljs-string">"if ("</span>), cond, @makeCode(<span class="hljs-string">") {\n"</span>), body, @makeCode(<span class="hljs-string">"\n<span class="hljs-subst">#{@tab}</span>}"</span>)
ifPart.unshift @makeCode @tab <span class="hljs-keyword">unless</span> child
<span class="hljs-keyword">return</span> ifPart <span class="hljs-keyword">unless</span> @elseBody
answer = ifPart.concat @makeCode(<span class="hljs-string">' else '</span>)
<span class="hljs-keyword">if</span> @isChain
o.chainChild = <span class="hljs-literal">yes</span>
answer = answer.concat @elseBody.unwrap().compileToFragments o, LEVEL_TOP
<span class="hljs-keyword">else</span>
answer = answer.concat @makeCode(<span class="hljs-string">"{\n"</span>), @elseBody.compileToFragments(merge(o, {indent}), LEVEL_TOP), @makeCode(<span class="hljs-string">"\n<span class="hljs-subst">#{@tab}</span>}"</span>)
answer</pre></div></div>
</li>
<li id="section-349">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-349">§</a>
</div>
<p>Compile the <code>If</code> as a conditional operator.</p>
</div>
<div class="content"><div class='highlight'><pre> compileExpression: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
cond = @processedCondition().compileToFragments o, LEVEL_COND
body = @bodyNode().compileToFragments o, LEVEL_LIST
alt = <span class="hljs-keyword">if</span> @elseBodyNode() <span class="hljs-keyword">then</span> @elseBodyNode().compileToFragments(o, LEVEL_LIST) <span class="hljs-keyword">else</span> [@makeCode(<span class="hljs-string">'void 0'</span>)]
fragments = cond.concat @makeCode(<span class="hljs-string">" ? "</span>), body, @makeCode(<span class="hljs-string">" : "</span>), alt
<span class="hljs-keyword">if</span> o.level >= LEVEL_COND <span class="hljs-keyword">then</span> @wrapInParentheses fragments <span class="hljs-keyword">else</span> fragments
unfoldSoak: <span class="hljs-function">-></span>
@soak <span class="hljs-keyword">and</span> this
processedCondition: <span class="hljs-function">-></span>
@processedConditionCache ?= <span class="hljs-keyword">if</span> @type <span class="hljs-keyword">is</span> <span class="hljs-string">'unless'</span> <span class="hljs-keyword">then</span> @condition.invert() <span class="hljs-keyword">else</span> @condition
isStatementAst: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
o.level <span class="hljs-keyword">is</span> LEVEL_TOP
astType: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
<span class="hljs-keyword">if</span> @isStatementAst o
<span class="hljs-string">'IfStatement'</span>
<span class="hljs-keyword">else</span>
<span class="hljs-string">'ConditionalExpression'</span>
astProperties: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
isStatement = @isStatementAst o
<span class="hljs-keyword">return</span>
test: @condition.ast o, <span class="hljs-keyword">if</span> isStatement <span class="hljs-keyword">then</span> LEVEL_PAREN <span class="hljs-keyword">else</span> LEVEL_COND
consequent:
<span class="hljs-keyword">if</span> isStatement
@body.ast o, LEVEL_TOP
<span class="hljs-keyword">else</span>
@bodyNode().ast o, LEVEL_TOP
alternate:
<span class="hljs-keyword">if</span> @isChain
@elseBody.unwrap().ast o, <span class="hljs-keyword">if</span> isStatement <span class="hljs-keyword">then</span> LEVEL_TOP <span class="hljs-keyword">else</span> LEVEL_COND
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> isStatement <span class="hljs-keyword">and</span> @elseBody?.expressions?.length <span class="hljs-keyword">is</span> <span class="hljs-number">1</span>
@elseBody.expressions[<span class="hljs-number">0</span>].ast o, LEVEL_TOP
<span class="hljs-keyword">else</span>
@elseBody?.ast(o, LEVEL_TOP) ? <span class="hljs-literal">null</span>
postfix: !!@postfix
inverted: @type <span class="hljs-keyword">is</span> <span class="hljs-string">'unless'</span></pre></div></div>
</li>
<li id="section-350">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-350">§</a>
</div>
<p>A sequence expression e.g. <code>(a; b)</code>.
Currently only used during AST generation.</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-built_in">exports</span>.Sequence = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Sequence</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Base</span></span>
children: [<span class="hljs-string">'expressions'</span>]
constructor: <span class="hljs-function"><span class="hljs-params">(@expressions)</span> -></span>
super()
astNode: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
<span class="hljs-keyword">return</span> @expressions[<span class="hljs-number">0</span>].ast(o) <span class="hljs-keyword">if</span> @expressions.length <span class="hljs-keyword">is</span> <span class="hljs-number">1</span>
super o
astType: <span class="hljs-function">-></span> <span class="hljs-string">'SequenceExpression'</span>
astProperties: <span class="hljs-function"><span class="hljs-params">(o)</span> -></span>
<span class="hljs-keyword">return</span>
expressions:
expression.ast(o) <span class="hljs-keyword">for</span> expression <span class="hljs-keyword">in</span> @expressions</pre></div></div>
</li>
<li id="section-351">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-351">§</a>
</div>
<h2 id="constants">Constants</h2>
</div>
</li>
<li id="section-352">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-352">§</a>
</div>
</div>
<div class="content"><div class='highlight'><pre>
UTILITIES =
modulo: <span class="hljs-function">-></span> <span class="hljs-string">'function(a, b) { return (+a % (b = +b) + b) % b; }'</span>
boundMethodCheck: <span class="hljs-function">-></span> <span class="hljs-string">"
function(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new Error('Bound instance method accessed before binding');
}
}
"</span></pre></div></div>
</li>
<li id="section-353">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-353">§</a>
</div>
<p>Shortcuts to speed up the lookup time for native functions.</p>
</div>
<div class="content"><div class='highlight'><pre> hasProp: <span class="hljs-function">-></span> <span class="hljs-string">'{}.hasOwnProperty'</span>
indexOf: <span class="hljs-function">-></span> <span class="hljs-string">'[].indexOf'</span>
slice : <span class="hljs-function">-></span> <span class="hljs-string">'[].slice'</span>
splice : <span class="hljs-function">-></span> <span class="hljs-string">'[].splice'</span></pre></div></div>
</li>
<li id="section-354">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-354">§</a>
</div>
<p>Levels indicate a node’s position in the AST. Useful for knowing if
parens are necessary or superfluous.</p>
</div>
<div class="content"><div class='highlight'><pre>LEVEL_TOP = <span class="hljs-number">1</span> <span class="hljs-comment"># ...;</span>
LEVEL_PAREN = <span class="hljs-number">2</span> <span class="hljs-comment"># (...)</span>
LEVEL_LIST = <span class="hljs-number">3</span> <span class="hljs-comment"># [...]</span>
LEVEL_COND = <span class="hljs-number">4</span> <span class="hljs-comment"># ... ? x : y</span>
LEVEL_OP = <span class="hljs-number">5</span> <span class="hljs-comment"># !...</span>
LEVEL_ACCESS = <span class="hljs-number">6</span> <span class="hljs-comment"># ...[0]</span></pre></div></div>
</li>
<li id="section-355">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-355">§</a>
</div>
<p>Tabs are two spaces for pretty printing.</p>
</div>
<div class="content"><div class='highlight'><pre>TAB = <span class="hljs-string">' '</span>
SIMPLENUM = <span class="hljs-regexp">/^[+-]?\d+$/</span>
SIMPLE_STRING_OMIT = <span class="hljs-regexp">/\s*\n\s*/g</span>
LEADING_BLANK_LINE = <span class="hljs-regexp">/^[^\n\S]*\n/</span>
TRAILING_BLANK_LINE = <span class="hljs-regexp">/\n[^\n\S]*$/</span>
STRING_OMIT = <span class="hljs-regexp">///
((?:\\\\)+) <span class="hljs-comment"># Consume (and preserve) an even number of backslashes.</span>
| \\[^\S\n]*\n\s* <span class="hljs-comment"># Remove escaped newlines.</span>
///</span>g
HEREGEX_OMIT = <span class="hljs-regexp">///
((?:\\\\)+) <span class="hljs-comment"># Consume (and preserve) an even number of backslashes.</span>
| \\(\s) <span class="hljs-comment"># Preserve escaped whitespace.</span>
| \s+(?:<span class="hljs-comment">#.*)? # Remove whitespace and comments.</span>
///</span>g</pre></div></div>
</li>
<li id="section-356">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-356">§</a>
</div>
<h2 id="helper-functions">Helper Functions</h2>
</div>
</li>
<li id="section-357">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-357">§</a>
</div>
</div>
</li>
<li id="section-358">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-358">§</a>
</div>
<p>Helper for ensuring that utility functions are assigned at the top level.</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-function"><span class="hljs-title">utility</span> = <span class="hljs-params">(name, o)</span> -></span>
{root} = o.scope
<span class="hljs-keyword">if</span> name <span class="hljs-keyword">of</span> root.utilities
root.utilities[name]
<span class="hljs-keyword">else</span>
ref = root.freeVariable name
root.assign ref, UTILITIES[name] o
root.utilities[name] = ref
<span class="hljs-function">
<span class="hljs-title">multident</span> = <span class="hljs-params">(code, tab, includingFirstLine = <span class="hljs-literal">yes</span>)</span> -></span>
endsWithNewLine = code[code.length - <span class="hljs-number">1</span>] <span class="hljs-keyword">is</span> <span class="hljs-string">'\n'</span>
code = (<span class="hljs-keyword">if</span> includingFirstLine <span class="hljs-keyword">then</span> tab <span class="hljs-keyword">else</span> <span class="hljs-string">''</span>) + code.replace <span class="hljs-regexp">/\n/g</span>, <span class="hljs-string">"$&<span class="hljs-subst">#{tab}</span>"</span>
code = code.replace <span class="hljs-regexp">/\s+$/</span>, <span class="hljs-string">''</span>
code = code + <span class="hljs-string">'\n'</span> <span class="hljs-keyword">if</span> endsWithNewLine
code</pre></div></div>
</li>
<li id="section-359">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-359">§</a>
</div>
<p>Wherever in CoffeeScript 1 we might’ve inserted a <code>makeCode "#{@tab}"</code> to
indent a line of code, now we must account for the possibility of comments
preceding that line of code. If there are such comments, indent each line of
such comments, and <em>then</em> indent the first following line of code.</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-function"><span class="hljs-title">indentInitial</span> = <span class="hljs-params">(fragments, node)</span> -></span>
<span class="hljs-keyword">for</span> fragment, fragmentIndex <span class="hljs-keyword">in</span> fragments
<span class="hljs-keyword">if</span> fragment.isHereComment
fragment.code = multident fragment.code, node.tab
<span class="hljs-keyword">else</span>
fragments.splice fragmentIndex, <span class="hljs-number">0</span>, node.makeCode <span class="hljs-string">"<span class="hljs-subst">#{node.tab}</span>"</span>
<span class="hljs-keyword">break</span>
fragments
<span class="hljs-function">
<span class="hljs-title">hasLineComments</span> = <span class="hljs-params">(node)</span> -></span>
<span class="hljs-keyword">return</span> <span class="hljs-literal">no</span> <span class="hljs-keyword">unless</span> node.comments
<span class="hljs-keyword">for</span> comment <span class="hljs-keyword">in</span> node.comments
<span class="hljs-keyword">return</span> <span class="hljs-literal">yes</span> <span class="hljs-keyword">if</span> comment.here <span class="hljs-keyword">is</span> <span class="hljs-literal">no</span>
<span class="hljs-keyword">return</span> <span class="hljs-literal">no</span></pre></div></div>
</li>
<li id="section-360">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-360">§</a>
</div>
<p>Move the <code>comments</code> property from one object to another, deleting it from
the first object.</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-function"><span class="hljs-title">moveComments</span> = <span class="hljs-params">(<span class="hljs-keyword">from</span>, to)</span> -></span>
<span class="hljs-keyword">return</span> <span class="hljs-keyword">unless</span> <span class="hljs-keyword">from</span>?.comments
attachCommentsToNode <span class="hljs-keyword">from</span>.comments, to
<span class="hljs-keyword">delete</span> <span class="hljs-keyword">from</span>.comments</pre></div></div>
</li>
<li id="section-361">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-361">§</a>
</div>
<p>Sometimes when compiling a node, we want to insert a fragment at the start
of an array of fragments; but if the start has one or more comment fragments,
we want to insert this fragment after those but before any non-comments.</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-function"><span class="hljs-title">unshiftAfterComments</span> = <span class="hljs-params">(fragments, fragmentToInsert)</span> -></span>
inserted = <span class="hljs-literal">no</span>
<span class="hljs-keyword">for</span> fragment, fragmentIndex <span class="hljs-keyword">in</span> fragments <span class="hljs-keyword">when</span> <span class="hljs-keyword">not</span> fragment.isComment
fragments.splice fragmentIndex, <span class="hljs-number">0</span>, fragmentToInsert
inserted = <span class="hljs-literal">yes</span>
<span class="hljs-keyword">break</span>
fragments.push fragmentToInsert <span class="hljs-keyword">unless</span> inserted
fragments
<span class="hljs-function">
<span class="hljs-title">isLiteralArguments</span> = <span class="hljs-params">(node)</span> -></span>
node <span class="hljs-keyword">instanceof</span> IdentifierLiteral <span class="hljs-keyword">and</span> node.value <span class="hljs-keyword">is</span> <span class="hljs-string">'arguments'</span>
<span class="hljs-function">
<span class="hljs-title">isLiteralThis</span> = <span class="hljs-params">(node)</span> -></span>
node <span class="hljs-keyword">instanceof</span> ThisLiteral <span class="hljs-keyword">or</span> (node <span class="hljs-keyword">instanceof</span> Code <span class="hljs-keyword">and</span> node.bound)
<span class="hljs-function">
<span class="hljs-title">shouldCacheOrIsAssignable</span> = <span class="hljs-params">(node)</span> -></span> node.shouldCache() <span class="hljs-keyword">or</span> node.isAssignable?()</pre></div></div>
</li>
<li id="section-362">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-362">§</a>
</div>
<p>Unfold a node’s child if soak, then tuck the node under created <code>If</code></p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-function"><span class="hljs-title">unfoldSoak</span> = <span class="hljs-params">(o, parent, name)</span> -></span>
<span class="hljs-keyword">return</span> <span class="hljs-keyword">unless</span> ifn = parent[name].unfoldSoak o
parent[name] = ifn.body
ifn.body = <span class="hljs-keyword">new</span> Value parent
ifn</pre></div></div>
</li>
<li id="section-363">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-363">§</a>
</div>
<p>Constructs a string or regex by escaping certain characters.</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-function"><span class="hljs-title">makeDelimitedLiteral</span> = <span class="hljs-params">(body, {delimiter: delimiterOption, escapeNewlines, double, includeDelimiters = <span class="hljs-literal">yes</span>, escapeDelimiter = <span class="hljs-literal">yes</span>, convertTrailingNullEscapes} = {})</span> -></span>
body = <span class="hljs-string">'(?:)'</span> <span class="hljs-keyword">if</span> body <span class="hljs-keyword">is</span> <span class="hljs-string">''</span> <span class="hljs-keyword">and</span> delimiterOption <span class="hljs-keyword">is</span> <span class="hljs-string">'/'</span>
escapeTemplateLiteralCurlies = delimiterOption <span class="hljs-keyword">is</span> <span class="hljs-string">'`'</span>
regex = <span class="hljs-regexp">///
(\\\\) <span class="hljs-comment"># Escaped backslash.</span>
| (\\0(?=\d)) <span class="hljs-comment"># Null character mistaken as octal escape.</span>
<span class="hljs-subst">#{
<span class="hljs-keyword">if</span> convertTrailingNullEscapes
<span class="hljs-regexp">/// | (\\0) $ ///</span>.source # Trailing <span class="hljs-literal">null</span> character that could be mistaken <span class="hljs-keyword">as</span> octal <span class="hljs-built_in">escape</span>.
<span class="hljs-keyword">else</span>
<span class="hljs-string">''</span>
}</span>
<span class="hljs-subst">#{
<span class="hljs-keyword">if</span> escapeDelimiter
<span class="hljs-regexp">/// | \\?(<span class="hljs-subst">#{delimiterOption}</span>) ///</span>.source # (Possibly escaped) delimiter.
<span class="hljs-keyword">else</span>
<span class="hljs-string">''</span>
}</span>
<span class="hljs-subst">#{
<span class="hljs-keyword">if</span> escapeTemplateLiteralCurlies
<span class="hljs-regexp">/// | \\?(\$\{) ///</span>.source # `<span class="language-javascript">${</span>` inside template literals must be escaped.
<span class="hljs-keyword">else</span>
<span class="hljs-string">''</span>
}</span>
| \\?(?:
<span class="hljs-subst">#{<span class="hljs-keyword">if</span> escapeNewlines <span class="hljs-keyword">then</span> <span class="hljs-string">'(\n)|'</span> <span class="hljs-keyword">else</span> <span class="hljs-string">''</span>}</span>
(\r)
| (\u2028)
| (\u2029)
) <span class="hljs-comment"># (Possibly escaped) newlines.</span>
| (\\.) <span class="hljs-comment"># Other escapes.</span>
///</span>g
body = body.replace regex, <span class="hljs-function"><span class="hljs-params">(match, backslash, nul, ...args)</span> -></span>
trailingNullEscape =
args.shift() <span class="hljs-keyword">if</span> convertTrailingNullEscapes
delimiter =
args.shift() <span class="hljs-keyword">if</span> escapeDelimiter
templateLiteralCurly =
args.shift() <span class="hljs-keyword">if</span> escapeTemplateLiteralCurlies
lf =
args.shift() <span class="hljs-keyword">if</span> escapeNewlines
[cr, ls, ps, other] = args
<span class="hljs-keyword">switch</span></pre></div></div>
</li>
<li id="section-364">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-364">§</a>
</div>
<p>Ignore escaped backslashes.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">when</span> backslash <span class="hljs-keyword">then</span> (<span class="hljs-keyword">if</span> double <span class="hljs-keyword">then</span> backslash + backslash <span class="hljs-keyword">else</span> backslash)
<span class="hljs-keyword">when</span> nul <span class="hljs-keyword">then</span> <span class="hljs-string">'\\x00'</span>
<span class="hljs-keyword">when</span> trailingNullEscape <span class="hljs-keyword">then</span> <span class="hljs-string">"\\x00"</span>
<span class="hljs-keyword">when</span> delimiter <span class="hljs-keyword">then</span> <span class="hljs-string">"\\<span class="hljs-subst">#{delimiter}</span>"</span>
<span class="hljs-keyword">when</span> templateLiteralCurly <span class="hljs-keyword">then</span> <span class="hljs-string">"\\${"</span>
<span class="hljs-keyword">when</span> lf <span class="hljs-keyword">then</span> <span class="hljs-string">'\\n'</span>
<span class="hljs-keyword">when</span> cr <span class="hljs-keyword">then</span> <span class="hljs-string">'\\r'</span>
<span class="hljs-keyword">when</span> ls <span class="hljs-keyword">then</span> <span class="hljs-string">'\\u2028'</span>
<span class="hljs-keyword">when</span> ps <span class="hljs-keyword">then</span> <span class="hljs-string">'\\u2029'</span>
<span class="hljs-keyword">when</span> other <span class="hljs-keyword">then</span> (<span class="hljs-keyword">if</span> double <span class="hljs-keyword">then</span> <span class="hljs-string">"\\<span class="hljs-subst">#{other}</span>"</span> <span class="hljs-keyword">else</span> other)
printedDelimiter = <span class="hljs-keyword">if</span> includeDelimiters <span class="hljs-keyword">then</span> delimiterOption <span class="hljs-keyword">else</span> <span class="hljs-string">''</span>
<span class="hljs-string">"<span class="hljs-subst">#{printedDelimiter}</span><span class="hljs-subst">#{body}</span><span class="hljs-subst">#{printedDelimiter}</span>"</span>
<span class="hljs-function">
<span class="hljs-title">sniffDirectives</span> = <span class="hljs-params">(expressions, {notFinalExpression} = {})</span> -></span>
index = <span class="hljs-number">0</span>
lastIndex = expressions.length - <span class="hljs-number">1</span>
<span class="hljs-keyword">while</span> index <= lastIndex
<span class="hljs-keyword">break</span> <span class="hljs-keyword">if</span> index <span class="hljs-keyword">is</span> lastIndex <span class="hljs-keyword">and</span> notFinalExpression
expression = expressions[index]
<span class="hljs-keyword">if</span> (unwrapped = expression?.unwrap?()) <span class="hljs-keyword">instanceof</span> PassthroughLiteral <span class="hljs-keyword">and</span> unwrapped.generated
index++
<span class="hljs-keyword">continue</span>
<span class="hljs-keyword">break</span> <span class="hljs-keyword">unless</span> expression <span class="hljs-keyword">instanceof</span> Value <span class="hljs-keyword">and</span> expression.isString() <span class="hljs-keyword">and</span> <span class="hljs-keyword">not</span> expression.unwrap().shouldGenerateTemplateLiteral()
expressions[index] =
<span class="hljs-keyword">new</span> Directive expression
.withLocationDataFrom expression
index++
<span class="hljs-function">
<span class="hljs-title">astAsBlockIfNeeded</span> = <span class="hljs-params">(node, o)</span> -></span>
unwrapped = node.unwrap()
<span class="hljs-keyword">if</span> unwrapped <span class="hljs-keyword">instanceof</span> Block <span class="hljs-keyword">and</span> unwrapped.expressions.length > <span class="hljs-number">1</span>
unwrapped.makeReturn <span class="hljs-literal">null</span>, <span class="hljs-literal">yes</span>
unwrapped.ast o, LEVEL_TOP
<span class="hljs-keyword">else</span>
node.ast o, LEVEL_PAREN</pre></div></div>
</li>
<li id="section-365">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-365">§</a>
</div>
<p>Helpers for <code>mergeLocationData</code> and <code>mergeAstLocationData</code> below.</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-function"><span class="hljs-title">lesser</span> = <span class="hljs-params">(a, b)</span> -></span> <span class="hljs-keyword">if</span> a < b <span class="hljs-keyword">then</span> a <span class="hljs-keyword">else</span> b
<span class="hljs-function"><span class="hljs-title">greater</span> = <span class="hljs-params">(a, b)</span> -></span> <span class="hljs-keyword">if</span> a > b <span class="hljs-keyword">then</span> a <span class="hljs-keyword">else</span> b
<span class="hljs-function">
<span class="hljs-title">isAstLocGreater</span> = <span class="hljs-params">(a, b)</span> -></span>
<span class="hljs-keyword">return</span> <span class="hljs-literal">yes</span> <span class="hljs-keyword">if</span> a.line > b.line
<span class="hljs-keyword">return</span> <span class="hljs-literal">no</span> <span class="hljs-keyword">unless</span> a.line <span class="hljs-keyword">is</span> b.line
a.column > b.column
<span class="hljs-function">
<span class="hljs-title">isLocationDataStartGreater</span> = <span class="hljs-params">(a, b)</span> -></span>
<span class="hljs-keyword">return</span> <span class="hljs-literal">yes</span> <span class="hljs-keyword">if</span> a.first_line > b.first_line
<span class="hljs-keyword">return</span> <span class="hljs-literal">no</span> <span class="hljs-keyword">unless</span> a.first_line <span class="hljs-keyword">is</span> b.first_line
a.first_column > b.first_column
<span class="hljs-function">
<span class="hljs-title">isLocationDataEndGreater</span> = <span class="hljs-params">(a, b)</span> -></span>
<span class="hljs-keyword">return</span> <span class="hljs-literal">yes</span> <span class="hljs-keyword">if</span> a.last_line > b.last_line
<span class="hljs-keyword">return</span> <span class="hljs-literal">no</span> <span class="hljs-keyword">unless</span> a.last_line <span class="hljs-keyword">is</span> b.last_line
a.last_column > b.last_column</pre></div></div>
</li>
<li id="section-366">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-366">§</a>
</div>
<p>Take two nodes’ location data and return a new <code>locationData</code> object that
encompasses the location data of both nodes. So the new <code>first_line</code> value
will be the earlier of the two nodes’ <code>first_line</code> values, the new
<code>last_column</code> the later of the two nodes’ <code>last_column</code> values, etc.</p>
<p>If you only want to extend the first node’s location data with the start or
end location data of the second node, pass the <code>justLeading</code> or <code>justEnding</code>
options. So e.g. if <code>first</code>’s range is [4, 5] and <code>second</code>’s range is [1, 10],
you’d get:</p>
<pre><code>mergeLocationData(first, second).range <span class="hljs-comment"># [1, 10]</span>
mergeLocationData(first, second, justLeading: <span class="hljs-literal">yes</span>).range <span class="hljs-comment"># [1, 5]</span>
mergeLocationData(first, second, justEnding: <span class="hljs-literal">yes</span>).range <span class="hljs-comment"># [4, 10]</span>
</code></pre>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-built_in">exports</span>.mergeLocationData = mergeLocationData = <span class="hljs-function"><span class="hljs-params">(locationDataA, locationDataB, {justLeading, justEnding} = {})</span> -></span>
<span class="hljs-keyword">return</span> <span class="hljs-built_in">Object</span>.assign(
<span class="hljs-keyword">if</span> justEnding
first_line: locationDataA.first_line
first_column: locationDataA.first_column
<span class="hljs-keyword">else</span>
<span class="hljs-keyword">if</span> isLocationDataStartGreater locationDataA, locationDataB
first_line: locationDataB.first_line
first_column: locationDataB.first_column
<span class="hljs-keyword">else</span>
first_line: locationDataA.first_line
first_column: locationDataA.first_column
,
<span class="hljs-keyword">if</span> justLeading
last_line: locationDataA.last_line
last_column: locationDataA.last_column
last_line_exclusive: locationDataA.last_line_exclusive
last_column_exclusive: locationDataA.last_column_exclusive
<span class="hljs-keyword">else</span>
<span class="hljs-keyword">if</span> isLocationDataEndGreater locationDataA, locationDataB
last_line: locationDataA.last_line
last_column: locationDataA.last_column
last_line_exclusive: locationDataA.last_line_exclusive
last_column_exclusive: locationDataA.last_column_exclusive
<span class="hljs-keyword">else</span>
last_line: locationDataB.last_line
last_column: locationDataB.last_column
last_line_exclusive: locationDataB.last_line_exclusive
last_column_exclusive: locationDataB.last_column_exclusive
,
range: [
<span class="hljs-keyword">if</span> justEnding
locationDataA.range[<span class="hljs-number">0</span>]
<span class="hljs-keyword">else</span>
lesser locationDataA.range[<span class="hljs-number">0</span>], locationDataB.range[<span class="hljs-number">0</span>]
,
<span class="hljs-keyword">if</span> justLeading
locationDataA.range[<span class="hljs-number">1</span>]
<span class="hljs-keyword">else</span>
greater locationDataA.range[<span class="hljs-number">1</span>], locationDataB.range[<span class="hljs-number">1</span>]
]
)</pre></div></div>
</li>
<li id="section-367">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-367">§</a>
</div>
<p>Take two AST nodes, or two AST nodes’ location data objects, and return a new
location data object that encompasses the location data of both nodes. So the
new <code>start</code> value will be the earlier of the two nodes’ <code>start</code> values, the
new <code>end</code> value will be the later of the two nodes’ <code>end</code> values, etc.</p>
<p>If you only want to extend the first node’s location data with the start or
end location data of the second node, pass the <code>justLeading</code> or <code>justEnding</code>
options. So e.g. if <code>first</code>’s range is [4, 5] and <code>second</code>’s range is [1, 10],
you’d get:</p>
<pre><code>mergeAstLocationData(first, second).range <span class="hljs-comment"># [1, 10]</span>
mergeAstLocationData(first, second, justLeading: <span class="hljs-literal">yes</span>).range <span class="hljs-comment"># [1, 5]</span>
mergeAstLocationData(first, second, justEnding: <span class="hljs-literal">yes</span>).range <span class="hljs-comment"># [4, 10]</span>
</code></pre>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-built_in">exports</span>.mergeAstLocationData = mergeAstLocationData = <span class="hljs-function"><span class="hljs-params">(nodeA, nodeB, {justLeading, justEnding} = {})</span> -></span>
<span class="hljs-keyword">return</span>
loc:
start:
<span class="hljs-keyword">if</span> justEnding
nodeA.loc.start
<span class="hljs-keyword">else</span>
<span class="hljs-keyword">if</span> isAstLocGreater nodeA.loc.start, nodeB.loc.start
nodeB.loc.start
<span class="hljs-keyword">else</span>
nodeA.loc.start
end:
<span class="hljs-keyword">if</span> justLeading
nodeA.loc.end
<span class="hljs-keyword">else</span>
<span class="hljs-keyword">if</span> isAstLocGreater nodeA.loc.end, nodeB.loc.end
nodeA.loc.end
<span class="hljs-keyword">else</span>
nodeB.loc.end
range: [
<span class="hljs-keyword">if</span> justEnding
nodeA.range[<span class="hljs-number">0</span>]
<span class="hljs-keyword">else</span>
lesser nodeA.range[<span class="hljs-number">0</span>], nodeB.range[<span class="hljs-number">0</span>]
,
<span class="hljs-keyword">if</span> justLeading
nodeA.range[<span class="hljs-number">1</span>]
<span class="hljs-keyword">else</span>
greater nodeA.range[<span class="hljs-number">1</span>], nodeB.range[<span class="hljs-number">1</span>]
]
start:
<span class="hljs-keyword">if</span> justEnding
nodeA.start
<span class="hljs-keyword">else</span>
lesser nodeA.start, nodeB.start
end:
<span class="hljs-keyword">if</span> justLeading
nodeA.end
<span class="hljs-keyword">else</span>
greater nodeA.end, nodeB.end</pre></div></div>
</li>
<li id="section-368">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-368">§</a>
</div>
<p>Convert Jison-style node class location data to Babel-style location data</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-built_in">exports</span>.jisonLocationDataToAstLocationData = jisonLocationDataToAstLocationData = <span class="hljs-function"><span class="hljs-params">({first_line, first_column, last_line_exclusive, last_column_exclusive, range})</span> -></span>
<span class="hljs-keyword">return</span>
loc:
start:
line: first_line + <span class="hljs-number">1</span>
column: first_column
end:
line: last_line_exclusive + <span class="hljs-number">1</span>
column: last_column_exclusive
range: [
range[<span class="hljs-number">0</span>]
range[<span class="hljs-number">1</span>]
]
start: range[<span class="hljs-number">0</span>]
end: range[<span class="hljs-number">1</span>]</pre></div></div>
</li>
<li id="section-369">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-369">§</a>
</div>
<p>Generate a zero-width location data that corresponds to the end of another node’s location.</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-function"><span class="hljs-title">zeroWidthLocationDataFromEndLocation</span> = <span class="hljs-params">({range: [, endRange], last_line_exclusive, last_column_exclusive})</span> -></span> {
first_line: last_line_exclusive
first_column: last_column_exclusive
last_line: last_line_exclusive
last_column: last_column_exclusive
last_line_exclusive
last_column_exclusive
range: [endRange, endRange]
}
<span class="hljs-function">
<span class="hljs-title">extractSameLineLocationDataFirst</span> = <span class="hljs-params">(numChars)</span> -></span> ({range: [startRange], first_line, first_column}) -> {
first_line
first_column
last_line: first_line
last_column: first_column + numChars - <span class="hljs-number">1</span>
last_line_exclusive: first_line
last_column_exclusive: first_column + numChars
range: [startRange, startRange + numChars]
}
<span class="hljs-function">
<span class="hljs-title">extractSameLineLocationDataLast</span> = <span class="hljs-params">(numChars)</span> -></span> ({range: [, endRange], last_line, last_column, last_line_exclusive, last_column_exclusive}) -> {
first_line: last_line
first_column: last_column - (numChars - <span class="hljs-number">1</span>)
last_line: last_line
last_column: last_column
last_line_exclusive
last_column_exclusive
range: [endRange - numChars, endRange]
}</pre></div></div>
</li>
<li id="section-370">
<div class="annotation">
<div class="sswrap ">
<a class="ss" href="#section-370">§</a>
</div>
<p>We don’t currently have a token corresponding to the empty space
between interpolation/JSX expression braces, so piece together the location
data by trimming the braces from the Interpolation’s location data.
Technically the last_line/last_column calculation here could be
incorrect if the ending brace is preceded by a newline, but
last_line/last_column aren’t used for AST generation anyway.</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-function"><span class="hljs-title">emptyExpressionLocationData</span> = <span class="hljs-params">({interpolationNode: element, openingBrace, closingBrace})</span> -></span>
first_line: element.locationData.first_line
first_column: element.locationData.first_column + openingBrace.length
last_line: element.locationData.last_line
last_column: element.locationData.last_column - closingBrace.length
last_line_exclusive: element.locationData.last_line
last_column_exclusive: element.locationData.last_column
range: [
element.locationData.range[<span class="hljs-number">0</span>] + openingBrace.length
element.locationData.range[<span class="hljs-number">1</span>] - closingBrace.length
]</pre></div></div>
</li>
</ul>
</div>
</body>
</html>
|