All files / src/compiler/phases/3-transform/client/visitors template.js

96.56% Statements 3063/3172
93.41% Branches 724/775
96.34% Functions 79/82
96.54% Lines 3049/3158

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 31592x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1020x 1020x 1020x 1020x 1020x 916x 1020x 916x 916x 5x 5x 916x 1020x 1020x 2x 2x 2x 2x 2x 2x 2x 2x 2x 3585x 3585x 3585x 3585x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 26x 26x 33x 33x 33x 1x 32x 23x 32x 9x 9x 33x 3585x 2x 2x 2x 2x 2x 2x 186x 186x 186x 186x 186x 186x 186x 186x 186x 14x 14x 14x 186x 186x 186x 2x 2x 2x 2x 2x 2x 2x 2x 2x 3585x 3585x 3585x 24x 24x 24x 24x 24x 2x 23x 18x 21x 4x 4x 24x 3585x 2x 2x 2x 2x 2x 477x 477x 477x 477x 477x 430x 430x 5x 5x 430x 477x 477x 477x 2x 2x 2x 2x 2x 2x 435x 435x 4x 312x 7x 5x 5x 7x 7x 5x 7x 4x 4x 4x 2x 2x 2x 2x 2x 2x 2x 33x 33x 31x 31x 31x 4x 4x 4x 4x 31x 31x 31x 31x 31x 53x 53x 53x 53x 53x 22x 22x 53x 31x 31x 31x 31x 31x 31x 22x 22x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 2x 2x 2x 2x 2x 2x 2x 2x 85x 85x 85x 85x 85x 85x 85x 85x 85x 85x 85x 85x 85x 142x 56x 56x 56x 56x 56x 56x 5x 5x 56x 5x 5x 5x 5x 56x 51x 51x 142x 86x 86x 142x 142x 142x 142x 85x 85x 85x 85x 85x 85x 85x 85x 85x 85x 85x 85x 85x 85x 85x 85x 85x 85x 85x 85x 85x 85x 85x 85x 85x 85x 6x 85x 79x 79x 85x 85x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 85x 2x 2x 2x 2x 2x 2x 2x 2x 2x 62x 62x 45x 3x 3x 3x 3x 45x 45x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 10x 10x 10x 10x     10x         10x 10x 10x 17x 7x 7x 17x 17x 17x 13x 17x 17x 17x 17x 17x 62x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 3x 3x 3x 6x 6x 6x 6x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 152x 152x 152x 152x 152x 152x 152x 152x 152x                       152x 152x 3x 3x 3x 149x 149x 149x 149x 152x 46x 46x 46x 46x 46x 46x 46x 152x 14x 103x 89x 89x 89x 149x 152x 120x 10x 118x 110x 110x 120x 152x 29x 29x 29x 152x 2x 2x 2x 2x 2x 2x 2x 2x 3x 3x 3x 3x 3x 3x 3x 3x 2x 1x 1x 1x 1x 2x 3x 1x 1x 1x 3x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 110x 110x 110x 110x 110x 110x 110x 110x 110x 110x 110x 110x 110x 110x 110x 110x 110x 110x 110x 110x 110x 110x 5x 5x 5x 5x 5x 5x 5x 110x 110x 110x 110x 5x 5x 110x 110x 54x 54x 54x 110x 56x 56x 56x 110x 2x 2x 2x 2x 2x 2x 2x 2x 54x 54x 54x 54x 54x 54x 2x 2x 2x 2x 452x 452x 452x 452x 452x 2x 2x 2x 2x 2x 2x 259x 259x 259x 2x 2x 2x 2x 2x 2x 2x 598x 598x 598x 598x 598x 598x 598x 598x 598x 598x 598x 598x 598x 598x 598x 598x 598x 598x 598x 598x 598x 598x 598x 598x 598x 598x 598x 598x 598x 598x 598x 598x 598x 598x 598x 598x 598x 598x 972x 972x 972x 972x 972x 470x 470x 972x 598x 646x 68x 646x 26x 26x 26x 2x 2x 26x 578x 26x 26x 17x 17x 17x 1x 1x 1x 1x 17x 17x 26x 9x 9x 552x 400x 1x 1x 1x 1x 1x 399x 400x 7x 7x 399x 400x 2x 2x 399x 399x 399x 400x 275x 275x 275x 275x 275x 275x 275x 275x 279x 279x 279x 38x 279x 275x 275x 275x 18x 18x 18x 18x 275x 275x 400x 124x 124x 526x 126x 33x 125x 93x 93x 93x 93x 93x 9x 9x 9x 9x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 126x 646x 598x 598x 7x 7x 598x 598x 24x 24x 25x 24x 24x 24x 24x 598x 598x 598x 598x 598x 598x 829x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 824x 824x 824x 829x 235x 235x 235x 235x 235x 235x 235x 82x 82x 235x 824x 829x 829x 829x 598x 598x 598x 598x 598x 296x 296x 236x 236x 236x 296x 296x 296x 296x 153x 153x 153x 153x 153x 153x 296x 83x 83x 296x 598x 598x 205x 205x 598x 598x 598x 475x 598x 598x 26x 26x 598x 598x 598x 598x 598x 598x 598x 598x 598x 598x 598x 598x 33x 33x 33x 33x 33x 33x 33x 33x 598x 598x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 598x 598x 598x 598x 598x 598x 598x 598x 598x 2x 2x 2x 2x 2x 2x 2x 2x 101x 101x 101x 101x 101x 101x 101x 101x 101x 101x 101x 101x 101x 101x 101x 129x 129x 120x 120x 120x 129x 129x 21x 21x 21x 21x 21x 21x 21x 21x 129x 101x 101x 101x 101x 101x 101x 101x 101x 101x 21x 21x 21x 101x 101x 101x 101x 101x 101x 101x 101x 101x 28x 28x 28x 101x 20x 20x 101x 101x 101x 2x 2x 2x 2x 74x 74x 74x 71x 71x 71x     71x 71x 74x 74x 74x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 3744x 3744x 3744x 3744x 3744x 3744x 3744x 3744x 3744x 3744x 3744x 3744x 3744x 161x 161x 3583x 3744x 3744x 3744x 3744x 3744x 3744x 3744x 3744x 3744x 3744x 3744x 3744x 3744x 3744x 3744x 3744x 3744x 3744x 3744x 3744x 3744x 3744x 3744x 3744x 3744x 3744x 3744x 3744x 3744x 3744x 3744x 3744x 3744x 209x 209x 3583x 3583x 3583x 3583x 3583x 3583x 2468x 2468x 74x 74x 74x 74x 74x 74x 74x 2468x 2468x 3583x 3583x 3744x 1552x 1552x 1552x 1552x 1552x 1552x 1552x 1552x 1552x 1552x 1552x 1552x 1552x 3x 3x 1552x 1552x 1552x 1552x 1552x 3744x 31x 31x 2031x 1977x 1977x 1977x 1977x 1977x 1977x 1977x 138x 138x 138x 138x 138x 138x 138x 138x 138x 138x 1977x 1839x 1839x 1839x 1839x 1839x 1839x 1839x 1839x 1839x 923x 923x 1835x 916x 916x 916x 2x 2x 916x 916x 916x 916x 916x 916x 916x 916x 1839x 1839x 1839x 1839x 1839x 2000x 23x 23x 3583x 3744x 1130x 1130x 3583x 3583x 3583x 3729x 3529x 3529x 3529x 3529x 3529x 3583x 3583x 3583x 2x 2x 2x 2x 2x 2x 2x 2468x 2468x 2468x 2468x 42x 42x 2468x 2426x 2426x 2421x 2468x 2468x 2x 2x 2x 2x 2x 1160x 1160x 1160x 1160x 1160x 1160x 2x 2x 2x 2x 2x 1138x 1138x 1138x 1138x 1138x 2x 2x 2x 2x 2x 2x 630x 630x 630x 630x 630x 610x 610x 610x 610x 35x 35x 35x 35x 35x 35x 35x 35x 35x 610x 610x 610x 183x 183x 183x 183x 183x 183x 183x 183x 183x 183x 183x 183x 183x 33x 183x 150x 150x 610x 2x 427x 425x 425x 630x 20x 20x 20x 20x 20x 20x 20x 20x 20x 630x 630x 2x 2x 630x 1x 1x 630x 4x 4x 630x 2x 2x 630x 1x 1x 630x 630x 630x 2x 2x 2x 2x 2x 2x 604x 604x 604x 604x 604x 604x 604x 587x 587x 587x 587x 587x 171x 171x 171x 117x 117x 171x 171x 136x 113x 113x 113x 113x 136x     136x 136x 136x 136x 136x 136x 136x 171x 35x     35x 35x 171x 171x 171x 171x 171x 171x 171x 171x 171x 171x 171x 171x 416x 586x 4x 4x 416x 416x 416x 416x 416x 416x 416x 416x 586x   586x   416x     416x 416x 416x 603x 17x 17x 17x 17x 433x 433x 604x 10x 603x 423x 423x 604x 2x 2x 2x 2x 2x 181x 181x 181x 181x 181x 181x 181x 181x 2x 181x 2x 2x 2x 181x 181x 181x 181x 181x 181x 181x 181x 181x 181x 181x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 5500x 5500x 5500x 5500x 5500x 5500x 5500x 5500x 5500x 5500x 5500x 5500x 4213x 3469x 3469x 3469x 2620x 2620x 2620x 2620x 2620x 849x 849x 849x 849x 849x 849x 849x 849x 849x 849x 849x 849x 849x 3469x 172x 3458x 630x 677x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 849x 849x 849x 4213x 744x 744x 744x 744x 744x 744x 744x 744x 744x 54x 744x 690x 593x 690x 593x 685x 97x 97x 744x 744x 744x 744x 4213x 5500x 5500x 9872x 9872x 9872x 6052x 9872x 3820x 1613x 1613x 1613x 3820x 3820x 3820x 3820x 3820x 3820x         3820x 3820x 84x 84x 3820x 3736x 3736x 3736x 3736x 3736x 3736x 3736x 3736x 3736x 3736x 3736x 3736x 3736x 3736x 3820x 3820x 9872x 9872x 5499x 2600x 2600x 5500x 2x 2x 2x 2x 2x 2x 5329x 5329x 5329x 5329x 5191x 5191x 5191x 5191x 5329x 5329x 2x 2x 2x 2x 2x 2x 1762x 1762x 1762x 1762x 26x 26x 1736x 1762x     1736x 1762x 1688x 1688x 1017x 1688x 671x 671x 671x 671x 671x 671x 671x 671x 1688x 48x 48x 48x 2x 2x 2x 2x 2x 2x 2x 797x 797x 797x 797x 797x 797x 797x 797x 797x 797x 797x 2712x 2712x 2712x 70x 12x 12x 70x 70x 2712x 797x 797x 2712x 2712x 2712x 1174x 1174x 2709x 9x 9x 9x 9x 1538x 1529x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 1529x 1509x 1509x 1529x 1529x 2712x 797x 797x 797x 797x 2x 2x 2x 2x 2839x 2839x 2x 2x 5x 5x 2x 2x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 2x 2x 126x 126x 126x 64x 64x 64x 64x 64x 64x 64x 64x 64x 64x 64x 64x 64x 2x 2x 126x 62x 62x 62x 62x 62x 62x 155x 155x 155x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x     62x 62x 155x 155x 155x 62x 2x 2x                                                     2x 2x 53x 53x 53x 53x 53x 53x 53x 53x 53x 18x 18x 53x 53x 53x 7x 7x 53x 53x 30x 53x 23x 23x 23x 23x 23x 23x 23x 23x 23x 2x 2x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 2x 2x   2x 2x   2x 2x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 18x 18x 135x 135x 2x 2x 3529x 3529x 3529x 3529x 71x 71x 71x 71x 71x 71x 71x 3529x 3529x 6x 6x 6x 3529x 3x 3x 3523x 3523x 3523x 3523x 3523x 3523x 3523x 3523x 3523x 3523x 3523x 3523x 3523x 3523x 3523x 3523x 3523x 3523x 3523x 3523x 3523x 3523x 3523x 3523x 3523x 3523x 3523x 3523x 3523x 3529x 3529x 3529x 3529x 3529x 3529x 3529x 7x 7x 7x 7x 7x 7x 3523x 3529x 2326x 1306x 1306x 1x 1x 1306x 1306x 129x 1306x 76x 76x 1306x 1230x 6x 6x 1230x 6x 6x 2326x 86x 86x 86x 86x 1x 1x 1018x 22x 933x 32x 912x 10x 880x 870x 282x 80x 80x 282x 123x 123x 123x 188x 79x 79x 75x 79x 6x 6x 870x     870x 870x 2326x 3523x 3529x 1x 1x 1x 1x 1x 1x 3523x 3529x 6x 6x 3523x 3529x 222x 222x 3523x 3529x 7x 7x 3523x 3529x 33x 33x 3523x 3523x 3523x 3523x 3523x 3523x 3523x 3523x 3529x 85x 1x 1x 85x 85x 85x 85x 85x 85x 85x 85x 85x 3529x 3438x 1250x 176x 176x   176x     176x 176x 176x 1074x 1250x 110x 110x 110x 964x 964x 1069x 961x 1250x 812x 812x 812x 812x 812x 809x 809x 809x 809x 809x 809x 809x 809x 809x 809x 809x 812x 155x 155x 1231x 1250x 1250x 1250x 1250x 3438x 3523x 3523x 3529x 2x 2x 3523x 3523x 3523x 3523x 3523x 3529x 1x 1x 3523x 3523x 3523x 3523x 3523x 3523x 3523x 3523x 3523x 3523x 3523x 3523x 3523x 3523x 3523x 3528x 3514x 3514x 3529x 3529x 3529x 3529x 3529x 3529x 3529x 3529x 3529x 3529x 3529x 3529x 7x 7x 3523x 3523x 3523x 3523x 1795x 1795x 1795x 1795x 1795x 3523x 3523x 3523x 3523x 3523x 3529x       3523x 3529x 3253x 3253x 2x 2x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 30x 10x 1x 1x 10x 30x 7x 20x 2x 13x 1x 10x   10x 10x 10x 30x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 9x 8x 8x 9x 9x 62x 62x 62x 62x 8x 8x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 2x 2x 452x 452x 452x 452x 452x 368x 368x 452x 452x 37x 37x 452x 452x 452x 452x 452x 452x 452x 452x 452x 452x 93x 452x 90x 90x 90x 20x 20x 90x 90x 90x 90x 90x 10x 90x 10x 10x 86x 80x 80x 452x 362x 362x 452x 452x 452x 452x 452x 452x 93x 285x 72x 93x 452x 7x 7x 452x 452x 84x 84x 452x 452x 68x 68x 452x 452x 452x 452x 452x 398x 398x 398x 398x 9x 9x 398x 398x 452x 452x 452x 62x 62x 62x 62x 62x 62x 452x 452x 37x 452x 415x 415x 415x 415x 415x 415x 415x 452x 452x 452x 452x 452x 452x 502x 64x         64x 64x 64x 64x 64x 64x 64x 64x 64x 64x 64x 64x 64x 64x 64x 20x 20x 20x 64x 44x 44x 44x 44x 44x 44x 502x 452x 452x 452x 452x 452x 452x 452x 452x 452x 452x 452x 790x 790x 452x 452x 55x 55x 55x 55x 81x 81x 55x 55x 452x 452x 452x 452x 452x 411x 411x 411x 411x 411x 411x 411x 452x 41x 41x 41x 41x 91x 91x 91x 91x 91x 91x 91x 91x 91x 91x 91x 91x 91x 91x 91x 91x 91x 91x 3x 3x 91x 41x 452x 452x 452x 452x 452x 452x 93x 93x 93x 6x 6x 6x 6x 93x 93x 452x 452x 452x 1x 1x 1x 1x 452x 452x 1x 1x 1x 1x 452x 452x 452x 452x 452x 452x 452x 452x 452x 452x 452x 26x 26x 26x 26x 26x 26x 26x 452x 452x 2x 2x 301x 301x 301x 301x 301x 301x 301x 301x 301x 301x 301x 301x 301x 100x 100x 100x 85x 85x 85x 100x 100x 100x 301x 301x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 301x 301x 2x 2x 81x 81x 81x 81x 81x 81x 81x 81x 81x 81x 42x 42x 42x 81x 81x 81x 69x 69x 63x 63x 63x 69x 69x 69x 81x 81x 81x 48x 48x 45x 45x 45x 48x 48x 48x 81x 81x 81x 81x 2x 2x 14x 14x 14x 14x 14x 14x 2x 2x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 22x 22x 22x 22x 22x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x     9x 9x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 5x 5x 40x 40x 40x 40x 40x 34x 40x 6x 6x 2x 2x 2x 2x 1x 2x 2x 423x 2x 2x 44x 44x 44x 26x 26x 44x 44x 44x 44x 44x 44x 44x 44x 44x 44x 44x 44x 44x 44x 26x 26x 44x 44x 44x 44x 2x 2x 292x 292x 292x 292x 292x 292x 292x 292x 292x 292x 292x 292x 292x 292x 292x 292x 292x 292x 292x 292x 292x 6x 6x 6x 6x 6x 6x 6x 6x 6x 292x 286x 286x 286x 286x     286x 286x 286x 3x 3x 3x 3x 3x 3x 3x 286x 286x 286x 286x 286x 4x 4x 286x 286x 286x     286x     286x     286x     286x     286x     286x     286x     286x     286x     286x     286x 286x 286x 286x 286x 286x     286x 286x 286x 286x 286x     286x 286x 286x 123x 123x 33x 123x 90x 90x 123x 123x 286x 286x     286x 286x 68x 68x 286x 286x 286x 6x 6x 6x 6x 6x 6x 6x 6x 286x 286x 286x 19x 19x 286x 2x 2x 286x 61x 61x 61x 18x 18x 61x 61x 61x 61x 61x 61x 61x 61x 61x 61x 201x 201x 201x 30x 61x 61x 61x 61x 30x 30x 30x 30x 30x 30x 30x 61x 61x 61x 61x 61x 61x 61x 61x 61x 61x 61x 61x 286x 286x   286x 286x 292x 292x 292x 292x 68x 290x 224x 224x 2x 2x 554x 554x 554x 554x 554x 554x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 553x 553x 2x 2x 6x 6x 6x 2x 2x 38x 38x 38x 38x 2x 2x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 2x 2x 5x 5x 5x 2x 2x 103x 103x 103x 8x 8x 8x 8x 14x 14x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 6x 8x 8x 8x 8x 8x 8x 8x 8x 8x 103x 95x 95x 95x 95x 95x 95x 2x 2x 123x 2x 2x 31x 31x 31x 31x 52x 24x 24x 52x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 2x 2x 231x 231x 231x 231x 231x 231x 231x 231x 231x 231x 231x 231x 231x 231x 231x 231x 231x 231x 167x 4x 4x 4x 167x 162x 162x 77x 77x 162x 83x 61x 83x 22x 22x 83x 163x 1x 1x 167x 231x 231x 231x 231x 231x 231x 231x 231x 231x 231x 231x 231x 43x 43x 231x 231x 231x 231x 231x 231x 231x 231x 2x 2x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 2x 2x 13x 13x 8x 8x 8x 8x 8x 8x 8x 8x 8x 11x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 2x 2x 4x 4x 4x 4x 2x 2x 13x 13x 13x 13x 2x 2x 4x 4x 4x 4x 2x 2x 2x 2x  
import {
	extract_identifiers,
	extract_paths,
	is_event_attribute,
	is_text_attribute,
	object,
	unwrap_optional
} from '../../../../utils/ast.js';
import { binding_properties } from '../../../bindings.js';
import { clean_nodes, determine_namespace_for_children, infer_namespace } from '../../utils.js';
import {
	DOMProperties,
	LoadErrorElements,
	PassiveEvents,
	VoidElements
} from '../../../constants.js';
import { is_custom_element_node, is_element_node } from '../../../nodes.js';
import * as b from '../../../../utils/builders.js';
import {
	with_loc,
	function_visitor,
	get_assignment_value,
	serialize_get_binding,
	serialize_set_binding
} from '../utils.js';
import {
	AttributeAliases,
	DOMBooleanAttributes,
	EACH_INDEX_REACTIVE,
	EACH_IS_ANIMATED,
	EACH_IS_CONTROLLED,
	EACH_IS_STRICT_EQUALS,
	EACH_ITEM_REACTIVE,
	EACH_KEYED,
	TEMPLATE_FRAGMENT,
	TEMPLATE_USE_IMPORT_NODE,
	TRANSITION_GLOBAL,
	TRANSITION_IN,
	TRANSITION_OUT
} from '../../../../../constants.js';
import { escape_html } from '../../../../../escaping.js';
import { regex_is_valid_identifier } from '../../../patterns.js';
import { javascript_visitors_runes } from './javascript-runes.js';
import { sanitize_template_string } from '../../../../utils/sanitize_template_string.js';
import { walk } from 'zimmerframe';
import { locator } from '../../../../state.js';
 
