All files / src/compiler/phases/2-analyze index.js

98.75% Statements 1591/1611
96.4% Branches 590/612
100% Functions 38/38
98.74% Lines 1570/1590

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 15912x 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 9562x 9562x 9562x 5575x 5575x 5575x 5575x 5575x 9562x 9562x 9562x 9562x 9562x 9562x 2x 2x 2x 2x 4764x 4764x 4764x 4764x 4764x 4764x 20x 20x 4764x 4764x 2x 2x 2x 2x 2x 2x 2x 2x 415x 415x 415x 22x 22x 393x 393x 393x 415x 34x 34x 359x 359x 359x 359x 359x 359x 359x 415x 4x 4x 4x 355x 415x 272x 402x 81x 81x 81x 6x 6x 6x 75x 75x 75x 141x 141x     141x 141x 141x 141x 141x 141x     141x 141x 64x 141x 64x 64x 64x 64x 141x 141x 64x 64x 64x 64x 64x 2x 2x 141x 15x 15x 141x 58x 58x 58x 81x     58x 81x 56x 56x 56x 56x 56x 4x 56x 52x 52x 56x 81x 332x 332x 415x 8x 8x 324x 415x     324x 415x     324x 324x 324x 415x 482x 482x     482x 482x 482x 482x 482x 2x 2x 480x 480x 480x 482x 482x 12x 482x 2x 2x 478x 478x 482x 6x 6x 472x 472x 480x 409x 409x 409x 409x 409x 409x 270x 409x 409x 482x 37x 37x 435x 435x 277x 277x 277x 2x 2x 2x 2x 2x 2x 2x 27x 27x 27x 58x 58x 1x 1x 58x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 27x 27x 27x 27x 27x 27x 2x 2x 2x 2x 2x 2x 2x 2x 4781x 4781x 4781x 4781x 4781x 4781x 4781x 4781x 4781x 4781x 4781x 4781x 3717x 3717x 5x 5x 1236x 1236x 1236x 1236x 1236x 1236x 1236x 2973x 3717x 969x 35x 969x 11x 11x 11x 11x 2x 969x 3717x 276x 276x 503x 1593x 1593x 1593x 1593x 1593x 503x 503x 5x 5x 5x 498x 498x 1593x 498x 276x 276x 5x 5x 271x 271x 44x 1x 44x 9x 13x 12x 12x 13x 9x 44x 270x 270x 270x 496x 496x 496x 496x 4x 4x 496x 1x 1x 496x 269x 269x 269x 269x 269x 269x 269x 3717x 4764x 4781x 4781x 4781x 4781x 4781x 4781x 4781x 4781x 4781x 4781x 4781x 4781x 4781x 4781x 4781x 4781x 4781x 4781x 4781x 4781x 4781x 4781x 4781x 4781x 4781x 4781x 4781x 4781x 4781x 4781x 4781x 4781x 1586x 4781x 4781x 4781x 4781x 4781x 4781x 4781x 4781x 4781x 386x 386x 386x 386x 386x 4781x 4781x 4781x 4781x 4781x 4781x 4781x 30x 30x 4x 4x 30x 30x 1x 1x 30x 30x 6x 6x 30x 30x 4764x 4781x 860x 860x     860x 860x 860x     860x 860x 2553x 2553x 2553x 2553x 2553x 2553x 2553x 2553x 2553x 2553x 2553x 2553x 2553x 2553x 2553x 2553x 2553x 2553x 2553x 2553x 828x 828x 860x 1656x 2060x 39x 120x 120x 146x 146x 146x 146x 146x 146x 21x 21x 125x 125x 146x 22x 146x 20x 47x 47x 47x 47x 47x 44x 47x 3x 3x 3x 47x 17x 17x 146x 8x 8x 8x 8x 28x 2060x 1656x 4781x 3904x 3904x 3904x 3904x 11702x 11702x 11702x 11702x 11702x 11702x 11702x 11702x 11702x 11702x 11702x 11702x 11702x 11702x 11702x 11702x 11702x 11702x 11702x 11702x 11702x 11702x 11702x 11702x 3812x 3904x 14505x 14505x 4041x 14505x 4041x 4041x 4041x 4041x 76x 76x 4041x 14505x 3812x 3812x 3812x 4639x 4781x 1x 1x 1x 1x 1x 4638x 4781x 1x 1x 4637x 4721x 386x 386x 386x 386x 918x 918x 368x 368x 368x 368x 1x 1x 386x 386x 386x 367x 367x 368x 382x 918x 759x 759x 759x 754x 754x 754x 754x 759x 539x 12x 12x 12x 527x 539x 539x 433x 433x 433x 742x 759x 431x 431x 431x 391x 431x 40x 40x 40x 40x 40x 40x 40x 40x 40x 759x 311x 311x 311x 311x 311x 311x 311x 311x 311x 311x 311x 311x 311x 759x 918x 368x 4619x 4619x 4619x 4619x 4619x 4619x 2x 2x 2x 2x 313x 313x 313x 308x 313x 8x 8x 305x 305x 305x 305x 305x 305x 305x 305x 305x 305x 305x 305x 313x 718x 718x 679x 711x 715x 715x 715x 715x 715x 715x 91x 91x 91x 715x 715x 715x 715x 335x 715x 249x 249x 466x 466x 466x 466x 679x 305x 305x 305x 305x 305x 305x 313x 288x 288x 288x 313x 11x 11x 305x 305x 313x 232x 313x 198x 198x 12x 12x 12x 12x 12x 198x 198x 200x 200x 141x 141x 141x 200x 198x 2x 2x 1437x 278x 278x 278x 256x 256x 256x 256x 256x 256x 278x 278x 1437x 1437x 2x 2x 98x 12x 12x 12x 12x 12x 12x 12x 12x 12x 98x 98x 2x 2x 31577x 31577x 26409x 46x 46x 26409x 26409x 37x 37x 26409x 26409x 8x 8x 26409x 26409x 26409x 26409x 428x 428x 428x 26409x 26409x 26409x 26409x 12067x 12067x 26409x 7068x 2526x 2526x 44x 2526x 4x 2526x 4x 4x 7038x 4542x 2104x 2104x 4542x 679x 4540x 3863x 3863x 29x 3863x   3863x 3863x 3863x 3863x 16796x 179x 179x 179x 179x 179x 179x 123x 123x 123x 179x 16673x 16673x 3863x 7068x 26409x 2x 2x 3731x 26x 26x 3705x 3705x 3705x 3731x 36x 66x 66x 66x 66x 66x 66x 66x 66x 66x 66x 47x 47x 23x 23x 66x 19x 19x 19x 19x 19x 66x 36x 36x 3669x 3669x 3728x 3524x 3731x 145x 145x 145x 145x 145x 145x 3524x 3524x 3524x 68x 68x 84x 84x 68x 68x 68x 3456x 3516x 3952x 4000x 4000x 4000x 3952x 3456x 2x 2x 69x 69x 14x 14x 14x 2x 2x 14x 69x 2x 2x 2x 2x 2x 20x 18x 20x 18x 18x 20x 18x 18x 20x 20x 20x   20x 20x 18x 18x 18x 18x 18x 18x 18x 18x 2x 2x 2x 2x 2x 2281x 2281x 2281x 2281x 22x 22x 2x 2x 1395x 1395x 1072x 1395x 1002x 1002x 1395x 1002x 1002x 1395x 1395x 1395x 1395x 201x 1395x 1395x 991x 1380x 1103x 1103x 1103x 1103x 1103x 1103x 410x 410x 404x 404x 286x 1103x 1103x 991x 1395x 190x 190x 190x 286x 264x 264x 264x 286x 286x 286x 286x 286x 286x 286x 286x 286x 286x 286x 286x 286x 286x 286x 45x 286x 37x 37x 37x 37x 286x 227x 227x 286x 190x 2x 2x 10x 10x 10x 10x 10x 10x 2x 2x 35x 17x 17x 18x 18x 32x 8x 35x 12x 12x 12x 12x 12x 12x 6x 35x 6x 6x 6x 6x 6x 6x 35x 2x 2x 2x 2x 2x 2x 2x 711x 711x 711x 711x 711x 326x 326x 326x 326x 5x 326x 1x 1x 326x 710x 710x 710x 710x 710x 2x 2x 2x 2x 2x 2x 3296x 3296x 3296x 3296x 3296x 3296x 3296x 3296x 3296x 3296x 3296x 3296x 3296x 2x 2x 2x 2x 2x 2x 2x 7681x 7681x 7681x 7681x 7681x 7276x 7276x 7681x 5910x 7663x 186x 186x 5724x 5724x 7640x 7681x 7681x 4060x 7681x 7681x 2x 2x 2x 2x 186829x 186829x 17545x 17545x 17545x 17545x 21116x 21116x 215x 215x 215x 215x 215x 215x 215x 21116x 6371x 6371x 21116x 17545x 17545x 51x 51x 51x 51x 186829x 169284x 169284x 143x 143x 143x 164x 164x 164x 164x 164x 164x 164x 164x 143x 4x 4x 4x 4x 143x 169284x 2x 2x 5184x 5104x 5104x 5104x 5104x 5176x 3284x 3284x 1892x 1892x 1892x 1892x 5176x 332x 332x 1560x 5176x 5104x 5104x 5184x 415x 415x 375x 375x 415x 415x 415x 415x 415x 415x 359x 277x 277x 359x 359x 415x 2x 2x 50x 2x 2x 260x 2x 2x 471x 471x 277x 157x 157x 157x 277x 471x 2x 2x 73x 14x 14x 14x 14x 73x 59x 59x 59x 59x 59x 2x 2x 6697x 2x 2x 43001x 43001x 35057x 43001x 10x 10x 10x 35047x 35047x 35047x 35047x 43001x 19074x 6031x 6031x 19074x 19074x 19074x 19074x 19074x 95x 95x 95x 44x 27x 27x 27x 95x 95x 19074x 36x 19074x 25x 19074x 24x 24x 19074x 2x 2x 4257x 4257x 4257x 711x 4257x 710x 710x 4257x 4257x 4257x 4257x 4257x 2671x 2671x 2671x 1378x 1378x 2671x 2671x 118x 118x 118x 118x 118x 118x 118x 118x 118x 2671x 4139x 4257x 140x 140x 140x 4257x 2932x 1023x 1023x 2932x 4139x 4139x 2x 2x 4563x 1489x 1489x 4563x 4563x 1078x 1078x 4563x 4563x 2x 2x 1045x 1045x 968x 968x 1045x 2x 2x 893x 893x 912x 912x 912x 912x 654x 912x 258x 190x 190x 258x 912x 616x 616x 912x 893x 893x 126x 126x 126x 126x 126x 126x 126x 126x 126x 845x 592x 592x 74x 74x 36x 36x 36x 36x 36x 36x 36x 36x 74x 592x 126x 126x 126x 126x 126x 126x 126x 706x 74x 58x 58x 58x 56x 56x 126x 845x 70x 70x 70x 126x 126x 126x 126x 126x 2x 2x 2x 2x 2x 8365x 8359x 8179x 8359x 8365x 8365x 8365x 8365x 8365x 8365x 8365x 58x 8365x 23x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 23x 8365x 8365x 8365x 8365x 8365x 8365x 8365x 8365x 44x 44x 44x 8365x 18x 18x 18x 8365x 8365x 2x 2x 140x 140x 140x 66x 24x 4x 4x 4x 4x 24x 66x 136x 140x 190x 190x 190x 190x 190x 184x 190x 6x 6x 6x 6x 6x 190x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 190x 110x 2x 2x 2x 2x 2x 8429x 8429x 8429x 6198x 184x 184x 6198x 8429x 8429x 8429x 8429x 2x 2x 2x 2x 64x 64x     64x 64x 64x 2x 2x 2x 2x 2x 64x 64x 64x 64x 64x 2x 2x 2x 2x 3812x 3812x 3812x 3812x 3812x 3812x 3812x 305x 256x 256x 256x 256x 305x 3812x 3812x 3812x 3812x 3812x 305x 256x 400x 339x 339x 400x 256x 305x 3812x 3812x 3812x 1x 1x 1x 3811x 3811x 3811x 3811x 3811x 3811x 3811x 3811x 3811x 3811x 3811x 3811x 340x 303x 339x 464x 464x 37x 37x 413x 303x 303x 3811x 3811x 3812x 303x 303x 3811x 3811x 3811x  
import is_reference from 'is-reference';
import { walk } from 'zimmerframe';
import * as e from '../../errors.js';
import * as w from '../../warnings.js';
import {
	extract_identifiers,
	extract_all_identifiers_from_expression,
	extract_paths,
	is_event_attribute,
	is_text_attribute,
	object
} from '../../utils/ast.js';
import * as b from '../../utils/builders.js';
import { MathMLElements, ReservedKeywords, Runes, SVGElements } from '../constants.js';
import { Scope, ScopeRoot, create_scopes, get_rune, set_scope } from '../scope.js';
import { merge } from '../visitors.js';
import { validation_legacy, validation_runes, validation_runes_js } from './validation.js';
import check_graph_for_cycles from './utils/check_graph_for_cycles.js';
import { regex_starts_with_newline } from '../patterns.js';
import { create_attribute, is_element_node } from '../nodes.js';
import { DelegatedEvents, namespace_mathml, namespace_svg } from '../../../constants.js';
import { should_proxy_or_freeze } from '../3-transform/client/utils.js';
import { analyze_css } from './css/css-analyze.js';
import { prune } from './css/css-prune.js';
import { hash } from './utils.js';
import { warn_unused } from './css/css-warn.js';
import { extract_svelte_ignore } from '../../utils/extract_svelte_ignore.js';
import { pop_ignore, push_ignore } from '../../state.js';
 
