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

95.74% Statements 1371/1432
94.56% Branches 626/662
95.52% Functions 64/67
95.7% Lines 1358/1419

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 14202x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 968x 968x 968x 968x 968x 8x 8x 8x 8x 968x 4x 4x 968x 968x 954x 954x 14x 968x 968x 968x 2x 2x 968x 2x 2x 2x 2x 2x 1395x 1395x 1488x 1488x 1488x 1488x 1488x 300x 1488x 4x 4x 1484x 1484x 1488x 60x 1488x 1x 1x 1483x 1488x 929x 192x 192x 192x 185x 185x 2x 2x 2x 2x 1x 2x 2x 185x 192x 927x 927x 927x 929x 23x 23x 929x 1488x 1386x 1386x 1386x 1386x 1386x 1386x 1386x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 9010x 9010x 9010x 9010x 9010x 9010x 9010x 9010x 9010x 9010x 6630x 4183x 4183x 4183x 776x 776x 776x 584x 584x 2x 2x 2x 2x 1x 2x 2x 584x 776x 4181x 4183x 5x 5x 4176x 4183x 526x 1x 1x 525x 525x 525x 526x 526x 28x 526x 2x 2x 526x 4175x 4183x 120x 120x 120x 4165x 4183x 4x 4x 4165x 4165x 4183x 2x 2x 4165x 4165x 6630x 22x 22x 1x 22x 1x 21x 20x 20x 66x 66x 64x 20x 20x 1x 1x 19x 22x 1x 22x 18x 18x 2447x 300x 300x 300x 300x 300x 7x 7x 7x 7x 3x 7x 4x 4x 7x 293x 300x 300x 2425x 996x 996x 996x 39x 1x 1x 1x 39x 2x 36x 14x 14x 39x 2x 2x 2x 2x 2x 2x 39x 993x 6630x 8978x 2x 2x 2x 2x 5092x 5092x 5092x 5092x 5092x 4x 5092x 2x 2x 5092x 2x 2x 2x 2x 2x 2x 203x 203x 203x 203x 203x 2x     2x 2x 201x 201x 203x 653x 653x 653x 421x 421x 421x 421x 421x 653x 198x 198x 653x 201x 201x 198x 1x 1x 197x 197x 198x 198x 8x 198x 189x 6x 6x 183x 183x 183x 189x 5x 5x 178x 178x 178x 189x 1x 4x 2x 2x 2x 4x 2x 1x 1x 2x 1x 1x 1x   189x 203x 1x 1x 203x 2x 2x 2x 2x 2x 3951x 3951x 2349x 2349x 3951x 6x 6x 3951x 2x 2x 2x 2x 2x 2x 5056x 4156x 4156x 1x 1x 4156x 2x 2x 2266x 2x 2x 992x 992x 992x 992x 992x 992x     986x 986x 986x 986x 992x 992x 992x 544x 544x 544x 542x 542x 542x 542x 542x 542x 542x 544x 2x 2x 542x 544x 1x 1x 541x 544x 1x 1x 540x 544x 1x 1x 544x 981x 992x 128x     128x 981x 992x 5x 5x 981x 981x 981x 981x 992x 992x 992x 992x 296x 992x 685x 1x 1x 684x 685x 681x 681x 3x 3x 3x 3x 3x 3x 678x 681x 2x 2x 94x 94x 82x 82x 94x 2x 2x 2x 2x 2x 2x 2x 2x 2x 676x 681x 380x 380x 380x 380x 3x 1x 1x 2x 2x 377x 380x 1x 1x 376x 380x     380x 672x 681x 68x 68x 82x 82x 82x 6x 68x 68x 1x 1x 68x 671x 681x 1x 1x 1x 1x 1x 1x 670x 680x 22x 22x 22x 22x 1x 22x 1x 1x 22x 685x 3x 3x 1x 1x 1x 1x 1x 2x 2x 685x 2x 2x 3x 2x 2x 263x 263x 263x 263x 263x 263x 263x 263x 263x 263x 263x 16x 263x 263x 2x 2x 2x 2x 1732x 49x 59x 59x 59x 58x 59x 1x 1x 59x 59x 48x 2x 2x 222x 222x 222x 222x 222x 222x 222x 222x 222x 222x 222x 1x 1x 2x 2x 8868x 24x 5x 1x 1x 5x 23x 8867x 8867x 8867x 8868x 8868x 2x 8868x 1x 1x 8867x 8867x 8867x 8868x 1183x 1183x 1183x 1183x 4825x 4825x 4825x 4825x 4825x 4825x 4404x 4825x 423x 423x 423x 4825x 4825x 2850x 2850x 1183x 2850x 1183x 1x 1x 1x 1x 1x 1x 1x     1x 1183x 1183x 1183x 4825x 186x 3x   3x 3x 3x 3x 1975x 1789x 1789x 1789x 1789x 1754x 1789x 37x 37x 4825x 1180x 8832x 8832x 8832x 8832x 8832x 8868x 8868x 8868x 100x 8868x 52x 52x 8832x 8832x 8832x 8832x 8832x 2x 2x 154x 154x 154x 154x 154x 154x 154x 154x 154x 61x     61x 154x 154x 154x 154x 13x 154x 1x 1x 2x 2x 672x 672x 2x 2x 967x 967x 2x 2x 179x 179x 179x 2x 2x 30x 2x 2x 106x 106x 106x 70x 1x 1x 70x 105x 105x 105x 105x 105x 106x 26x 26x 102x 18x 18x 1x 1x 18x 106x 1x 1x 25x 100x 12x 12x 99x 106x 1x 106x 11x 11x 11x 11x 11x 1x 1x 11x 2x 2x 77x 1x 1x 2x 2x 56x 56x 1x 1x 2x 2x 143x 143x 143x 143x 143x 2x 2x 69x 69x 2x 2x 67x 67x 109x 60x 60x 60x 109x     109x 2x 2x 491x 353x 343x 168x 1x 1x 167x 168x 1x 1x 168x 353x     353x 2x 2x 2x 2x 2x 25814x 25814x 3958x     3958x 2x 2x 33x 33x 1x 1x 32x 32x 32x 1x 1x 2x 2x 367x 2x 2x 7144x 7055x 4144x     4144x 7144x 2x 2x 2x 2x 6095x 6095x 6095x 344x 344x 344x 5584x 280x 6095x 343x 343x 1x 6095x     2x 2x 1466x 1466x 1466x 2x 2x 321x 321x 317x 321x 4x 4x 2x 2x 101x 101x 2x 2x 2x 2x 2x 2x 2x 2x 32x 32x 32x 32x 32x 1x 1x 31x 32x 3x 3x 32x 2x 2x 2x 2x 2x 2x 2x 2884x 2884x 2884x 1612x 1612x 1612x 2884x 236x     1376x 2884x 50x 50x 50x 50x 50x 50x 48x 48x 50x 2x 2x 1326x 1326x 2884x 2884x 2884x 229x 2884x 1111x 1111x 5x 5x 215x 2884x 152x 2x 2x 150x 150x 2x 2x 152x 211x 2808x 10x     10x 211x 2808x 14x     14x 211x 2808x 24x     24x 211x 2808x 4x     4x 211x 2763x 7x 1x 1x 7x 210x 2760x 4x     4x 2884x 2x 2x 2x 2x 2x 7809x 7809x 7809x 8307x 8307x 7541x 8307x 1x 1x 8307x 7808x 2x 2x 2x 2x 2x 2x 1x 1x 1x 2x 2x   2x 2x 29x 23x 23x 23x 23x 23x 23x 23x 23x 20x 2x 2x 50x 1x 1x 49x 2x 2x 37x 37x 37x 37x 27x 27x 27x 37x 1x 37x 1x 26x 1x 25x     2x 2x 7x 2x 2x 4x 2x 2x 90x 90x 90x 90x 232x 232x 232x 36x 232x 26x 26x 4x 4x 26x 232x 90x 90x 90x 90x 90x 2x 2x 85x 85x 85x 85x 85x 85x 1x 1x 2x 2x 135x 2x 2x 2x 2x 14366x 14366x 14366x 14366x 14366x 14366x 14366x 1600x 14366x 1557x 1557x 1557x 1557x 1557x 108x 108x 108x 108x 108x 108x 108x 2x 1x 1x 1x 1x 1x 108x 1555x 1557x 2x 2x 1557x 14366x 2x 2x 2x 2x 2x 2x 2x 2x 5549x 5549x 86x 190x 190x 190x 190x 5549x 92x 156x 156x 156x 156x 5463x 3928x 3928x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 6x 18x 12x 12x 18x 3928x 5549x 2x 2x 2x 2x 2x 2x 2x 2x 539x 539x 1x 1x 1x 539x 2x 2x 2x 2x 2x 2x 4211x 4211x 4211x 4211x 2920x 2920x 2920x 681x 2x 2x 679x 681x 1x 1x 681x 2917x 2920x 1x 1x 2920x 4195x 4195x 4195x 4195x 4195x 4195x 4202x 1225x 1225x 1225x 4195x 4211x 36x 2x 2x 36x 4211x 2x 2x 2x 403x 1x 1x 2x 2x     2x 2x 43x 15x 9x 9x 9x 9x 9x 9x 9x 9x 8x 43x 28x 9x 1x 1x 1x 2x 2x 15x     2x 2x 2837x 2837x 1x 2837x 1x   1x 1x 1x 1x 2835x 2835x 2x 2x 169x 169x 169x 169x 169x 163x 169x 1x 1x 168x 2x 2x 99x 99x 99x 99x 99x 99x 2x 2x 6x 6x 6x 6x 6x 6x     6x 6x 6x             2x 2x 2x 2x 2x 92x 2x 2x 10x 2x 2x 18x 2x 2x   2x 2x 143x 2x 2x 1714x 1714x 1714x 1714x 1714x 1714x 1230x 1230x 1230x 1230x 1714x 1x 1714x 1x 1229x 242x 1x 1x 241x 241x 241x 242x 1x 1x 240x 242x     240x 242x 1x 1x 239x 242x 236x 344x 316x     316x 316x 1x 1x 315x 315x 316x 316x 316x     316x 344x 235x 242x 1224x 1714x 110x 110x 110x 22x 110x     110x 2x 2x 17x 17x 17x 2x 2x 312x 312x 312x 294x 294x 2x 2x 2x 2x 2x 2x 2x  
/** @import { AssignmentExpression, CallExpression, Expression, Identifier, Node, Pattern, PrivateIdentifier, Super, UpdateExpression, VariableDeclarator } from 'estree' */
/** @import { Attribute, Component, ElementLike, Fragment, RegularElement, SvelteComponent, SvelteElement, SvelteNode, SvelteSelf, TransitionDirective } from '#compiler' */
/** @import { NodeLike } from '../../errors.js' */
/** @import { AnalysisState, Context, Visitors } from './types.js' */
import is_reference from 'is-reference';
import * as e from '../../errors.js';
import {
	extract_identifiers,
	get_attribute_expression,
	get_parent,
	is_expression_attribute,
	is_text_attribute,
	object,
	unwrap_optional
} from '../../utils/ast.js';
import * as w from '../../warnings.js';
import fuzzymatch from '../1-parse/utils/fuzzymatch.js';
import { binding_properties } from '../bindings.js';
import {
	ContentEditableBindings,
	EventModifiers,
	Runes,
	SVGElements,
	VoidElements
} from '../constants.js';
import { is_custom_element_node } from '../nodes.js';
import {
	regex_illegal_attribute_character,
	regex_not_whitespace,
	regex_only_whitespaces
} from '../patterns.js';
import { Scope, get_rune } from '../scope.js';
import { merge } from '../visitors.js';
import { a11y_validators } from './a11y.js';
import {
	is_tag_valid_with_ancestor,
	is_tag_valid_with_parent
} from '../../../html-tree-validation.js';
 