/**
 * @param {import('#compiler').RegularElement | import('#compiler').SvelteElement} element
 * @param {import('#compiler').Attribute} attribute
 * @param {{ state: { metadata: { namespace: import('#compiler').Namespace }}}} context
 */
function get_attribute_name(element, attribute, context) {
	let name = attribute.name;
	if (
		!element.metadata.svg &&
		!element.metadata.mathml &&
		context.state.metadata.namespace !== 'foreign'
	) {
		name = name.toLowerCase();
		if (name in AttributeAliases) {
			name = AttributeAliases[name];
		}
	}
	return name;
}
 
/**
 * Serializes each style directive into something like `$.set_style(element, style_property, value)`
 * and adds it either to init or update, depending on whether or not the value or the attributes are dynamic.
 * @param {import('#compiler').StyleDirective[]} style_directives
 * @param {import('estree').Identifier} element_id
 * @param {import('../types.js').ComponentContext} context
 * @param {boolean} is_attributes_reactive
 */
function serialize_style_directives(style_directives, element_id, context, is_attributes_reactive) {
	const state = context.state;
 
	for (const directive of style_directives) {
		let value =
			directive.value === true
				? serialize_get_binding({ name: directive.name, type: 'Identifier' }, context.state)
				: serialize_attribute_value(directive.value, context)[1];
 
		const update = b.stmt(
			b.call(
				'$.set_style',
				element_id,
				b.literal(directive.name),
				value,
				/** @type {import('estree').Expression} */ (
					directive.modifiers.includes('important') ? b.true : undefined
				)
			)
		);
 
		const contains_call_expression =
			Array.isArray(directive.value) &&
			directive.value.some(
				(v) => v.type === 'ExpressionTag' && v.metadata.contains_call_expression
			);
 
		if (!is_attributes_reactive && contains_call_expression) {
			state.init.push(serialize_update(update));
		} else if (is_attributes_reactive || directive.metadata.dynamic || contains_call_expression) {
			state.update.push(update);
		} else {
			state.init.push(update);
		}
	}
}
 
/**
 * For unfortunate legacy reasons, directive names can look like this `use:a.b-c`
 * This turns that string into a member expression
 * @param {string} name
 */
function parse_directive_name(name) {
	// this allow for accessing members of an object
	const parts = name.split('.');
	let part = /** @type {string} */ (parts.shift());
 
	/** @type {import('estree').Identifier | import('estree').MemberExpression} */
	let expression = b.id(part);
 
	while ((part = /** @type {string} */ (parts.shift()))) {
		const computed = !regex_is_valid_identifier.test(part);
		expression = b.member(expression, computed ? b.literal(part) : b.id(part), computed);
	}
 
	return expression;
}
 
/**
 * Serializes each class directive into something like `$.class_toogle(element, class_name, value)`
 * and adds it either to init or update, depending on whether or not the value or the attributes are dynamic.
 * @param {import('#compiler').ClassDirective[]} class_directives
 * @param {import('estree').Identifier} element_id
 * @param {import('../types.js').ComponentContext} context
 * @param {boolean} is_attributes_reactive
 */
function serialize_class_directives(class_directives, element_id, context, is_attributes_reactive) {
	const state = context.state;
	for (const directive of class_directives) {
		const value = /** @type {import('estree').Expression} */ (context.visit(directive.expression));
		const update = b.stmt(b.call('$.toggle_class', element_id, b.literal(directive.name), value));
		const contains_call_expression = directive.expression.type === 'CallExpression';
 
		if (!is_attributes_reactive && contains_call_expression) {
			state.init.push(serialize_update(update));
		} else if (is_attributes_reactive || directive.metadata.dynamic || contains_call_expression) {
			state.update.push(update);
		} else {
			state.init.push(update);
		}
	}
}
 
/**
 * @param {import('#compiler').Binding[]} references
 * @param {import('../types.js').ComponentContext} context
 */
function serialize_transitive_dependencies(references, context) {
	/** @type {Set<import('#compiler').Binding>} */
	const dependencies = new Set();
 
	for (const ref of references) {
		const deps = collect_transitive_dependencies(ref);
		for (const dep of deps) {
			dependencies.add(dep);
		}
	}
 
	return [...dependencies].map((dep) => serialize_get_binding({ ...dep.node }, context.state));
}
 
/**
 * @param {import('#compiler').Binding} binding
 * @param {Set<import('#compiler').Binding>} seen
 * @returns {import('#compiler').Binding[]}
 */
function collect_transitive_dependencies(binding, seen = new Set()) {
	if (binding.kind !== 'legacy_reactive') return [];
 
	for (const dep of binding.legacy_dependencies) {
		if (!seen.has(dep)) {
			seen.add(dep);
			for (const transitive_dep of collect_transitive_dependencies(dep, seen)) {
				seen.add(transitive_dep);
			}
		}
	}
 
	return [...seen];
}
 
/**
 * Special case: if we have a value binding on a select element, we need to set up synchronization
 * between the value binding and inner signals, for indirect updates
 * @param {import('#compiler').BindDirective} value_binding
 * @param {import('../types.js').ComponentContext} context
 */
function setup_select_synchronization(value_binding, context) {
	if (context.state.analysis.runes) return;
 
	let bound = value_binding.expression;
	while (bound.type === 'MemberExpression') {
		bound = /** @type {import('estree').Identifier | import('estree').MemberExpression} */ (
			bound.object
		);
	}
 
	/** @type {string[]} */
	const names = [];
 
	for (const [name, refs] of context.state.scope.references) {
		if (
			refs.length > 0 &&
			// prevent infinite loop
			name !== bound.name
		) {
			names.push(name);
		}
	}
 
	const invalidator = b.call(
		'$.invalidate_inner_signals',
		b.thunk(
			b.block(
				names.map((name) => {
					const serialized = serialize_get_binding(b.id(name), context.state);
					return b.stmt(serialized);
				})
			)
		)
	);
 
	context.state.init.push(
		b.stmt(
			b.call(
				'$.template_effect',
				b.thunk(
					b.block([
						b.stmt(
							/** @type {import('estree').Expression} */ (context.visit(value_binding.expression))
						),
						b.stmt(invalidator)
					])
				)
			)
		)
	);
}
 
/**
 * @param {Array<import('#compiler').Attribute | import('#compiler').SpreadAttribute>} attributes
 * @param {import('../types.js').ComponentContext} context
 * @param {import('#compiler').RegularElement} element
 * @param {import('estree').Identifier} element_id
 * @param {boolean} needs_select_handling
 */
function serialize_element_spread_attributes(
	attributes,
	context,
	element,
	element_id,
	needs_select_handling
) {
	let needs_isolation = false;
 
	/** @type {import('estree').ObjectExpression['properties']} */
	const values = [];
 
	for (const attribute of attributes) {
		if (attribute.type === 'Attribute') {
			const name = get_attribute_name(element, attribute, context);
			// TODO: handle contains_call_expression
			const [, value] = serialize_attribute_value(attribute.value, context);
 
			if (
				is_event_attribute(attribute) &&
				(attribute.value[0].expression.type === 'ArrowFunctionExpression' ||
					attribute.value[0].expression.type === 'FunctionExpression')
			) {
				// Give the event handler a stable ID so it isn't removed and readded on every update
				const id = context.state.scope.generate('event_handler');
				context.state.init.push(b.var(id, value));
				values.push(b.init(attribute.name, b.id(id)));
			} else {
				values.push(b.init(name, value));
			}
		} else {
			values.push(b.spread(/** @type {import('estree').Expression} */ (context.visit(attribute))));
		}
 
		needs_isolation ||=
			attribute.type === 'SpreadAttribute' && attribute.metadata.contains_call_expression;
	}
 
	const lowercase_attributes =
		element.metadata.svg || element.metadata.mathml || is_custom_element_node(element)
			? b.false
			: b.true;
	const id = context.state.scope.generate('attributes');
 
	const update = b.stmt(
		b.assignment(
			'=',
			b.id(id),
			b.call(
				'$.set_attributes',
				element_id,
				b.id(id),
				b.object(values),
				lowercase_attributes,
				b.literal(context.state.analysis.css.hash)
			)
		)
	);
 
	context.state.init.push(b.let(id));
 
	// objects could contain reactive getters -> play it safe and always assume spread attributes are reactive
	if (needs_isolation) {
		context.state.init.push(serialize_update(update));
	} else {
		context.state.update.push(update);
	}
 
	if (needs_select_handling) {
		context.state.init.push(
			b.stmt(b.call('$.init_select', element_id, b.thunk(b.member(b.id(id), b.id('value')))))
		);
		context.state.update.push(
			b.if(
				b.binary('in', b.literal('value'), b.id(id)),
				b.block([
					// This ensures a one-way street to the DOM in case it's <select {value}>
					// and not <select bind:value>. We need it in addition to $.init_select
					// because the select value is not reflected as an attribute, so the
					// mutation observer wouldn't notice.
					b.stmt(b.call('$.select_option', element_id, b.member(b.id(id), b.id('value'))))
				])
			)
		);
	}
}
 