/**
 * @param {import('#compiler').Script | null} script
 * @param {ScopeRoot} root
 * @param {boolean} allow_reactive_declarations
 * @param {Scope | null} parent
 * @returns {import('../types.js').Js}
 */
function js(script, root, allow_reactive_declarations, parent) {
	/** @type {import('estree').Program} */
	const ast = script?.content ?? {
		type: 'Program',
		sourceType: 'module',
		start: -1,
		end: -1,
		body: []
	};
 
	const { scope, scopes } = create_scopes(ast, root, allow_reactive_declarations, parent);
 
	return { ast, scope, scopes };
}
 
/**
 * @param {string} filename
 */
function get_component_name(filename) {
	const parts = filename.split(/[/\\]/);
	const basename = /** @type {string} */ (parts.pop());
	const last_dir = /** @type {string} */ (parts.at(-1));
	let name = basename.replace('.svelte', '');
	if (name === 'index' && last_dir && last_dir !== 'src') {
		name = last_dir;
	}
	return name[0].toUpperCase() + name.slice(1);
}
 
/**
 * Checks if given event attribute can be delegated/hoisted and returns the corresponding info if so
 * @param {string} event_name
 * @param {import('estree').Expression | null} handler
 * @param {import('./types').Context} context
 * @returns {null | import('#compiler').DelegatedEvent}
 */
