All files / src/compiler/phases/3-transform/server transform-server.js

97.37% Statements 2376/2440
97.1% Branches 571/588
92.64% Functions 63/68
97.36% Lines 2362/2426

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 24272x 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 13959x 13959x 13959x 2x 2x 2x 2x 2x 2x 2176x 2176x 2176x 2x 2x 2x 2x 2x 2417x 2417x 2417x 2x 2x 2x 2x 2x 2x 3684x 3684x 3684x 3684x 3684x 3684x 3684x 3684x 3684x 3684x 3684x 4958x 4958x 4958x 4958x 4958x 3684x 3684x 3684x 21427x 21427x 2417x 1893x 1893x 2417x 21427x 19010x 19010x 19010x 19010x 16834x 19010x 2176x 2176x 731x 731x 731x 731x 731x 731x 1445x 2176x 2176x 19010x 21427x 3684x 3684x 3065x 3065x 3684x 3684x 3684x 2x 2x 2x 2x 2x 2x 2x 2x 7159x 7159x 7159x 7159x 7159x 7159x 7159x 7159x 7159x 7159x 7159x 7159x 4209x 4209x 4209x 4209x 3480x 3480x 3480x 2623x 2623x 2623x 917x 2623x 5x 2623x 2618x 2618x 2623x 2623x 857x 3470x       857x 3470x       857x 857x 857x 857x 857x 857x 857x 857x 857x 729x 729x 729x 729x 729x 729x 729x 729x 729x 3864x 2534x 2534x 1094x 1094x 2534x 8x 8x 8x 8x 1440x     1432x 1432x 1432x 1432x 1432x 1432x 2534x 729x 729x 729x 7159x 7159x 11422x 11422x 11422x 11422x 11422x 11422x 5408x 11422x 6014x 11422x 5408x 1599x 1599x 1599x 5408x 5408x 5408x 5408x 5408x 11422x 7159x 7159x 2610x 2610x 7159x 2x 2x 2x 2x 2x 2x 2x 2x 3741x 3741x 3741x 3741x 3741x 3741x 3741x 3741x 3741x 3741x 3741x 3741x 3741x 152x 152x 3589x 3589x 3589x 3589x 3589x 3589x 3589x 3589x 3589x 3589x 3589x 3741x 219x 219x 3589x 3741x 3741x 3741x 3741x 3741x 3741x 3741x 3741x 3741x 3575x 3575x 3741x 14x 14x 3589x 3589x 3589x 2x 2x 2x 2x 2x 2x 2x 389x 389x 385x 385x 4x 4x 4x 4x 4x 4x 10x 10x 4x 4x 4x 2x 2x 2x 2x 2x 2x 12073x 12073x 12073x 12073x 3006x 3006x 3006x 9067x 12073x 210x 210x 210x 210x 210x 210x 210x 210x 8857x 12073x 26x 26x 8831x 8831x 8831x 2x 2x 2x 2x 2x 851x 851x 756x 756x 756x 756x 90x 90x 90x 90x 756x 851x 95x 95x 851x 2x 2x 2x 2x 1023x 1023x 1023x 2x 2x 2x 2x 2x 2x 2x 1120x 1120x 1120x 1120x 1120x 1120x 1063x 1120x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 114x 114x 114x 114x 114x 57x 57x 41x 41x 41x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 1063x 1120x     1063x 1063x 1063x 1120x 287x 287x 287x 1063x 1120x 40x 40x 1023x 1023x 1120x 1120x 1120x 1120x 1017x 1120x     1017x 1017x 1120x 1120x 1120x 1120x 1120x 1120x 201x 1120x 166x 166x 166x 851x 851x 1117x 756x 42x 42x 42x 42x 42x 42x 714x 1120x 8x 8x 8x 8x 8x 8x 8x 8x 87x 87x 2x 2x 2x 2x 2x 2x 1392x 1392x 1392x 1278x 1278x 1278x 1278x 1392x 1392x 2x 2x 2x 2x 15195x 11793x 20x 20x 11773x 11773x 2x 2x 1006x 2x 2x 127x 127x 127x 127x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 2x 2x 9x 9x 9x 118x 118x 2x 2x 2x 2x 2x 4417x 4417x 4417x 4417x 2x 2x 935x 935x 935x 935x 935x 2x 2x 2x 2x 2x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 102x 102x 60x 102x 60x 60x 60x 60x 60x 60x 54x 54x 13x 13x 13x 13x 13x 13x 13x 13x 1x 13x 12x 12x 13x 54x 60x 102x 36x 36x 36x 12x 12x     12x 12x 12x 12x 36x 36x 36x 36x 36x 36x 36x 36x 102x 102x 60x 102x 60x 60x 60x 60x 60x 60x 13x 13x 13x 13x 13x 13x 13x 13x 13x 1x 13x 12x 12x 12x 12x 12x 12x 12x 12x                   12x 13x 13x 13x 60x 89x 89x 89x 36x 36x 2x 2x 47x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x                   41x 6x 2x 2x 581x 581x 581x 582x 582x 582x 165x 165x 165x 417x 582x 76x 76x 76x 76x 46x 46x 17x 46x 13x 13x 13x 13x 13x 76x 76x 8x 8x 8x 8x 76x 76x 76x 76x 76x 76x 76x 76x 76x 76x 76x 76x 76x 76x 76x 76x 341x 341x 341x 341x 582x 582x 582x 582x 2x 2x 2x 2x 2x 2x 2x 2x 339x 573x 334x 334x 334x 5x 564x 3x 3x 3x 3x 3x 2x 2x 2x 581x 581x 581x 581x 581x 2x 2x 271x 271x 132x 132x 132x 46x 46x 86x 86x 132x 132x 60x 132x 16x 16x 132x 209x 2x 2x 710x 710x 710x     710x 710x 2x 2x 708x 710x 8x 8x 700x 700x 2x 2x 281x 26x 26x 1x 1x 26x 280x 280x 280x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2129x 2129x 2129x 2129x 2129x 2129x 2129x 27x 27x 2102x 2129x     2102x 2129x 2037x 2037x 1148x 1148x 468x 468x 1148x 1148x 2037x 889x 889x 2037x 65x 65x 65x 65x 65x 65x 65x 2073x 27x 27x 65x 65x 2073x 166x 166x 85x 85x 61x 61x 61x 85x 166x 81x 81x 81x 81x 81x 81x 81x 34x 34x 81x 166x 65x 65x 65x 2x 2x 2x 2x 2x 2x 2x 2x 2x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 161x 63x 63x 63x 63x 63x 63x 63x 161x 98x 98x 161x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 8x 5x 5x 5x 5x 5x 5x 8x 8x 3x 8x 8x 8x 3x 3x 3x 3x 3x 3x 8x 8x 8x 8x 8x 8x 8x 8x 97x 97x 2x 2x 2x 2x 2x 2x 2x 605x 605x 605x 605x 605x 605x 605x 605x 605x 605x 605x 605x 605x 605x 605x 605x 605x 605x 605x 605x 605x 605x 605x 821x 821x 821x 821x 821x 461x 461x 821x 605x 707x 68x 707x 21x 639x 473x 47x 47x 47x 47x 426x 473x 7x 7x 426x 426x 426x 618x 91x 91x 91x 91x 91x 91x 91x 91x 91x 91x 91x 91x 91x 91x 91x 91x 91x 707x 605x 605x 7x 7x 605x 605x 605x 605x 605x 605x 792x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 787x 787x 792x 229x 229x 229x 229x 229x 229x 82x 82x 229x 787x 792x 792x 792x 605x 605x 605x 605x 605x 605x 285x 285x 225x 225x 225x 285x 285x 285x 285x 143x 143x 143x 143x 143x 143x 143x 285x 82x 82x 82x 285x 605x 605x 65x 65x 605x 605x 605x 467x 605x 605x 21x 21x 605x 605x 605x 605x 605x 605x 605x 20x 20x 20x 605x 605x 605x 605x 605x 605x 605x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 605x 605x 5x 5x 605x 605x 605x 2x 2x 2x 2x 2x 2x 1395x 1395x 1395x 1395x 1395x 2x 2x 2x 2x 2537x 2537x 2537x 2537x 2877x 2877x 2877x 2877x 2877x 2877x 2877x 450x 450x 450x 2427x 2877x 2040x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 31x 31x 31x 31x 31x 31x 5x 5x 2035x 2035x 2035x 2035x 2035x 2035x 2040x 2040x 2040x 2040x 2040x 993x 993x 993x 993x 2040x 2040x 2040x 2040x 2040x 2040x 387x 387x 387x 387x 387x 2877x 2877x 2877x 2877x 2537x 2537x 2537x 2537x 2537x 2x 2x 142x 142x 140x 140x 140x 140x 140x 140x 140x 140x 140x 140x 140x 140x 2x 2x 2x 2x 2x 2422x 2422x 2x 2x 49x 49x 49x 49x 49x 2x 2x 126x 126x 126x 126x 2x 2x                                       2x 2x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 17x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 2x 2x   2x 2x   2x 2x 3552x 3552x 3552x 3552x 3552x 3552x 3552x 3552x 3552x 3552x 3552x 3552x 3552x 3552x 3552x 3543x 3552x 3552x 3552x 3552x 3552x 3552x 16x 16x 16x 16x 16x 16x 16x 3552x 3552x 3552x 3552x 3552x 3552x 3552x 3552x 3552x 3552x 3552x 3552x 7x 7x 3552x 3552x 48x 48x 48x 48x 3552x 3552x 3552x 3552x 16x 16x 16x 16x 16x 5x 16x 11x 11x 11x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 3552x 3552x 3269x 3269x 3552x 48x 48x 2x 2x 59x 59x 21x 21x 21x 21x 59x 59x 6x 5x 5x 6x 6x 59x 59x 59x 59x 59x 59x 59x 59x 59x 59x 59x 59x 59x 59x 59x 59x 59x 59x 59x 59x 59x 59x 59x 59x 59x 59x 6x 6x 6x 6x 59x 59x 59x 59x 59x 59x 59x 59x 59x 59x 59x 59x 59x 59x 59x 59x 59x 59x 59x 59x 59x 59x 59x 59x 59x 6x 6x 2x 2x 447x 447x 447x 447x 447x 447x 447x 447x 447x 447x 447x 447x 447x 447x 447x 447x 447x 447x 447x 40x 40x 447x 1x 1x 447x 447x 447x 447x 447x 447x 447x 447x 447x 447x 447x 447x 447x 447x 447x 447x 447x 447x 447x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 447x 421x 421x 2x 2x 300x 300x 300x 300x 300x 300x 300x 300x 300x 300x 300x 300x 300x 300x 300x 300x 300x 300x 2x 2x 81x 81x 81x 81x 81x 81x 81x 81x 81x 81x 81x 81x 81x 81x 81x 81x 81x 81x 81x 81x 81x 81x 81x 81x 81x 81x 81x 81x 81x 81x 81x 81x 81x 81x 81x 81x 2x 2x 13x 13x 13x 13x 13x 2x 2x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 4x 4x 2x 2x 547x 547x 547x 547x 547x 2x 2x 11x 11x 11x 11x 11x 2x 2x 47x 47x 47x 47x 47x 47x 47x 47x 47x 2x 2x 103x 8x 8x 8x 8x 14x 14x 8x 8x 8x 8x 8x 8x 8x 8x 8x 6x 8x 8x 8x 8x 8x 8x 8x 8x 8x 103x 95x 95x 95x 2x 2x 123x 2x 2x 31x 52x 24x 24x 24x 24x 24x 24x 52x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 2x 2x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 2x 2x 231x 231x 231x 231x 231x 231x 231x 231x 231x 231x 231x 231x 231x 231x 231x 231x 167x 4x 167x 162x 162x 77x 162x 83x 61x 83x 22x 22x 83x 163x 1x 1x 167x 231x 231x 231x 231x 231x 231x 231x 231x 231x 231x 231x 231x 231x 231x 231x 231x 2x 2x 30x 30x 30x 30x 30x 30x 30x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 3611x 3611x 3611x 3611x 3611x 3611x 3611x 3611x 3611x 3611x 3611x 3611x 3611x 3611x 3611x 3611x 3611x 3611x 3611x 3611x 3611x 3611x 2421x 1367x 5x 5x 5x 2x 5x           5x 5x 5x 1367x 1211x 496x 1211x 30x 30x 1211x 1211x 2421x 305x 272x 305x 96x 96x 24x 24x 24x 21x 96x 305x 1x 1x 304x 205x 205x 305x 186x 298x 6x 6x 6x 6x 6x 6x 298x 5x 5x 5x 5x 5x 5x 180x 65x 65x 65x 65x 65x 65x 65x 126x 126x 126x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 42x 42x 42x 65x 23x 23x 23x 65x 65x 65x 65x 65x 65x 65x 65x 175x 110x 110x 110x 110x 110x 110x 110x 110x 110x 110x 110x 110x 110x 110x 110x 110x 1052x 98x 98x 738x 25x 651x 33x 626x 10x 593x 583x 583x 2421x 3611x 3611x 17x 17x 17x 17x 17x 10x 10x 17x 3611x 3611x 19x 19x 19x 19x 19x 19x 4x 4x 19x 3611x 3611x 3611x 3611x 3611x 97x 97x 97x 97x 97x 97x 97x 3611x 3514x 1329x 920x 920x 920x 920x 920x 920x 920x 920x 920x 917x 917x 917x 917x 917x 917x 917x 917x 917x 917x 920x 920x 409x 409x 409x 409x 409x 409x 409x 409x 409x 409x 409x 409x 409x 3514x 3611x 3611x 3611x 2x 2x 2x 2x 2x 2x 2x 17x 17x 22x 17x 17x 10x 10x 17x 17x 5x 5x 15x 2x 2x 2x 2x 2x 2x 2x 2x 2x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 2x 2x 2x 2x 2x 2x 19x 19x 28x 28x 28x 28x 28x 1x 1x 28x 19x 19x 15x 19x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 19x 2x 2x 2x 2x 2x 2x 2x 2206x 2206x 2206x 2206x 2206x 2206x 2206x 2206x 2206x 2206x 4432x 4432x 4432x 4432x 2206x 2206x 4432x 4432x 4432x 4432x 4432x 2206x 2206x 2206x 2206x 2206x 2206x 2206x 2206x 2206x 2206x 2206x 2206x 2206x 2206x 2206x 2206x 2206x 2206x 2206x 2206x 2206x 2206x 2206x 2206x 2206x 2206x 2206x 2206x 2206x 2206x 2206x 2206x 2206x 811x 811x 2206x 2206x 1904x 1885x 1885x 19x 19x 1904x 2206x 2206x 2206x 2206x 2206x 2206x 2206x 2206x 2206x 2206x 2206x 2206x 2206x 2206x 2206x 2206x 2206x 2206x 2206x 2206x 2206x 140x 140x     140x 140x 140x 105x 140x 91x 88x 88x 65x 65x 88x 91x 140x 140x 140x 2206x 2206x 46x 46x 46x 46x 46x 46x 2206x 2206x 2206x 2206x 70x 70x 271x 1x 1x 70x 70x 70x 271x 1x 1x 70x 70x 70x 70x 70x 70x 70x 70x 70x 70x 70x 70x 70x 70x 70x 70x 70x 70x 70x 70x 70x 70x 70x 70x 70x 2206x 2206x 2206x 2206x 2206x 2206x 84x 84x 84x 84x 84x 2206x 2206x 2206x 2206x 2206x 7895x 2078x 2078x 7895x 2206x 136x 136x 2206x 1116x 1116x 1116x 1116x 2206x 2206x 2206x 2206x 2206x 2206x 2206x 2206x 2206x 8x 8x 8x 33x 33x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 2206x 2206x 21x 21x 21x 21x 2206x 2206x 3x 3x 2206x 2206x 2206x 2206x 2206x 2206x 2206x 2206x 2206x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 2206x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 2202x 2134x 2134x 2206x 2206x 2206x 2206x 2206x 2206x 2206x 2x 2x 2x 2x 2x 2x 2x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x  
import { walk } from 'zimmerframe';
import { set_scope, get_rune } from '../../scope.js';
import {
	extract_identifiers,
	extract_paths,
	is_event_attribute,
	unwrap_optional
} from '../../../utils/ast.js';
import * as b from '../../../utils/builders.js';
import is_reference from 'is-reference';
import {
	ContentEditableBindings,
	VoidElements,
	WhitespaceInsensitiveAttributes
} from '../../constants.js';
import {
	clean_nodes,
	determine_namespace_for_children,
	escape_html,
	infer_namespace,
	transform_inspect_rune
} from '../utils.js';
import { create_attribute, is_custom_element_node, is_element_node } from '../../nodes.js';
import { error } from '../../../errors.js';
import { binding_properties } from '../../bindings.js';
import { regex_starts_with_newline, regex_whitespaces_strict } from '../../patterns.js';
import {
	DOMBooleanAttributes,
	HYDRATION_END,
	HYDRATION_END_ELSE,
	HYDRATION_START
} from '../../../../constants.js';
import { sanitize_template_string } from '../../../utils/sanitize_template_string.js';
import { BLOCK_CLOSE, BLOCK_CLOSE_ELSE } from '../../../../internal/server/hydration.js';
 