/**
 * @param {Attribute} attribute
 * @param {ElementLike} parent
 */
function validate_attribute(attribute, parent) {
	if (
		Array.isArray(attribute.value) &&
		attribute.value.length === 1 &&
		attribute.value[0].type === 'ExpressionTag' &&
		(parent.type === 'Component' ||
			parent.type === 'SvelteComponent' ||
			parent.type === 'SvelteSelf' ||
			(parent.type === 'RegularElement' && is_custom_element_node(parent)))
	) {
		w.attribute_quoted(attribute);
	}
 
	if (attribute.value === true || !Array.isArray(attribute.value) || attribute.value.length === 1) {
		return;
	}
 
	const is_quoted = attribute.value.at(-1)?.end !== attribute.end;
 
	if (!is_quoted) {
		e.attribute_unquoted_sequence(attribute);
	}
}
 
/**
 * @param {Component | SvelteComponent | SvelteSelf} node
 * @param {Context} context
 */
function validate_component(node, context) {
	for (const attribute of node.attributes) {
		if (
			attribute.type !== 'Attribute' &&
			attribute.type !== 'SpreadAttribute' &&
			attribute.type !== 'LetDirective' &&
			attribute.type !== 'OnDirective' &&
			attribute.type !== 'BindDirective'
		) {
			e.component_invalid_directive(attribute);
		}
 
		if (
			attribute.type === 'OnDirective' &&
			(attribute.modifiers.length > 1 || attribute.modifiers.some((m) => m !== 'once'))
		) {
			e.event_handler_invalid_component_modifier(attribute);
		}
 
		if (attribute.type === 'Attribute') {
			if (context.state.analysis.runes) {
				validate_attribute(attribute, node);
 
				if (is_expression_attribute(attribute)) {
					const expression = get_attribute_expression(attribute);
					if (expression.type === 'SequenceExpression') {
						let i = /** @type {number} */ (expression.start);
						while (--i > 0) {
							const char = context.state.analysis.source[i];
							if (char === '(') break; // parenthesized sequence expressions are ok
							if (char === '{') e.attribute_invalid_sequence_expression(expression);
						}
					}
				}
			}
 
			validate_attribute_name(attribute);
 
			if (attribute.name === 'slot') {
				validate_slot_attribute(context, attribute, true);
			}
		}
	}
 
	context.next({
		...context.state,
		parent_element: null,
		component_slots: new Set()
	});
}
 