function get_delegated_event(event_name, handler, context) {
	// Handle delegated event handlers. Bail-out if not a delegated event.
	if (!handler || !DelegatedEvents.includes(event_name)) {
		return null;
	}
 
	// If we are not working with a RegularElement, then bail-out.
	const element = context.path.at(-1);
	if (element?.type !== 'RegularElement') {
		return null;
	}
 
	/** @type {import('#compiler').DelegatedEvent} */
	const non_hoistable = { type: 'non-hoistable' };
	/** @type {import('estree').FunctionExpression | import('estree').FunctionDeclaration | import('estree').ArrowFunctionExpression | null} */
	let target_function = null;
	let binding = null;
 
	if (element.metadata.has_spread) {
		// event attribute becomes part of the dynamic spread array
		return non_hoistable;
	}
 
	if (handler.type === 'ArrowFunctionExpression' || handler.type === 'FunctionExpression') {
		target_function = handler;
	} else if (handler.type === 'Identifier') {
		binding = context.state.scope.get(handler.name);
 
		if (context.state.analysis.module.scope.references.has(handler.name)) {
			// If a binding with the same name is referenced in the module scope (even if not declared there), bail-out
			return non_hoistable;
		}
 
		if (binding != null) {
			for (const { path } of binding.references) {
				const parent = path.at(-1);
				if (parent == null) {
					return non_hoistable;
				}
 
				/** @type {import('#compiler').RegularElement | null} */
				let element = null;
				/** @type {string | null} */
				let event_name = null;
				if (parent.type === 'OnDirective') {
					element = /** @type {import('#compiler').RegularElement} */ (path.at(-2));
					event_name = parent.name;
				} else if (
					parent.type === 'ExpressionTag' &&
					is_event_attribute(/** @type {import('#compiler').Attribute} */ (path.at(-2)))
				) {
					element = /** @type {import('#compiler').RegularElement} */ (path.at(-3));
					const attribute = /** @type {import('#compiler').Attribute} */ (path.at(-2));
					event_name = get_attribute_event_name(attribute.name);
				}
 
				if (element && event_name) {
					if (
						element.type !== 'RegularElement' ||
						determine_element_spread(element).metadata.has_spread ||
						!DelegatedEvents.includes(event_name)
					) {
						return non_hoistable;
					}
				} else if (parent.type !== 'FunctionDeclaration' && parent.type !== 'VariableDeclarator') {
					return non_hoistable;
				}
			}
		}
 
		// If the binding is exported, bail-out
		if (context.state.analysis.exports.find((node) => node.name === handler.name)) {
			return non_hoistable;
		}
 
		if (binding !== null && binding.initial !== null && !binding.mutated && !binding.is_called) {
			const binding_type = binding.initial.type;
 
			if (
				binding_type === 'ArrowFunctionExpression' ||
				binding_type === 'FunctionDeclaration' ||
				binding_type === 'FunctionExpression'
			) {
				target_function = binding.initial;
			}
		}
	}
 
	// If we can't find a function, bail-out
	if (target_function == null) {
		return non_hoistable;
	}
	// If the function is marked as non-hoistable, bail-out
	if (target_function.metadata.hoistable === 'impossible') {
		return non_hoistable;
	}
	// If the function has more than one arg, then bail-out
	if (target_function.params.length > 1) {
		return non_hoistable;
	}
 
	const visited_references = new Set();
	const scope = target_function.metadata.scope;
	for (const [reference] of scope.references) {
		// Bail-out if the arguments keyword is used
		if (reference === 'arguments') {
			return non_hoistable;
		}
		const binding = scope.get(reference);
		const local_binding = context.state.scope.get(reference);
 
		// If we are referencing a binding that is shadowed in another scope then bail out.
		if (local_binding !== null && binding !== null && local_binding.node !== binding.node) {
			return non_hoistable;
		}
 
		// If we have multiple references to the same store using $ prefix, bail out.
		if (
			binding !== null &&
			binding.kind === 'store_sub' &&
			visited_references.has(reference.slice(1))
		) {
			return non_hoistable;
		}
 
		// If we reference the index within an each block, then bail-out.
		if (binding !== null && binding.initial?.type === 'EachBlock') {
			return non_hoistable;
		}
 
		if (
			binding !== null &&
			// Bail-out if the the binding is a rest param
			(binding.declaration_kind === 'rest_param' ||
				// Bail-out if we reference anything from the EachBlock (for now) that mutates in non-runes mode,
				(((!context.state.analysis.runes && binding.kind === 'each') ||
					// or any normal not reactive bindings that are mutated.
					binding.kind === 'normal' ||
					// or any reactive imports (those are rewritten) (can only happen in legacy mode)
					binding.kind === 'legacy_reactive_import') &&
					binding.mutated))
		) {
			return non_hoistable;
		}
		visited_references.add(reference);
	}
 
	return { type: 'hoistable', function: target_function };
}
 
/**
 * @param {import('estree').Program} ast
 * @param {import('#compiler').ValidatedModuleCompileOptions} options
 * @returns {import('../types.js').Analysis}
 */
export function analyze_module(ast, options) {
	const { scope, scopes } = create_scopes(ast, new ScopeRoot(), false, null);
 
	for (const [name, references] of scope.references) {
		if (name[0] !== '$' || ReservedKeywords.includes(name)) continue;
		if (name === '$' || name[1] === '$') {
			e.global_reference_invalid(references[0].node, name);
		}
	}
 
	walk(
		/** @type {import('estree').Node} */ (ast),
		{ scope, analysis: { runes: true } },
		// @ts-expect-error TODO clean this mess up
		merge(set_scope(scopes), validation_runes_js, runes_scope_js_tweaker)
	);
 
	return {
		module: { ast, scope, scopes },
		name: options.filename || 'module',
		accessors: false,
		runes: true,
		immutable: true
	};
}
 
/**
 * @param {import('#compiler').Root} root
 * @param {string} source
 * @param {import('#compiler').ValidatedCompileOptions} options
 * @returns {import('../types.js').ComponentAnalysis}
 */