export const block_open = t_string(`<!--${HYDRATION_START}-->`);
export const block_close = t_string(`<!--${HYDRATION_END}-->`);
 
/**
 * @param {string} value
 * @returns {import('./types').TemplateString}
 */
function t_string(value) {
	return { type: 'string', value };
}
 
/**
 * @param {import('estree').Expression} value
 * @param {boolean} [needs_escaping]
 * @returns {import('./types').TemplateExpression}
 */
function t_expression(value, needs_escaping = false) {
	return { type: 'expression', value, needs_escaping };
}
 
/**
 * @param {import('estree').Statement} value
 * @returns {import('./types').TemplateStatement}
 */
function t_statement(value) {
	return { type: 'statement', value };
}
 
/**
 * @param {import('./types').Template[]} template
 * @param {import('estree').Identifier} out
 * @returns {import('estree').Statement[]}
 */
function serialize_template(template, out = b.id('out')) {
	/** @type {import('estree').TemplateElement[]} */
	let quasis = [];
 
	/** @type {import('estree').Expression[]} */
	let expressions = [];
 
	/** @type {import('estree').Statement[]} */
	const statements = [];
 
	const flush_payload = () => {
		statements.push(
			b.stmt(b.assignment('+=', b.member(b.id('$$payload'), out), b.template(quasis, expressions)))
		);
		quasis = [];
		expressions = [];
	};
 
	for (let i = 0; i < template.length; i++) {
		const template_item = template[i];
		if (template_item.type === 'statement') {
			if (quasis.length !== 0) {
				flush_payload();
			}
			statements.push(template_item.value);
		} else {
			let last = quasis.at(-1);
			if (!last) quasis.push((last = b.quasi('', false)));
 
			if (template_item.type === 'string') {
				last.value.raw += sanitize_template_string(template_item.value);
			} else if (template_item.type === 'expression') {
				const value = template_item.value;
				if (value.type === 'TemplateLiteral') {
					const raw = value.quasis[0].value.raw;
					last.value.raw += template_item.needs_escaping ? sanitize_template_string(raw) : raw;
					quasis.push(...value.quasis.slice(1));
					expressions.push(...value.expressions);
					continue;
				}
				expressions.push(value);
				quasis.push(b.quasi('', i + 1 === template.length || template[i + 1].type === 'statement'));
			}
		}
	}
 
	if (quasis.length !== 0) {
		flush_payload();
	}
 
	return statements;
}
 
/**
 * Processes an array of template nodes, joining sibling text/expression nodes and
 * recursing into child nodes.
 * @param {Array<import('#compiler').SvelteNode | import('./types').Anchor>} nodes
 * @param {import('#compiler').SvelteNode} parent
 * @param {import('./types').ComponentContext} context
 */