const react_attributes = new Map([
	['className', 'class'],
	['htmlFor', 'for']
]);
 
/**
 * @param {RegularElement | SvelteElement} node
 * @param {Context} context
 */
function validate_element(node, context) {
	let has_animate_directive = false;
 
	/** @type {TransitionDirective | null} */
	let in_transition = null;
 
	/** @type {TransitionDirective | null} */
	let out_transition = null;
 
	for (const attribute of node.attributes) {
		if (attribute.type === 'Attribute') {
			const is_expression = is_expression_attribute(attribute);
 
			if (context.state.analysis.runes) {
				validate_attribute(attribute, node);
 
				if (is_expression) {
					const expression = get_attribute_expression(attribute);
					if (expression.type === 'SequenceExpression') {
						let i = /** @type {number} */ (expression.start);
						while (--i > 0) {
							const char = context.state.analysis.source[i];
							if (char === '(') break; // parenthesized sequence expressions are ok
							if (char === '{') e.attribute_invalid_sequence_expression(expression);
						}
					}
				}
			}
 
			if (regex_illegal_attribute_character.test(attribute.name)) {
				e.attribute_invalid_name(attribute, attribute.name);
			}
 
			if (attribute.name.startsWith('on') && attribute.name.length > 2) {
				if (!is_expression) {
					e.attribute_invalid_event_handler(attribute);
				}
 
				const value = get_attribute_expression(attribute);
				if (
					value.type === 'Identifier' &&
					value.name === attribute.name &&
					!context.state.scope.get(value.name)
				) {
					w.attribute_global_event_reference(attribute, attribute.name);
				}
			}
 
			if (attribute.name === 'slot') {
				/** @type {RegularElement | SvelteElement | Component | SvelteComponent | SvelteSelf | undefined} */
				validate_slot_attribute(context, attribute);
			}
 
			if (attribute.name === 'is' && context.state.options.namespace !== 'foreign') {
				w.attribute_avoid_is(attribute);
			}
 
			const correct_name = react_attributes.get(attribute.name);
			if (correct_name) {
				w.attribute_invalid_property_name(attribute, attribute.name, correct_name);
			}
 
			validate_attribute_name(attribute);
		} else if (attribute.type === 'AnimateDirective') {
			const parent = context.path.at(-2);
			if (parent?.type !== 'EachBlock') {
				e.animation_invalid_placement(attribute);
			} else if (!parent.key) {
				e.animation_missing_key(attribute);
			} else if (
				parent.body.nodes.filter(
					(n) =>
						n.type !== 'Comment' &&
						n.type !== 'ConstTag' &&
						(n.type !== 'Text' || n.data.trim() !== '')
				).length > 1
			) {
				e.animation_invalid_placement(attribute);
			}
 
			if (has_animate_directive) {
				e.animation_duplicate(attribute);
			} else {
				has_animate_directive = true;
			}
		} else if (attribute.type === 'TransitionDirective') {
			const existing = /** @type {TransitionDirective | null} */ (
				(attribute.intro && in_transition) || (attribute.outro && out_transition)
			);
 
			if (existing) {
				const a = existing.intro ? (existing.outro ? 'transition' : 'in') : 'out';
				const b = attribute.intro ? (attribute.outro ? 'transition' : 'in') : 'out';
 
				if (a === b) {
					e.transition_duplicate(attribute, a);
				} else {
					e.transition_conflict(attribute, a, b);
				}
			}
 
			if (attribute.intro) in_transition = attribute;
			if (attribute.outro) out_transition = attribute;
		} else if (attribute.type === 'OnDirective') {
			let has_passive_modifier = false;
			let conflicting_passive_modifier = '';
			for (const modifier of attribute.modifiers) {
				if (!EventModifiers.includes(modifier)) {
					const list = `${EventModifiers.slice(0, -1).join(', ')} or ${EventModifiers.at(-1)}`;
					e.event_handler_invalid_modifier(attribute, list);
				}
				if (modifier === 'passive') {
					has_passive_modifier = true;
				} else if (modifier === 'nonpassive' || modifier === 'preventDefault') {
					conflicting_passive_modifier = modifier;
				}
				if (has_passive_modifier && conflicting_passive_modifier) {
					e.event_handler_invalid_modifier_combination(
						attribute,
						'passive',
						conflicting_passive_modifier
					);
				}
			}
		}
	}
}
 
/**
 * @param {Attribute} attribute
 */
function validate_attribute_name(attribute) {
	if (
		attribute.name.includes(':') &&
		!attribute.name.startsWith('xmlns:') &&
		!attribute.name.startsWith('xlink:') &&
		!attribute.name.startsWith('xml:')
	) {
		w.attribute_illegal_colon(attribute);
	}
}
 
/**
 * @param {Context} context
 * @param {Attribute} attribute
 * @param {boolean} is_component
 */
function validate_slot_attribute(context, attribute, is_component = false) {
	const parent = context.path.at(-2);
	let owner = undefined;
 
	if (parent?.type === 'SnippetBlock') {
		if (!is_text_attribute(attribute)) {
			e.slot_attribute_invalid(attribute);
		}
		return;
	}
 
	let i = context.path.length;
	while (i--) {
		const ancestor = context.path[i];
		if (
			!owner &&
			(ancestor.type === 'Component' ||
				ancestor.type === 'SvelteComponent' ||
				ancestor.type === 'SvelteSelf' ||
				ancestor.type === 'SvelteElement' ||
				(ancestor.type === 'RegularElement' && is_custom_element_node(ancestor)))
		) {
			owner = ancestor;
		}
	}
 
	if (owner) {
		if (!is_text_attribute(attribute)) {
			e.slot_attribute_invalid(attribute);
		}
 
		if (
			owner.type === 'Component' ||
			owner.type === 'SvelteComponent' ||
			owner.type === 'SvelteSelf'
		) {
			if (owner !== parent) {
				e.slot_attribute_invalid_placement(attribute);
			}
 
			const name = attribute.value[0].data;
 
			if (context.state.component_slots.has(name)) {
				e.slot_attribute_duplicate(attribute, name, owner.name);
			}
 
			context.state.component_slots.add(name);
 
			if (name === 'default') {
				for (const node of owner.fragment.nodes) {
					if (node.type === 'Text' && regex_only_whitespaces.test(node.data)) {
						continue;
					}
 
					if (node.type === 'RegularElement' || node.type === 'SvelteFragment') {
						if (node.attributes.some((a) => a.type === 'Attribute' && a.name === 'slot')) {
							continue;
						}
					}
 
					e.slot_default_duplicate(node);
				}
			}
		}
	} else if (!is_component) {
		e.slot_attribute_invalid_placement(attribute);
	}
}
 