export function analyze_component(root, source, options) {
	const scope_root = new ScopeRoot();
 
	const module = js(root.module, scope_root, false, null);
	const instance = js(root.instance, scope_root, true, module.scope);
 
	const { scope, scopes } = create_scopes(root.fragment, scope_root, false, instance.scope);
 
	/** @type {import('../types.js').Template} */
	const template = { ast: root.fragment, scope, scopes };
 
	// create synthetic bindings for store subscriptions
	for (const [name, references] of module.scope.references) {
		if (name[0] !== '$' || ReservedKeywords.includes(name)) continue;
		if (name === '$' || name[1] === '$') {
			e.global_reference_invalid(references[0].node, name);
		}
 
		const store_name = name.slice(1);
		const declaration = instance.scope.get(store_name);
 
		// If we're not in legacy mode through the compiler option, assume the user
		// is referencing a rune and not a global store.
		if (
			options.runes === false ||
			!Runes.includes(/** @type {any} */ (name)) ||
			(declaration !== null &&
				// const state = $state(0) is valid
				get_rune(declaration.initial, instance.scope) === null &&
				// allow `import { derived } from 'svelte/store'` in the same file as `const x = $derived(..)` because one is not a subscription to the other
				!(
					name === '$derived' &&
					declaration.initial?.type === 'ImportDeclaration' &&
					declaration.initial.source.value === 'svelte/store'
				))
		) {
			let is_nested_store_subscription_node = undefined;
			search: for (const reference of references) {
				for (let i = reference.path.length - 1; i >= 0; i--) {
					const scope =
						scopes.get(reference.path[i]) ||
						module.scopes.get(reference.path[i]) ||
						instance.scopes.get(reference.path[i]);
					if (scope) {
						const owner = scope?.owner(store_name);
						if (!!owner && owner !== module.scope && owner !== instance.scope) {
							is_nested_store_subscription_node = reference.node;
							break search;
						}
						break;
					}
				}
			}
 
			if (is_nested_store_subscription_node) {
				e.store_invalid_scoped_subscription(is_nested_store_subscription_node);
			}
 
			if (options.runes !== false) {
				if (declaration === null && /[a-z]/.test(store_name[0])) {
					e.global_reference_invalid(references[0].node, name);
				} else if (declaration !== null && Runes.includes(/** @type {any} */ (name))) {
					for (const { node, path } of references) {
						if (path.at(-1)?.type === 'CallExpression') {
							w.store_rune_conflict(node, store_name);
						}
					}
				}
			}
 
			if (module.ast) {
				for (const { node, path } of references) {
					// if the reference is inside context="module", error. this is a bit hacky but it works
					if (
						/** @type {number} */ (node.start) > /** @type {number} */ (module.ast.start) &&
						/** @type {number} */ (node.end) < /** @type {number} */ (module.ast.end) &&
						// const state = $state(0) is valid
						get_rune(/** @type {import('estree').Node} */ (path.at(-1)), module.scope) === null
					) {
						e.store_invalid_subscription(node);
					}
				}
			}
 
			const binding = instance.scope.declare(b.id(name), 'store_sub', 'synthetic');
			binding.references = references;
			instance.scope.references.set(name, references);
			module.scope.references.delete(name);
		}
	}
 
	const component_name = get_component_name(options.filename ?? 'Component');
 
	const runes =
		options.runes ??
		Array.from(module.scope.references).some(([name]) => Runes.includes(/** @type {any} */ (name)));
 
	// TODO remove all the ?? stuff, we don't need it now that we're validating the config
	/** @type {import('../types.js').ComponentAnalysis} */
	const analysis = {
		name: module.scope.generate(options.name ?? component_name),
		root: scope_root,
		module,
		instance,
		template,
		elements: [],
		runes,
		immutable: runes || options.immutable,
		exports: [],
		uses_props: false,
		uses_rest_props: false,
		uses_slots: false,
		uses_component_bindings: false,
		uses_render_tags: false,
		needs_context: false,
		needs_props: false,
		event_directive_node: null,
		uses_event_attributes: false,
		custom_element: options.customElementOptions ?? options.customElement,
		inject_styles: options.css === 'injected' || options.customElement,
		accessors: options.customElement
			? true
			: (runes ? false : !!options.accessors) ||
				// because $set method needs accessors
				!!options.legacy?.componentApi,
		reactive_statements: new Map(),
		binding_groups: new Map(),
		slot_names: new Map(),
		top_level_snippets: [],
		css: {
			ast: root.css,
			hash: root.css
				? options.cssHash({
						css: root.css.content.styles,
						filename: options.filename ?? '<unknown>',
						name: component_name,
						hash
					})
				: '',
			keyframes: []
		},
		source
	};
 
	if (root.options) {
		for (const attribute of root.options.attributes) {
			if (attribute.name === 'accessors') {
				w.options_deprecated_accessors(attribute);
			}
 
			if (attribute.name === 'customElement' && !options.customElement) {
				w.options_missing_custom_element(attribute);
			}
 
			if (attribute.name === 'immutable') {
				w.options_deprecated_immutable(attribute);
			}
		}
	}
 
	if (analysis.runes) {
		const props_refs = module.scope.references.get('$$props');
		if (props_refs) {
			e.legacy_props_invalid(props_refs[0].node);
		}
 
		const rest_props_refs = module.scope.references.get('$$restProps');
		if (rest_props_refs) {
			e.legacy_rest_props_invalid(rest_props_refs[0].node);
		}
 
		for (const { ast, scope, scopes } of [module, instance, template]) {
			/** @type {import('./types').AnalysisState} */
			const state = {
				scope,
				analysis,
				options,
				ast_type: ast === instance.ast ? 'instance' : ast === template.ast ? 'template' : 'module',
				parent_element: null,
				has_props_rune: false,
				component_slots: new Set(),
				expression: null,
				private_derived_state: [],
				function_depth: scope.function_depth
			};
 
			walk(
				/** @type {import('#compiler').SvelteNode} */ (ast),
				state,
				merge(set_scope(scopes), validation_runes, runes_scope_tweaker, common_visitors)
			);
		}
 
		// warn on any nonstate declarations that are a) reassigned and b) referenced in the template
		for (const scope of [module.scope, instance.scope]) {
			outer: for (const [name, binding] of scope.declarations) {
				if (binding.kind === 'normal' && binding.reassigned) {
					inner: for (const { path } of binding.references) {
						if (path[0].type !== 'Fragment') continue;
						for (let i = 1; i < path.length; i += 1) {
							const type = path[i].type;
							if (
								type === 'FunctionDeclaration' ||
								type === 'FunctionExpression' ||
								type === 'ArrowFunctionExpression'
							) {
								continue inner;
							}
							// bind:this doesn't need to be a state reference if it will never change
							if (
								type === 'BindDirective' &&
								/** @type {import('#compiler').BindDirective} */ (path[i]).name === 'this'
							) {
								for (let j = i - 1; j >= 0; j -= 1) {
									const type = path[j].type;
									if (
										type === 'IfBlock' ||
										type === 'EachBlock' ||
										type === 'AwaitBlock' ||
										type === 'KeyBlock'
									) {
										w.non_reactive_update(binding.node, name);
										continue outer;
									}
								}
								continue inner;
							}
						}
 
						w.non_reactive_update(binding.node, name);
						continue outer;
					}
				}
			}
		}
	} else {
		instance.scope.declare(b.id('$$props'), 'rest_prop', 'synthetic');
		instance.scope.declare(b.id('$$restProps'), 'rest_prop', 'synthetic');
 
		for (const { ast, scope, scopes } of [module, instance, template]) {
			/** @type {import('./types').LegacyAnalysisState} */
			const state = {
				scope,
				analysis,
				options,
				parent_element: null,
				has_props_rune: false,
				ast_type: ast === instance.ast ? 'instance' : ast === template.ast ? 'template' : 'module',
				instance_scope: instance.scope,
				reactive_statement: null,
				reactive_statements: analysis.reactive_statements,
				component_slots: new Set(),
				expression: null,
				private_derived_state: [],
				function_depth: scope.function_depth
			};
 
			walk(
				/** @type {import('#compiler').SvelteNode} */ (ast),
				state,
				// @ts-expect-error TODO
				merge(set_scope(scopes), validation_legacy, legacy_scope_tweaker, common_visitors)
			);
		}
 
		for (const [name, binding] of instance.scope.declarations) {
			if (
				(binding.kind === 'prop' || binding.kind === 'bindable_prop') &&
				binding.node.name !== '$$props'
			) {
				const references = binding.references.filter(
					(r) => r.node !== binding.node && r.path.at(-1)?.type !== 'ExportSpecifier'
				);
				if (!references.length && !instance.scope.declarations.has(`$${name}`)) {
					w.export_let_unused(binding.node, name);
				}
			}
		}
 
		analysis.reactive_statements = order_reactive_statements(analysis.reactive_statements);
	}
 
	if (analysis.event_directive_node && analysis.uses_event_attributes) {
		e.mixed_event_handler_syntaxes(
			analysis.event_directive_node,
			analysis.event_directive_node.name
		);
	}
 
	if (analysis.uses_render_tags && (analysis.uses_slots || analysis.slot_names.size > 0)) {
		e.slot_snippet_conflict(analysis.slot_names.values().next().value);
	}
 
	if (analysis.css.ast) {
		analyze_css(analysis.css.ast, analysis);
 
		// mark nodes as scoped/unused/empty etc
		for (const element of analysis.elements) {
			prune(analysis.css.ast, element);
		}
 
		const { comment } = analysis.css.ast.content;
		const should_ignore_unused =
			comment &&
			extract_svelte_ignore(comment.start, comment.data, analysis.runes).includes(
				'css_unused_selector'
			);
 
		if (!should_ignore_unused) {
			warn_unused(analysis.css.ast);
		}
 
		outer: for (const element of analysis.elements) {
			if (element.metadata.scoped) {
				// Dynamic elements in dom mode always use spread for attributes and therefore shouldn't have a class attribute added to them
				// TODO this happens during the analysis phase, which shouldn't know anything about client vs server
				if (element.type === 'SvelteElement' && options.generate === 'client') continue;
 
				/** @type {import('#compiler').Attribute | undefined} */
				let class_attribute = undefined;
 
				for (const attribute of element.attributes) {
					if (attribute.type === 'SpreadAttribute') {
						// The spread method appends the hash to the end of the class attribute on its own
						continue outer;
					}
 
					if (attribute.type !== 'Attribute') continue;
					if (attribute.name.toLowerCase() !== 'class') continue;
 
					class_attribute = attribute;
				}
 
				if (class_attribute && class_attribute.value !== true) {
					const chunks = class_attribute.value;
 
					if (chunks.length === 1 && chunks[0].type === 'Text') {
						chunks[0].data += ` ${analysis.css.hash}`;
					} else {
						chunks.push({
							type: 'Text',
							data: ` ${analysis.css.hash}`,
							raw: ` ${analysis.css.hash}`,
							start: -1,
							end: -1,
							parent: null
						});
					}
				} else {
					element.attributes.push(
						create_attribute('class', -1, -1, [
							{
								type: 'Text',
								data: analysis.css.hash,
								raw: analysis.css.hash,
								parent: null,
								start: -1,
								end: -1
							}
						])
					);
				}
			}
		}
	}
 
	// TODO
	// analysis.stylesheet.warn_on_unused_selectors(analysis);
 
	return analysis;
}
 