function process_children(nodes, parent, { visit, state }) {
	/** @typedef {Array<import('#compiler').Text | import('#compiler').Comment | import('#compiler').ExpressionTag | import('./types').Anchor>} Sequence */
 
	/** @type {Sequence} */
	let sequence = [];
	let did_flush = false;
 
	/**
	 * @param {Sequence} sequence
	 * @param {boolean} final
	 */
	function flush_sequence(sequence, final) {
		const is_single_flush = !did_flush && final;
		did_flush = true;
 
		if (sequence.length === 1) {
			const node = sequence[0];
 
			if (node.type === 'Text') {
				if (
					is_single_flush &&
					parent.type === 'RegularElement' &&
					(parent.name === 'script' || parent.name === 'style')
				) {
					state.template.push(t_string(node.data));
				} else {
					state.template.push(t_string(escape_html(node.data)));
				}
				return;
			}
 
			if (node.type === 'Comment') {
				state.template.push(t_string(`<!--${escape_html(node.data)}-->`));
				return;
			}
 
			if (node.type === 'Anchor') {
				state.template.push(t_expression(node.id));
				return;
			}
 
			const expression = b.call(
				'$.escape',
				/** @type {import('estree').Expression} */ (visit(node.expression))
			);
			state.template.push(t_expression(expression));
 
			return;
		}
 
		/** @type {import('estree').TemplateElement[]} */
		const quasis = [];
 
		/** @type {import('estree').Expression[]} */
		const expressions = [];
 
		quasis.push(b.quasi('', false));
 
		for (let i = 0; i < sequence.length; i++) {
			const node = sequence[i];
			if (node.type === 'Text' || node.type === 'Comment') {
				let last = /** @type {import('estree').TemplateElement} */ (quasis.at(-1));
				last.value.raw += node.type === 'Comment' ? `<!--${node.data}-->` : escape_html(node.data);
			} else if (node.type === 'ExpressionTag' && node.expression.type === 'Literal') {
				let last = /** @type {import('estree').TemplateElement} */ (quasis.at(-1));
				if (node.expression.value != null) {
					last.value.raw += escape_html(node.expression.value + '');
				}
			} else if (node.type === 'Anchor') {
				expressions.push(node.id);
				quasis.push(b.quasi('', i + 1 === sequence.length));
			} else {
				expressions.push(
					b.call('$.escape', /** @type {import('estree').Expression} */ (visit(node.expression)))
				);
				quasis.push(b.quasi('', i + 1 === sequence.length));
			}
		}
 
		state.template.push(t_expression(b.template(quasis, expressions), true));
	}
 
	for (let i = 0; i < nodes.length; i += 1) {
		const node = nodes[i];
 
		if (
			node.type === 'Text' ||
			node.type === 'Comment' ||
			node.type === 'ExpressionTag' ||
			node.type === 'Anchor'
		) {
			sequence.push(node);
		} else {
			if (sequence.length > 0) {
				flush_sequence(sequence, false);
				sequence = [];
			}
 
			visit(node, {
				...state
			});
		}
	}
 
	if (sequence.length > 0) {
		flush_sequence(sequence, true);
	}
}
 
/**
 * @param {import('#compiler').SvelteNode} parent
 * @param {import('#compiler').SvelteNode[]} nodes
 * @param {import('./types').ComponentContext} context
 * @param {import('./types').Anchor} [anchor]
 * @returns {import('estree').Statement[]}
 */
function create_block(parent, nodes, context, anchor) {
	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 && !anchor) {
		return [];
	}
 
	/** @type {import('./types').ComponentServerTransformState} */
	const state = {
		...context.state,
		init: [],
		template: [],
		metadata: {
			namespace
		}
	};
 
	for (const node of hoisted) {
		context.visit(node, state);
	}
 
	process_children(anchor ? [anchor, ...trimmed, anchor] : trimmed, parent, {
		...context,
		state
	});
 
	/** @type {import('estree').Statement[]} */
	const body = [];
 
	if (state.template.length > 0) {
		body.push(...state.init);
		body.push(...serialize_template(state.template));
	} else {
		body.push(...state.init);
	}
 
	return body;
}
 
/**
 * @param {import('estree').VariableDeclarator} declarator
 * @param {import('../../scope').Scope} scope
 * @param {import('estree').Expression} value
 * @returns {import('estree').VariableDeclarator[]}
 */
function create_state_declarators(declarator, scope, value) {
	if (declarator.id.type === 'Identifier') {
		return [b.declarator(declarator.id, value)];
	}
 
	const tmp = scope.generate('tmp');
	const paths = extract_paths(declarator.id);
	return [
		b.declarator(b.id(tmp), value), // TODO inject declarator for opts, so we can use it below
		...paths.map((path) => {
			const value = path.expression?.(b.id(tmp));
			return b.declarator(path.node, value);
		})
	];
}
 
/**
 * @param {import('estree').Identifier} node
 * @param {import('./types').ServerTransformState} state
 * @returns {import('estree').Expression}
 */
function serialize_get_binding(node, state) {
	const binding = state.scope.get(node.name);
 
	if (binding === null || node === binding.node) {
		// No associated binding or the declaration itself which shouldn't be transformed
		return node;
	}
 
	if (binding.kind === 'store_sub') {
		const store_id = b.id(node.name.slice(1));
		return b.call(
			'$.store_get',
			b.assignment('??=', b.id('$$store_subs'), b.object([])),
			b.literal(node.name),
			serialize_get_binding(store_id, state)
		);
	}
 
	if (binding.expression) {
		return typeof binding.expression === 'function' ? binding.expression(node) : binding.expression;
	}
 
	return node;
}
 
/**
 * @param {import('estree').AssignmentExpression} node
 * @param {Pick<import('zimmerframe').Context<import('#compiler').SvelteNode, import('./types').ServerTransformState>, 'visit' | 'state'>} context
 */
function get_assignment_value(node, { state, visit }) {
	if (node.left.type === 'Identifier') {
		const operator = node.operator;
		return operator === '='
			? /** @type {import('estree').Expression} */ (visit(node.right))
			: // turn something like x += 1 into x = x + 1
				b.binary(
					/** @type {import('estree').BinaryOperator} */ (operator.slice(0, -1)),
					serialize_get_binding(node.left, state),
					/** @type {import('estree').Expression} */ (visit(node.right))
				);
	} else {
		return /** @type {import('estree').Expression} */ (visit(node.right));
	}
}
 
/**
 * @param {string} name
 */
function is_store_name(name) {
	return name[0] === '$' && /[A-Za-z_]/.test(name[1]);
}
 
/**
 * @param {import('estree').AssignmentExpression} node
 * @param {import('zimmerframe').Context<import('#compiler').SvelteNode, import('./types').ServerTransformState>} context
 * @param {() => any} fallback
 * @returns {import('estree').Expression}
 */
function serialize_set_binding(node, context, fallback) {
	const { state, visit } = context;
 
	if (
		node.left.type === 'ArrayPattern' ||
		node.left.type === 'ObjectPattern' ||
		node.left.type === 'RestElement'
	) {
		// Turn assignment into an IIFE, so that `$.set` calls etc don't produce invalid code
		const tmp_id = context.state.scope.generate('tmp');
 
		/** @type {import('estree').AssignmentExpression[]} */
		const original_assignments = [];
 
		/** @type {import('estree').Expression[]} */
		const assignments = [];
 
		const paths = extract_paths(node.left);
 
		for (const path of paths) {
			const value = path.expression?.(b.id(tmp_id));
			const assignment = b.assignment('=', path.node, value);
			original_assignments.push(assignment);
			assignments.push(serialize_set_binding(assignment, context, () => assignment));
		}
 
		if (assignments.every((assignment, i) => assignment === original_assignments[i])) {
			// No change to output -> nothing to transform -> we can keep the original assignment
			return fallback();
		}
 
		return b.call(
			b.thunk(
				b.block([
					b.const(tmp_id, /** @type {import('estree').Expression} */ (visit(node.right))),
					b.stmt(b.sequence(assignments)),
					b.return(b.id(tmp_id))
				])
			)
		);
	}
 
	if (node.left.type !== 'Identifier' && node.left.type !== 'MemberExpression') {
		error(node, 'INTERNAL', `Unexpected assignment type ${node.left.type}`);
	}
 
	let left = node.left;
 
	while (left.type === 'MemberExpression') {
		// @ts-expect-error
		left = left.object;
	}
 
	if (left.type !== 'Identifier') {
		return fallback();
	}
 
	const is_store = is_store_name(left.name);
	const left_name = is_store ? left.name.slice(1) : left.name;
	const binding = state.scope.get(left_name);
 
	if (!binding) return fallback();
 
	if (binding.mutation !== null) {
		return binding.mutation(node, context);
	}
 
	if (
		binding.kind !== 'state' &&
		binding.kind !== 'frozen_state' &&
		binding.kind !== 'prop' &&
		binding.kind !== 'bindable_prop' &&
		binding.kind !== 'each' &&
		binding.kind !== 'legacy_reactive' &&
		!is_store
	) {
		// TODO error if it's a computed (or rest prop)? or does that already happen elsewhere?
		return fallback();
	}
 
	const value = get_assignment_value(node, { state, visit });
	if (left === node.left) {
		if (is_store) {
			return b.call(
				'$.store_set',
				b.id(left_name),
				/** @type {import('estree').Expression} */ (visit(node.right))
			);
		}
		return fallback();
	} else if (is_store) {
		return b.call(
			'$.mutate_store',
			b.assignment('??=', b.id('$$store_subs'), b.object([])),
			b.literal(left.name),
			b.id(left_name),
			b.assignment(node.operator, /** @type {import('estree').Pattern} */ (visit(node.left)), value)
		);
	}
	return fallback();
}
 
/**
 * @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 && context.state.metadata.namespace !== 'foreign') {
		name = name.toLowerCase();
		// don't lookup boolean aliases here, the server runtime function does only
		// check for the lowercase variants of boolean attributes
	}
	return name;
}
 
/** @type {import('./types').Visitors} */
const global_visitors = {
	Identifier(node, { path, state }) {
		if (is_reference(node, /** @type {import('estree').Node} */ (path.at(-1)))) {
			if (node.name === '$$props') {
				return b.id('$$sanitized_props');
			}
			return serialize_get_binding(node, state);
		}
	},
	AssignmentExpression(node, context) {
		return serialize_set_binding(node, context, context.next);
	},
	UpdateExpression(node, context) {
		const { state, next } = context;
		const argument = node.argument;
 
		if (argument.type === 'Identifier' && state.scope.get(argument.name)?.kind === 'store_sub') {
			let fn = '$.update_store';
			if (node.prefix) fn += '_pre';
 
			/** @type {import('estree').Expression[]} */
			const args = [
				b.assignment('??=', b.id('$$store_subs'), b.object([])),
				b.literal(argument.name),
				b.id(argument.name.slice(1))
			];
			if (node.operator === '--') {
				args.push(b.literal(-1));
			}
 
			return b.call(fn, ...args);
		}
		return next();
	}
};
 