/**
 * Serializes dynamic element attribute assignments.
 * Returns the `true` if spread is deemed reactive.
 * @param {Array<import('#compiler').Attribute | import('#compiler').SpreadAttribute>} attributes
 * @param {import('../types.js').ComponentContext} context
 * @param {import('estree').Identifier} element_id
 * @returns {boolean}
 */
function serialize_dynamic_element_attributes(attributes, context, element_id) {
	if (attributes.length === 0) {
		if (context.state.analysis.css.hash) {
			context.state.init.push(
				b.stmt(b.call('$.set_class', element_id, b.literal(context.state.analysis.css.hash)))
			);
		}
		return false;
	}
 
	// TODO why are we always treating this as a spread? needs docs, if that's not an error
 
	let needs_isolation = false;
	let is_reactive = false;
 
	/** @type {import('estree').ObjectExpression['properties']} */
	const values = [];
 
	for (const attribute of attributes) {
		if (attribute.type === 'Attribute') {
			const [, value] = serialize_attribute_value(attribute.value, context);
 
			if (
				is_event_attribute(attribute) &&
				(attribute.value[0].expression.type === 'ArrowFunctionExpression' ||
					attribute.value[0].expression.type === 'FunctionExpression')
			) {
				// Give the event handler a stable ID so it isn't removed and readded on every update
				const id = context.state.scope.generate('event_handler');
				context.state.init.push(b.var(id, value));
				values.push(b.init(attribute.name, b.id(id)));
			} else {
				values.push(b.init(attribute.name, value));
			}
		} else {
			values.push(b.spread(/** @type {import('estree').Expression} */ (context.visit(attribute))));
		}
 
		is_reactive ||=
			attribute.metadata.dynamic ||
			// objects could contain reactive getters -> play it safe and always assume spread attributes are reactive
			attribute.type === 'SpreadAttribute';
		needs_isolation ||=
			attribute.type === 'SpreadAttribute' && attribute.metadata.contains_call_expression;
	}
 
	if (needs_isolation || is_reactive) {
		const id = context.state.scope.generate('attributes');
		context.state.init.push(b.let(id));
 
		const update = b.stmt(
			b.assignment(
				'=',
				b.id(id),
				b.call(
					'$.set_dynamic_element_attributes',
					element_id,
					b.id(id),
					b.object(values),
					b.literal(context.state.analysis.css.hash)
				)
			)
		);
 
		if (needs_isolation) {
			context.state.init.push(serialize_update(update));
			return false;
		}
 
		context.state.update.push(update);
		return true;
	}
 
	context.state.init.push(
		b.stmt(
			b.call(
				'$.set_dynamic_element_attributes',
				element_id,
				b.literal(null),
				b.object(values),
				b.literal(context.state.analysis.css.hash)
			)
		)
	);
	return false;
}
 
/**
 * Serializes an assignment to an element property by adding relevant statements to either only
 * the init or the the init and update arrays, depending on whether or not the value is dynamic.
 * Resulting code for static looks something like this:
 * ```js
 * element.property = value;
 * // or
 * $.set_attribute(element, property, value);
 * });
 * ```
 * Resulting code for dynamic looks something like this:
 * ```js
 * let value;
 * $.template_effect(() => {
 * 	if (value !== (value = 'new value')) {
 * 		element.property = value;
 * 		// or
 * 		$.set_attribute(element, property, value);
 * 	}
 * });
 * ```
 * Returns true if attribute is deemed reactive, false otherwise.
 * @param {import('#compiler').RegularElement} element
 * @param {import('estree').Identifier} node_id
 * @param {import('#compiler').Attribute} attribute
 * @param {import('../types.js').ComponentContext} context
 * @returns {boolean}
 */
function serialize_element_attribute_update_assignment(element, node_id, attribute, context) {
	const state = context.state;
	const name = get_attribute_name(element, attribute, context);
	const is_svg = context.state.metadata.namespace === 'svg';
	const is_mathml = context.state.metadata.namespace === 'mathml';
	let [contains_call_expression, value] = serialize_attribute_value(attribute.value, context);
 
	// The foreign namespace doesn't have any special handling, everything goes through the attr function
	if (context.state.metadata.namespace === 'foreign') {
		const statement = b.stmt(b.call('$.set_attribute', node_id, b.literal(name), value));

		if (attribute.metadata.dynamic) {
			const id = state.scope.generate(`${node_id.name}_${name}`);
			serialize_update_assignment(state, id, undefined, value, statement);
			return true;
		} else {
			state.init.push(statement);
			return false;
		}
	}
 
	if (name === 'autofocus') {
		state.init.push(b.stmt(b.call('$.autofocus', node_id, value)));
		return false;
	}
 
	/** @type {import('estree').Statement} */
	let update;
 
	if (name === 'class') {
		update = b.stmt(
			b.call(
				is_svg ? '$.set_svg_class' : is_mathml ? '$.set_mathml_class' : '$.set_class',
				node_id,
				value
			)
		);
	} else if (DOMProperties.includes(name)) {
		update = b.stmt(b.assignment('=', b.member(node_id, b.id(name)), value));
	} else {
		const callee = name.startsWith('xlink') ? '$.set_xlink_attribute' : '$.set_attribute';
		update = b.stmt(b.call(callee, node_id, b.literal(name), value));
	}
 
	if (attribute.metadata.dynamic) {
		if (contains_call_expression) {
			state.init.push(serialize_update(update));
		} else {
			state.update.push(update);
		}
		return true;
	} else {
		state.init.push(update);
		return false;
	}
}
 
/**
 * Like `serialize_element_attribute_update_assignment` but without any special attribute treatment.
 * @param {import('estree').Identifier}	node_id
 * @param {import('#compiler').Attribute} attribute
 * @param {import('../types.js').ComponentContext} context
 * @returns {boolean}
 */
function serialize_custom_element_attribute_update_assignment(node_id, attribute, context) {
	const state = context.state;
	const name = attribute.name; // don't lowercase, as we set the element's property, which might be case sensitive
	let [contains_call_expression, value] = serialize_attribute_value(attribute.value, context);
 
	const update = b.stmt(b.call('$.set_custom_element_data', node_id, b.literal(name), value));
 
	if (attribute.metadata.dynamic) {
		if (contains_call_expression) {
			state.init.push(serialize_update(update));
		} else {
			state.update.push(update);
		}
		return true;
	} else {
		state.init.push(update);
		return false;
	}
}
 
/**
 * Serializes an assignment to the value property of a `<select>`, `<option>` or `<input>` element
 * that needs the hidden `__value` property.
 * Returns true if attribute is deemed reactive, false otherwise.
 * @param {string} element
 * @param {import('estree').Identifier} node_id
 * @param {import('#compiler').Attribute} attribute
 * @param {import('../types.js').ComponentContext} context
 * @returns {boolean}
 */
function serialize_element_special_value_attribute(element, node_id, attribute, context) {
	const state = context.state;
	const [, value] = serialize_attribute_value(attribute.value, context);
 
	const inner_assignment = b.assignment(
		'=',
		b.member(node_id, b.id('value')),
		b.conditional(
			b.binary('==', b.literal(null), b.assignment('=', b.member(node_id, b.id('__value')), value)),
			b.literal(''), // render null/undefined values as empty string to support placeholder options
			value
		)
	);
	const is_reactive = attribute.metadata.dynamic;
	const is_select_with_value =
		// attribute.metadata.dynamic would give false negatives because even if the value does not change,
		// the inner options could still change, so we need to always treat it as reactive
		element === 'select' && attribute.value !== true && !is_text_attribute(attribute);
 
	const update = b.stmt(
		is_select_with_value
			? b.sequence([
					inner_assignment,
					// This ensures a one-way street to the DOM in case it's <select {value}>
					// and not <select bind:value>. We need it in addition to $.init_select
					// because the select value is not reflected as an attribute, so the
					// mutation observer wouldn't notice.
					b.call('$.select_option', node_id, value)
				])
			: inner_assignment
	);
 
	if (is_select_with_value) {
		state.init.push(b.stmt(b.call('$.init_select', node_id, b.thunk(value))));
	}
 
	if (is_reactive) {
		const id = state.scope.generate(`${node_id.name}_value`);
		serialize_update_assignment(state, id, undefined, value, update);
		return true;
	} else {
		state.init.push(update);
		return false;
	}
}
 
/**
 * @param {import('../types.js').ComponentClientTransformState} state
 * @param {string} id
 * @param {import('estree').Expression | undefined} init
 * @param {import('estree').Expression} value
 * @param {import('estree').ExpressionStatement} update
 */
function serialize_update_assignment(state, id, init, value, update) {
	state.init.push(b.var(id, init));
	state.update.push(
		b.if(b.binary('!==', b.id(id), b.assignment('=', b.id(id), value)), b.block([update]))
	);
}
 
/**
 * @param {import('../types.js').ComponentContext} context
 */
function collect_parent_each_blocks(context) {
	return /** @type {import('#compiler').EachBlock[]} */ (
		context.path.filter((node) => node.type === 'EachBlock')
	);
}
 
/**
 * Svelte legacy mode should use safe equals in most places, runes mode shouldn't
 * @param {import('../types.js').ComponentClientTransformState} state
 * @param {import('estree').Expression} arg
 */
function create_derived(state, arg) {
	return b.call(state.analysis.runes ? '$.derived' : '$.derived_safe_equal', arg);
}
 
/**
 * @param {import('#compiler').Component | import('#compiler').SvelteComponent | import('#compiler').SvelteSelf} node
 * @param {string} component_name
 * @param {import('../types.js').ComponentContext} context
 * @returns {import('estree').Statement}
 */
function serialize_inline_component(node, component_name, context) {
	/** @type {Array<import('estree').Property[] | import('estree').Expression>} */
	const props_and_spreads = [];
 
	/** @type {import('estree').ExpressionStatement[]} */
	const lets = [];
 
	/** @type {Record<string, import('#compiler').TemplateNode[]>} */
	const children = {};
 
	/** @type {Record<string, import('estree').Expression[]>} */
	const events = {};
 
	/** @type {import('estree').Property[]} */
	const custom_css_props = [];
 
	/** @type {import('estree').Identifier | import('estree').MemberExpression | null} */
	let bind_this = null;
 
	const binding_initializers = [];
 
	/**
	 * If this component has a slot property, it is a named slot within another component. In this case
	 * the slot scope applies to the component itself, too, and not just its children.
	 */
	let slot_scope_applies_to_itself = false;
 
	/**
	 * Components may have a children prop and also have child nodes. In this case, we assume
	 * that the child component isn't using render tags yet and pass the slot as $$slots.default.
	 * We're not doing it for spread attributes, as this would result in too many false positives.
	 */
	let has_children_prop = false;
 
	/**
	 * @param {import('estree').Property} prop
	 */
	function push_prop(prop) {
		const current = props_and_spreads.at(-1);
		const current_is_props = Array.isArray(current);
		const props = current_is_props ? current : [];
		props.push(prop);
		if (!current_is_props) {
			props_and_spreads.push(props);
		}
	}
	for (const attribute of node.attributes) {
		if (attribute.type === 'LetDirective') {
			lets.push(/** @type {import('estree').ExpressionStatement} */ (context.visit(attribute)));
		} else if (attribute.type === 'OnDirective') {
			events[attribute.name] ||= [];
			let handler = serialize_event_handler(attribute, context);
			if (attribute.modifiers.includes('once')) {
				handler = b.call('$.once', handler);
			}
			events[attribute.name].push(handler);
		} else if (attribute.type === 'SpreadAttribute') {
			const expression = /** @type {import('estree').Expression} */ (context.visit(attribute));
			if (attribute.metadata.dynamic) {
				let value = expression;
 
				if (attribute.metadata.contains_call_expression) {
					const id = b.id(context.state.scope.generate('spread_element'));
					context.state.init.push(b.var(id, b.call('$.derived', b.thunk(value))));
					value = b.call('$.get', id);
				}
 
				props_and_spreads.push(b.thunk(value));
			} else {
				props_and_spreads.push(expression);
			}
		} else if (attribute.type === 'Attribute') {
			if (attribute.name.startsWith('--')) {
				custom_css_props.push(
					b.init(attribute.name, serialize_attribute_value(attribute.value, context)[1])
				);
				continue;
			}
 
			if (attribute.name === 'slot') {
				slot_scope_applies_to_itself = true;
			}
 
			if (attribute.name === 'children') {
				has_children_prop = true;
			}
 
			const [, value] = serialize_attribute_value(attribute.value, context);
 
			if (attribute.metadata.dynamic) {
				let arg = value;
 
				// When we have a non-simple computation, anything other than an Identifier or Member expression,
				// then there's a good chance it needs to be memoized to avoid over-firing when read within the
				// child component.
				const should_wrap_in_derived =
					Array.isArray(attribute.value) &&
					attribute.value.some((n) => {
						return (
							n.type === 'ExpressionTag' &&
							n.expression.type !== 'Identifier' &&
							n.expression.type !== 'MemberExpression'
						);
					});
 
				if (should_wrap_in_derived) {
					const id = b.id(context.state.scope.generate(attribute.name));
					context.state.init.push(b.var(id, create_derived(context.state, b.thunk(value))));
					arg = b.call('$.get', id);
				}
 
				push_prop(b.get(attribute.name, [b.return(arg)]));
			} else {
				push_prop(b.init(attribute.name, value));
			}
		} else if (attribute.type === 'BindDirective') {
			if (attribute.name === 'this') {
				bind_this = attribute.expression;
			} else {
				const expression = /** @type {import('estree').Expression} */ (
					context.visit(attribute.expression)
				);
 
				if (context.state.options.dev) {
					binding_initializers.push(
						b.stmt(b.call(b.id('$.add_owner_effect'), b.thunk(expression), b.id(component_name)))
					);
				}
 
				push_prop(b.get(attribute.name, [b.return(expression)]));
 
				const assignment = b.assignment('=', attribute.expression, b.id('$$value'));
				push_prop(
					b.set(attribute.name, [
						b.stmt(serialize_set_binding(assignment, context, () => context.visit(assignment)))
					])
				);
			}
		}
	}
 
	if (slot_scope_applies_to_itself) {
		context.state.init.push(...lets);
	}
 
	if (Object.keys(events).length > 0) {
		const events_expression = b.object(
			Object.keys(events).map((name) =>
				b.init(name, events[name].length > 1 ? b.array(events[name]) : events[name][0])
			)
		);
		push_prop(b.init('$$events', events_expression));
	}
 
	/** @type {import('estree').Statement[]} */
	const snippet_declarations = [];
 
	// Group children by slot
	for (const child of node.fragment.nodes) {
		if (child.type === 'SnippetBlock') {
			// the SnippetBlock visitor adds a declaration to `init`, but if it's directly
			// inside a component then we want to hoist them into a block so that they
			// can be used as props without creating conflicts
			context.visit(child, {
				...context.state,
				init: snippet_declarations
			});
 
			push_prop(b.prop('init', child.expression, child.expression));
 
			continue;
		}
 
		let slot_name = 'default';
 
		if (is_element_node(child)) {
			const attribute = /** @type {import('#compiler').Attribute | undefined} */ (
				child.attributes.find(
					(attribute) => attribute.type === 'Attribute' && attribute.name === 'slot'
				)
			);
 
			if (attribute !== undefined) {
				slot_name = /** @type {import('#compiler').Text[]} */ (attribute.value)[0].data;
			}
		}
 
		children[slot_name] = children[slot_name] || [];
		children[slot_name].push(child);
	}
 
	// Serialize each slot
	/** @type {import('estree').Property[]} */
	const serialized_slots = [];
	for (const slot_name of Object.keys(children)) {
		const body = create_block(node, `${node.name}_${slot_name}`, children[slot_name], context);
		if (body.length === 0) continue;
 
		const slot_fn = b.arrow(
			[b.id('$$anchor'), b.id('$$slotProps')],
			b.block([...(slot_name === 'default' && !slot_scope_applies_to_itself ? lets : []), ...body])
		);
 
		if (slot_name === 'default' && !has_children_prop) {
			push_prop(
				b.init('children', context.state.options.dev ? b.call('$.wrap_snippet', slot_fn) : slot_fn)
			);
			// We additionally add the default slot as a boolean, so that the slot render function on the other
			// side knows it should get the content to render from $$props.children
			serialized_slots.push(b.init(slot_name, b.true));
		} else {
			serialized_slots.push(b.init(slot_name, slot_fn));
		}
	}
 
	if (serialized_slots.length > 0) {
		push_prop(b.init('$$slots', b.object(serialized_slots)));
	}
 
	const props_expression =
		props_and_spreads.length === 0 ||
		(props_and_spreads.length === 1 && Array.isArray(props_and_spreads[0]))
			? b.object(/** @type {import('estree').Property[]} */ (props_and_spreads[0]) || [])
			: b.call(
					'$.spread_props',
					...props_and_spreads.map((p) => (Array.isArray(p) ? b.object(p) : p))
				);
	/** @param {import('estree').Identifier} node_id */
	let fn = (node_id) =>
		b.call(
			context.state.options.dev
				? b.call('$.validate_component', b.id(component_name))
				: component_name,
			node_id,
			props_expression
		);
 
	if (bind_this !== null) {
		const prev = fn;
		fn = (node_id) =>
			serialize_bind_this(
				/** @type {import('estree').Identifier | import('estree').MemberExpression} */ (bind_this),
				context,
				prev(node_id)
			);
	}
 
	if (Object.keys(custom_css_props).length > 0) {
		const prev = fn;
		fn = (node_id) =>
			b.call(
				'$.css_props',
				node_id,
				// TODO would be great to do this at runtime instead. Svelte 4 also can't handle cases today
				// where it's not statically determinable whether the component is used in a svg or html context
				context.state.metadata.namespace === 'svg' || context.state.metadata.namespace === 'mathml'
					? b.false
					: b.true,
				b.thunk(b.object(custom_css_props)),
				b.arrow([b.id('$$node')], prev(b.id('$$node')))
			);
	}
 
	const statements = [
		...snippet_declarations,
		...binding_initializers,
		b.stmt(fn(context.state.node))
	];
 
	return statements.length > 1 ? b.block(statements) : statements[0];
}
 