/**
 * @param {Fragment | null | undefined} node
 * @param {Context} context
 */
function validate_block_not_empty(node, context) {
	if (!node) return;
	// Assumption: If the block has zero elements, someone's in the middle of typing it out,
	// so don't warn in that case because it would be distracting.
	if (node.nodes.length === 1 && node.nodes[0].type === 'Text' && !node.nodes[0].raw.trim()) {
		w.block_empty(node.nodes[0]);
	}
}
 
/**
 * @type {Visitors}
 */
const validation = {
	MemberExpression(node, context) {
		if (node.object.type === 'Identifier' && node.property.type === 'Identifier') {
			const binding = context.state.scope.get(node.object.name);
			if (binding?.kind === 'rest_prop' && node.property.name.startsWith('$$')) {
				e.props_illegal_name(node.property);
			}
		}
	},
	AssignmentExpression(node, context) {
		validate_assignment(node, node.left, context.state);
	},
	BindDirective(node, context) {
		validate_no_const_assignment(node, node.expression, context.state.scope, true);
 
		const assignee = node.expression;
		const left = object(assignee);
 
		if (left === null) {
			e.bind_invalid_expression(node);
		}
 
		const binding = context.state.scope.get(left.name);
 
		if (
			assignee.type === 'Identifier' &&
			node.name !== 'this' // bind:this also works for regular variables
		) {
			// reassignment
			if (
				!binding ||
				(binding.kind !== 'state' &&
					binding.kind !== 'frozen_state' &&
					binding.kind !== 'prop' &&
					binding.kind !== 'bindable_prop' &&
					binding.kind !== 'each' &&
					binding.kind !== 'store_sub' &&
					!binding.mutated)
			) {
				e.bind_invalid_value(node.expression);
			}
 
			if (binding.kind === 'derived') {
				e.constant_binding(node.expression, 'derived state');
			}
 
			if (context.state.analysis.runes && binding.kind === 'each') {
				e.each_item_invalid_assignment(node);
			}
 
			if (binding.kind === 'snippet') {
				e.snippet_parameter_assignment(node);
			}
		}
 
		if (node.name === 'group') {
			if (!binding) {
				throw new Error('Cannot find declaration for bind:group');
			}
		}
 
		if (binding?.kind === 'each' && binding.metadata?.inside_rest) {
			w.bind_invalid_each_rest(binding.node, binding.node.name);
		}
 
		const parent = context.path.at(-1);
 
		if (
			parent?.type === 'RegularElement' ||
			parent?.type === 'SvelteElement' ||
			parent?.type === 'SvelteWindow' ||
			parent?.type === 'SvelteDocument' ||
			parent?.type === 'SvelteBody'
		) {
			if (context.state.options.namespace === 'foreign' && node.name !== 'this') {
				e.bind_invalid_name(node, node.name, 'Foreign elements only support `bind:this`');
			}
 
			if (node.name in binding_properties) {
				const property = binding_properties[node.name];
				if (property.valid_elements && !property.valid_elements.includes(parent.name)) {
					e.bind_invalid_target(
						node,
						node.name,
						property.valid_elements.map((valid_element) => `<${valid_element}>`).join(', ')
					);
				}
 
				if (property.invalid_elements && property.invalid_elements.includes(parent.name)) {
					const valid_bindings = Object.entries(binding_properties)
						.filter(([_, binding_property]) => {
							return (
								binding_property.valid_elements?.includes(parent.name) ||
								(!binding_property.valid_elements &&
									!binding_property.invalid_elements?.includes(parent.name))
							);
						})
						.map(([property_name]) => property_name)
						.sort();
					e.bind_invalid_name(
						node,
						node.name,
						`Possible bindings for <${parent.name}> are ${valid_bindings.join(', ')}`
					);
				}
 
				if (parent.name === 'input' && node.name !== 'this') {
					const type = /** @type {Attribute | undefined} */ (
						parent.attributes.find((a) => a.type === 'Attribute' && a.name === 'type')
					);
					if (type && !is_text_attribute(type)) {
						if (node.name !== 'value' || type.value === true) {
							e.attribute_invalid_type(type);
						}
						return; // bind:value can handle dynamic `type` attributes
					}
 
					if (node.name === 'checked' && type?.value[0].data !== 'checkbox') {
						e.bind_invalid_target(node, node.name, '<input type="checkbox">');
					}
 
					if (node.name === 'files' && type?.value[0].data !== 'file') {
						e.bind_invalid_target(node, node.name, '<input type="file">');
					}
				}
 
				if (parent.name === 'select' && node.name !== 'this') {
					const multiple = parent.attributes.find(
						(a) =>
							a.type === 'Attribute' &&
							a.name === 'multiple' &&
							!is_text_attribute(a) &&
							a.value !== true
					);
					if (multiple) {
						e.attribute_invalid_multiple(multiple);
					}
				}
 
				if (node.name === 'offsetWidth' && SVGElements.includes(parent.name)) {
					e.bind_invalid_target(
						node,
						node.name,
						`non-<svg> elements. Use 'clientWidth' for <svg> instead`
					);
				}
 
				if (ContentEditableBindings.includes(node.name)) {
					const contenteditable = /** @type {Attribute} */ (
						parent.attributes.find((a) => a.type === 'Attribute' && a.name === 'contenteditable')
					);
					if (!contenteditable) {
						e.attribute_contenteditable_missing(node);
					} else if (!is_text_attribute(contenteditable) && contenteditable.value !== true) {
						e.attribute_contenteditable_dynamic(contenteditable);
					}
				}
			} else {
				const match = fuzzymatch(node.name, Object.keys(binding_properties));
				if (match) {
					const property = binding_properties[match];
					if (!property.valid_elements || property.valid_elements.includes(parent.name)) {
						e.bind_invalid_name(node, node.name, `Did you mean '${match}'?`);
					}
				}
				e.bind_invalid_name(node, node.name);
			}
		}
	},
	ExportDefaultDeclaration(node) {
		e.module_illegal_default_export(node);
	},
	ConstTag(node, context) {
		const parent = context.path.at(-1);
		const grand_parent = context.path.at(-2);
		if (
			parent?.type !== 'Fragment' ||
			(grand_parent?.type !== 'IfBlock' &&
				grand_parent?.type !== 'SvelteFragment' &&
				grand_parent?.type !== 'Component' &&
				grand_parent?.type !== 'SvelteComponent' &&
				grand_parent?.type !== 'EachBlock' &&
				grand_parent?.type !== 'AwaitBlock' &&
				grand_parent?.type !== 'SnippetBlock' &&
				((grand_parent?.type !== 'RegularElement' && grand_parent?.type !== 'SvelteElement') ||
					!grand_parent.attributes.some((a) => a.type === 'Attribute' && a.name === 'slot')))
		) {
			e.const_tag_invalid_placement(node);
		}
	},
	ImportDeclaration(node, context) {
		if (node.source.value === 'svelte' && context.state.analysis.runes) {
			for (const specifier of node.specifiers) {
				if (specifier.type === 'ImportSpecifier') {
					if (
						specifier.imported.name === 'beforeUpdate' ||
						specifier.imported.name === 'afterUpdate'
					) {
						e.runes_mode_invalid_import(specifier, specifier.imported.name);
					}
				}
			}
		}
	},
	LetDirective(node, context) {
		const parent = context.path.at(-1);
		if (
			parent === undefined ||
			(parent.type !== 'Component' &&
				parent.type !== 'RegularElement' &&
				parent.type !== 'SlotElement' &&
				parent.type !== 'SvelteElement' &&
				parent.type !== 'SvelteComponent' &&
				parent.type !== 'SvelteSelf' &&
				parent.type !== 'SvelteFragment')
		) {
			e.let_directive_invalid_placement(node);
		}
	},
	RegularElement(node, context) {
		if (node.name === 'textarea' && node.fragment.nodes.length > 0) {
			for (const attribute of node.attributes) {
				if (attribute.type === 'Attribute' && attribute.name === 'value') {
					e.textarea_invalid_content(node);
				}
			}
		}
 
		const binding = context.state.scope.get(node.name);
		if (
			binding !== null &&
			binding.declaration_kind === 'import' &&
			binding.references.length === 0
		) {
			w.component_name_lowercase(node, node.name);
		}
 
		validate_element(node, context);
 
		if (context.state.parent_element) {
			let past_parent = false;
			let only_warn = false;
 
			for (let i = context.path.length - 1; i >= 0; i--) {
				const ancestor = context.path[i];
 
				if (
					ancestor.type === 'IfBlock' ||
					ancestor.type === 'EachBlock' ||
					ancestor.type === 'AwaitBlock' ||
					ancestor.type === 'KeyBlock'
				) {
					// We're creating a separate template string inside blocks, which means client-side this would work
					only_warn = true;
				}
 
				if (!past_parent) {
					if (
						ancestor.type === 'RegularElement' &&
						ancestor.name === context.state.parent_element
					) {
						if (!is_tag_valid_with_parent(node.name, context.state.parent_element)) {
							if (only_warn) {
								w.node_invalid_placement_ssr(
									node,
									`\`<${node.name}>\``,
									context.state.parent_element
								);
							} else {
								e.node_invalid_placement(node, `\`<${node.name}>\``, context.state.parent_element);
							}
						}
 
						past_parent = true;
					}
				} else if (ancestor.type === 'RegularElement') {
					if (!is_tag_valid_with_ancestor(node.name, ancestor.name)) {
						if (only_warn) {
							w.node_invalid_placement_ssr(node, `\`<${node.name}>\``, ancestor.name);
						} else {
							e.node_invalid_placement(node, `\`<${node.name}>\``, ancestor.name);
						}
					}
				} else if (
					ancestor.type === 'Component' ||
					ancestor.type === 'SvelteComponent' ||
					ancestor.type === 'SvelteElement' ||
					ancestor.type === 'SvelteSelf' ||
					ancestor.type === 'SnippetBlock'
				) {
					break;
				}
			}
		}
 
		// Strip off any namespace from the beginning of the node name.
		const node_name = node.name.replace(/[a-zA-Z-]*:/g, '');
 
		if (
			context.state.analysis.source[node.end - 2] === '/' &&
			context.state.options.namespace !== 'foreign' &&
			!VoidElements.includes(node_name) &&
			!SVGElements.includes(node_name)
		) {
			w.element_invalid_self_closing_tag(node, node.name);
		}
 
		context.next({
			...context.state,
			parent_element: node.name
		});
	},
	RenderTag(node, context) {
		const callee = unwrap_optional(node.expression).callee;
 
		node.metadata.dynamic =
			callee.type !== 'Identifier' || context.state.scope.get(callee.name)?.kind !== 'normal';
 
		context.state.analysis.uses_render_tags = true;
 
		const raw_args = unwrap_optional(node.expression).arguments;
		for (const arg of raw_args) {
			if (arg.type === 'SpreadElement') {
				e.render_tag_invalid_spread_argument(arg);
			}
		}
 
		if (
			callee.type === 'MemberExpression' &&
			callee.property.type === 'Identifier' &&
			['bind', 'apply', 'call'].includes(callee.property.name)
		) {
			e.render_tag_invalid_call_expression(node);
		}
	},
	IfBlock(node, context) {
		validate_block_not_empty(node.consequent, context);
		validate_block_not_empty(node.alternate, context);
	},
	EachBlock(node, context) {
		validate_block_not_empty(node.body, context);
		validate_block_not_empty(node.fallback, context);
	},
	AwaitBlock(node, context) {
		validate_block_not_empty(node.pending, context);
		validate_block_not_empty(node.then, context);
		validate_block_not_empty(node.catch, context);
	},
	KeyBlock(node, context) {
		validate_block_not_empty(node.fragment, context);
	},
	SnippetBlock(node, context) {
		validate_block_not_empty(node.body, context);
 
		for (const arg of node.parameters) {
			if (arg.type === 'RestElement') {
				e.snippet_invalid_rest_parameter(arg);
			}
		}
 
		context.next({ ...context.state, parent_element: null });
 
		const { path } = context;
		const parent = path.at(-2);
		if (!parent) return;
 
		if (
			parent.type === 'Component' &&
			parent.attributes.some(
				(attribute) =>
					(attribute.type === 'Attribute' || attribute.type === 'BindDirective') &&
					attribute.name === node.expression.name
			)
		) {
			e.snippet_shadowing_prop(node, node.expression.name);
		}
 
		if (node.expression.name !== 'children') return;
 
		if (
			parent.type === 'Component' ||
			parent.type === 'SvelteComponent' ||
			parent.type === 'SvelteSelf'
		) {
			if (
				parent.fragment.nodes.some(
					(node) => node.type !== 'SnippetBlock' && (node.type !== 'Text' || node.data.trim())
				)
			) {
				e.snippet_conflict(node);
			}
		}
	},
	StyleDirective(node) {
		if (node.modifiers.length > 1 || (node.modifiers.length && node.modifiers[0] !== 'important')) {
			e.style_directive_invalid_modifier(node);
		}
	},
	SvelteHead(node) {
		const attribute = node.attributes[0];
		if (attribute) {
			e.svelte_head_illegal_attribute(attribute);
		}
	},
	SvelteElement(node, context) {
		validate_element(node, context);
		context.next({
			...context.state,
			parent_element: null
		});
	},
	SvelteFragment(node, context) {
		const parent = context.path.at(-2);
		if (parent?.type !== 'Component' && parent?.type !== 'SvelteComponent') {
			e.svelte_fragment_invalid_placement(node);
		}
 
		for (const attribute of node.attributes) {
			if (attribute.type === 'Attribute') {
				if (attribute.name === 'slot') {
					validate_slot_attribute(context, attribute);
				}
			} else if (attribute.type !== 'LetDirective') {
				e.svelte_fragment_invalid_attribute(attribute);
			}
		}
	},
	SlotElement(node) {
		for (const attribute of node.attributes) {
			if (attribute.type === 'Attribute') {
				if (attribute.name === 'name') {
					if (!is_text_attribute(attribute)) {
						e.slot_element_invalid_name(attribute);
					}
					const slot_name = attribute.value[0].data;
					if (slot_name === 'default') {
						e.slot_element_invalid_name_default(attribute);
					}
				}
			} else if (attribute.type !== 'SpreadAttribute' && attribute.type !== 'LetDirective') {
				e.slot_element_invalid_attribute(attribute);
			}
		}
	},
	Component: validate_component,
	SvelteComponent: validate_component,
	SvelteSelf: validate_component,
	Text(node, context) {
		if (!node.parent) return;
		if (context.state.parent_element && regex_not_whitespace.test(node.data)) {
			if (!is_tag_valid_with_parent('#text', context.state.parent_element)) {
				e.node_invalid_placement(node, 'Text node', context.state.parent_element);
			}
		}
	},
	TitleElement(node) {
		const attribute = node.attributes[0];
		if (attribute) {
			e.title_illegal_attribute(attribute);
		}
 
		const child = node.fragment.nodes.find((n) => n.type !== 'Text' && n.type !== 'ExpressionTag');
		if (child) {
			e.title_invalid_content(child);
		}
	},
	UpdateExpression(node, context) {
		validate_assignment(node, node.argument, context.state);
	},
	ExpressionTag(node, context) {
		if (!node.parent) return;
		if (context.state.parent_element) {
			if (!is_tag_valid_with_parent('#text', context.state.parent_element)) {
				e.node_invalid_placement(node, '`{expression}`', context.state.parent_element);
			}
		}
	}
};
 