/** @type {import('./types').Visitors<import('./types').LegacyAnalysisState>} */
const legacy_scope_tweaker = {
	LabeledStatement(node, { next, path, state }) {
		if (
			state.ast_type !== 'instance' ||
			node.label.name !== '$' ||
			/** @type {import('#compiler').SvelteNode} */ (path.at(-1)).type !== 'Program'
		) {
			return next();
		}
 
		// Find all dependencies of this `$: {...}` statement
		/** @type {import('../types.js').ReactiveStatement} */
		const reactive_statement = {
			assignments: new Set(),
			dependencies: []
		};
 
		next({ ...state, reactive_statement, function_depth: state.scope.function_depth + 1 });
 
		// Every referenced binding becomes a dependency, unless it's on
		// the left-hand side of an `=` assignment
		for (const [name, nodes] of state.scope.references) {
			const binding = state.scope.get(name);
			if (binding === null) continue;
 
			for (const { node, path } of nodes) {
				/** @type {import('estree').Expression} */
				let left = node;
 
				let i = path.length - 1;
				let parent = /** @type {import('estree').Expression} */ (path.at(i));
				while (parent.type === 'MemberExpression') {
					left = parent;
					parent = /** @type {import('estree').Expression} */ (path.at(--i));
				}
 
				if (
					parent.type === 'AssignmentExpression' &&
					parent.operator === '=' &&
					parent.left === left
				) {
					continue;
				}
 
				reactive_statement.dependencies.push(binding);
				break;
			}
		}
 
		state.reactive_statements.set(node, reactive_statement);
 
		// Ideally this would be in the validation file, but that isn't possible because this visitor
		// calls "next" before setting the reactive statements.
		if (
			reactive_statement.dependencies.length &&
			reactive_statement.dependencies.every(
				(d) => d.scope === state.analysis.module.scope && d.declaration_kind !== 'const'
			)
		) {
			w.reactive_declaration_module_script(node);
		}
 
		if (
			node.body.type === 'ExpressionStatement' &&
			node.body.expression.type === 'AssignmentExpression'
		) {
			let ids = extract_identifiers(node.body.expression.left);
			if (node.body.expression.left.type === 'MemberExpression') {
				const id = object(node.body.expression.left);
				if (id !== null) {
					ids = [id];
				}
			}
 
			for (const id of ids) {
				const binding = state.scope.get(id.name);
				if (binding?.kind === 'legacy_reactive') {
					// TODO does this include `let double; $: double = x * 2`?
					binding.legacy_dependencies = Array.from(reactive_statement.dependencies);
				}
			}
		}
	},
	AssignmentExpression(node, { state, next }) {
		if (state.reactive_statement) {
			const id = node.left.type === 'MemberExpression' ? object(node.left) : node.left;
			if (id !== null) {
				for (const id of extract_identifiers(node.left)) {
					const binding = state.scope.get(id.name);
 
					if (binding) {
						state.reactive_statement.assignments.add(binding);
					}
				}
			}
		}
 
		next();
	},
	UpdateExpression(node, { state, next }) {
		if (state.reactive_statement) {
			const id = node.argument.type === 'MemberExpression' ? object(node.argument) : node.argument;
			if (id?.type === 'Identifier') {
				const binding = state.scope.get(id.name);
 
				if (binding) {
					state.reactive_statement.assignments.add(binding);
				}
			}
		}
 
		next();
	},
	Identifier(node, { state, path }) {
		const parent = /** @type {import('estree').Node} */ (path.at(-1));
		if (is_reference(node, parent)) {
			if (node.name === '$$props') {
				state.analysis.uses_props = true;
			}
 
			if (node.name === '$$restProps') {
				state.analysis.uses_rest_props = true;
			}
 
			if (node.name === '$$slots') {
				state.analysis.uses_slots = true;
			}
 
			let binding = state.scope.get(node.name);
 
			if (binding?.kind === 'store_sub') {
				// get the underlying store to mark it as reactive in case it's mutated
				binding = state.scope.get(node.name.slice(1));
			}
 
			if (
				binding !== null &&
				binding.kind === 'normal' &&
				((binding.scope === state.instance_scope && binding.declaration_kind !== 'function') ||
					binding.declaration_kind === 'import')
			) {
				if (binding.declaration_kind === 'import') {
					if (
						binding.mutated &&
						// TODO could be more fine-grained - not every mention in the template implies a state binding
						(state.reactive_statement || state.ast_type === 'template') &&
						parent.type === 'MemberExpression'
					) {
						binding.kind = 'legacy_reactive_import';
					}
				} else if (
					binding.mutated &&
					// TODO could be more fine-grained - not every mention in the template implies a state binding
					(state.reactive_statement || state.ast_type === 'template')
				) {
					binding.kind = 'state';
				} else if (
					state.reactive_statement &&
					parent.type === 'AssignmentExpression' &&
					parent.left === binding.node
				) {
					binding.kind = 'derived';
				} else {
					let idx = -1;
					let ancestor = path.at(idx);
					while (ancestor) {
						if (ancestor.type === 'EachBlock') {
							// Ensures that the array is reactive when only its entries are mutated
							// TODO: this doesn't seem correct. We should be checking at the points where
							// the identifier (the each expression) is used in a way that makes it reactive.
							// This just forces the collection identifier to always be reactive even if it's
							// not.
							if (ancestor.expression === (idx === -1 ? node : path.at(idx + 1))) {
								binding.kind = 'state';
								break;
							}
						}
						ancestor = path.at(--idx);
					}
				}
			}
		}
	},
	ExportNamedDeclaration(node, { next, state }) {
		if (state.ast_type !== 'instance') {
			return next();
		}
 
		state.analysis.needs_props = true;
 
		if (!node.declaration) {
			for (const specifier of node.specifiers) {
				const binding = /** @type {import('#compiler').Binding} */ (
					state.scope.get(specifier.local.name)
				);
				if (
					binding !== null &&
					(binding.kind === 'state' ||
						binding.kind === 'frozen_state' ||
						(binding.kind === 'normal' &&
							(binding.declaration_kind === 'let' || binding.declaration_kind === 'var')))
				) {
					binding.kind = 'bindable_prop';
					if (specifier.exported.name !== specifier.local.name) {
						binding.prop_alias = specifier.exported.name;
					}
				} else {
					state.analysis.exports.push({
						name: specifier.local.name,
						alias: specifier.exported.name
					});
				}
			}
			return next();
		}
 
		if (
			node.declaration.type === 'FunctionDeclaration' ||
			node.declaration.type === 'ClassDeclaration'
		) {
			state.analysis.exports.push({
				name: /** @type {import('estree').Identifier} */ (node.declaration.id).name,
				alias: null
			});
			return next();
		}
 
		if (node.declaration.type === 'VariableDeclaration') {
			if (node.declaration.kind === 'const') {
				for (const declarator of node.declaration.declarations) {
					for (const node of extract_identifiers(declarator.id)) {
						state.analysis.exports.push({ name: node.name, alias: null });
					}
				}
				return next();
			}
 
			for (const declarator of node.declaration.declarations) {
				for (const id of extract_identifiers(declarator.id)) {
					const binding = /** @type {import('#compiler').Binding} */ (state.scope.get(id.name));
					binding.kind = 'bindable_prop';
				}
			}
		}
	},
	StyleDirective(node, { state }) {
		// the case for node.value different from true is already covered by the Identifier visitor
		if (node.value === true) {
			// get the binding for node.name and change the binding to state
			let binding = state.scope.get(node.name);
			if (binding?.mutated && binding.kind === 'normal') {
				binding.kind = 'state';
			}
		}
	}
};
 