/**
 * Serializes `bind:this` for components and elements.
 * @param {import('estree').Identifier | import('estree').MemberExpression} bind_this
 * @param {import('zimmerframe').Context<import('#compiler').SvelteNode, import('../types.js').ComponentClientTransformState>} context
 * @param {import('estree').Expression} node
 * @returns
 */
function serialize_bind_this(bind_this, context, node) {
	let i = 0;
	/** @type {Map<import('#compiler').Binding, [arg_idx: number, transformed: import('estree').Expression, expression: import('#compiler').Binding['expression']]>} */
	const each_ids = new Map();
	// Transform each reference to an each block context variable into a $$value_<i> variable
	// by temporarily changing the `expression` of the corresponding binding.
	// These $$value_<i> variables will be filled in by the bind_this runtime function through its last argument.
	// Note that we only do this for each context variables, the consequence is that the value might be stale in
	// some scenarios where the value is a member expression with changing computed parts or using a combination of multiple
	// variables, but that was the same case in Svelte 4, too. Once legacy mode is gone completely, we can revisit this.
	walk(
		bind_this,
		{},
		{
			Identifier(node) {
				const binding = context.state.scope.get(node.name);
				if (!binding || each_ids.has(binding)) return;
 
				const associated_node = Array.from(context.state.scopes.entries()).find(
					([_, scope]) => scope === binding?.scope
				)?.[0];
				if (associated_node?.type === 'EachBlock') {
					each_ids.set(binding, [
						i,
						/** @type {import('estree').Expression} */ (context.visit(node)),
						binding.expression
					]);
					binding.expression = b.id('$$value_' + i);
					i++;
				}
			}
		}
	);
 
	const bind_this_id = /** @type {import('estree').Expression} */ (context.visit(bind_this));
	const ids = Array.from(each_ids.values()).map((id) => b.id('$$value_' + id[0]));
	const assignment = b.assignment('=', bind_this, b.id('$$value'));
	const update = serialize_set_binding(assignment, context, () => context.visit(assignment));
 
	for (const [binding, [, , expression]] of each_ids) {
		// reset expressions to what they were before
		binding.expression = expression;
	}
 
	/** @type {import('estree').Expression[]} */
	const args = [node, b.arrow([b.id('$$value'), ...ids], update), b.arrow([...ids], bind_this_id)];
	// If we're mutating a property, then it might already be non-existent.
	// If we make all the object nodes optional, then it avoids any runtime exceptions.
	/** @type {import('estree').Expression | import('estree').Super} */
	let bind_node = bind_this_id;
 
	while (bind_node?.type === 'MemberExpression') {
		bind_node.optional = true;
		bind_node = bind_node.object;
	}
	if (each_ids.size) {
		args.push(b.thunk(b.array(Array.from(each_ids.values()).map((id) => id[1]))));
	}
 
	return b.call('$.bind_this', ...args);
}
 
/**
 * @param {import('../types.js').SourceLocation[]} locations
 */
function serialize_locations(locations) {
	return b.array(
		locations.map((loc) => {
			const expression = b.array([b.literal(loc[0]), b.literal(loc[1])]);
 
			if (loc.length === 3) {
				expression.elements.push(serialize_locations(loc[2]));
			}
 
			return expression;
		})
	);
}
 
/**
 * Creates a new block which looks roughly like this:
 * ```js
 * // hoisted:
 * const block_name = $.template(`...`);
 *
 * // for the main block:
 * const id = block_name();
 * // init stuff and possibly render effect
 * $.append($$anchor, id);
 * ```
 * Adds the hoisted parts to `context.state.hoisted` and returns the statements of the main block.
 * @param {import('#compiler').SvelteNode} parent
 * @param {string} name
 * @param {import('#compiler').SvelteNode[]} nodes
 * @param {import('../types.js').ComponentContext} context
 * @returns {import('estree').Statement[]}
 */
function create_block(parent, name, nodes, context) {
	const namespace = infer_namespace(context.state.metadata.namespace, parent, nodes, context.path);
 
	const { hoisted, trimmed } = clean_nodes(
		parent,
		nodes,
		context.path,
		namespace,
		context.state.preserve_whitespace,
		context.state.options.preserveComments
	);
 
	if (hoisted.length === 0 && trimmed.length === 0) {
		return [];
	}
 
	const is_single_element = trimmed.length === 1 && trimmed[0].type === 'RegularElement';
	const is_single_child_not_needing_template =
		trimmed.length === 1 &&
		(trimmed[0].type === 'SvelteFragment' || trimmed[0].type === 'TitleElement');
 
	const template_name = context.state.scope.root.unique(name);
 
	/** @type {import('estree').Statement[]} */
	const body = [];
 
	/** @type {import('estree').Statement | undefined} */
	let close = undefined;
 
	/** @type {import('../types').ComponentClientTransformState} */
	const state = {
		...context.state,
		before_init: [],
		init: [],
		update: [],
		after_update: [],
		template: [],
		locations: [],
		metadata: {
			context: {
				template_needs_import_node: false,
				template_contains_script_tag: false
			},
			namespace,
			bound_contenteditable: context.state.metadata.bound_contenteditable
		}
	};
 
	for (const node of hoisted) {
		context.visit(node, state);
	}
 
	/**
	 * @param {import('estree').Identifier} template_name
	 * @param {import('estree').Expression[]} args
	 */
	const add_template = (template_name, args) => {
		let call = b.call(get_template_function(namespace, state), ...args);
		if (context.state.options.dev) {
			call = b.call(
				'$.add_locations',
				call,
				b.member(b.id(context.state.analysis.name), b.id('filename')),
				serialize_locations(state.locations)
			);
		}
 
		context.state.hoisted.push(b.var(template_name, call));
	};
 
	if (is_single_element) {
		const element = /** @type {import('#compiler').RegularElement} */ (trimmed[0]);
 
		const id = b.id(context.state.scope.generate(element.name));
 
		context.visit(element, {
			...state,
			node: id
		});
 
		/** @type {import('estree').Expression[]} */
		const args = [b.template([b.quasi(state.template.join(''), true)], [])];
 
		if (state.metadata.context.template_needs_import_node) {
			args.push(b.literal(TEMPLATE_USE_IMPORT_NODE));
		}
 
		add_template(template_name, args);
 
		body.push(b.var(id, b.call(template_name)), ...state.before_init, ...state.init);
		close = b.stmt(b.call('$.append', b.id('$$anchor'), id));
	} else if (is_single_child_not_needing_template) {
		context.visit(trimmed[0], state);
		body.push(...state.before_init, ...state.init);
	} else if (trimmed.length > 0) {
		const id = b.id(context.state.scope.generate('fragment'));
 
		const use_space_template =
			trimmed.some((node) => node.type === 'ExpressionTag') &&
			trimmed.every((node) => node.type === 'Text' || node.type === 'ExpressionTag');
 
		if (use_space_template) {
			// special case — we can use `$.text` instead of creating a unique template
			const id = b.id(context.state.scope.generate('text'));
 
			process_children(trimmed, () => id, false, {
				...context,
				state
			});
 
			body.push(b.var(id, b.call('$.text', b.id('$$anchor'))), ...state.before_init, ...state.init);
			close = b.stmt(b.call('$.append', b.id('$$anchor'), id));
		} else {
			/** @type {(is_text: boolean) => import('estree').Expression} */
			const expression = (is_text) =>
				is_text ? b.call('$.first_child', id, b.true) : b.call('$.first_child', id);
 
			process_children(trimmed, expression, false, { ...context, state });
 
			const use_comment_template = state.template.length === 1 && state.template[0] === '<!>';
 
			if (use_comment_template) {
				// special case — we can use `$.comment` instead of creating a unique template
				body.push(b.var(id, b.call('$.comment')));
			} else {
				let flags = TEMPLATE_FRAGMENT;
 
				if (state.metadata.context.template_needs_import_node) {
					flags |= TEMPLATE_USE_IMPORT_NODE;
				}
 
				add_template(template_name, [
					b.template([b.quasi(state.template.join(''), true)], []),
					b.literal(flags)
				]);
 
				body.push(b.var(id, b.call(template_name)));
			}
 
			body.push(...state.before_init, ...state.init);
 
			close = b.stmt(b.call('$.append', b.id('$$anchor'), id));
		}
	} else {
		body.push(...state.before_init, ...state.init);
	}
 
	if (state.update.length > 0) {
		body.push(serialize_render_stmt(state));
	}
 
	body.push(...state.after_update);
 
	if (close !== undefined) {
		// It's important that close is the last statement in the block, as any previous statements
		// could contain element insertions into the template, which the close statement needs to
		// know of when constructing the list of current inner elements.
		body.push(close);
	}
 
	return body;
}
 
/**
 *
 * @param {import('#compiler').Namespace} namespace
 * @param {import('../types.js').ComponentClientTransformState} state
 * @returns
 */
function get_template_function(namespace, state) {
	const contains_script_tag = state.metadata.context.template_contains_script_tag;
	return namespace === 'svg'
		? contains_script_tag
			? '$.svg_template_with_script'
			: '$.svg_template'
		: namespace === 'mathml'
			? '$.mathml_template'
			: contains_script_tag
				? '$.template_with_script'
				: '$.template';
}
 
/**
 *
 * @param {import('estree').Statement} statement
 */
function serialize_update(statement) {
	const body =
		statement.type === 'ExpressionStatement' ? statement.expression : b.block([statement]);
 
	return b.stmt(b.call('$.template_effect', b.thunk(body)));
}
 
/**
 *
 * @param {import('../types.js').ComponentClientTransformState} state
 */
function serialize_render_stmt(state) {
	return state.update.length === 1
		? serialize_update(state.update[0])
		: b.stmt(b.call('$.template_effect', b.thunk(b.block(state.update))));
}
 
/**
 * Serializes the event handler function of the `on:` directive
 * @param {Pick<import('#compiler').OnDirective, 'name' | 'modifiers' | 'expression'>} node
 * @param {import('../types.js').ComponentContext} context
 */