export const validation_legacy = merge(validation, a11y_validators, {
	VariableDeclarator(node, { state }) {
		ensure_no_module_import_conflict(node, state);
 
		if (node.init?.type !== 'CallExpression') return;
 
		const callee = node.init.callee;
		if (
			callee.type !== 'Identifier' ||
			(callee.name !== '$state' && callee.name !== '$derived' && callee.name !== '$props')
		) {
			return;
		}
 
		if (state.scope.get(callee.name)?.kind !== 'store_sub') {
			e.rune_invalid_usage(node.init, callee.name);
		}
	},
	AssignmentExpression(node, { state, path }) {
		const parent = path.at(-1);
		if (parent && parent.type === 'ConstTag') return;
		validate_assignment(node, node.left, state);
	},
	LabeledStatement(node, { path, state }) {
		if (
			node.label.name === '$' &&
			(state.ast_type !== 'instance' || /** @type {SvelteNode} */ (path.at(-1)).type !== 'Program')
		) {
			w.reactive_declaration_invalid_placement(node);
		}
	},
	UpdateExpression(node, { state }) {
		validate_assignment(node, node.argument, state);
	}
});
 
/**
 *
 * @param {Node} node
 * @param {Scope} scope
 * @param {string} name
 */
function validate_export(node, scope, name) {
	const binding = scope.get(name);
	if (!binding) return;
 
	if (binding.kind === 'derived') {
		e.derived_invalid_export(node);
	}
 
	if ((binding.kind === 'state' || binding.kind === 'frozen_state') && binding.reassigned) {
		e.state_invalid_export(node);
	}
}
 