/** @type {import('./types').Visitors} */
const javascript_visitors = {
	Program(node, { visit }) {
		return /** @type {import('estree').Program} */ ({
			...node,
			body: node.body.map((node) => /** @type {import('estree').Node} */ (visit(node)))
		});
	},
	BlockStatement(node, { visit }) {
		return /** @type {import('estree').BlockStatement} */ ({
			...node,
			body: node.body.map((node) => /** @type {import('estree').Node} */ (visit(node)))
		});
	}
};
 
/** @type {import('./types').Visitors} */
const javascript_visitors_runes = {
	ClassBody(node, { state, visit }) {
		/** @type {Map<string, import('../../3-transform/client/types.js').StateField>} */
		const public_derived = new Map();
 
		/** @type {Map<string, import('../../3-transform/client/types.js').StateField>} */
		const private_derived = new Map();
 
		/** @type {string[]} */
		const private_ids = [];
 
		for (const definition of node.body) {
			if (
				definition.type === 'PropertyDefinition' &&
				(definition.key.type === 'Identifier' || definition.key.type === 'PrivateIdentifier')
			) {
				const { type, name } = definition.key;
 
				const is_private = type === 'PrivateIdentifier';
				if (is_private) private_ids.push(name);
 
				if (definition.value?.type === 'CallExpression') {
					const rune = get_rune(definition.value, state.scope);
					if (rune === '$derived' || rune === '$derived.by') {
						/** @type {import('../../3-transform/client/types.js').StateField} */
						const field = {
							kind: rune === '$derived.by' ? 'derived_call' : 'derived',
							// @ts-expect-error this is set in the next pass
							id: is_private ? definition.key : null
						};
 
						if (is_private) {
							private_derived.set(name, field);
						} else {
							public_derived.set(name, field);
						}
					}
				}
			}
		}
 
		// each `foo = $derived()` needs a backing `#foo` field
		for (const [name, field] of public_derived) {
			let deconflicted = name;
			while (private_ids.includes(deconflicted)) {
				deconflicted = '_' + deconflicted;
			}
 
			private_ids.push(deconflicted);
			field.id = b.private_id(deconflicted);
		}
 
		/** @type {Array<import('estree').MethodDefinition | import('estree').PropertyDefinition>} */
		const body = [];
 
		const child_state = { ...state, private_derived };
 
		// Replace parts of the class body
		for (const definition of node.body) {
			if (
				definition.type === 'PropertyDefinition' &&
				(definition.key.type === 'Identifier' || definition.key.type === 'PrivateIdentifier')
			) {
				const name = definition.key.name;
 
				const is_private = definition.key.type === 'PrivateIdentifier';
				const field = (is_private ? private_derived : public_derived).get(name);
 
				if (definition.value?.type === 'CallExpression' && field !== undefined) {
					const init = /** @type {import('estree').Expression} **/ (
						visit(definition.value.arguments[0], child_state)
					);
					const value =
						field.kind === 'derived_call'
							? b.call('$.once', init)
							: b.call('$.once', b.thunk(init));
 
					if (is_private) {
						body.push(b.prop_def(field.id, value));
					} else {
						// #foo;
						const member = b.member(b.this, field.id);
						body.push(b.prop_def(field.id, value));
 
						// get foo() { return this.#foo; }
						body.push(b.method('get', definition.key, [], [b.return(b.call(member))]));
 
						if ((field.kind === 'derived' || field.kind === 'derived_call') && state.options.dev) {
							body.push(
								b.method(
									'set',
									definition.key,
									[b.id('_')],
									[b.throw_error(`Cannot update a derived property ('${name}')`)]
								)
							);
						}
					}
 
					continue;
				}
			}
 
			body.push(/** @type {import('estree').MethodDefinition} **/ (visit(definition, child_state)));
		}
 
		return { ...node, body };
	},
	PropertyDefinition(node, { state, next, visit }) {
		if (node.value != null && node.value.type === 'CallExpression') {
			const rune = get_rune(node.value, state.scope);
 
			if (rune === '$state' || rune === '$state.frozen' || rune === '$derived') {
				return {
					...node,
					value:
						node.value.arguments.length === 0
							? null
							: /** @type {import('estree').Expression} */ (visit(node.value.arguments[0]))
				};
			}
			if (rune === '$derived.by') {
				return {
					...node,
					value:
						node.value.arguments.length === 0
							? null
							: b.call(/** @type {import('estree').Expression} */ (visit(node.value.arguments[0])))
				};
			}
		}
		next();
	},
	VariableDeclaration(node, { state, visit }) {
		const declarations = [];
 
		for (const declarator of node.declarations) {
			const init = declarator.init;
			const rune = get_rune(init, state.scope);
			if (!rune || rune === '$effect.active' || rune === '$inspect') {
				declarations.push(/** @type {import('estree').VariableDeclarator} */ (visit(declarator)));
				continue;
			}
 
			if (rune === '$props') {
				// remove $bindable() from props declaration and handle rest props
				let uses_rest_props = false;
				const id = walk(declarator.id, null, {
					AssignmentPattern(node) {
						if (
							node.right.type === 'CallExpression' &&
							get_rune(node.right, state.scope) === '$bindable'
						) {
							const right = node.right.arguments.length
								? /** @type {import('estree').Expression} */ (visit(node.right.arguments[0]))
								: b.id('undefined');
							return b.assignment_pattern(node.left, right);
						}
					},
					RestElement(node, { path }) {
						if (path.at(-1) === declarator.id) {
							uses_rest_props = true;
						}
					}
				});
 
				const exports = /** @type {import('../../types').ComponentAnalysis} */ (
					state.analysis
				).exports.map(({ name, alias }) => b.literal(alias ?? name));
 
				declarations.push(
					b.declarator(
						id,
						uses_rest_props && exports.length > 0
							? b.call('$.rest_props', b.id('$$props'), b.array(exports))
							: b.id('$$props')
					)
				);
				continue;
			}
 
			const args = /** @type {import('estree').CallExpression} */ (init).arguments;
			const value =
				args.length === 0
					? b.id('undefined')
					: /** @type {import('estree').Expression} */ (visit(args[0]));
 
			if (rune === '$derived.by') {
				declarations.push(
					b.declarator(
						/** @type {import('estree').Pattern} */ (visit(declarator.id)),
						b.call(value)
					)
				);
				continue;
			}
 
			if (declarator.id.type === 'Identifier') {
				declarations.push(b.declarator(declarator.id, value));
				continue;
			}
 
			if (rune === '$derived') {
				declarations.push(
					b.declarator(/** @type {import('estree').Pattern} */ (visit(declarator.id)), value)
				);
				continue;
			}
 
			declarations.push(...create_state_declarators(declarator, state.scope, value));
		}
 
		return {
			...node,
			declarations
		};
	},
	ExpressionStatement(node, context) {
		const expression = node.expression;
		if (expression.type === 'CallExpression') {
			const callee = expression.callee;
 
			if (callee.type === 'Identifier' && callee.name === '$effect') {
				return b.empty;
			}
 
			if (
				callee.type === 'MemberExpression' &&
				callee.object.type === 'Identifier' &&
				callee.object.name === '$effect'
			) {
				return b.empty;
			}
		}
		context.next();
	},
	CallExpression(node, context) {
		const rune = get_rune(node, context.state.scope);
 
		if (rune === '$host') {
			return b.id('undefined');
		}
 
		if (rune === '$effect.active') {
			return b.literal(false);
		}
 
		if (rune === '$inspect' || rune === '$inspect().with') {
			return transform_inspect_rune(node, context);
		}
 
		context.next();
	},
	MemberExpression(node, context) {
		if (node.object.type === 'ThisExpression' && node.property.type === 'PrivateIdentifier') {
			const field = context.state.private_derived.get(node.property.name);
			if (field) {
				return b.call(node);
			}
		}
 
		context.next();
	}
};
 
/**
 *
 * @param {true | Array<import('#compiler').Text | import('#compiler').ExpressionTag>} attribute_value
 * @param {import('./types').ComponentContext} context
 * @param {boolean} trim_whitespace
 * @param {boolean} is_component
 * @returns {import('estree').Expression}
 */
function serialize_attribute_value(
	attribute_value,
	context,
	trim_whitespace = false,
	is_component = false
) {
	if (attribute_value === true) {
		return b.true;
	}
 
	if (attribute_value.length === 0) {
		return b.literal(''); // is this even possible?
	}
 
	if (attribute_value.length === 1) {
		const value = attribute_value[0];
		if (value.type === 'Text') {
			let data = value.data;
			if (trim_whitespace) {
				data = data.replace(regex_whitespaces_strict, ' ').trim();
			}
 
			return b.literal(is_component ? data : escape_html(data, true));
		} else {
			return /** @type {import('estree').Expression} */ (context.visit(value.expression));
		}
	}
 
	/** @type {import('estree').TemplateElement[]} */
	const quasis = [];
 
	/** @type {import('estree').Expression[]} */
	const expressions = [];
 
	if (attribute_value[0].type !== 'Text') {
		quasis.push(b.quasi('', false));
	}
 
	let i = 0;
	for (const node of attribute_value) {
		i++;
		if (node.type === 'Text') {
			let data = node.data;
			if (trim_whitespace) {
				// don't trim, space could be important to separate from expression tag
				data = data.replace(regex_whitespaces_strict, ' ');
			}
			quasis.push(b.quasi(data, i === attribute_value.length));
		} else {
			expressions.push(
				b.call(
					'$.stringify',
					/** @type {import('estree').Expression} */ (context.visit(node.expression))
				)
			);
			if (i === attribute_value.length || attribute_value[i]?.type !== 'Text') {
				quasis.push(b.quasi('', true));
			}
		}
	}
 
	return b.template(quasis, expressions);
}
 
/**
 *
 * @param {import('#compiler').RegularElement | import('#compiler').SvelteElement} element
 * @param {Array<import('#compiler').Attribute | import('#compiler').SpreadAttribute>} attributes
 * @param {import('#compiler').StyleDirective[]} style_directives
 * @param {import('#compiler').ClassDirective[]} class_directives
 * @param {import('./types').ComponentContext} context
 */