/** @type {import('zimmerframe').Visitors<import('#compiler').SvelteNode, { scope: Scope, analysis: { runes: true } }>} */
const runes_scope_js_tweaker = {
	VariableDeclarator(node, { state }) {
		if (node.init?.type !== 'CallExpression') return;
		const rune = get_rune(node.init, state.scope);
		if (rune === null) return;
 
		const callee = node.init.callee;
		if (callee.type !== 'Identifier' && callee.type !== 'MemberExpression') return;
 
		if (
			rune !== '$state' &&
			rune !== '$state.frozen' &&
			rune !== '$derived' &&
			rune !== '$derived.by'
		)
			return;
 
		for (const path of extract_paths(node.id)) {
			// @ts-ignore this fails in CI for some insane reason
			const binding = /** @type {import('#compiler').Binding} */ (state.scope.get(path.node.name));
			binding.kind =
				rune === '$state' ? 'state' : rune === '$state.frozen' ? 'frozen_state' : 'derived';
		}
	}
};
 
/** @type {import('./types').Visitors} */
const runes_scope_tweaker = {
	CallExpression(node, { state, next }) {
		const rune = get_rune(node, state.scope);
 
		// `$inspect(foo)` should not trigger the `static-state-reference` warning
		if (rune === '$inspect') {
			next({ ...state, function_depth: state.function_depth + 1 });
		}
	},
	VariableDeclarator(node, { state }) {
		const init = node.init;
		if (!init || init.type !== 'CallExpression') return;
		const rune = get_rune(init, state.scope);
		if (rune === null) return;
 
		const callee = init.callee;
		if (callee.type !== 'Identifier' && callee.type !== 'MemberExpression') return;
 
		if (
			rune !== '$state' &&
			rune !== '$state.frozen' &&
			rune !== '$derived' &&
			rune !== '$derived.by' &&
			rune !== '$props'
		)
			return;
 
		for (const path of extract_paths(node.id)) {
			// @ts-ignore this fails in CI for some insane reason
			const binding = /** @type {import('#compiler').Binding} */ (state.scope.get(path.node.name));
			binding.kind =
				rune === '$state'
					? 'state'
					: rune === '$state.frozen'
						? 'frozen_state'
						: rune === '$derived' || rune === '$derived.by'
							? 'derived'
							: path.is_rest
								? 'rest_prop'
								: 'prop';
		}
 
		if (rune === '$props') {
			state.analysis.needs_props = true;
 
			for (const property of /** @type {import('estree').ObjectPattern} */ (node.id).properties) {
				if (property.type !== 'Property') continue;
 
				const name =
					property.value.type === 'AssignmentPattern'
						? /** @type {import('estree').Identifier} */ (property.value.left).name
						: /** @type {import('estree').Identifier} */ (property.value).name;
				const alias =
					property.key.type === 'Identifier'
						? property.key.name
						: String(/** @type {import('estree').Literal} */ (property.key).value);
				let initial = property.value.type === 'AssignmentPattern' ? property.value.right : null;
 
				const binding = /** @type {import('#compiler').Binding} */ (state.scope.get(name));
				binding.prop_alias = alias;
 
				// rewire initial from $props() to the actual initial value, stripping $bindable() if necessary
				if (
					initial?.type === 'CallExpression' &&
					initial.callee.type === 'Identifier' &&
					initial.callee.name === '$bindable'
				) {
					binding.initial = /** @type {import('estree').Expression | null} */ (
						initial.arguments[0] ?? null
					);
					binding.kind = 'bindable_prop';
				} else {
					binding.initial = initial;
				}
			}
		}
	},
	ExportSpecifier(node, { state }) {
		if (state.ast_type !== 'instance') return;
 
		state.analysis.exports.push({
			name: node.local.name,
			alias: node.exported.name
		});
	},
	ExportNamedDeclaration(node, { next, state }) {
		if (!node.declaration || state.ast_type !== 'instance') {
			return next();
		}
 
		if (
			node.declaration.type === 'FunctionDeclaration' ||
			node.declaration.type === 'ClassDeclaration'
		) {
			state.analysis.exports.push({
				name: /** @type {import('estree').Identifier} */ (node.declaration.id).name,
				alias: null
			});
			return next();
		}
 
		if (node.declaration.type === 'VariableDeclaration' && node.declaration.kind === 'const') {
			for (const declarator of node.declaration.declarations) {
				for (const node of extract_identifiers(declarator.id)) {
					state.analysis.exports.push({ name: node.name, alias: null });
				}
			}
		}
	}
};
 
/**
 * @param {import('estree').CallExpression} node
 * @param {import('./types').Context} context
 * @returns {boolean}
 */
function is_known_safe_call(node, context) {
	const callee = node.callee;
 
	// String / Number / BigInt / Boolean casting calls
	if (callee.type === 'Identifier') {
		const name = callee.name;
		const binding = context.state.scope.get(name);
		if (
			binding === null &&
			(name === 'BigInt' || name === 'String' || name === 'Number' || name === 'Boolean')
		) {
			return true;
		}
	}
 
	// TODO add more cases
 
	return false;
}
 
/**
 * @param {import('estree').ArrowFunctionExpression | import('estree').FunctionExpression | import('estree').FunctionDeclaration} node
 * @param {import('./types').Context} context
 */
const function_visitor = (node, context) => {
	// TODO retire this in favour of a more general solution based on bindings
	node.metadata = {
		// module context -> already hoisted
		hoistable: context.state.ast_type === 'module' ? 'impossible' : false,
		hoistable_params: [],
		scope: context.state.scope
	};
 
	context.next({
		...context.state,
		function_depth: context.state.function_depth + 1
	});
};
 
/**
 * A 'safe' identifier means that the `foo` in `foo.bar` or `foo()` will not
 * call functions that require component context to exist
 * @param {import('estree').Expression | import('estree').Super} expression
 * @param {Scope} scope
 */
function is_safe_identifier(expression, scope) {
	let node = expression;
	while (node.type === 'MemberExpression') node = node.object;
 
	if (node.type !== 'Identifier') return false;
 
	const binding = scope.get(node.name);
	if (!binding) return true;
 
	if (binding.kind === 'store_sub') {
		return is_safe_identifier({ name: node.name.slice(1), type: 'Identifier' }, scope);
	}
 
	return (
		binding.declaration_kind !== 'import' &&
		binding.kind !== 'prop' &&
		binding.kind !== 'bindable_prop' &&
		binding.kind !== 'rest_prop'
	);
}
 