/**
 * @param {CallExpression} node
 * @param {Scope} scope
 * @param {SvelteNode[]} path
 * @returns
 */
function validate_call_expression(node, scope, path) {
	const rune = get_rune(node, scope);
	if (rune === null) return;
 
	const parent = /** @type {SvelteNode} */ (get_parent(path, -1));
 
	if (rune === '$props') {
		if (parent.type === 'VariableDeclarator') return;
		e.props_invalid_placement(node);
	}
 
	if (rune === '$bindable') {
		if (parent.type === 'AssignmentPattern' && path.at(-3)?.type === 'ObjectPattern') {
			const declarator = path.at(-4);
			if (
				declarator?.type === 'VariableDeclarator' &&
				get_rune(declarator.init, scope) === '$props'
			) {
				return;
			}
		}
		e.bindable_invalid_location(node);
	}
 
	if (
		rune === '$state' ||
		rune === '$state.frozen' ||
		rune === '$derived' ||
		rune === '$derived.by'
	) {
		if (parent.type === 'VariableDeclarator') return;
		if (parent.type === 'PropertyDefinition' && !parent.static && !parent.computed) return;
		e.state_invalid_placement(node, rune);
	}
 
	if (rune === '$effect' || rune === '$effect.pre') {
		if (parent.type !== 'ExpressionStatement') {
			e.effect_invalid_placement(node);
		}
 
		if (node.arguments.length !== 1) {
			e.rune_invalid_arguments_length(node, rune, 'exactly one argument');
		}
	}
 
	if (rune === '$effect.tracking') {
		if (node.arguments.length !== 0) {
			e.rune_invalid_arguments(node, rune);
		}
	}
 
	if (rune === '$effect.root') {
		if (node.arguments.length !== 1) {
			e.rune_invalid_arguments_length(node, rune, 'exactly one argument');
		}
	}
 
	if (rune === '$inspect') {
		if (node.arguments.length < 1) {
			e.rune_invalid_arguments_length(node, rune, 'one or more arguments');
		}
	}
 
	if (rune === '$inspect().with') {
		if (node.arguments.length !== 1) {
			e.rune_invalid_arguments_length(node, rune, 'exactly one argument');
		}
	}
 
	if (rune === '$state.snapshot') {
		if (node.arguments.length !== 1) {
			e.rune_invalid_arguments_length(node, rune, 'exactly one argument');
		}
	}
 
	if (rune === '$state.is') {
		if (node.arguments.length !== 2) {
			e.rune_invalid_arguments_length(node, rune, 'exactly two arguments');
		}
	}
}
 
/**
 * @param {VariableDeclarator} node
 * @param {AnalysisState} state
 */
function ensure_no_module_import_conflict(node, state) {
	const ids = extract_identifiers(node.id);
	for (const id of ids) {
		if (
			state.scope === state.analysis.instance.scope &&
			state.analysis.module.scope.get(id.name)?.declaration_kind === 'import'
		) {
			e.declaration_duplicate_module_import(node.id);
		}
	}
}
 