function serialize_element_spread_attributes(
	element,
	attributes,
	style_directives,
	class_directives,
	context
) {
	/** @type {import('estree').Expression[]} */
	const values = [];
 
	for (const attribute of attributes) {
		if (attribute.type === 'Attribute') {
			const name = get_attribute_name(element, attribute, context);
			const value = serialize_attribute_value(
				attribute.value,
				context,
				WhitespaceInsensitiveAttributes.includes(name)
			);
			values.push(b.object([b.prop('init', b.literal(name), value)]));
		} else {
			values.push(/** @type {import('estree').Expression} */ (context.visit(attribute)));
		}
	}
 
	const lowercase_attributes =
		element.metadata.svg || (element.type === 'RegularElement' && is_custom_element_node(element))
			? b.false
			: b.true;
	const is_svg = element.metadata.svg ? b.true : b.false;
	/** @type {import('estree').Expression[]} */
	const args = [
		b.array(values),
		lowercase_attributes,
		is_svg,
		b.literal(context.state.analysis.css.hash)
	];
 
	if (style_directives.length > 0 || class_directives.length > 0) {
		const styles = style_directives.map((directive) =>
			b.init(
				directive.name,
				directive.value === true
					? b.id(directive.name)
					: serialize_attribute_value(directive.value, context, true)
			)
		);
		const expressions = class_directives.map((directive) =>
			b.conditional(directive.expression, b.literal(directive.name), b.literal(''))
		);
		const classes = expressions.length
			? b.call(
					b.member(
						b.call(b.member(b.array(expressions), b.id('filter')), b.id('Boolean')),
						b.id('join')
					),
					b.literal(' ')
				)
			: b.literal('');
		args.push(
			b.object([
				b.init('styles', styles.length === 0 ? b.literal(null) : b.object(styles)),
				b.init('classes', classes)
			])
		);
	}
	context.state.template.push(t_expression(b.call('$.spread_attributes', ...args)));
}
 
/**
 * @param {import('#compiler').Component | import('#compiler').SvelteComponent | import('#compiler').SvelteSelf} node
 * @param {string | import('estree').Expression} component_name
 * @param {import('./types').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').Property[]} */
	const custom_css_props = [];
 
	/** @type {import('estree').ExpressionStatement[]} */
	const lets = [];
 
	/** @type {Record<string, import('#compiler').TemplateNode[]>} */
	const children = {};
 
	/**
	 * 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;
 
	/**
	 * @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 === 'SpreadAttribute') {
			props_and_spreads.push(/** @type {import('estree').Expression} */ (context.visit(attribute)));
		} else if (attribute.type === 'Attribute') {
			if (attribute.name.startsWith('--')) {
				const value = serialize_attribute_value(attribute.value, context, false, true);
				custom_css_props.push(b.init(attribute.name, value));
				continue;
			}
 
			if (attribute.name === 'slot') {
				slot_scope_applies_to_itself = true;
			}
 
			const value = serialize_attribute_value(attribute.value, context, false, true);
			push_prop(b.prop('init', b.key(attribute.name), value));
		} else if (attribute.type === 'BindDirective' && attribute.name !== 'this') {
			// TODO this needs to turn the whole thing into a while loop because the binding could be mutated eagerly in the child
			push_prop(
				b.get(attribute.name, [
					b.return(/** @type {import('estree').Expression} */ (context.visit(attribute.expression)))
				])
			);
			push_prop(
				b.set(attribute.name, [
					b.stmt(
						/** @type {import('estree').Expression} */ (
							context.visit(b.assignment('=', attribute.expression, b.id('$$value')))
						)
					),
					b.stmt(b.assignment('=', b.id('$$settled'), b.false))
				])
			);
		}
	}
 
	if (slot_scope_applies_to_itself) {
		context.state.init.push(...lets);
	}
 
	/** @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, children[slot_name], context);
		if (body.length === 0) continue;
 
		const slot_fn = b.arrow(
			[b.id('$$payload'), b.id('$$slotProps')],
			b.block([...(slot_name === 'default' && !slot_scope_applies_to_itself ? lets : []), ...body])
		);
 
		if (slot_name === 'default') {
			push_prop(
				b.prop(
					'init',
					b.id('children'),
					context.state.options.dev ? b.call('$.add_snippet_symbol', slot_fn) : slot_fn
				)
			);
		} else {
			const slot = b.prop('init', b.literal(slot_name), slot_fn);
			serialized_slots.push(slot);
		}
	}
 
	if (serialized_slots.length > 0) {
		push_prop(b.prop('init', b.id('$$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',
					b.array(props_and_spreads.map((p) => (Array.isArray(p) ? b.object(p) : p)))
				);
 
	/** @type {import('estree').Statement} */
	let statement = b.stmt(
		(typeof component_name === 'string' ? b.call : b.maybe_call)(
			context.state.options.dev
				? b.call(
						'$.validate_component',
						typeof component_name === 'string' ? b.id(component_name) : component_name
					)
				: component_name,
			b.id('$$payload'),
			props_expression
		)
	);
 
	if (custom_css_props.length > 0) {
		statement = b.stmt(
			b.call(
				'$.css_props',
				b.id('$$payload'),
				b.literal(context.state.metadata.namespace === 'svg' ? false : true),
				b.object(custom_css_props),
				b.thunk(b.block([statement]))
			)
		);
	}
 
	if (snippet_declarations.length > 0) {
		statement = b.block([...snippet_declarations, statement]);
	}
 
	return statement;
}
 
/**
 * Returns true if the attribute contains a single static text node.
 * @param {import('#compiler').Attribute} attribute
 * @returns {attribute is import('#compiler').Attribute & { value: [import('#compiler').Text] }}
 */
function is_text_attribute(attribute) {
	return (
		attribute.value !== true && attribute.value.length === 1 && attribute.value[0].type === 'Text'
	);
}
 
/** @type {import('./types').Visitors} */
const javascript_visitors_legacy = {
	VariableDeclaration(node, { state, visit }) {
		/** @type {import('estree').VariableDeclarator[]} */
		const declarations = [];
 
		for (const declarator of node.declarations) {
			const bindings = /** @type {import('#compiler').Binding[]} */ (
				state.scope.get_bindings(declarator)
			);
			const has_state = bindings.some((binding) => binding.kind === 'state');
			const has_props = bindings.some((binding) => binding.kind === 'bindable_prop');
 
			if (!has_state && !has_props) {
				declarations.push(/** @type {import('estree').VariableDeclarator} */ (visit(declarator)));
				continue;
			}
 
			if (has_props) {
				if (declarator.id.type !== 'Identifier') {
					// Turn export let into props. It's really really weird because export let { x: foo, z: [bar]} = ..
					// means that foo and bar are the props (i.e. the leafs are the prop names), not x and z.
					const tmp = state.scope.generate('tmp');
					const paths = extract_paths(declarator.id);
					declarations.push(
						b.declarator(
							b.id(tmp),
							/** @type {import('estree').Expression} */ (
								visit(/** @type {import('estree').Expression} */ (declarator.init))
							)
						)
					);
					for (const path of paths) {
						const value = path.expression?.(b.id(tmp));
						const name = /** @type {import('estree').Identifier} */ (path.node).name;
						const binding = /** @type {import('#compiler').Binding} */ (state.scope.get(name));
						const prop = b.member(b.id('$$props'), b.literal(binding.prop_alias ?? name), true);
						declarations.push(b.declarator(path.node, b.call('$.value_or_fallback', prop, value)));
					}
					continue;
				}
 
				const binding = /** @type {import('#compiler').Binding} */ (
					state.scope.get(declarator.id.name)
				);
				const prop = b.member(
					b.id('$$props'),
					b.literal(binding.prop_alias ?? declarator.id.name),
					true
				);
				const init = declarator.init
					? b.call(
							'$.value_or_fallback',
							prop,
							/** @type {import('estree').Expression} */ (visit(declarator.init))
						)
					: prop;
 
				declarations.push(b.declarator(declarator.id, init));
 
				continue;
			}
 
			declarations.push(
				...create_state_declarators(
					declarator,
					state.scope,
					/** @type {import('estree').Expression} */ (declarator.init && visit(declarator.init))
				)
			);
		}
 
		return {
			...node,
			declarations
		};
	},
	LabeledStatement(node, context) {
		if (context.path.length > 1) return;
		if (node.label.name !== '$') return;
 
		// TODO bail out if we're in module context
 
		// these statements will be topologically ordered later
		context.state.legacy_reactive_statements.set(
			node,
			// people could do "break $" inside, so we need to keep the label
			b.labeled('$', /** @type {import('estree').ExpressionStatement} */ (context.visit(node.body)))
		);
 
		return b.empty;
	}
};
 