/** @type {import('./types').Visitors} */
const common_visitors = {
	_(node, { state, next, path }) {
		const parent = path.at(-1);
		if (parent?.type === 'Fragment' && node.type !== 'Comment' && node.type !== 'Text') {
			const idx = parent.nodes.indexOf(/** @type {any} */ (node));
			/** @type {string[]} */
			const ignores = [];
			for (let i = idx - 1; i >= 0; i--) {
				const prev = parent.nodes[i];
				if (prev.type === 'Comment') {
					ignores.push(
						...extract_svelte_ignore(
							prev.start + 4 /* '<!--'.length */,
							prev.data,
							state.analysis.runes
						)
					);
				} else if (prev.type !== 'Text') {
					break;
				}
			}
 
			if (ignores.length > 0) {
				push_ignore(ignores);
				next();
				pop_ignore();
			}
		} else {
			const comments = /** @type {any} */ (node).leadingComments;
			if (comments) {
				/** @type {string[]} */
				const ignores = [];
				for (const comment of comments) {
					ignores.push(
						...extract_svelte_ignore(
							comment.start + 2 /* '//'.length */,
							comment.value,
							state.analysis.runes
						)
					);
				}
				if (ignores.length > 0) {
					push_ignore(ignores);
					next();
					pop_ignore();
				}
			}
		}
	},
	Attribute(node, context) {
		if (node.value === true) return;
 
		context.next();
 
		node.metadata.dynamic = node.value.some((chunk) => {
			if (chunk.type !== 'ExpressionTag') {
				return false;
			}
 
			if (
				chunk.expression.type === 'FunctionExpression' ||
				chunk.expression.type === 'ArrowFunctionExpression'
			) {
				return false;
			}
 
			return chunk.metadata.dynamic || chunk.metadata.contains_call_expression;
		});
 
		if (is_event_attribute(node)) {
			const parent = context.path.at(-1);
			if (parent?.type === 'RegularElement' || parent?.type === 'SvelteElement') {
				context.state.analysis.uses_event_attributes = true;
			}
 
			const expression = node.value[0].expression;
 
			const delegated_event = get_delegated_event(node.name.slice(2), expression, context);
 
			if (delegated_event !== null) {
				if (delegated_event.type === 'hoistable') {
					delegated_event.function.metadata.hoistable = true;
				}
				node.metadata.delegated = delegated_event;
			}
		}
	},
	ClassDirective(node, context) {
		context.next({ ...context.state, expression: node });
	},
	SpreadAttribute(node, context) {
		context.next({ ...context.state, expression: node });
	},
	SlotElement(node, context) {
		let name = 'default';
		for (const attr of node.attributes) {
			if (attr.type === 'Attribute' && attr.name === 'name' && is_text_attribute(attr)) {
				name = attr.value[0].data;
				break;
			}
		}
		context.state.analysis.slot_names.set(name, node);
	},
	StyleDirective(node, context) {
		if (node.value === true) {
			const binding = context.state.scope.get(node.name);
			if (binding?.kind !== 'normal') {
				node.metadata.dynamic = true;
			}
		} else {
			context.next();
			node.metadata.dynamic = node.value.some(
				(node) => node.type === 'ExpressionTag' && node.metadata.dynamic
			);
		}
	},
	ExpressionTag(node, context) {
		context.next({ ...context.state, expression: node });
	},
	Identifier(node, context) {
		const parent = /** @type {import('estree').Node} */ (context.path.at(-1));
		if (!is_reference(node, parent)) return;
 
		if (node.name === '$$slots') {
			context.state.analysis.uses_slots = true;
			return;
		}
 
		const binding = context.state.scope.get(node.name);
 
		// if no binding, means some global variable
		if (binding && binding.kind !== 'normal') {
			if (context.state.expression) {
				context.state.expression.metadata.dynamic = true;
			}
 
			if (
				context.state.analysis.runes &&
				node !== binding.node &&
				context.state.function_depth === binding.scope.function_depth &&
				// If we have $state that can be proxied or frozen and isn't re-assigned, then that means
				// it's likely not using a primitive value and thus this warning isn't that helpful.
				((binding.kind === 'state' &&
					(binding.reassigned ||
						(binding.initial?.type === 'CallExpression' &&
							binding.initial.arguments.length === 1 &&
							binding.initial.arguments[0].type !== 'SpreadElement' &&
							!should_proxy_or_freeze(binding.initial.arguments[0], context.state.scope)))) ||
					binding.kind === 'frozen_state' ||
					binding.kind === 'derived') &&
				// We're only concerned with reads here
				(parent.type !== 'AssignmentExpression' || parent.left !== node) &&
				parent.type !== 'UpdateExpression'
			) {
				w.state_referenced_locally(node);
			}
		}
	},
	CallExpression(node, context) {
		const { expression } = context.state;
		if (
			(expression?.type === 'ExpressionTag' || expression?.type === 'SpreadAttribute') &&
			!is_known_safe_call(node, context)
		) {
			expression.metadata.contains_call_expression = true;
		}
 
		const callee = node.callee;
		const rune = get_rune(node, context.state.scope);
 
		if (callee.type === 'Identifier') {
			const binding = context.state.scope.get(callee.name);
 
			if (binding !== null) {
				binding.is_called = true;
			}
 
			if (rune === '$derived') {
				// special case — `$derived(foo)` is treated as `$derived(() => foo)`
				// for the purposes of identifying static state references
				context.next({
					...context.state,
					function_depth: context.state.function_depth + 1
				});
 
				return;
			}
		}
 
		if (rune === '$effect' || rune === '$effect.pre') {
			// `$effect` needs context because Svelte needs to know whether it should re-run
			// effects that invalidate themselves, and that's determined by whether we're in runes mode
			context.state.analysis.needs_context = true;
		} else if (rune === null) {
			if (!is_safe_identifier(callee, context.state.scope)) {
				context.state.analysis.needs_context = true;
			}
		}
 
		context.next();
	},
	MemberExpression(node, context) {
		if (context.state.expression) {
			context.state.expression.metadata.dynamic = true;
		}
 
		if (!is_safe_identifier(node, context.state.scope)) {
			context.state.analysis.needs_context = true;
		}
 
		context.next();
	},
	OnDirective(node, { state, path, next }) {
		const parent = path.at(-1);
		if (parent?.type === 'SvelteElement' || parent?.type === 'RegularElement') {
			state.analysis.event_directive_node ??= node;
		}
		next();
	},
	BindDirective(node, context) {
		let i = context.path.length;
		while (i--) {
			const parent = context.path[i];
			if (
				parent.type === 'Component' ||
				parent.type === 'SvelteComponent' ||
				parent.type === 'SvelteSelf'
			) {
				if (node.name !== 'this') {
					context.state.analysis.uses_component_bindings = true;
				}
				break;
			} else if (is_element_node(parent)) {
				break;
			}
		}
 
		if (node.name !== 'group') return;
 
		// Traverse the path upwards and find all EachBlocks who are (indirectly) contributing to bind:group,
		// i.e. one of their declarations is referenced in the binding. This allows group bindings to work
		// correctly when referencing a variable declared in an EachBlock by using the index of the each block
		// entries as keys.
		i = context.path.length;
		const each_blocks = [];
		const [keypath, expression_ids] = extract_all_identifiers_from_expression(node.expression);
		let ids = expression_ids;
		while (i--) {
			const parent = context.path[i];
			if (parent.type === 'EachBlock') {
				const references = ids.filter((id) => parent.metadata.declarations.has(id.name));
				if (references.length > 0) {
					parent.metadata.contains_group_binding = true;
					for (const binding of parent.metadata.references) {
						binding.mutated = true;
					}
					each_blocks.push(parent);
					ids = ids.filter((id) => !references.includes(id));
					ids.push(...extract_all_identifiers_from_expression(parent.expression)[1]);
				}
			}
		}
 
		// The identifiers that make up the binding expression form they key for the binding group.
		// If the same identifiers in the same order are used in another bind:group, they will be in the same group.
		// (there's an edge case where `bind:group={a[i]}` will be in a different group than `bind:group={a[j]}` even when i == j,
		//  but this is a limitation of the current static analysis we do; it also never worked in Svelte 4)
		const bindings = expression_ids.map((id) => context.state.scope.get(id.name));
		let group_name;
		outer: for (const [[key, b], group] of context.state.analysis.binding_groups) {
			if (b.length !== bindings.length || key !== keypath) continue;
			for (let i = 0; i < bindings.length; i++) {
				if (bindings[i] !== b[i]) continue outer;
			}
			group_name = group;
		}
 
		if (!group_name) {
			group_name = context.state.scope.root.unique('binding_group');
			context.state.analysis.binding_groups.set([keypath, bindings], group_name);
		}
 
		node.metadata = {
			binding_group_name: group_name,
			parent_each_blocks: each_blocks
		};
	},
	ArrowFunctionExpression: function_visitor,
	FunctionExpression: function_visitor,
	FunctionDeclaration: function_visitor,
	RegularElement(node, context) {
		if (context.state.options.namespace !== 'foreign') {
			if (SVGElements.includes(node.name)) node.metadata.svg = true;
			else if (MathMLElements.includes(node.name)) node.metadata.mathml = true;
		}
 
		determine_element_spread(node);
 
		// Special case: Move the children of <textarea> into a value attribute if they are dynamic
		if (
			context.state.options.namespace !== 'foreign' &&
			node.name === 'textarea' &&
			node.fragment.nodes.length > 0
		) {
			if (node.fragment.nodes.length > 1 || node.fragment.nodes[0].type !== 'Text') {
				const first = node.fragment.nodes[0];
				if (first.type === 'Text') {
					// The leading newline character needs to be stripped because of a qirk:
					// It is ignored by browsers if the tag and its contents are set through
					// innerHTML, but we're now setting it through the value property at which
					// point it is _not_ ignored, so we need to strip it ourselves.
					// see https://html.spec.whatwg.org/multipage/syntax.html#element-restrictions
					// see https://html.spec.whatwg.org/multipage/grouping-content.html#the-pre-element
					first.data = first.data.replace(regex_starts_with_newline, '');
					first.raw = first.raw.replace(regex_starts_with_newline, '');
				}
 
				node.attributes.push(
					create_attribute(
						'value',
						/** @type {import('#compiler').Text} */ (node.fragment.nodes.at(0)).start,
						/** @type {import('#compiler').Text} */ (node.fragment.nodes.at(-1)).end,
						// @ts-ignore
						node.fragment.nodes
					)
				);
 
				node.fragment.nodes = [];
			}
		}
 
		// Special case: single expression tag child of option element -> add "fake" attribute
		// to ensure that value types are the same (else for example numbers would be strings)
		if (
			context.state.options.namespace !== 'foreign' &&
			node.name === 'option' &&
			node.fragment.nodes?.length === 1 &&
			node.fragment.nodes[0].type === 'ExpressionTag' &&
			!node.attributes.some(
				(attribute) => attribute.type === 'Attribute' && attribute.name === 'value'
			)
		) {
			const child = node.fragment.nodes[0];
			node.attributes.push(create_attribute('value', child.start, child.end, [child]));
		}
 
		context.state.analysis.elements.push(node);
	},
	SvelteElement(node, context) {
		context.state.analysis.elements.push(node);
 
		for (const attribute of node.attributes) {
			if (attribute.type === 'Attribute') {
				if (attribute.name === 'xmlns' && is_text_attribute(attribute)) {
					node.metadata.svg = attribute.value[0].data === namespace_svg;
					node.metadata.mathml = attribute.value[0].data === namespace_mathml;
					return;
				}
			}
		}
 
		for (let i = context.path.length - 1; i >= 0; i--) {
			const ancestor = context.path[i];
			if (
				ancestor.type === 'Component' ||
				ancestor.type === 'SvelteComponent' ||
				ancestor.type === 'SvelteFragment' ||
				ancestor.type === 'SnippetBlock'
			) {
				// Inside a slot or a snippet -> this resets the namespace, so assume the component namespace
				node.metadata.svg = context.state.options.namespace === 'svg';
				node.metadata.mathml = context.state.options.namespace === 'mathml';
				return;
			}
			if (ancestor.type === 'SvelteElement' || ancestor.type === 'RegularElement') {
				node.metadata.svg =
					ancestor.type === 'RegularElement' && ancestor.name === 'foreignObject'
						? false
						: ancestor.metadata.svg;
				node.metadata.mathml =
					ancestor.type === 'RegularElement' && ancestor.name === 'foreignObject'
						? false
						: ancestor.metadata.mathml;
				return;
			}
		}
	}
};
 