/**
 * @type {Visitors}
 */
export const validation_runes_js = {
	ImportDeclaration(node) {
		if (typeof node.source.value === 'string' && node.source.value.startsWith('svelte/internal')) {
			e.import_svelte_internal_forbidden(node);
		}
	},
	ExportSpecifier(node, { state }) {
		validate_export(node, state.scope, node.local.name);
	},
	ExportNamedDeclaration(node, { state, next }) {
		if (node.declaration?.type !== 'VariableDeclaration') return;
 
		// visit children, so bindings are correctly initialised
		next();
 
		for (const declarator of node.declaration.declarations) {
			for (const id of extract_identifiers(declarator.id)) {
				validate_export(node, state.scope, id.name);
			}
		}
	},
	CallExpression(node, { state, path }) {
		if (get_rune(node, state.scope) === '$host') {
			e.host_invalid_placement(node);
		}
		validate_call_expression(node, state.scope, path);
	},
	VariableDeclarator(node, { state }) {
		const init = node.init;
		const rune = get_rune(init, state.scope);
 
		if (rune === null) return;
 
		const args = /** @type {CallExpression} */ (init).arguments;
 
		if ((rune === '$derived' || rune === '$derived.by') && args.length !== 1) {
			e.rune_invalid_arguments_length(node, rune, 'exactly one argument');
		} else if (rune === '$state' && args.length > 1) {
			e.rune_invalid_arguments_length(node, rune, 'zero or one arguments');
		} else if (rune === '$props') {
			e.props_invalid_placement(node);
		} else if (rune === '$bindable') {
			e.bindable_invalid_location(node);
		}
	},
	AssignmentExpression(node, { state }) {
		validate_assignment(node, node.left, state);
	},
	UpdateExpression(node, { state }) {
		validate_assignment(node, node.argument, state);
	},
	ClassBody(node, context) {
		/** @type {string[]} */
		const private_derived_state = [];
 
		for (const definition of node.body) {
			if (
				definition.type === 'PropertyDefinition' &&
				definition.key.type === 'PrivateIdentifier' &&
				definition.value?.type === 'CallExpression'
			) {
				const rune = get_rune(definition.value, context.state.scope);
				if (rune === '$derived' || rune === '$derived.by') {
					private_derived_state.push(definition.key.name);
				}
			}
		}
 
		context.next({
			...context.state,
			private_derived_state
		});
	},
	ClassDeclaration(node, context) {
		// In modules, we allow top-level module scope only, in components, we allow the component scope,
		// which is function_depth of 1. With the exception of `new class` which is also not allowed at
		// component scope level either.
		const allowed_depth = context.state.ast_type === 'module' ? 0 : 1;
 
		if (context.state.scope.function_depth > allowed_depth) {
			w.perf_avoid_nested_class(node);
		}
	},
	NewExpression(node, context) {
		if (node.callee.type === 'ClassExpression' && context.state.scope.function_depth > 0) {
			w.perf_avoid_inline_class(node);
		}
	},
	Identifier(node, { path, state }) {
		let i = path.length;
		let parent = /** @type {Expression} */ (path[--i]);
 
		if (
			Runes.includes(/** @type {Runes[number]} */ (node.name)) &&
			is_reference(node, parent) &&
			state.scope.get(node.name) === null &&
			state.scope.get(node.name.slice(1)) === null
		) {
			/** @type {Expression} */
			let current = node;
			let name = node.name;
 
			while (parent.type === 'MemberExpression') {
				if (parent.computed) e.rune_invalid_computed_property(parent);
				name += `.${/** @type {Identifier} */ (parent.property).name}`;
 
				current = parent;
				parent = /** @type {Expression} */ (path[--i]);
 
				if (!Runes.includes(/** @type {Runes[number]} */ (name))) {
					if (name === '$effect.active') {
						e.rune_renamed(parent, '$effect.active', '$effect.tracking');
					}
 
					e.rune_invalid_name(parent, name);
				}
			}
 
			if (parent.type !== 'CallExpression') {
				e.rune_missing_parentheses(current);
			}
		}
	}
};
 
/**
 * @param {NodeLike} node
 * @param {Pattern | Expression} argument
 * @param {Scope} scope
 * @param {boolean} is_binding
 */
function validate_no_const_assignment(node, argument, scope, is_binding) {
	if (argument.type === 'ArrayPattern') {
		for (const element of argument.elements) {
			if (element) {
				validate_no_const_assignment(node, element, scope, is_binding);
			}
		}
	} else if (argument.type === 'ObjectPattern') {
		for (const element of argument.properties) {
			if (element.type === 'Property') {
				validate_no_const_assignment(node, element.value, scope, is_binding);
			}
		}
	} else if (argument.type === 'Identifier') {
		const binding = scope.get(argument.name);
		if (binding?.declaration_kind === 'const' && binding.kind !== 'each') {
			// e.invalid_const_assignment(
			// 	node,
			// 	is_binding,
			// 	// This takes advantage of the fact that we don't assign initial for let directives and then/catch variables.
			// 	// If we start doing that, we need another property on the binding to differentiate, or give up on the more precise error message.
			// 	binding.kind !== 'state' &&
			// 		binding.kind !== 'frozen_state' &&
			// 		(binding.kind !== 'normal' || !binding.initial)
			// );
 
			// TODO have a more specific error message for assignments to things like `{:then foo}`
			const thing = 'constant';
 
			if (is_binding) {
				e.constant_binding(node, thing);
			} else {
				e.constant_assignment(node, thing);
			}
		}
	}
}
 
/**
 * Validates that the opening of a control flow block is `{` immediately followed by the expected character.
 * In legacy mode whitespace is allowed inbetween. TODO remove once legacy mode is gone and move this into parser instead.
 * @param {{start: number; end: number}} node
 * @param {AnalysisState} state
 * @param {string} expected
 */
function validate_opening_tag(node, state, expected) {
	if (state.analysis.source[node.start + 1] !== expected) {
		// avoid a sea of red and only mark the first few characters
		e.block_unexpected_character({ start: node.start, end: node.start + 5 }, expected);
	}
}
 
/**
 * @param {AssignmentExpression | UpdateExpression} node
 * @param {Pattern | Expression} argument
 * @param {AnalysisState} state
 */