/** @type {import('./types').ComponentVisitors} */
const template_visitors = {
	Fragment(node, context) {
		const body = create_block(node, node.nodes, context);
		return b.block(body);
	},
	HtmlTag(node, context) {
		const state = context.state;
		state.template.push(block_open);
		const raw = /** @type {import('estree').Expression} */ (context.visit(node.expression));
		context.state.template.push(t_expression(raw));
		state.template.push(block_close);
	},
	ConstTag(node, { state, visit }) {
		const declaration = node.declaration.declarations[0];
		const pattern = /** @type {import('estree').Pattern} */ (visit(declaration.id));
		const init = /** @type {import('estree').Expression} */ (visit(declaration.init));
		state.init.push(b.declaration('const', pattern, init));
	},
	DebugTag(node, { state, visit }) {
		state.template.push(
			t_statement(
				b.stmt(
					b.call(
						'console.log',
						b.object(
							node.identifiers.map((identifier) =>
								b.prop(
									'init',
									identifier,
									/** @type {import('estree').Expression} */ (visit(identifier))
								)
							)
						)
					)
				)
			),
			t_statement(b.debugger)
		);
	},
	RenderTag(node, context) {
		const state = context.state;
 
		state.template.push(block_open);
 
		const callee = unwrap_optional(node.expression).callee;
		const raw_args = unwrap_optional(node.expression).arguments;
 
		const expression = /** @type {import('estree').Expression} */ (context.visit(callee));
		const snippet_function = state.options.dev
			? b.call('$.validate_snippet', expression)
			: expression;
 
		const snippet_args = raw_args.map((arg) => {
			return /** @type {import('estree').Expression} */ (context.visit(arg));
		});
 
		state.template.push(
			t_statement(
				b.stmt(
					(node.expression.type === 'CallExpression' ? b.call : b.maybe_call)(
						snippet_function,
						b.id('$$payload'),
						...snippet_args
					)
				)
			)
		);
 
		state.template.push(block_close);
	},
	ClassDirective(node) {
		error(node, 'INTERNAL', 'Node should have been handled elsewhere');
	},
	StyleDirective(node) {
		error(node, 'INTERNAL', 'Node should have been handled elsewhere');
	},
	RegularElement(node, context) {
		const metadata = {
			...context.state.metadata,
			namespace: determine_namespace_for_children(node, context.state.metadata.namespace)
		};
 
		context.state.template.push(t_string(`<${node.name}`));
		const body_expression = serialize_element_attributes(node, context);
		context.state.template.push(t_string('>'));
 
		/** @type {import('./types').ComponentServerTransformState} */
		const state = {
			...context.state,
			metadata,
			preserve_whitespace:
				context.state.preserve_whitespace ||
				((node.name === 'pre' || node.name === 'textarea') && metadata.namespace !== 'foreign')
		};
 
		/** @type {import('./types').ComponentContext} */
		const inner_context =
			body_expression !== null
				? {
						...context,
						state: {
							...state,
							template: [],
							init: []
						}
					}
				: { ...context, state };
 
		const { hoisted, trimmed } = clean_nodes(
			node,
			node.fragment.nodes,
			inner_context.path,
			metadata.namespace,
			state.preserve_whitespace,
			state.options.preserveComments
		);
 
		for (const node of hoisted) {
			inner_context.visit(node, state);
		}
 
		if (context.state.options.dev) {
			context.state.template.push(
				t_statement(b.stmt(b.call('$.push_element', b.literal(node.name), b.id('$$payload'))))
			);
		}
 
		process_children(trimmed, node, inner_context);
 
		if (body_expression !== null) {
			let body_id;
			const expression = body_expression.escape
				? b.call('$.escape', body_expression.expression)
				: body_expression.expression;
			if (expression.type === 'Identifier') {
				body_id = expression;
			} else {
				body_id = b.id(context.state.scope.generate('$$body'));
				context.state.template.push(t_statement(b.const(body_id, expression)));
			}
 
			// Use the body expression as the body if it's truthy, otherwise use the inner template
			context.state.template.push(
				t_statement(
					b.if(
						body_id,
						b.block(serialize_template([t_expression(body_id)])),
						b.block([
							...inner_context.state.init,
							...serialize_template(inner_context.state.template)
						])
					)
				)
			);
		}
 
		if (!VoidElements.includes(node.name) && metadata.namespace !== 'foreign') {
			context.state.template.push(t_string(`</${node.name}>`));
		}
		if (context.state.options.dev) {
			context.state.template.push(t_statement(b.stmt(b.call('$.pop_element'))));
		}
	},
	SvelteElement(node, context) {
		let tag = /** @type {import('estree').Expression} */ (context.visit(node.tag));
		if (tag.type !== 'Identifier') {
			const tag_id = context.state.scope.generate('$$tag');
			context.state.init.push(b.const(tag_id, tag));
			tag = b.id(tag_id);
		}
 
		if (context.state.options.dev) {
			if (node.fragment.nodes.length > 0) {
				context.state.init.push(b.stmt(b.call('$.validate_void_dynamic_element', b.thunk(tag))));
			}
			context.state.init.push(b.stmt(b.call('$.validate_dynamic_element_tag', b.thunk(tag))));
		}
 
		const metadata = {
			...context.state.metadata,
			namespace: determine_namespace_for_children(node, context.state.metadata.namespace)
		};
		/** @type {import('./types').ComponentContext} */
		const inner_context = {
			...context,
			state: {
				...context.state,
				metadata,
				template: [],
				init: []
			}
		};
 
		context.state.template.push(block_open);
 
		const main = create_block(node, node.fragment.nodes, {
			...context,
			state: { ...context.state, metadata }
		});
 
		serialize_element_attributes(node, inner_context);
 
		if (context.state.options.dev) {
			context.state.template.push(
				t_statement(b.stmt(b.call('$.push_element', tag, b.id('$$payload'))))
			);
		}
 
		context.state.template.push(
			t_statement(
				b.if(
					tag,
 
					b.stmt(
						b.call(
							'$.element',
							b.id('$$payload'),
							tag,
							b.thunk(
								b.block([
									...inner_context.state.init,
									...serialize_template(inner_context.state.template)
								])
							),
							b.thunk(b.block(main))
						)
					)
				)
			),
			block_close
		);
		if (context.state.options.dev) {
			context.state.template.push(t_statement(b.stmt(b.call('$.pop_element'))));
		}
	},
	EachBlock(node, context) {
		const state = context.state;
		state.template.push(block_open);
 
		const each_node_meta = node.metadata;
		const collection = /** @type {import('estree').Expression} */ (context.visit(node.expression));
		const item = each_node_meta.item;
		const index =
			each_node_meta.contains_group_binding || !node.index
				? each_node_meta.index
				: b.id(node.index);
		const children = node.body.nodes;
 
		const array_id = state.scope.root.unique('each_array');
		state.init.push(b.const(array_id, b.call('$.ensure_array_like', collection)));
 
		/** @type {import('estree').Statement[]} */
		const each = [b.const(item, b.member(array_id, index, true))];
 
		if (node.context.type !== 'Identifier') {
			each.push(b.const(/** @type {import('estree').Pattern} */ (node.context), item));
		}
		if (index.name !== node.index && node.index != null) {
			each.push(b.let(node.index, index));
		}
 
		each.push(b.stmt(b.assignment('+=', b.id('$$payload.out'), b.literal(block_open.value))));
 
		each.push(
			.../** @type {import('estree').Statement[]} */ (create_block(node, children, context))
		);
 
		each.push(b.stmt(b.assignment('+=', b.id('$$payload.out'), b.literal(block_close.value))));
 
		const for_loop = b.for(
			b.let(index, b.literal(0)),
			b.binary('<', index, b.member(array_id, b.id('length'))),
			b.update('++', index, false),
			b.block(each)
		);
 
		const close = b.stmt(b.assignment('+=', b.id('$$payload.out'), b.literal(BLOCK_CLOSE)));
 
		if (node.fallback) {
			const fallback = create_block(node, node.fallback.nodes, context);
 
			fallback.push(b.stmt(b.assignment('+=', b.id('$$payload.out'), b.literal(BLOCK_CLOSE_ELSE))));
 
			state.template.push(
				t_statement(
					b.if(
						b.binary('!==', b.member(array_id, b.id('length')), b.literal(0)),
						b.block([for_loop, close]),
						b.block(fallback)
					)
				)
			);
		} else {
			state.template.push(t_statement(for_loop), t_statement(close));
		}
	},
	IfBlock(node, context) {
		const state = context.state;
		state.template.push(block_open);
 
		const consequent = create_block(node, node.consequent.nodes, context);
		const alternate = node.alternate ? create_block(node, node.alternate.nodes, context) : [];
 
		consequent.push(b.stmt(b.assignment('+=', b.id('$$payload.out'), b.literal(BLOCK_CLOSE))));
		alternate.push(b.stmt(b.assignment('+=', b.id('$$payload.out'), b.literal(BLOCK_CLOSE_ELSE))));
 
		state.template.push(
			t_statement(
				b.if(
					/** @type {import('estree').Expression} */ (context.visit(node.test)),
					b.block(consequent),
					b.block(alternate)
				)
			)
		);
	},
	AwaitBlock(node, context) {
		const state = context.state;
		state.template.push(block_open);
 
		state.template.push(
			t_statement(
				b.stmt(
					b.call(
						'$.await',
						/** @type {import('estree').Expression} */ (context.visit(node.expression)),
						b.thunk(
							node.pending
								? /** @type {import('estree').BlockStatement} */ (context.visit(node.pending))
								: b.block([])
						),
						b.arrow(
							node.value
								? [/** @type {import('estree').Pattern} */ (context.visit(node.value))]
								: [],
							node.then
								? /** @type {import('estree').BlockStatement} */ (context.visit(node.then))
								: b.block([])
						),
						b.arrow(
							node.error
								? [/** @type {import('estree').Pattern} */ (context.visit(node.error))]
								: [],
							node.catch
								? /** @type {import('estree').BlockStatement} */ (context.visit(node.catch))
								: b.block([])
						)
					)
				)
			)
		);
 
		state.template.push(block_close);
	},
	KeyBlock(node, context) {
		const state = context.state;
		state.template.push(block_open);
		const body = create_block(node, node.fragment.nodes, context);
		state.template.push(t_statement(b.block(body)));
		state.template.push(block_close);
	},
	SnippetBlock(node, context) {
		const fn = b.function_declaration(
			node.expression,
			[b.id('$$payload'), ...node.parameters],
			/** @type {import('estree').BlockStatement} */ (context.visit(node.body))
		);
		// @ts-expect-error - TODO remove this hack once $$render_inner for legacy bindings is gone
		fn.___snippet = true;
		// TODO hoist where possible
		context.state.init.push(fn);
 
		if (context.state.options.dev) {
			context.state.init.push(b.stmt(b.call('$.add_snippet_symbol', node.expression)));
		}
	},
	Component(node, context) {
		const state = context.state;
		state.template.push(block_open);
		const call = serialize_inline_component(node, node.name, context);
		state.template.push(t_statement(call));
		state.template.push(block_close);
	},
	SvelteSelf(node, context) {
		const state = context.state;
		state.template.push(block_open);
		const call = serialize_inline_component(node, context.state.analysis.name, context);
		state.template.push(t_statement(call));
		state.template.push(block_close);
	},
	SvelteComponent(node, context) {
		const state = context.state;
		state.template.push(block_open);
		const call = serialize_inline_component(
			node,
			/** @type {import('estree').Expression} */ (context.visit(node.expression)),
			context
		);
		state.template.push(t_statement(call));
		state.template.push(block_close);
	},
	LetDirective(node, { state }) {
		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.id(name), b.id(binding.node.name));
			}
 
			return b.const(
				name,
				b.call(
					b.thunk(
						b.block([
							b.let(
								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, b.member(b.id('$$slotProps'), b.id(node.name)));
		}
	},
	SpreadAttribute(node, { visit }) {
		return visit(node.expression);
	},
	SvelteFragment(node, context) {
		for (const attribute of node.attributes) {
			if (attribute.type === 'LetDirective') {
				context.state.template.push(
					t_statement(
						/** @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))
		};
		const body = create_block(node, node.fragment.nodes, {
			...context,
			state
		});
 
		context.state.template.push(t_statement(b.block(body)));
	},
	TitleElement(node, context) {
		const state = context.state;
 
		/** @type {import('./types').ComponentServerTransformState} */
		const inner_state = {
			...state,
			init: [],
			template: []
		};
 
		process_children(node.fragment.nodes, node, {
			...context,
			state: inner_state
		});
 
		// Use `=` so that later title changes override earlier ones
		state.init.push(b.stmt(b.assignment('=', b.id('$$payload.title'), b.literal('<title>'))));
		inner_state.template.push(t_string('</title>'));
		// title is guaranteed to contain only text/expression tag children
		state.init.push(...serialize_template(inner_state.template, b.id('title')));
	},
	SlotElement(node, context) {
		const state = context.state;
		state.template.push(block_open);
 
		/** @type {import('estree').Property[]} */
		const props = [];
 
		/** @type {import('estree').Expression[]} */
		const spreads = [];
 
		/** @type {import('estree').ExpressionStatement[]} */
		const lets = [];
 
		/** @type {import('estree').Expression} */
		let expression = b.member_id('$$props.children');
 
		for (const attribute of node.attributes) {
			if (attribute.type === 'SpreadAttribute') {
				spreads.push(/** @type {import('estree').Expression} */ (context.visit(attribute)));
			} else if (attribute.type === 'Attribute') {
				const value = serialize_attribute_value(attribute.value, context);
				if (attribute.name === 'name') {
					expression = b.member(b.member_id('$$props.$$slots'), value, true, true);
				} 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.array([b.object(props), ...spreads]));
		const fallback =
			node.fragment.nodes.length === 0
				? b.literal(null)
				: b.thunk(b.block(create_block(node, node.fragment.nodes, context)));
		const slot = b.call('$.slot', b.id('$$payload'), expression, props_expression, fallback);
 
		state.template.push(t_statement(b.stmt(slot)));
		state.template.push(block_close);
	},
	SvelteHead(node, context) {
		const state = context.state;
		const body = create_block(node, node.fragment.nodes, context);
		state.template.push(
			t_statement(
				b.stmt(b.call('$.head', b.id('$$payload'), b.arrow([b.id('$$payload')], b.block(body))))
			)
		);
	},
	// @ts-ignore: need to extract this out somehow
	CallExpression: javascript_visitors_runes.CallExpression
};
 
/**
 * Writes the output to the template output. Some elements may have attributes on them that require the
 * their output to be the child content instead. In this case, an object is returned.
 * @param {import('#compiler').RegularElement | import('#compiler').SvelteElement} node
 * @param {import('zimmerframe').Context<import('#compiler').SvelteNode, import('./types').ComponentServerTransformState>} context
 */
function serialize_element_attributes(node, context) {
	/** @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 = [];
 
	/** @type {{ escape: boolean; expression: import('estree').Expression } | null} */
	let content = null;
 
	let has_spread = false;
	// Use the index to keep the attributes order which is important for spreading
	let class_attribute_idx = -1;
	let style_attribute_idx = -1;
 
	for (const attribute of node.attributes) {
		if (attribute.type === 'Attribute') {
			if (attribute.name === 'value' && node.name === 'textarea') {
				if (
					attribute.value !== true &&
					attribute.value[0].type === 'Text' &&
					regex_starts_with_newline.test(attribute.value[0].data)
				) {
					// Two or more leading newlines are required to restore the leading newline immediately after `<textarea>`.
					// see https://html.spec.whatwg.org/multipage/syntax.html#element-restrictions
					// also see related code in analysis phase
					attribute.value[0].data = '\n' + attribute.value[0].data;
				}
				content = { escape: true, expression: serialize_attribute_value(attribute.value, context) };
 
				// omit event handlers
			} else if (!is_event_attribute(attribute)) {
				if (attribute.name === 'class') {
					class_attribute_idx = attributes.length;
				} else if (attribute.name === 'style') {
					style_attribute_idx = attributes.length;
				}
				attributes.push(attribute);
			}
		} else if (attribute.type === 'BindDirective') {
			if (attribute.name === 'value' && node.name === 'select') continue;
			if (
				attribute.name === 'value' &&
				attributes.some(
					(attr) =>
						attr.type === 'Attribute' &&
						attr.name === 'type' &&
						is_text_attribute(attr) &&
						attr.value[0].data === 'file'
				)
			) {
				continue;
			}
			if (attribute.name === 'this') continue;
 
			const binding = binding_properties[attribute.name];
			if (binding?.omit_in_ssr) continue;
 
			if (ContentEditableBindings.includes(attribute.name)) {
				content = {
					escape: false,
					expression: /** @type {import('estree').Expression} */ (
						context.visit(attribute.expression)
					)
				};
			} else if (attribute.name === 'value' && node.name === 'textarea') {
				content = {
					escape: true,
					expression: /** @type {import('estree').Expression} */ (
						context.visit(attribute.expression)
					)
				};
			} else if (attribute.name === 'group') {
				const value_attribute = /** @type {import('#compiler').Attribute | undefined} */ (
					node.attributes.find((attr) => attr.type === 'Attribute' && attr.name === 'value')
				);
				if (!value_attribute) continue;
 
				const is_checkbox = node.attributes.some(
					(attr) =>
						attr.type === 'Attribute' &&
						attr.name === 'type' &&
						is_text_attribute(attr) &&
						attr.value[0].data === 'checkbox'
				);
				attributes.push(
					create_attribute('checked', -1, -1, [
						{
							type: 'ExpressionTag',
							start: -1,
							end: -1,
							parent: attribute,
							expression: is_checkbox
								? b.call(
										b.member(attribute.expression, b.id('includes')),
										serialize_attribute_value(value_attribute.value, context)
									)
								: b.binary(
										'===',
										attribute.expression,
										serialize_attribute_value(value_attribute.value, context)
									),
							metadata: {
								contains_call_expression: false,
								dynamic: false
							}
						}
					])
				);
			} else {
				attributes.push(
					create_attribute(attribute.name, -1, -1, [
						{
							type: 'ExpressionTag',
							start: -1,
							end: -1,
							parent: attribute,
							expression: attribute.expression,
							metadata: {
								contains_call_expression: false,
								dynamic: false
							}
						}
					])
				);
			}
		} else if (attribute.type === 'SpreadAttribute') {
			attributes.push(attribute);
			has_spread = 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 {
			context.visit(attribute);
		}
	}
 
	if (class_directives.length > 0 && !has_spread) {
		const class_attribute = serialize_class_directives(
			class_directives,
			/** @type {import('#compiler').Attribute | null} */ (attributes[class_attribute_idx] ?? null)
		);
		if (class_attribute_idx === -1) {
			attributes.push(class_attribute);
		}
	}
 
	if (style_directives.length > 0 && !has_spread) {
		serialize_style_directives(
			style_directives,
			/** @type {import('#compiler').Attribute | null} */ (attributes[style_attribute_idx] ?? null),
			context
		);
		if (style_attribute_idx > -1) {
			attributes.splice(style_attribute_idx, 1);
		}
	}
 
	// Let bindings first, they can be used on attributes
	context.state.init.push(...lets);
 
	if (has_spread) {
		serialize_element_spread_attributes(
			node,
			attributes,
			style_directives,
			class_directives,
			context
		);
	} else {
		for (const attribute of /** @type {import('#compiler').Attribute[]} */ (attributes)) {
			if (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,
						WhitespaceInsensitiveAttributes.includes(name)
					)
				).value;
				if (name !== 'class' || literal_value) {
					context.state.template.push(
						t_string(
							` ${attribute.name}${
								DOMBooleanAttributes.includes(name) && literal_value === true
									? ''
									: `="${literal_value === true ? '' : String(literal_value)}"`
							}`
						)
					);
				}
				continue;
			}
 
			const name = get_attribute_name(node, attribute, context);
			const is_boolean = DOMBooleanAttributes.includes(name);
			const value = serialize_attribute_value(
				attribute.value,
				context,
				WhitespaceInsensitiveAttributes.includes(name)
			);
 
			context.state.template.push(
				t_expression(b.call('$.attr', b.literal(name), value, b.literal(is_boolean)))
			);
		}
	}
 
	return content;
}
 
/**
 *
 * @param {import('#compiler').ClassDirective[]} class_directives
 * @param {import('#compiler').Attribute | null} class_attribute
 * @returns
 */
function serialize_class_directives(class_directives, class_attribute) {
	const expressions = class_directives.map((directive) =>
		b.conditional(directive.expression, b.literal(directive.name), b.literal(''))
	);
	if (class_attribute === null) {
		class_attribute = create_attribute('class', -1, -1, []);
	}
	const last = /** @type {any[]} */ (class_attribute.value).at(-1);
	if (last?.type === 'Text') {
		last.data += ' ';
		last.raw += ' ';
	} else if (last) {
		/** @type {import('#compiler').Text[]} */ (class_attribute.value).push({
			type: 'Text',
			start: -1,
			end: -1,
			parent: class_attribute,
			data: ' ',
			raw: ' '
		});
	}
	/** @type {import('#compiler').ExpressionTag[]} */ (class_attribute.value).push({
		type: 'ExpressionTag',
		start: -1,
		end: -1,
		parent: class_attribute,
		expression: b.call(
			b.member(
				b.call(b.member(b.array(expressions), b.id('filter')), b.id('Boolean')),
				b.id('join')
			),
			b.literal(' ')
		),
		metadata: { contains_call_expression: false, dynamic: false }
	});
	return class_attribute;
}
 
/**
 * @param {import('#compiler').StyleDirective[]} style_directives
 * @param {import('#compiler').Attribute | null} style_attribute
 * @param {import('./types').ComponentContext} context
 */
function serialize_style_directives(style_directives, style_attribute, context) {
	const styles = style_directives.map((directive) => {
		let value =
			directive.value === true
				? b.id(directive.name)
				: serialize_attribute_value(directive.value, context, true);
		if (directive.modifiers.includes('important')) {
			value = b.binary('+', value, b.literal(' !important'));
		}
		return b.init(directive.name, value);
	});
	if (style_attribute === null) {
		context.state.template.push(t_expression(b.call('$.add_styles', b.object(styles))));
	} else {
		context.state.template.push(
			t_expression(
				b.call(
					'$.add_styles',
					b.call(
						'$.merge_styles',
						serialize_attribute_value(style_attribute.value, context, true),
						b.object(styles)
					)
				)
			)
		);
	}
}
 
/**
 * @param {import('../../types').ComponentAnalysis} analysis
 * @param {import('#compiler').ValidatedCompileOptions} options
 * @returns {import('estree').Program}
 */
export function server_component(analysis, options) {
	/** @type {import('./types').ComponentServerTransformState} */
	const state = {
		analysis,
		options,
		scope: analysis.module.scope,
		scopes: analysis.template.scopes,
		hoisted: [b.import_all('$', 'svelte/internal/server')],
		legacy_reactive_statements: new Map(),
		// these should be set by create_block - if they're called outside, it's a bug
		get init() {
			/** @type {any[]} */
			const a = [];
			a.push = () => error(null, 'INTERNAL', 'init.push should not be called outside create_block');
			return a;
		},
		get template() {
			/** @type {any[]} */
			const a = [];
			a.push = () =>
				error(null, 'INTERNAL', 'template.push should not be called outside create_block');
			return a;
		},
		metadata: {
			namespace: options.namespace
		},
		preserve_whitespace: options.preserveWhitespace,
		private_derived: new Map()
	};
 
	const module = /** @type {import('estree').Program} */ (
		walk(
			/** @type {import('#compiler').SvelteNode} */ (analysis.module.ast),
			state,
			// @ts-expect-error TODO: zimmerframe types
			{
				...set_scope(analysis.module.scopes),
				...global_visitors,
				...javascript_visitors,
				...(analysis.runes ? javascript_visitors_runes : javascript_visitors_legacy)
			}
		)
	);
 
	const instance = /** @type {import('estree').Program} */ (
		walk(
			/** @type {import('#compiler').SvelteNode} */ (analysis.instance.ast),
			{ ...state, scope: analysis.instance.scope },
			// @ts-expect-error TODO: zimmerframe types
			{
				...set_scope(analysis.instance.scopes),
				...global_visitors,
				...javascript_visitors,
				...(analysis.runes ? javascript_visitors_runes : javascript_visitors_legacy),
				ImportDeclaration(node) {
					state.hoisted.push(node);
					return b.empty;
				},
				ExportNamedDeclaration(node, context) {
					if (node.declaration) {
						return context.visit(node.declaration);
					}
 
					return b.empty;
				}
			}
		)
	);
 
	const template = /** @type {import('estree').Program} */ (
		walk(
			/** @type {import('#compiler').SvelteNode} */ (analysis.template.ast),
			{ ...state, scope: analysis.template.scope },
			// @ts-expect-error TODO: zimmerframe types
			{
				...set_scope(analysis.template.scopes),
				...global_visitors,
				...template_visitors
			}
		)
	);
 
	/** @type {import('estree').VariableDeclarator[]} */
	const legacy_reactive_declarations = [];
 
	for (const [node] of analysis.reactive_statements) {
		const statement = [...state.legacy_reactive_statements].find(([n]) => n === node);
		if (statement === undefined) {
			error(node, 'INTERNAL', 'Could not find reactive statement');
		}
 
		if (
			node.body.type === 'ExpressionStatement' &&
			node.body.expression.type === 'AssignmentExpression'
		) {
			for (const id of extract_identifiers(node.body.expression.left)) {
				const binding = analysis.instance.scope.get(id.name);
				if (binding?.kind === 'legacy_reactive') {
					legacy_reactive_declarations.push(b.declarator(id));
				}
			}
		}
 
		instance.body.push(statement[1]);
	}
 
	if (legacy_reactive_declarations.length > 0) {
		instance.body.unshift({
			type: 'VariableDeclaration',
			kind: 'let',
			declarations: legacy_reactive_declarations
		});
	}
 
	// If the component binds to a child, we need to put the template in a loop and repeat until legacy bindings are stable.
	// We can remove this once the legacy syntax is gone.
	if (analysis.uses_component_bindings) {
		const snippets = template.body.filter(
			(node) =>
				node.type === 'FunctionDeclaration' &&
				// @ts-expect-error
				node.___snippet
		);
		const rest = template.body.filter(
			(node) =>
				node.type !== 'FunctionDeclaration' ||
				// @ts-expect-error
				!node.___snippet
		);
		template.body = [
			...snippets,
			b.let('$$settled', b.true),
			b.let('$$inner_payload'),
			b.stmt(
				b.function(
					b.id('$$render_inner'),
					[b.id('$$payload')],
					b.block(/** @type {import('estree').Statement[]} */ (rest))
				)
			),
			b.do_while(
				b.unary('!', b.id('$$settled')),
				b.block([
					b.stmt(b.assignment('=', b.id('$$settled'), b.true)),
					b.stmt(
						b.assignment('=', b.id('$$inner_payload'), b.call('$.copy_payload', b.id('$$payload')))
					),
					b.stmt(b.call('$$render_inner', b.id('$$inner_payload')))
				])
			),
			b.stmt(b.call('$.assign_payload', b.id('$$payload'), b.id('$$inner_payload')))
		];
	}
 
	if (
		[...analysis.instance.scope.declarations.values()].some(
			(binding) => binding.kind === 'store_sub'
		)
	) {
		instance.body.unshift(b.var('$$store_subs'));
		template.body.push(
			b.if(b.id('$$store_subs'), b.stmt(b.call('$.unsubscribe_stores', b.id('$$store_subs'))))
		);
	}
	// Propagate values of bound props upwards if they're undefined in the parent and have a value.
	// Don't do this as part of the props retrieval because people could eagerly mutate the prop in the instance script.
	/** @type {import('estree').Property[]} */
	const props = [];
	for (const [name, binding] of analysis.instance.scope.declarations) {
		if (binding.kind === 'bindable_prop' && !name.startsWith('$$')) {
			props.push(b.init(binding.prop_alias ?? name, b.id(name)));
		}
	}
	for (const { name, alias } of analysis.exports) {
		props.push(b.init(alias ?? name, b.id(name)));
	}
	if (props.length > 0) {
		// This has no effect in runes mode other than throwing an error when someone passes
		// undefined to a binding that has a default value.
		template.body.push(b.stmt(b.call('$.bind_props', b.id('$$props'), b.object(props))));
	}
 
	const component_block = b.block([
		b.stmt(b.call('$.push', b.literal(analysis.runes))),
		.../** @type {import('estree').Statement[]} */ (instance.body),
		.../** @type {import('estree').Statement[]} */ (template.body),
		b.stmt(b.call('$.pop'))
	]);
 
	if (analysis.uses_rest_props) {
		/** @type {string[]} */
		const named_props = analysis.exports.map(({ name, alias }) => alias ?? name);
		for (const [name, binding] of analysis.instance.scope.declarations) {
			if (binding.kind === 'bindable_prop') named_props.push(binding.prop_alias ?? name);
		}
 
		component_block.body.unshift(
			b.const(
				'$$restProps',
				b.call(
					'$.rest_props',
					b.id('$$sanitized_props'),
					b.array(named_props.map((name) => b.literal(name)))
				)
			)
		);
	}
 
	if (analysis.uses_props || analysis.uses_rest_props) {
		component_block.body.unshift(
			b.const('$$sanitized_props', b.call('$.sanitize_props', b.id('$$props')))
		);
	}
 
	if (analysis.uses_slots) {
		component_block.body.unshift(b.const('$$slots', b.call('$.sanitize_slots', b.id('$$props'))));
	}
 
	const body = [...state.hoisted, ...module.body];
 
	const component_function = b.function_declaration(
		b.id(analysis.name),
		[b.id('$$payload'), b.id('$$props')],
		component_block
	);
	if (options.legacy.componentApi) {
		body.unshift(b.imports([['render', '$$_render']], 'svelte/server'));
		body.push(
			component_function,
			b.stmt(
				b.assignment(
					'=',
					b.member_id(`${analysis.name}.render`),
					b.function(
						null,
						[b.id('$$props'), b.id('$$opts')],
						b.block([
							b.return(
								b.call(
									'$$_render',
									b.id(analysis.name),
									b.object([
										b.init('props', b.id('$$props')),
										b.init('context', b.member(b.id('$$opts'), b.id('context'), false, true))
									])
								)
							)
						])
					)
				)
			),
			b.export_default(b.id(analysis.name))
		);
	} else if (options.dev) {
		body.push(
			component_function,
			b.stmt(
				b.assignment(
					'=',
					b.member_id(`${analysis.name}.render`),
					b.function(
						null,
						[],
						b.block([
							b.throw_error(
								`Component.render(...) is no longer valid in Svelte 5. ` +
									'See https://svelte-5-preview.vercel.app/docs/breaking-changes#components-are-no-longer-classes for more information'
							)
						])
					)
				)
			),
			b.export_default(b.id(analysis.name))
		);
	} else {
		body.push(b.export_default(component_function));
	}
 
	return {
		type: 'Program',
		sourceType: 'module',
		body
	};
}
 
/**
 * @param {import('../../types').Analysis} analysis
 * @param {import('#compiler').ValidatedModuleCompileOptions} options
 * @returns {import('estree').Program}
 */
export function server_module(analysis, options) {
	/** @type {import('./types').ServerTransformState} */
	const state = {
		analysis,
		options,
		scope: analysis.module.scope,
		scopes: analysis.module.scopes,
		// this is an anomaly — it can only be used in components, but it needs
		// to be present for `javascript_visitors` and so is included in module
		// transform state as well as component transform state
		legacy_reactive_statements: new Map(),
		private_derived: new Map()
	};
 
	const module = /** @type {import('estree').Program} */ (
		walk(/** @type {import('#compiler').SvelteNode} */ (analysis.module.ast), state, {
			...set_scope(analysis.module.scopes),
			...global_visitors,
			...javascript_visitors,
			...javascript_visitors_runes
		})
	);
 
	return {
		type: 'Program',
		sourceType: 'module',
		body: [b.import_all('$', 'svelte/internal/server'), ...module.body]
	};
}