function serialize_event_handler(node, { state, visit }) {
	/** @type {import('estree').Expression} */
	let handler;
 
	if (node.expression) {
		handler = node.expression;
 
		// Event handlers can be dynamic (source/store/prop/conditional etc)
		const dynamic_handler = () =>
			b.function(
				null,
				[b.rest(b.id('$$args'))],
				b.block([
					b.const('$$callback', /** @type {import('estree').Expression} */ (visit(handler))),
					b.return(
						b.call(b.member(b.id('$$callback'), b.id('apply'), false, true), b.this, b.id('$$args'))
					)
				])
			);
 
		if (handler.type === 'Identifier' || handler.type === 'MemberExpression') {
			const id = object(handler);
			const binding = id === null ? null : state.scope.get(id.name);
			if (
				binding !== null &&
				(binding.kind === 'state' ||
					binding.kind === 'frozen_state' ||
					binding.declaration_kind === 'import' ||
					binding.kind === 'legacy_reactive' ||
					binding.kind === 'derived' ||
					binding.kind === 'prop' ||
					binding.kind === 'bindable_prop' ||
					binding.kind === 'store_sub')
			) {
				handler = dynamic_handler();
			} else {
				handler = /** @type {import('estree').Expression} */ (visit(handler));
			}
		} else if (handler.type === 'ConditionalExpression' || handler.type === 'LogicalExpression') {
			handler = dynamic_handler();
		} else {
			handler = /** @type {import('estree').Expression} */ (visit(handler));
		}
	} else {
		state.analysis.needs_props = true;
 
		// Function + .call to preserve "this" context as much as possible
		handler = b.function(
			null,
			[b.id('$$arg')],
			b.block([b.stmt(b.call('$.bubble_event.call', b.this, b.id('$$props'), b.id('$$arg')))])
		);
	}
 
	if (node.modifiers.includes('stopPropagation')) {
		handler = b.call('$.stopPropagation', handler);
	}
	if (node.modifiers.includes('stopImmediatePropagation')) {
		handler = b.call('$.stopImmediatePropagation', handler);
	}
	if (node.modifiers.includes('preventDefault')) {
		handler = b.call('$.preventDefault', handler);
	}
	if (node.modifiers.includes('self')) {
		handler = b.call('$.self', handler);
	}
	if (node.modifiers.includes('trusted')) {
		handler = b.call('$.trusted', handler);
	}
 
	return handler;
}
 
/**
 * Serializes an event handler function of the `on:` directive or an attribute starting with `on`
 * @param {{name: string; modifiers: string[]; expression: import('estree').Expression | null; delegated?: import('#compiler').DelegatedEvent | null; }} node
 * @param {import('../types.js').ComponentContext} context
 */
function serialize_event(node, context) {
	const state = context.state;
 
	/** @type {import('estree').Statement} */
	let statement;
 
	if (node.expression) {
		let handler = serialize_event_handler(node, context);
		const event_name = node.name;
		const delegated = node.delegated;
 
		if (delegated != null) {
			let delegated_assignment;
 
			if (!state.events.has(event_name)) {
				state.events.add(event_name);
			}
			// Hoist function if we can, otherwise we leave the function as is
			if (delegated.type === 'hoistable') {
				if (delegated.function === node.expression) {
					const func_name = context.state.scope.root.unique('on_' + event_name);
					state.hoisted.push(b.var(func_name, handler));
					handler = func_name;
				}
				if (node.modifiers.includes('once')) {
					handler = b.call('$.once', handler);
				}
				const hoistable_params = /** @type {import('estree').Expression[]} */ (
					delegated.function.metadata.hoistable_params
				);
				// When we hoist a function we assign an array with the function and all
				// hoisted closure params.
				const args = [handler, ...hoistable_params];
				delegated_assignment = b.array(args);
			} else {
				if (node.modifiers.includes('once')) {
					handler = b.call('$.once', handler);
				}
				delegated_assignment = handler;
			}
 
			state.init.push(
				b.stmt(
					b.assignment(
						'=',
						b.member(context.state.node, b.id('__' + event_name)),
						delegated_assignment
					)
				)
			);
			return;
		}
 
		if (node.modifiers.includes('once')) {
			handler = b.call('$.once', handler);
		}
 
		const args = [
			b.literal(event_name),
			context.state.node,
			handler,
			b.literal(node.modifiers.includes('capture'))
		];
 
		if (node.modifiers.includes('passive')) {
			args.push(b.literal(true));
		} else if (node.modifiers.includes('nonpassive')) {
			args.push(b.literal(false));
		} else if (PassiveEvents.includes(node.name)) {
			args.push(b.literal(true));
		}
 
		// Events need to run in order with bindings/actions
		statement = b.stmt(b.call('$.event', ...args));
	} else {
		statement = b.stmt(
			b.call('$.event', b.literal(node.name), state.node, serialize_event_handler(node, context))
		);
	}
 
	const parent = /** @type {import('#compiler').SvelteNode} */ (context.path.at(-1));
	if (parent.type === 'SvelteDocument' || parent.type === 'SvelteWindow') {
		state.before_init.push(statement);
	} else {
		state.after_update.push(statement);
	}
}
 
/**
 * @param {import('#compiler').Attribute & { value: [import('#compiler').ExpressionTag] }} node
 * @param {import('../types').ComponentContext} context
 */
function serialize_event_attribute(node, context) {
	/** @type {string[]} */
	const modifiers = [];
 
	let event_name = node.name.slice(2);
	if (
		event_name.endsWith('capture') &&
		event_name !== 'ongotpointercapture' &&
		event_name !== 'onlostpointercapture'
	) {
		event_name = event_name.slice(0, -7);
		modifiers.push('capture');
	}
 
	serialize_event(
		{
			name: event_name,
			expression: node.value[0].expression,
			modifiers,
			delegated: node.metadata.delegated
		},
		context
	);
}
 
/**
 * Processes an array of template nodes, joining sibling text/expression nodes
 * (e.g. `{a} b {c}`) into a single update function. Along the way it creates
 * corresponding template node references these updates are applied to.
 * @param {import('#compiler').SvelteNode[]} nodes
 * @param {(is_text: boolean) => import('estree').Expression} expression
 * @param {boolean} is_element
 * @param {import('../types.js').ComponentContext} context
 */
function process_children(nodes, expression, is_element, { visit, state }) {
	const within_bound_contenteditable = state.metadata.bound_contenteditable;
 
	/** @typedef {Array<import('#compiler').Text | import('#compiler').ExpressionTag>} Sequence */
 
	/** @type {Sequence} */
	let sequence = [];
 
	/**
	 * @param {Sequence} sequence
	 */
	function flush_sequence(sequence) {
		if (sequence.length === 1) {
			const node = sequence[0];
 
			if (node.type === 'Text') {
				let prev = expression;
				expression = () => b.call('$.sibling', prev(true));
				state.template.push(node.raw);
				return;
			}
 
			state.template.push(' ');
 
			const text_id = get_node_id(expression(true), state, 'text');
 
			const update = b.stmt(
				b.call(
					'$.set_text',
					text_id,
					/** @type {import('estree').Expression} */ (visit(node.expression))
				)
			);
 
			if (node.metadata.contains_call_expression && !within_bound_contenteditable) {
				state.init.push(serialize_update(update));
			} else if (node.metadata.dynamic && !within_bound_contenteditable) {
				state.update.push(update);
			} else {
				state.init.push(
					b.stmt(
						b.assignment(
							'=',
							b.member(text_id, b.id('nodeValue')),
							b.call(
								'$.stringify',
								/** @type {import('estree').Expression} */ (visit(node.expression))
							)
						)
					)
				);
			}
 
			expression = (is_text) =>
				is_text ? b.call('$.sibling', text_id, b.true) : b.call('$.sibling', text_id);
		} else {
			const text_id = get_node_id(expression(true), state, 'text');
 
			state.template.push(' ');
 
			const [contains_call_expression, value] = serialize_template_literal(sequence, visit, state);
 
			const update = b.stmt(b.call('$.set_text', text_id, value));
 
			if (contains_call_expression && !within_bound_contenteditable) {
				state.init.push(serialize_update(update));
			} else if (
				sequence.some((node) => node.type === 'ExpressionTag' && node.metadata.dynamic) &&
				!within_bound_contenteditable
			) {
				state.update.push(update);
			} else {
				state.init.push(b.stmt(b.assignment('=', b.member(text_id, b.id('nodeValue')), value)));
			}
 
			expression = (is_text) =>
				is_text ? b.call('$.sibling', text_id, b.true) : b.call('$.sibling', text_id);
		}
	}
 
	for (let i = 0; i < nodes.length; i += 1) {
		const node = nodes[i];
 
		if (node.type === 'Text' || node.type === 'ExpressionTag') {
			sequence.push(node);
		} else {
			if (sequence.length > 0) {
				flush_sequence(sequence);
				sequence = [];
			}
 
			if (
				node.type === 'SvelteHead' ||
				node.type === 'TitleElement' ||
				node.type === 'SnippetBlock'
			) {
				// These nodes do not contribute to the sibling/child tree
				// TODO what about e.g. ConstTag and all the other things that
				// get hoisted inside clean_nodes?
				visit(node, state);
			} else {
				if (node.type === 'EachBlock' && nodes.length === 1 && is_element) {
					node.metadata.is_controlled = true;
					visit(node, state);
				} else {
					const id = get_node_id(
						expression(false),
						state,
						node.type === 'RegularElement' ? node.name : 'node'
					);
 
					expression = (is_text) =>
						is_text ? b.call('$.sibling', id, b.true) : b.call('$.sibling', id);
 
					visit(node, {
						...state,
						node: id
					});
				}
			}
		}
	}
 
	if (sequence.length > 0) {
		flush_sequence(sequence);
	}
}
 
/**
 * @param {import('estree').Expression} expression
 * @param {import('../types.js').ComponentClientTransformState} state
 * @param {string} name
 */
function get_node_id(expression, state, name) {
	let id = expression;
 
	if (id.type !== 'Identifier') {
		id = b.id(state.scope.generate(name));
 
		state.init.push(b.var(id, expression));
	}
	return id;
}
 
/**
 * @param {true | Array<import('#compiler').Text | import('#compiler').ExpressionTag>} attribute_value
 * @param {import('../types').ComponentContext} context
 * @returns {[boolean, import('estree').Expression]}
 */
function serialize_attribute_value(attribute_value, context) {
	let contains_call_expression = false;
 
	if (attribute_value === true) {
		return [contains_call_expression, b.literal(true)];
	}
 
	if (attribute_value.length === 0) {
		return [contains_call_expression, b.literal('')]; // is this even possible?
	}
 
	if (attribute_value.length === 1) {
		const value = attribute_value[0];
		if (value.type === 'Text') {
			return [contains_call_expression, b.literal(value.data)];
		} else {
			if (value.type === 'ExpressionTag') {
				contains_call_expression = value.metadata.contains_call_expression;
			}
			return [
				contains_call_expression,
				/** @type {import('estree').Expression} */ (context.visit(value.expression))
			];
		}
	}
 
	return serialize_template_literal(attribute_value, context.visit, context.state);
}
 
/**
 * @param {Array<import('#compiler').Text | import('#compiler').ExpressionTag>} values
 * @param {(node: import('#compiler').SvelteNode) => any} visit
 * @param {import("../types.js").ComponentClientTransformState} state
 * @returns {[boolean, import('estree').TemplateLiteral]}
 */
function serialize_template_literal(values, visit, state) {
	/** @type {import('estree').TemplateElement[]} */
	const quasis = [];
 
	/** @type {import('estree').Expression[]} */
	const expressions = [];
	let contains_call_expression = false;
	let contains_multiple_call_expression = false;
	quasis.push(b.quasi(''));
 
	for (let i = 0; i < values.length; i++) {
		const node = values[i];
 
		if (node.type === 'ExpressionTag' && node.metadata.contains_call_expression) {
			if (contains_call_expression) {
				contains_multiple_call_expression = true;
			}
			contains_call_expression = true;
		}
	}
 
	for (let i = 0; i < values.length; i++) {
		const node = values[i];
 
		if (node.type === 'Text') {
			const last = /** @type {import('estree').TemplateElement} */ (quasis.at(-1));
			last.value.raw += sanitize_template_string(node.data);
		} else if (node.type === 'ExpressionTag' && node.expression.type === 'Literal') {
			const last = /** @type {import('estree').TemplateElement} */ (quasis.at(-1));
			if (node.expression.value != null) {
				last.value.raw += sanitize_template_string(node.expression.value + '');
			}
		} else {
			if (contains_multiple_call_expression) {
				const id = b.id(state.scope.generate('stringified_text'));
 
				state.init.push(
					b.const(
						id,
						create_derived(
							state,
							b.thunk(/** @type {import('estree').Expression} */ (visit(node.expression)))
						)
					)
				);
				expressions.push(b.call('$.get', id));
			} else {
				expressions.push(b.call('$.stringify', visit(node.expression)));
			}
			quasis.push(b.quasi('', i + 1 === values.length));
		}
	}
 
	// TODO instead of this tuple, return a `{ dynamic, complex, value }` object. will DRY stuff out
	return [contains_call_expression, b.template(quasis, expressions)];
}
 