function validate_assignment(node, argument, state) {
	validate_no_const_assignment(node, argument, state.scope, false);
 
	if (argument.type === 'Identifier') {
		const binding = state.scope.get(argument.name);
 
		if (state.analysis.runes) {
			if (binding?.kind === 'derived') {
				e.constant_assignment(node, 'derived state');
			}
 
			if (binding?.kind === 'each') {
				e.each_item_invalid_assignment(node);
			}
		}
 
		if (binding?.kind === 'snippet') {
			e.snippet_parameter_assignment(node);
		}
	}
 
	let object = /** @type {Expression | Super} */ (argument);
 
	/** @type {Expression | PrivateIdentifier | null} */
	let property = null;
 
	while (object.type === 'MemberExpression') {
		property = object.property;
		object = object.object;
	}
 
	if (object.type === 'ThisExpression' && property?.type === 'PrivateIdentifier') {
		if (state.private_derived_state.includes(property.name)) {
			e.constant_assignment(node, 'derived state');
		}
	}
}
 
export const validation_runes = merge(validation, a11y_validators, {
	ImportDeclaration(node) {
		if (typeof node.source.value === 'string' && node.source.value.startsWith('svelte/internal')) {
			e.import_svelte_internal_forbidden(node);
		}
	},
	LabeledStatement(node, { path }) {
		if (node.label.name !== '$' || path.at(-1)?.type !== 'Program') return;
		e.legacy_reactive_statement_invalid(node);
	},
	ExportNamedDeclaration(node, { state, next }) {
		if (state.ast_type === 'module') {
			if (node.declaration?.type !== 'VariableDeclaration') return;
 
			// visit children, so bindings are correctly initialised
			next();
 
			for (const declarator of node.declaration.declarations) {
				for (const id of extract_identifiers(declarator.id)) {
					validate_export(node, state.scope, id.name);
				}
			}
		} else {
			if (node.declaration?.type !== 'VariableDeclaration') return;
			if (node.declaration.kind !== 'let') return;
			if (state.analysis.instance.scope !== state.scope) return;
			e.legacy_export_invalid(node);
		}
	},
	ExportSpecifier(node, { state }) {
		if (state.ast_type === 'module') {
			validate_export(node, state.scope, node.local.name);
		}
	},
	CallExpression(node, { state, path }) {
		const rune = get_rune(node, state.scope);
		if (rune === '$bindable' && node.arguments.length > 1) {
			e.rune_invalid_arguments_length(node, '$bindable', 'zero or one arguments');
		} else if (rune === '$host') {
			if (node.arguments.length > 0) {
				e.rune_invalid_arguments(node, '$host');
			} else if (state.ast_type === 'module' || !state.analysis.custom_element) {
				e.host_invalid_placement(node);
			}
		}
 
		validate_call_expression(node, state.scope, path);
	},
	EachBlock(node, { next, state }) {
		validate_opening_tag(node, state, '#');
 
		const context = node.context;
		if (
			context.type === 'Identifier' &&
			(context.name === '$state' || context.name === '$derived')
		) {
			e.state_invalid_placement(node, context.name);
		}
		next({ ...state });
	},
	IfBlock(node, { state, path }) {
		const parent = path.at(-1);
		const expected =
			path.at(-2)?.type === 'IfBlock' && parent?.type === 'Fragment' && parent.nodes.length === 1
				? ':'
				: '#';
		validate_opening_tag(node, state, expected);
	},
	AwaitBlock(node, { state }) {
		validate_opening_tag(node, state, '#');
 
		if (node.value) {
			const start = /** @type {number} */ (node.value.start);
			const match = state.analysis.source.substring(start - 10, start).match(/{(\s*):then\s+$/);
			if (match && match[1] !== '') {
				e.block_unexpected_character({ start: start - 10, end: start }, ':');
			}
		}
 
		if (node.error) {
			const start = /** @type {number} */ (node.error.start);
			const match = state.analysis.source.substring(start - 10, start).match(/{(\s*):catch\s+$/);
			if (match && match[1] !== '') {
				e.block_unexpected_character({ start: start - 10, end: start }, ':');
			}
		}
	},
	KeyBlock(node, { state }) {
		validate_opening_tag(node, state, '#');
	},
	SnippetBlock(node, { state }) {
		validate_opening_tag(node, state, '#');
	},
	ConstTag(node, { state }) {
		validate_opening_tag(node, state, '@');
	},
	HtmlTag(node, { state }) {
		validate_opening_tag(node, state, '@');
	},
	DebugTag(node, { state }) {
		validate_opening_tag(node, state, '@');
	},
	RenderTag(node, { state }) {
		validate_opening_tag(node, state, '@');
	},
	VariableDeclarator(node, { state }) {
		ensure_no_module_import_conflict(node, state);
 
		const init = node.init;
		const rune = get_rune(init, state.scope);
 
		if (rune === null) return;
 
		const args = /** @type {CallExpression} */ (init).arguments;
 
		// TODO some of this is duplicated with above, seems off
		if ((rune === '$derived' || rune === '$derived.by') && args.length !== 1) {
			e.rune_invalid_arguments_length(node, rune, 'exactly one argument');
		} else if (rune === '$state' && args.length > 1) {
			e.rune_invalid_arguments_length(node, rune, 'zero or one arguments');
		} else if (rune === '$props') {
			if (state.has_props_rune) {
				e.props_duplicate(node);
			}
 
			state.has_props_rune = true;
 
			if (args.length > 0) {
				e.rune_invalid_arguments(node, rune);
			}
 
			if (node.id.type !== 'ObjectPattern' && node.id.type !== 'Identifier') {
				e.props_invalid_identifier(node);
			}
 
			if (state.scope !== state.analysis.instance.scope) {
				e.props_invalid_placement(node);
			}
 
			if (node.id.type === 'ObjectPattern') {
				for (const property of node.id.properties) {
					if (property.type === 'Property') {
						if (property.computed) {
							e.props_invalid_pattern(property);
						}
 
						if (property.key.type === 'Identifier' && property.key.name.startsWith('$$')) {
							e.props_illegal_name(property);
						}
 
						const value =
							property.value.type === 'AssignmentPattern' ? property.value.left : property.value;
 
						if (value.type !== 'Identifier') {
							e.props_invalid_pattern(property);
						}
					}
				}
			}
		}
 
		if (rune === '$derived') {
			const arg = args[0];
			if (
				arg.type === 'CallExpression' &&
				(arg.callee.type === 'ArrowFunctionExpression' || arg.callee.type === 'FunctionExpression')
			) {
				w.derived_iife(node);
			}
		}
	},
	SlotElement(node, { state }) {
		if (!state.analysis.custom_element) {
			w.slot_element_deprecated(node);
		}
	},
	OnDirective(node, { path }) {
		const parent_type = path.at(-1)?.type;
		// Don't warn on component events; these might not be under the author's control so the warning would be unactionable
		if (parent_type === 'RegularElement' || parent_type === 'SvelteElement') {
			w.event_directive_deprecated(node, node.name);
		}
	},
	// TODO this is a code smell. need to refactor this stuff
	ClassBody: validation_runes_js.ClassBody,
	ClassDeclaration: validation_runes_js.ClassDeclaration,
	Identifier: validation_runes_js.Identifier,
	NewExpression: validation_runes_js.NewExpression
});