/**
 * @param {import('#compiler').RegularElement} node
 */
function determine_element_spread(node) {
	let has_spread = false;
	for (const attribute of node.attributes) {
		if (!has_spread && attribute.type === 'SpreadAttribute') {
			has_spread = true;
		}
	}
	node.metadata.has_spread = has_spread;
 
	return node;
}
 
/**
 * @param {string} event_name
 */
function get_attribute_event_name(event_name) {
	if (is_capture_event(event_name)) {
		event_name = event_name.slice(0, -7);
	}
	event_name = event_name.slice(2);
	return event_name;
}
 
/**
 * @param {string} name
 * @returns boolean
 */
function is_capture_event(name) {
	return (
		name.endsWith('capture') && name !== 'ongotpointercapture' && name !== 'onlostpointercapture'
	);
}
 
/**
 * @param {Map<import('estree').LabeledStatement, import('../types.js').ReactiveStatement>} unsorted_reactive_declarations
 */
function order_reactive_statements(unsorted_reactive_declarations) {
	/** @typedef {[import('estree').LabeledStatement, import('../types.js').ReactiveStatement]} Tuple */
 
	/** @type {Map<string, Array<Tuple>>} */
	const lookup = new Map();
 
	for (const [node, declaration] of unsorted_reactive_declarations) {
		for (const binding of declaration.assignments) {
			const statements = lookup.get(binding.node.name) ?? [];
			statements.push([node, declaration]);
			lookup.set(binding.node.name, statements);
		}
	}
 
	/** @type {Array<[string, string]>} */
	const edges = [];
 
	for (const [, { assignments, dependencies }] of unsorted_reactive_declarations) {
		for (const assignment of assignments) {
			for (const dependency of dependencies) {
				if (!assignments.has(dependency)) {
					edges.push([assignment.node.name, dependency.node.name]);
				}
			}
		}
	}
 
	const cycle = check_graph_for_cycles(edges);
	if (cycle?.length) {
		const declaration = /** @type {Tuple[]} */ (lookup.get(cycle[0]))[0];
		e.reactive_declaration_cycle(declaration[0], cycle.join(' → '));
	}
 
	// We use a map and take advantage of the fact that the spec says insertion order is preserved when iterating
	/** @type {Map<import('estree').LabeledStatement, import('../types.js').ReactiveStatement>} */
	const reactive_declarations = new Map();
 
	/**
	 *
	 * @param {import('estree').LabeledStatement} node
	 * @param {import('../types.js').ReactiveStatement} declaration
	 * @returns
	 */
	const add_declaration = (node, declaration) => {
		if ([...reactive_declarations.values()].includes(declaration)) return;
 
		for (const binding of declaration.dependencies) {
			if (declaration.assignments.has(binding)) continue;
			for (const [node, earlier] of lookup.get(binding.node.name) ?? []) {
				add_declaration(node, earlier);
			}
		}
 
		reactive_declarations.set(node, declaration);
	};
 
	for (const [node, declaration] of unsorted_reactive_declarations) {
		add_declaration(node, declaration);
	}
 
	return reactive_declarations;
}