/** @type {import('../types').ComponentVisitors} */
export const template_visitors = {
	Fragment(node, context) {
		const body = create_block(node, 'root', node.nodes, context);
		return b.block(body);
	},
	Comment(node, context) {
		// We'll only get here if comments are not filtered out, which they are unless preserveComments is true
		context.state.template.push(`<!--${node.data}-->`);
	},
	HtmlTag(node, context) {
		context.state.template.push('<!>');
 
		// push into init, so that bindings run afterwards, which might trigger another run and override hydration
		context.state.init.push(
			b.stmt(
				b.call(
					'$.html',
					context.state.node,
					b.thunk(/** @type {import('estree').Expression} */ (context.visit(node.expression))),
					b.literal(context.state.metadata.namespace === 'svg'),
					b.literal(context.state.metadata.namespace === 'mathml')
				)
			)
		);
	},
	ConstTag(node, { state, visit }) {
		const declaration = node.declaration.declarations[0];
		// TODO we can almost certainly share some code with $derived(...)
		if (declaration.id.type === 'Identifier') {
			state.init.push(
				b.const(
					declaration.id,
					create_derived(
						state,
						b.thunk(/** @type {import('estree').Expression} */ (visit(declaration.init)))
					)
				)
			);
 
			// we need to eagerly evaluate the expression in order to hit any
			// 'Cannot access x before initialization' errors
			if (state.options.dev) {
				state.init.push(b.stmt(b.call('$.get', declaration.id)));
			}
		} else {
			const identifiers = extract_identifiers(declaration.id);
			const tmp = b.id(state.scope.generate('computed_const'));
 
			// Make all identifiers that are declared within the following computed regular
			// variables, as they are not signals in that context yet
			for (const node of identifiers) {
				const binding = /** @type {import('#compiler').Binding} */ (state.scope.get(node.name));
				binding.expression = node;
			}
 
			// TODO optimise the simple `{ x } = y` case — we can just return `y`
			// instead of destructuring it only to return a new object
			const fn = b.arrow(
				[],
				b.block([
					b.const(
						/** @type {import('estree').Pattern} */ (visit(declaration.id)),
						/** @type {import('estree').Expression} */ (visit(declaration.init))
					),
					b.return(b.object(identifiers.map((node) => b.prop('init', node, node))))
				])
			);
 
			state.init.push(b.const(tmp, create_derived(state, fn)));
 
			// we need to eagerly evaluate the expression in order to hit any
			// 'Cannot access x before initialization' errors
			if (state.options.dev) {
				state.init.push(b.stmt(b.call('$.get', tmp)));
			}
 
			for (const node of identifiers) {
				const binding = /** @type {import('#compiler').Binding} */ (state.scope.get(node.name));
				binding.expression = b.member(b.call('$.get', tmp), node);
			}
		}
	},
	DebugTag(node, { state, visit }) {
		state.init.push(
			b.stmt(
				b.call(
					'$.template_effect',
					b.thunk(
						b.block([
							b.stmt(
								b.call(
									'console.log',
									b.object(
										node.identifiers.map((identifier) =>
											b.prop(
												'init',
												identifier,
												/** @type {import('estree').Expression} */ (visit(identifier))
											)
										)
									)
								)
							),
							b.debugger
						])
					)
				)
			)
		);
	},
	RenderTag(node, context) {
		context.state.template.push('<!>');
		const callee = unwrap_optional(node.expression).callee;
		const raw_args = unwrap_optional(node.expression).arguments;
		const is_reactive =
			callee.type !== 'Identifier' || context.state.scope.get(callee.name)?.kind !== 'normal';
 
		/** @type {import('estree').Expression[]} */
		const args = [context.state.node];
		for (const arg of raw_args) {
			args.push(b.thunk(/** @type {import('estree').Expression} */ (context.visit(arg))));
		}
 
		let snippet_function = /** @type {import('estree').Expression} */ (context.visit(callee));
		if (context.state.options.dev) {
			snippet_function = b.call('$.validate_snippet', snippet_function);
		}
 
		if (is_reactive) {
			context.state.init.push(b.stmt(b.call('$.snippet', b.thunk(snippet_function), ...args)));
		} else {
			context.state.init.push(
				b.stmt(
					(node.expression.type === 'CallExpression' ? b.call : b.maybe_call)(
						snippet_function,
						...args
					)
				)
			);
		}
	},
	AnimateDirective(node, { state, visit }) {
		const expression =
			node.expression === null
				? b.literal(null)
				: b.thunk(/** @type {import('estree').Expression} */ (visit(node.expression)));
 
		state.init.push(
			b.stmt(
				b.call(
					'$.animation',
					state.node,
					b.thunk(
						/** @type {import('estree').Expression} */ (visit(parse_directive_name(node.name)))
					),
					expression
				)
			)
		);
	},
	ClassDirective(node, { state, next }) {
		throw new Error('Node should have been handled elsewhere');
	},
	StyleDirective(node, { state, next }) {
		throw new Error('Node should have been handled elsewhere');
	},
	TransitionDirective(node, { state, visit }) {
		let flags = node.modifiers.includes('global') ? TRANSITION_GLOBAL : 0;
		if (node.intro) flags |= TRANSITION_IN;
		if (node.outro) flags |= TRANSITION_OUT;
 
		const args = [
			b.literal(flags),
			state.node,
			b.thunk(/** @type {import('estree').Expression} */ (visit(parse_directive_name(node.name))))
		];
 
		if (node.expression) {
			args.push(b.thunk(/** @type {import('estree').Expression} */ (visit(node.expression))));
		}
 
		state.init.push(b.stmt(b.call('$.transition', ...args)));
	},
	RegularElement(node, context) {
		/** @type {import('../types.js').SourceLocation} */
		let location = [-1, -1];
 
		if (context.state.options.dev) {
			const loc = locator(node.start);
			if (loc) {
				location[0] = loc.line;
				location[1] = loc.column;
				context.state.locations.push(location);
			}
		}
 
		if (node.name === 'noscript') {
			context.state.template.push('<!>');
			return;
		}
		if (node.name === 'script') {
			context.state.metadata.context.template_contains_script_tag = true;
		}
 
		const metadata = context.state.metadata;
		const child_metadata = {
			...context.state.metadata,
			namespace: determine_namespace_for_children(node, context.state.metadata.namespace)
		};
 
		context.state.template.push(`<${node.name}`);
 
		/** @type {Array<import('#compiler').Attribute | import('#compiler').SpreadAttribute>} */
		const attributes = [];
 
		/** @type {import('#compiler').ClassDirective[]} */
		const class_directives = [];
 
		/** @type {import('#compiler').StyleDirective[]} */
		const style_directives = [];
 
		/** @type {import('estree').ExpressionStatement[]} */
		const lets = [];
 
		const is_custom_element = is_custom_element_node(node);
		let needs_input_reset = false;
		let needs_content_reset = false;
 
		/** @type {import('#compiler').BindDirective | null} */
		let value_binding = null;
 
		/** If true, needs `__value` for inputs */
		let needs_special_value_handling = node.name === 'option' || node.name === 'select';
		let is_content_editable = false;
		let has_content_editable_binding = false;
		let img_might_be_lazy = false;
		let might_need_event_replaying = false;
 
		if (is_custom_element) {
			// cloneNode is faster, but it does not instantiate the underlying class of the
			// custom element until the template is connected to the dom, which would
			// cause problems when setting properties on the custom element.
			// Therefore we need to use importNode instead, which doesn't have this caveat.
			metadata.context.template_needs_import_node = true;
		}
 
		for (const attribute of node.attributes) {
			if (attribute.type === 'Attribute') {
				attributes.push(attribute);
				if (node.name === 'img' && attribute.name === 'loading') {
					img_might_be_lazy = true;
				}
				if (
					(attribute.name === 'value' || attribute.name === 'checked') &&
					!is_text_attribute(attribute)
				) {
					needs_input_reset = true;
					needs_content_reset = true;
				} else if (
					attribute.name === 'contenteditable' &&
					(attribute.value === true ||
						(is_text_attribute(attribute) && attribute.value[0].data === 'true'))
				) {
					is_content_editable = true;
				}
			} else if (attribute.type === 'SpreadAttribute') {
				attributes.push(attribute);
				needs_input_reset = true;
				needs_content_reset = true;
				if (LoadErrorElements.includes(node.name)) {
					might_need_event_replaying = true;
				}
			} else if (attribute.type === 'ClassDirective') {
				class_directives.push(attribute);
			} else if (attribute.type === 'StyleDirective') {
				style_directives.push(attribute);
			} else if (attribute.type === 'LetDirective') {
				lets.push(/** @type {import('estree').ExpressionStatement} */ (context.visit(attribute)));
			} else {
				if (attribute.type === 'BindDirective') {
					if (attribute.name === 'group' || attribute.name === 'checked') {
						needs_special_value_handling = true;
						needs_input_reset = true;
					} else if (attribute.name === 'value') {
						value_binding = attribute;
						needs_content_reset = true;
						needs_input_reset = true;
					} else if (
						attribute.name === 'innerHTML' ||
						attribute.name === 'innerText' ||
						attribute.name === 'textContent'
					) {
						has_content_editable_binding = true;
					}
				} else if (attribute.type === 'UseDirective' && LoadErrorElements.includes(node.name)) {
					might_need_event_replaying = true;
				}
				context.visit(attribute);
			}
		}
 
		if (child_metadata.namespace === 'foreign') {
			// input/select etc could mean something completely different in foreign namespace, so don't special-case them
			needs_content_reset = false;
			needs_input_reset = false;
			needs_special_value_handling = false;
			value_binding = null;
		}
 
		if (is_content_editable && has_content_editable_binding) {
			child_metadata.bound_contenteditable = true;
		}
 
		if (needs_input_reset && (node.name === 'input' || node.name === 'select')) {
			context.state.init.push(b.stmt(b.call('$.remove_input_attr_defaults', context.state.node)));
		}
 
		if (needs_content_reset && node.name === 'textarea') {
			context.state.init.push(b.stmt(b.call('$.remove_textarea_child', context.state.node)));
		}
 
		if (value_binding !== null && node.name === 'select') {
			setup_select_synchronization(value_binding, context);
		}
 
		const node_id = context.state.node;
 
		// Let bindings first, they can be used on attributes
		context.state.init.push(...lets);
 
		// Then do attributes
		let is_attributes_reactive = false;
		if (node.metadata.has_spread) {
			if (node.name === 'img') {
				img_might_be_lazy = true;
			}
			serialize_element_spread_attributes(
				attributes,
				context,
				node,
				node_id,
				// If value binding exists, that one takes care of calling $.init_select
				value_binding === null && node.name === 'select' && child_metadata.namespace !== 'foreign'
			);
			is_attributes_reactive = true;
		} else {
			for (const attribute of /** @type {import('#compiler').Attribute[]} */ (attributes)) {
				if (is_event_attribute(attribute)) {
					if (
						(attribute.name === 'onload' || attribute.name === 'onerror') &&
						LoadErrorElements.includes(node.name)
					) {
						might_need_event_replaying = true;
					}
					serialize_event_attribute(attribute, context);
					continue;
				}
 
				if (needs_special_value_handling && attribute.name === 'value') {
					serialize_element_special_value_attribute(node.name, node_id, attribute, context);
					continue;
				}
 
				if (
					attribute.name !== 'autofocus' &&
					(attribute.value === true || is_text_attribute(attribute))
				) {
					const name = get_attribute_name(node, attribute, context);
					const literal_value = /** @type {import('estree').Literal} */ (
						serialize_attribute_value(attribute.value, context)[1]
					).value;
					if (name !== 'class' || literal_value) {
						// TODO namespace=foreign probably doesn't want to do template stuff at all and instead use programmatic methods
						// to create the elements it needs.
						context.state.template.push(
							` ${attribute.name}${
								DOMBooleanAttributes.includes(name) && literal_value === true
									? ''
									: `="${literal_value === true ? '' : escape_html(literal_value, true)}"`
							}`
						);
						continue;
					}
				}
 
				const is =
					is_custom_element && child_metadata.namespace !== 'foreign'
						? serialize_custom_element_attribute_update_assignment(node_id, attribute, context)
						: serialize_element_attribute_update_assignment(node, node_id, attribute, context);
				if (is) is_attributes_reactive = true;
			}
		}
 
		// Apply the src and loading attributes for <img> elements after the element is appended to the document
		if (img_might_be_lazy) {
			context.state.after_update.push(b.stmt(b.call('$.handle_lazy_img', node_id)));
		}
 
		// class/style directives must be applied last since they could override class/style attributes
		serialize_class_directives(class_directives, node_id, context, is_attributes_reactive);
		serialize_style_directives(style_directives, node_id, context, is_attributes_reactive);
 
		if (might_need_event_replaying) {
			context.state.after_update.push(b.stmt(b.call('$.replay_events', node_id)));
		}
 
		context.state.template.push('>');
 
		/** @type {import('../types.js').SourceLocation[]} */
		const child_locations = [];
 
		/** @type {import('../types').ComponentClientTransformState} */
		const state = {
			...context.state,
			metadata: child_metadata,
			locations: child_locations,
			scope: /** @type {import('../../../scope').Scope} */ (
				context.state.scopes.get(node.fragment)
			),
			preserve_whitespace:
				context.state.preserve_whitespace ||
				((node.name === 'pre' || node.name === 'textarea') &&
					child_metadata.namespace !== 'foreign')
		};
 
		const { hoisted, trimmed } = clean_nodes(
			node,
			node.fragment.nodes,
			context.path,
			child_metadata.namespace,
			node.name === 'script' || state.preserve_whitespace,
			state.options.preserveComments
		);
 
		for (const node of hoisted) {
			context.visit(node, state);
		}
 
		process_children(
			trimmed,
			() =>
				b.call(
					'$.child',
					node.name === 'template'
						? b.member(context.state.node, b.id('content'))
						: context.state.node
				),
			true,
			{ ...context, state }
		);
 
		if (child_locations.length > 0) {
			// @ts-expect-error
			location.push(child_locations);
		}
 
		if (!VoidElements.includes(node.name)) {
			context.state.template.push(`</${node.name}>`);
		}
	},
	SvelteElement(node, context) {
		context.state.template.push(`<!>`);
 
		/** @type {Array<import('#compiler').Attribute | import('#compiler').SpreadAttribute>} */
		const attributes = [];
 
		/** @type {import('#compiler').Attribute['value'] | undefined} */
		let dynamic_namespace = undefined;
 
		/** @type {import('#compiler').ClassDirective[]} */
		const class_directives = [];
 
		/** @type {import('#compiler').StyleDirective[]} */
		const style_directives = [];
 
		/** @type {import('estree').ExpressionStatement[]} */
		const lets = [];
 
		// Create a temporary context which picks up the init/update statements.
		// They'll then be added to the function parameter of $.element
		const element_id = b.id(context.state.scope.generate('$$element'));
 
		/** @type {import('../types').ComponentContext} */
		const inner_context = {
			...context,
			state: {
				...context.state,
				node: element_id,
				before_init: [],
				init: [],
				update: [],
				after_update: []
			}
		};
 
		for (const attribute of node.attributes) {
			if (attribute.type === 'Attribute') {
				if (attribute.name === 'xmlns' && !is_text_attribute(attribute)) {
					dynamic_namespace = attribute.value;
				}
				attributes.push(attribute);
			} else if (attribute.type === 'SpreadAttribute') {
				attributes.push(attribute);
			} else if (attribute.type === 'ClassDirective') {
				class_directives.push(attribute);
			} else if (attribute.type === 'StyleDirective') {
				style_directives.push(attribute);
			} else if (attribute.type === 'LetDirective') {
				lets.push(/** @type {import('estree').ExpressionStatement} */ (context.visit(attribute)));
			} else {
				context.visit(attribute, inner_context.state);
			}
		}
 
		// Let bindings first, they can be used on attributes
		context.state.init.push(...lets); // create computeds in the outer context; the dynamic element is the single child of this slot
 
		// Then do attributes
		// Always use spread because we don't know whether the element is a custom element or not,
		// therefore we need to do the "how to set an attribute" logic at runtime.
		const is_attributes_reactive =
			serialize_dynamic_element_attributes(attributes, inner_context, element_id) !== null;
 
		// class/style directives must be applied last since they could override class/style attributes
		serialize_class_directives(class_directives, element_id, inner_context, is_attributes_reactive);
		serialize_style_directives(style_directives, element_id, inner_context, is_attributes_reactive);
 
		const get_tag = b.thunk(/** @type {import('estree').Expression} */ (context.visit(node.tag)));
 
		if (context.state.options.dev && context.state.metadata.namespace !== 'foreign') {
			if (node.fragment.nodes.length > 0) {
				context.state.init.push(b.stmt(b.call('$.validate_void_dynamic_element', get_tag)));
			}
			context.state.init.push(b.stmt(b.call('$.validate_dynamic_element_tag', get_tag)));
		}
 
		/** @type {import('estree').Statement[]} */
		const inner = inner_context.state.init;
		if (inner_context.state.update.length > 0) {
			inner.push(serialize_render_stmt(inner_context.state));
		}
		inner.push(...inner_context.state.after_update);
		inner.push(
			...create_block(node, 'dynamic_element', node.fragment.nodes, {
				...context,
				state: {
					...context.state,
					metadata: {
						...context.state.metadata,
						namespace: determine_namespace_for_children(node, context.state.metadata.namespace)
					}
				}
			})
		);
 
		const location = context.state.options.dev && locator(node.start);
 
		context.state.init.push(
			b.stmt(
				b.call(
					'$.element',
					context.state.node,
					get_tag,
					node.metadata.svg || node.metadata.mathml ? b.true : b.false,
					inner.length > 0 && b.arrow([element_id, b.id('$$anchor')], b.block(inner)),
					dynamic_namespace && b.thunk(serialize_attribute_value(dynamic_namespace, context)[1]),
					location && b.array([b.literal(location.line), b.literal(location.column)])
				)
			)
		);
	},
	EachBlock(node, context) {
		const each_node_meta = node.metadata;
		const collection = /** @type {import('estree').Expression} */ (context.visit(node.expression));
		let each_item_is_reactive = true;
 
		if (!each_node_meta.is_controlled) {
			context.state.template.push('<!>');
		}
 
		if (each_node_meta.array_name !== null) {
			context.state.init.push(b.const(each_node_meta.array_name, b.thunk(collection)));
		}
 
		// The runtime needs to know what kind of each block this is in order to optimize for the
		// key === item (we avoid extra allocations). In that case, the item doesn't need to be reactive.
		// We can guarantee this by knowing that in order for the item of the each block to change, they
		// would need to mutate the key/item directly in the array. Given that in runes mode we use ===
		// equality, we can apply a fast-path (as long as the index isn't reactive).
		let each_type = 0;
 
		if (
			node.key &&
			(node.key.type !== 'Identifier' || !node.index || node.key.name !== node.index)
		) {
			each_type |= EACH_KEYED;
			// If there's a destructuring, then we likely need the generated $$index
			if (node.index || node.context.type !== 'Identifier') {
				each_type |= EACH_INDEX_REACTIVE;
			}
			if (
				context.state.analysis.runes &&
				node.key.type === 'Identifier' &&
				node.context.type === 'Identifier' &&
				node.context.name === node.key.name &&
				(each_type & EACH_INDEX_REACTIVE) === 0
			) {
				// Fast-path for when the key === item
				each_item_is_reactive = false;
			} else {
				each_type |= EACH_ITEM_REACTIVE;
			}
		} else {
			each_type |= EACH_ITEM_REACTIVE;
		}
 
		// Since `animate:` can only appear on elements that are the sole child of a keyed each block,
		// we can determine at compile time whether the each block is animated or not (in which
		// case it should measure animated elements before and after reconciliation).
		if (
			node.key &&
			node.body.nodes.some((child) => {
				if (child.type !== 'RegularElement' && child.type !== 'SvelteElement') return false;
				return child.attributes.some((attr) => attr.type === 'AnimateDirective');
			})
		) {
			each_type |= EACH_IS_ANIMATED;
		}
 
		if (each_node_meta.is_controlled) {
			each_type |= EACH_IS_CONTROLLED;
		}
 
		if (context.state.analysis.runes) {
			each_type |= EACH_IS_STRICT_EQUALS;
		}
 
		// If the array is a store expression, we need to invalidate it when the array is changed.
		// This doesn't catch all cases, but all the ones that Svelte 4 catches, too.
		let store_to_invalidate = '';
		if (node.expression.type === 'Identifier' || node.expression.type === 'MemberExpression') {
			const id = object(node.expression);
			if (id) {
				const binding = context.state.scope.get(id.name);
				if (binding?.kind === 'store_sub') {
					store_to_invalidate = id.name;
				}
			}
		}
 
		// Legacy mode: find the parent each blocks which contain the arrays to invalidate
		const indirect_dependencies = collect_parent_each_blocks(context).flatMap((block) => {
			const array = /** @type {import('estree').Expression} */ (context.visit(block.expression));
			const transitive_dependencies = serialize_transitive_dependencies(
				block.metadata.references,
				context
			);
			return [array, ...transitive_dependencies];
		});
		if (each_node_meta.array_name) {
			indirect_dependencies.push(b.call(each_node_meta.array_name));
		} else {
			indirect_dependencies.push(collection);
			const transitive_dependencies = serialize_transitive_dependencies(
				each_node_meta.references,
				context
			);
			indirect_dependencies.push(...transitive_dependencies);
		}
 
		/**
		 * @param {import('estree').Pattern} expression_for_id
		 * @returns {import('#compiler').Binding['mutation']}
		 */
		const create_mutation = (expression_for_id) => {
			return (assignment, context) => {
				if (assignment.left.type !== 'Identifier' && assignment.left.type !== 'MemberExpression') {
					// serialize_set_binding turns other patterns into IIFEs and separates the assignments
					// into separate expressions, at which point this is called again with an identifier or member expression
					return serialize_set_binding(assignment, context, () => assignment);
				}
				const left = object(assignment.left);
				const value = get_assignment_value(assignment, context);
				const invalidate = b.call(
					'$.invalidate_inner_signals',
					b.thunk(b.sequence(indirect_dependencies))
				);
				const invalidate_store = store_to_invalidate
					? b.call('$.invalidate_store', b.id('$$subscriptions'), b.literal(store_to_invalidate))
					: undefined;
 
				const sequence = [];
				if (!context.state.analysis.runes) sequence.push(invalidate);
				if (invalidate_store) sequence.push(invalidate_store);
 
				if (left === assignment.left) {
					const assign = b.assignment('=', expression_for_id, value);
					sequence.unshift(assign);
					return b.sequence(sequence);
				} else {
					const original_left = /** @type {import('estree').MemberExpression} */ (assignment.left);
					const left = context.visit(original_left);
					const assign = b.assignment(assignment.operator, left, value);
					sequence.unshift(assign);
					return b.sequence(sequence);
				}
			};
		};
 
		// We need to generate a unique identifier in case there's a bind:group below
		// which needs a reference to the index
		const index =
			each_node_meta.contains_group_binding || !node.index
				? each_node_meta.index
				: b.id(node.index);
		const item = each_node_meta.item;
		const binding = /** @type {import('#compiler').Binding} */ (context.state.scope.get(item.name));
		binding.expression = (id) => {
			const item_with_loc = with_loc(item, id);
			return b.call('$.unwrap', item_with_loc);
		};
		if (node.index) {
			const index_binding = /** @type {import('#compiler').Binding} */ (
				context.state.scope.get(node.index)
			);
			index_binding.expression = (id) => {
				const index_with_loc = with_loc(index, id);
				return b.call('$.unwrap', index_with_loc);
			};
		}
 
		/** @type {import('estree').Statement[]} */
		const declarations = [];
 
		if (node.context.type === 'Identifier') {
			binding.mutation = create_mutation(
				b.member(
					each_node_meta.array_name ? b.call(each_node_meta.array_name) : collection,
					index,
					true
				)
			);
		} else {
			const unwrapped = binding.expression(binding.node);
			const paths = extract_paths(node.context);
 
			for (const path of paths) {
				const name = /** @type {import('estree').Identifier} */ (path.node).name;
				const binding = /** @type {import('#compiler').Binding} */ (context.state.scope.get(name));
				const needs_derived = path.has_default_value; // to ensure that default value is only called once
				const fn = b.thunk(
					/** @type {import('estree').Expression} */ (context.visit(path.expression?.(unwrapped)))
				);
 
				declarations.push(
					b.let(path.node, needs_derived ? b.call('$.derived_safe_equal', fn) : fn)
				);
				binding.expression = needs_derived ? b.call('$.get', b.id(name)) : b.call(name);
				binding.mutation = create_mutation(
					/** @type {import('estree').Pattern} */ (path.update_expression(unwrapped))
				);
 
				// we need to eagerly evaluate the expression in order to hit any
				// 'Cannot access x before initialization' errors
				if (context.state.options.dev) {
					declarations.push(b.stmt(binding.expression));
				}
			}
		}
 
		// TODO should use context.visit?
		const children = create_block(node, 'each_block', node.body.nodes, context);
 
		const key_function = node.key
			? b.arrow(
					[node.context.type === 'Identifier' ? node.context : b.id('$$item'), index],
					declarations.length > 0
						? b.block(
								declarations.concat(
									b.return(/** @type {import('estree').Expression} */ (context.visit(node.key)))
								)
							)
						: /** @type {import('estree').Expression} */ (context.visit(node.key))
				)
			: b.id('$.index');
 
		if (node.index && each_node_meta.contains_group_binding) {
			// We needed to create a unique identifier for the index above, but we want to use the
			// original index name in the template, therefore create another binding
			declarations.push(b.let(node.index, index));
		}
 
		if (context.state.options.dev && (each_type & EACH_KEYED) !== 0) {
			context.state.init.push(
				b.stmt(b.call('$.validate_each_keys', b.thunk(collection), key_function))
			);
		}
 
		/** @type {import('estree').Expression[]} */
		const args = [
			context.state.node,
			b.literal(each_type),
			each_node_meta.array_name ? each_node_meta.array_name : b.thunk(collection),
			key_function,
			b.arrow([b.id('$$anchor'), item, index], b.block(declarations.concat(children)))
		];
 
		if (node.fallback) {
			args.push(
				b.arrow(
					[b.id('$$anchor')],
					/** @type {import('estree').BlockStatement} */ (context.visit(node.fallback))
				)
			);
		}
 
		context.state.init.push(b.stmt(b.call('$.each', ...args)));
	},
	IfBlock(node, context) {
		context.state.template.push('<!>');
 
		const consequent = /** @type {import('estree').BlockStatement} */ (
			context.visit(node.consequent)
		);
 
		const args = [
			context.state.node,
			b.thunk(/** @type {import('estree').Expression} */ (context.visit(node.test))),
			b.arrow([b.id('$$anchor')], consequent)
		];
 
		if (node.alternate || node.elseif) {
			args.push(
				node.alternate
					? b.arrow(
							[b.id('$$anchor')],
							/** @type {import('estree').BlockStatement} */ (context.visit(node.alternate))
						)
					: b.literal(null)
			);
		}
 
		if (node.elseif) {
			// We treat this...
			//
			//   {#if x}
			//     ...
			//   {:else}
			//     {#if y}
			//       <div transition:foo>...</div>
			//     {/if}
			//   {/if}
			//
			// ...slightly differently to this...
			//
			//   {#if x}
			//     ...
			//   {:else if y}
			//     <div transition:foo>...</div>
			//   {/if}
			//
			// ...even though they're logically equivalent. In the first case, the
			// transition will only play when `y` changes, but in the second it
			// should play when `x` or `y` change — both are considered 'local'
			args.push(b.literal(true));
		}
 
		context.state.init.push(b.stmt(b.call('$.if', ...args)));
	},
	AwaitBlock(node, context) {
		context.state.template.push('<!>');
 
		context.state.init.push(
			b.stmt(
				b.call(
					'$.await',
					context.state.node,
					b.thunk(/** @type {import('estree').Expression} */ (context.visit(node.expression))),
					node.pending
						? b.arrow(
								[b.id('$$anchor')],
								/** @type {import('estree').BlockStatement} */ (context.visit(node.pending))
							)
						: b.literal(null),
					node.then
						? b.arrow(
								node.value
									? [
											b.id('$$anchor'),
											/** @type {import('estree').Pattern} */ (context.visit(node.value))
										]
									: [b.id('$$anchor')],
								/** @type {import('estree').BlockStatement} */ (context.visit(node.then))
							)
						: b.literal(null),
					node.catch
						? b.arrow(
								node.error
									? [
											b.id('$$anchor'),
											/** @type {import('estree').Pattern} */ (context.visit(node.error))
										]
									: [b.id('$$anchor')],
								/** @type {import('estree').BlockStatement} */ (context.visit(node.catch))
							)
						: b.literal(null)
				)
			)
		);
	},
	KeyBlock(node, context) {
		context.state.template.push('<!>');
		const key = /** @type {import('estree').Expression} */ (context.visit(node.expression));
		const body = /** @type {import('estree').Expression} */ (context.visit(node.fragment));
		context.state.init.push(
			b.stmt(b.call('$.key', context.state.node, b.thunk(key), b.arrow([b.id('$$anchor')], body)))
		);
	},
	SnippetBlock(node, context) {
		// TODO hoist where possible
		/** @type {import('estree').Pattern[]} */
		const args = [b.id('$$anchor')];
 
		/** @type {import('estree').BlockStatement} */
		let body;
 
		/** @type {import('estree').Statement[]} */
		const declarations = [];
 
		for (let i = 0; i < node.parameters.length; i++) {
			const argument = node.parameters[i];
 
			if (!argument) continue;
 
			if (argument.type === 'Identifier') {
				args.push({
					type: 'AssignmentPattern',
					left: argument,
					right: b.id('$.noop')
				});
				const binding = /** @type {import('#compiler').Binding} */ (
					context.state.scope.get(argument.name)
				);
				binding.expression = b.call(argument);
				continue;
			}
 
			let arg_alias = `$$arg${i}`;
			args.push(b.id(arg_alias));
 
			const paths = extract_paths(argument);
 
			for (const path of paths) {
				const name = /** @type {import('estree').Identifier} */ (path.node).name;
				const binding = /** @type {import('#compiler').Binding} */ (context.state.scope.get(name));
				const needs_derived = path.has_default_value; // to ensure that default value is only called once
				const fn = b.thunk(
					/** @type {import('estree').Expression} */ (
						context.visit(path.expression?.(b.maybe_call(b.id(arg_alias))))
					)
				);
 
				declarations.push(
					b.let(path.node, needs_derived ? b.call('$.derived_safe_equal', fn) : fn)
				);
				binding.expression = needs_derived ? b.call('$.get', b.id(name)) : b.call(name);
 
				// we need to eagerly evaluate the expression in order to hit any
				// 'Cannot access x before initialization' errors
				if (context.state.options.dev) {
					declarations.push(b.stmt(binding.expression));
				}
			}
		}
 
		body = b.block([
			...declarations,
			.../** @type {import('estree').BlockStatement} */ (context.visit(node.body)).body
		]);
 
		/** @type {import('estree').Expression} */
		let snippet = b.arrow(args, body);
 
		if (context.state.options.dev) {
			snippet = b.call('$.wrap_snippet', snippet);
		}
 
		const declaration = b.var(node.expression, snippet);
 
		// Top-level snippets are hoisted so they can be referenced in the `<script>`
		if (context.path.length === 1 && context.path[0].type === 'Fragment') {
			context.state.analysis.top_level_snippets.push(declaration);
		} else {
			context.state.init.push(declaration);
		}
	},
	FunctionExpression: function_visitor,
	ArrowFunctionExpression: function_visitor,
	FunctionDeclaration(node, context) {
		context.next({ ...context.state, in_constructor: false });
	},
	OnDirective(node, context) {
		serialize_event(node, context);
	},
	UseDirective(node, { state, next, visit }) {
		const params = [b.id('$$node')];
 
		if (node.expression) {
			params.push(b.id('$$action_arg'));
		}
 
		/** @type {import('estree').Expression[]} */
		const args = [
			state.node,
			b.arrow(
				params,
				b.call(
					/** @type {import('estree').Expression} */ (visit(parse_directive_name(node.name))),
					...params
				)
			)
		];
 
		if (node.expression) {
			args.push(b.thunk(/** @type {import('estree').Expression} */ (visit(node.expression))));
		}
 
		// actions need to run after attribute updates in order with bindings/events
		state.after_update.push(b.stmt(b.call('$.action', ...args)));
		next();
	},
	BindDirective(node, context) {
		const { state, path, visit } = context;
		const expression = node.expression;
		const getter = b.thunk(/** @type {import('estree').Expression} */ (visit(expression)));
		const assignment = b.assignment('=', expression, b.id('$$value'));
		const setter = b.arrow(
			[b.id('$$value')],
			serialize_set_binding(
				assignment,
				context,
				() => /** @type {import('estree').Expression} */ (visit(assignment)),
				{
					skip_proxy_and_freeze: true
				}
			)
		);
 
		/** @type {import('estree').CallExpression} */
		let call_expr;
 
		const property = binding_properties[node.name];
		if (property && property.event) {
			call_expr = b.call(
				'$.bind_property',
				b.literal(node.name),
				b.literal(property.event),
				b.literal(property.type ?? 'get'),
				state.node,
				getter,
				setter
			);
		} else {
			// special cases
			switch (node.name) {
				// window
				case 'online':
					call_expr = b.call(`$.bind_online`, setter);
					break;
 
				case 'scrollX':
				case 'scrollY':
					call_expr = b.call(
						'$.bind_window_scroll',
						b.literal(node.name === 'scrollX' ? 'x' : 'y'),
						getter,
						setter
					);
					break;
 
				case 'innerWidth':
				case 'innerHeight':
				case 'outerWidth':
				case 'outerHeight':
					call_expr = b.call('$.bind_window_size', b.literal(node.name), setter);
					break;
 
				// media
				case 'muted':
					call_expr = b.call(`$.bind_muted`, state.node, getter, setter);
					break;
				case 'paused':
					call_expr = b.call(`$.bind_paused`, state.node, getter, setter);
					break;
				case 'volume':
					call_expr = b.call(`$.bind_volume`, state.node, getter, setter);
					break;
				case 'playbackRate':
					call_expr = b.call(`$.bind_playback_rate`, state.node, getter, setter);
					break;
				case 'currentTime':
					call_expr = b.call(`$.bind_current_time`, state.node, getter, setter);
					break;
				case 'buffered':
					call_expr = b.call(`$.bind_buffered`, state.node, setter);
					break;
				case 'played':
					call_expr = b.call(`$.bind_played`, state.node, setter);
					break;
				case 'seekable':
					call_expr = b.call(`$.bind_seekable`, state.node, setter);
					break;
				case 'seeking':
					call_expr = b.call(`$.bind_seeking`, state.node, setter);
					break;
				case 'ended':
					call_expr = b.call(`$.bind_ended`, state.node, setter);
					break;
				case 'readyState':
					call_expr = b.call(`$.bind_ready_state`, state.node, setter);
					break;
 
				// dimensions
				case 'contentRect':
				case 'contentBoxSize':
				case 'borderBoxSize':
				case 'devicePixelContentBoxSize':
					call_expr = b.call('$.bind_resize_observer', state.node, b.literal(node.name), setter);
					break;
 
				case 'clientWidth':
				case 'clientHeight':
				case 'offsetWidth':
				case 'offsetHeight':
					call_expr = b.call('$.bind_element_size', state.node, b.literal(node.name), setter);
					break;
 
				// various
				case 'value': {
					const parent = path.at(-1);
					if (parent?.type === 'RegularElement' && parent.name === 'select') {
						call_expr = b.call(`$.bind_select_value`, state.node, getter, setter);
					} else {
						call_expr = b.call(`$.bind_value`, state.node, getter, setter);
					}
					break;
				}
 
				case 'files':
					call_expr = b.call(`$.bind_files`, state.node, getter, setter);
					break;
 
				case 'this':
					call_expr = serialize_bind_this(node.expression, context, state.node);
					break;
				case 'textContent':
				case 'innerHTML':
				case 'innerText':
					call_expr = b.call(
						'$.bind_content_editable',
						b.literal(node.name),
						state.node,
						getter,
						setter
					);
					break;
 
				// checkbox/radio
				case 'checked':
					call_expr = b.call(`$.bind_checked`, state.node, getter, setter);
					break;
				case 'focused':
					call_expr = b.call(`$.bind_focused`, state.node, setter);
					break;
				case 'group': {
					/** @type {import('estree').CallExpression[]} */
					const indexes = [];
					for (const parent_each_block of node.metadata.parent_each_blocks) {
						indexes.push(b.call('$.unwrap', parent_each_block.metadata.index));
					}
 
					// We need to additionally invoke the value attribute signal to register it as a dependency,
					// so that when the value is updated, the group binding is updated
					let group_getter = getter;
					const parent = path.at(-1);
					if (parent?.type === 'RegularElement') {
						const value = /** @type {any[]} */ (
							/** @type {import('#compiler').Attribute} */ (
								parent.attributes.find(
									(a) =>
										a.type === 'Attribute' &&
										a.name === 'value' &&
										!is_text_attribute(a) &&
										a.value !== true
								)
							)?.value
						);
						if (value !== undefined) {
							group_getter = b.thunk(
								b.block([
									b.stmt(serialize_attribute_value(value, context)[1]),
									b.return(/** @type {import('estree').Expression} */ (visit(expression)))
								])
							);
						}
					}
 
					call_expr = b.call(
						'$.bind_group',
						node.metadata.binding_group_name,
						b.array(indexes),
						state.node,
						group_getter,
						setter
					);
					break;
				}
 
				default:
					throw new Error('unknown binding ' + node.name);
			}
		}
 
		// Bindings need to happen after attribute updates, therefore after the render effect, and in order with events/actions.
		// bind:this is a special case as it's one-way and could influence the render effect.
		if (node.name === 'this') {
			state.init.push(b.stmt(call_expr));
		} else {
			state.after_update.push(b.stmt(call_expr));
		}
	},
	Component(node, context) {
		context.state.template.push('<!>');
 
		const binding = context.state.scope.get(
			node.name.includes('.') ? node.name.slice(0, node.name.indexOf('.')) : node.name
		);
		if (binding !== null && binding.kind !== 'normal') {
			// Handle dynamic references to what seems like static inline components
			const component = serialize_inline_component(node, '$$component', context);
			context.state.init.push(
				b.stmt(
					b.call(
						'$.component',
						context.state.node,
						// TODO use untrack here to not update when binding changes?
						// Would align with Svelte 4 behavior, but it's arguably nicer/expected to update this
						b.thunk(
							/** @type {import('estree').Expression} */ (context.visit(b.member_id(node.name)))
						),
						b.arrow([b.id('$$component')], b.block([component]))
					)
				)
			);
			return;
		}
		const component = serialize_inline_component(node, node.name, context);
		context.state.init.push(component);
	},
	SvelteSelf(node, context) {
		context.state.template.push('<!>');
		const component = serialize_inline_component(node, context.state.analysis.name, context);
		context.state.init.push(component);
	},
	SvelteComponent(node, context) {
		context.state.template.push('<!>');
 
		let component = serialize_inline_component(node, '$$component', context);
		if (context.state.options.dev) {
			component = b.stmt(b.call('$.validate_dynamic_component', b.thunk(b.block([component]))));
		}
		context.state.init.push(
			b.stmt(
				b.call(
					'$.component',
					context.state.node,
					b.thunk(/** @type {import('estree').Expression} */ (context.visit(node.expression))),
					b.arrow([b.id('$$component')], b.block([component]))
				)
			)
		);
	},
	Attribute(node, context) {
		if (is_event_attribute(node)) {
			serialize_event_attribute(node, context);
		}
	},
	LetDirective(node, { state }) {
		// let:x        -->  const x = $.derived(() => $$slotProps.x);
		// let:x={{y, z}}  -->  const derived_x = $.derived(() => { const { y, z } = $$slotProps.x; return { y, z }));
		if (node.expression && node.expression.type !== 'Identifier') {
			const name = state.scope.generate(node.name);
			const bindings = state.scope.get_bindings(node);
 
			for (const binding of bindings) {
				binding.expression = b.member(b.call('$.get', b.id(name)), b.id(binding.node.name));
			}
 
			return b.const(
				name,
				b.call(
					'$.derived',
					b.thunk(
						b.block([
							b.let(
								/** @type {import('estree').Expression} */ (node.expression).type ===
									'ObjectExpression'
									? // @ts-expect-error types don't match, but it can't contain spread elements and the structure is otherwise fine
										b.object_pattern(node.expression.properties)
									: // @ts-expect-error types don't match, but it can't contain spread elements and the structure is otherwise fine
										b.array_pattern(node.expression.elements),
								b.member(b.id('$$slotProps'), b.id(node.name))
							),
							b.return(b.object(bindings.map((binding) => b.init(binding.node.name, binding.node))))
						])
					)
				)
			);
		} else {
			const name = node.expression === null ? node.name : node.expression.name;
			return b.const(
				name,
				create_derived(state, b.thunk(b.member(b.id('$$slotProps'), b.id(node.name))))
			);
		}
	},
	SpreadAttribute(node, { visit }) {
		return visit(node.expression);
	},
	SvelteFragment(node, context) {
		/** @type {import('estree').Statement[]} */
		const lets = [];
 
		for (const attribute of node.attributes) {
			if (attribute.type === 'LetDirective') {
				lets.push(/** @type {import('estree').ExpressionStatement} */ (context.visit(attribute)));
			}
		}
 
		const state = {
			...context.state,
			// TODO this logic eventually belongs in create_block, when fragments are used everywhere
			scope: /** @type {import('../../../scope').Scope} */ (context.state.scopes.get(node.fragment))
		};
 
		context.state.init.push(...lets);
		context.state.init.push(
			...create_block(
				node,
				'slot_template',
				/** @type {import('#compiler').SvelteNode[]} */ (node.fragment.nodes),
				{
					...context,
					state
				}
			)
		);
	},
	SlotElement(node, context) {
		// <slot {a}>fallback</slot>  -->   $.slot($$slots.default, { get a() { .. } }, () => ...fallback);
		context.state.template.push('<!>');
 
		/** @type {import('estree').Property[]} */
		const props = [];
 
		/** @type {import('estree').Expression[]} */
		const spreads = [];
 
		/** @type {import('estree').ExpressionStatement[]} */
		const lets = [];
 
		let is_default = true;
 
		/** @type {import('estree').Expression} */
		let name = b.literal('default');
 
		for (const attribute of node.attributes) {
			if (attribute.type === 'SpreadAttribute') {
				spreads.push(
					b.thunk(/** @type {import('estree').Expression} */ (context.visit(attribute)))
				);
			} else if (attribute.type === 'Attribute') {
				const [, value] = serialize_attribute_value(attribute.value, context);
				if (attribute.name === 'name') {
					name = value;
					is_default = false;
				} else if (attribute.name !== 'slot') {
					if (attribute.metadata.dynamic) {
						props.push(b.get(attribute.name, [b.return(value)]));
					} else {
						props.push(b.init(attribute.name, value));
					}
				}
			} else if (attribute.type === 'LetDirective') {
				lets.push(/** @type {import('estree').ExpressionStatement} */ (context.visit(attribute)));
			}
		}
 
		// Let bindings first, they can be used on attributes
		context.state.init.push(...lets);
 
		const props_expression =
			spreads.length === 0
				? b.object(props)
				: b.call('$.spread_props', b.object(props), ...spreads);
		const fallback =
			node.fragment.nodes.length === 0
				? b.literal(null)
				: b.arrow(
						[b.id('$$anchor')],
						b.block(create_block(node, 'fallback', node.fragment.nodes, context))
					);
 
		const expression = is_default
			? b.call('$.default_slot', b.id('$$props'))
			: b.member(b.member(b.id('$$props'), b.id('$$slots')), name, true, true);
 
		const slot = b.call('$.slot', context.state.node, expression, props_expression, fallback);
		context.state.init.push(b.stmt(slot));
	},
	SvelteHead(node, context) {
		// TODO attributes?
		context.state.init.push(
			b.stmt(
				b.call(
					'$.head',
					b.arrow(
						[b.id('$$anchor')],
						b.block(create_block(node, 'head', node.fragment.nodes, context))
					)
				)
			)
		);
	},
	TitleElement(node, { state, visit }) {
		// TODO throw validation error when attributes present / when children something else than text/expression tags
		if (node.fragment.nodes.length === 1 && node.fragment.nodes[0].type === 'Text') {
			state.init.push(
				b.stmt(
					b.assignment(
						'=',
						b.member(b.id('$.document'), b.id('title')),
						b.literal(/** @type {import('#compiler').Text} */ (node.fragment.nodes[0]).data)
					)
				)
			);
		} else {
			state.update.push(
				b.stmt(
					b.assignment(
						'=',
						b.member(b.id('$.document'), b.id('title')),
						serialize_template_literal(/** @type {any} */ (node.fragment.nodes), visit, state)[1]
					)
				)
			);
		}
	},
	SvelteBody(node, context) {
		context.next({
			...context.state,
			node: b.id('$.document.body')
		});
	},
	SvelteWindow(node, context) {
		context.next({
			...context.state,
			node: b.id('$.window')
		});
	},
	SvelteDocument(node, context) {
		context.next({
			...context.state,
			node: b.id('$.document')
		});
	},
	CallExpression: javascript_visitors_runes.CallExpression,
	VariableDeclaration: javascript_visitors_runes.VariableDeclaration
};