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

95.9% Statements 1241/1294
94.78% Branches 582/614
96.49% Functions 55/57
95.86% Lines 1228/1281

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 12822x 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 776x 776x 10x 776x 776x 776x 2x 2x 776x 2x 2x 2x 2x 2x 1300x 1300x 1423x 1423x 1423x 1423x 1423x 262x 1423x 4x 4x 1419x 1419x 1423x 60x 1423x 1x 1x 1418x 1423x 908x 183x 183x 183x 178x 178x 2x 2x 2x 2x 1x 2x 2x 178x 183x 906x 906x 906x 908x 21x 21x 908x 1423x 1291x 1291x 1291x 1291x 1291x 1291x 1291x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 8646x 8646x 8646x 8646x 8646x 8646x 8646x 8646x 8646x 8646x 6353x 3989x 3989x 3989x 593x 593x 593x 461x 461x 2x 2x 2x 2x 1x 2x 2x 461x 593x 3987x 3989x 5x 5x 3982x 3989x 418x 1x 1x 417x 417x 417x 418x 418x 20x 418x 2x 2x 418x 3981x 3989x 118x 118x 118x 3971x 3989x     3971x 3971x 3989x 2x 2x 3971x 3971x 6353x 22x 22x 1x 22x 1x 21x 20x 20x 66x 66x 64x 20x 20x 1x 1x 19x 22x 1x 22x 18x 18x 2364x 284x 284x 284x 284x 284x 7x 7x 7x 7x 3x 7x 4x 4x 7x 277x 284x 284x 2342x 973x 973x 973x 36x 1x 1x 1x 36x 2x 33x 11x 11x 36x 2x 2x 2x 2x 2x 2x 36x 970x 6353x 8614x 2x 2x 2x 2x 4877x 4877x 4877x 4877x 4877x 4x 4877x 2x 2x 4877x 2x 2x 2x 2x 2x 2x 199x 199x 199x 199x 199x 647x 647x 647x 417x 417x 417x 417x 417x 647x 196x 196x 647x 199x 199x 196x 1x 1x 195x 195x 196x 196x 8x 196x 187x 6x 6x 181x 181x 181x 187x 5x 5x 176x 176x 176x 187x 1x 4x 2x 2x 2x 4x 2x 1x 1x 2x 1x 1x 1x   187x 199x 1x 1x 199x 2x 2x 2x 2x 2x 3743x 3743x 2229x 2229x 3743x 8x 8x 3743x 2x 2x 2x 2x 2x 2x 2147x 2x 2x 925x 925x 925x 925x 925x 925x     919x 919x 919x 919x 925x 925x 925x 514x 514x 514x 512x 512x 512x 512x 512x 512x 512x 514x 2x 2x 512x 514x 1x 1x 511x 514x 1x 1x 510x 514x 1x 1x 514x 914x 925x 126x     126x 914x 925x 5x 5x 914x 914x 914x 914x 925x 925x 925x 925x 258x 925x 656x 1x 1x 655x 656x 652x 652x 3x 3x 3x 3x 3x 3x 649x 652x 2x 2x 94x 94x 82x 82x 94x 2x 2x 2x 2x 2x 2x 2x 2x 2x 647x 652x 362x 362x 362x 362x 3x 1x 1x 2x 2x 359x 362x 1x 1x 358x 362x     362x 643x 652x 68x 68x 82x 82x 82x 6x 68x 68x 1x 1x 68x 642x 652x 1x 1x 1x 1x 1x 1x 641x 651x 20x 20x 20x 20x 1x 20x 1x 1x 20x 656x 3x 3x 1x 1x 1x 1x 1x 2x 2x 656x 2x 2x 3x 2x 2x 263x 263x 263x 263x 263x 263x 263x 263x 263x 263x 263x 16x 263x 263x 2x 2x 2x 2x 1619x 37x 43x 43x 43x 42x 43x 1x 1x 43x 43x 36x 2x 2x 216x 216x 216x 216x 216x 216x 216x 216x 216x 216x 216x 1x 1x 2x 2x 8507x 24x 5x 1x 1x 5x 23x 8506x 8506x 8506x 8507x 8507x 2x 8507x 1x 1x 8506x 8506x 8506x 8507x 1140x     1140x 8474x 8474x 8474x 8507x 15x 15x 20x 1x 1x 20x 14x 8473x 8507x 1482x 1482x 2075x 2075x 2075x 1x 2075x 1x 1x 2075x 1481x 8472x 8507x 5068x 5068x 9691x 1x 1x 9691x 5067x 8471x 8471x 8507x 8507x 8507x 100x 8507x 52x 52x 8471x 8471x 8471x 8471x 8471x 2x 2x 109x 109x 109x 109x 39x     39x 109x 109x 109x 109x 109x 13x 109x 1x 1x 2x 2x 625x 625x 2x 2x 930x 930x 2x 2x 171x 171x 171x 2x 2x 30x 2x 2x 90x 90x 90x 90x 90x 90x 90x 20x 20x 88x 16x 16x 1x 1x 16x 90x 1x 1x 19x 87x 10x 10x 86x 90x 1x 90x 9x 9x 9x 9x 9x 1x 1x 9x 2x 2x 76x 1x 1x 2x 2x 54x 54x 1x 1x 2x 2x 140x 140x 140x 140x 140x 2x 2x 69x 69x 2x 2x 67x 67x 109x 60x 60x 60x 109x     109x 2x 2x 479x 346x 336x 163x 1x 1x 162x 163x 1x 1x 163x 346x     346x 2x 2x 2x 2x 2x 24634x 24634x 3788x     3788x 2x 2x 33x 33x 1x 1x 32x 32x 32x 1x 1x 2x 2x 346x 2x 2x 6808x 6719x 4059x     4059x 6808x 2x 2x 2x 2x 6057x 6057x 6057x 347x 347x 347x 5655x 277x 6057x 346x 346x 1x 6057x     2x 2x 1441x 1441x 1441x 2x 2x 317x 317x 313x 313x 317x 4x 4x 2x 2x 100x 100x 2x 2x 2x 2x 2x 2x 2x 2x 24x 24x 24x 24x 24x 1x 1x 23x 24x 3x 3x 24x 2x 2x 2x 2x 2x 2x 2x 2419x 2419x 2419x 1405x 1405x 1405x 2419x 191x     1214x 2419x 38x 38x 38x 38x 38x 38x 36x 36x 38x 2x 2x 1176x 1176x 2419x 2419x 2419x 209x 2419x 981x 981x 5x 5x 195x 2419x 146x 2x 2x 144x 144x 2x 2x 146x 191x 2355x 8x     8x 191x 2355x 6x     6x 191x 2355x 22x     22x 191x 2355x 4x     4x 191x 2313x 5x 1x 1x 5x 190x 2310x 4x     4x 2419x 2x 2x 2x 2x 2x 7519x 7519x 7519x 8009x 8009x 7255x 8009x 1x 1x 8009x 7518x 2x 2x 2x 2x 2x 2x 1x 1x 1x 2x 2x   2x 2x 17x 15x 15x 15x 15x 15x 15x 15x 15x 12x 2x 2x 26x 1x 1x 25x 2x 2x 23x 23x 23x 23x 21x 21x 21x 23x 1x 23x 1x 20x 1x 19x     2x 2x 3x 2x 2x 4x 2x 2x 84x 84x 84x 84x 226x 226x 226x 36x 226x 26x 26x 4x 4x 26x 226x 84x 84x 84x 84x 84x 2x 2x 81x 81x 81x 81x 81x 81x 1x 1x 2x 2x 117x 2x 2x 117x 2x 2x 2x 2x 2x 2x 2x 2x 5312x 5312x 86x 190x 190x 190x 190x 5312x 92x 156x 156x 156x 156x 5226x 3781x 3781x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 6x 18x 12x 12x 18x 3781x 5312x 2x 2x 2x 2x 2x 2x 4041x 4041x 4041x 4041x 2812x 2812x 2812x 597x 2x 2x 595x 597x 1x 1x 597x 2809x 2812x 1x 1x 2812x 4025x 4025x 4025x 4025x 4025x 4025x 4032x 1159x 1159x 1159x 4025x 4041x 36x 2x 2x 36x 4041x 2x 2x 2x 311x 1x 1x 2x 2x 11907x 11907x 11907x 11907x 11907x 11907x 11907x 1372x 11907x 1334x 1334x 1334x 1334x 1334x 88x 88x 88x 88x 88x 88x 88x     88x 1334x 1334x 2x 2x 1334x 2x 2x     2x 2x 39x 15x 9x 9x 9x 9x 9x 9x 9x 9x 8x 39x 24x 7x 1x 1x 1x 2x 2x 11x     2x 2x 2396x 2396x 1x 2396x 1x   1x 1x 1x 1x 2394x 2394x 2x 2x 141x 141x 141x 135x 141x 1x 1x 140x 2x 2x 1462x 1462x 1462x 1462x 1462x 1462x 1052x 1052x 1052x 1052x 1462x 1x 1462x 1x 1051x 196x 1x 1x 195x 195x 195x 196x 1x 1x 194x 196x     194x 196x 1x 1x 193x 196x 293x 271x     271x 271x 271x 271x 271x     271x 293x 193x 1047x 1462x 96x 96x 96x 22x 96x     96x 2x 2x 15x 15x 15x 2x 2x 302x 302x 302x 284x 284x 2x 2x 2x 2x 2x 2x  
import is_reference from 'is-reference';
import {
	disallowed_paragraph_contents,
	interactive_elements,
	is_tag_valid_with_parent
} from '../../../constants.js';
import * as e from '../../errors.js';
import {
	extract_identifiers,
	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';
 
/** @param {import('#compiler').Attribute} attribute */
function validate_attribute(attribute) {
	if (attribute.value === true || attribute.value.length === 1) return;
 
	const is_quoted = attribute.value.at(-1)?.end !== attribute.end;
 
	if (!is_quoted) {
		e.attribute_unquoted_sequence(attribute);
	}
}
 
/**
 * @param {import('#compiler').Component | import('#compiler').SvelteComponent | import('#compiler').SvelteSelf} node
 * @param {import('zimmerframe').Context<import('#compiler').SvelteNode, import('./types.js').AnalysisState>} 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);
 
				if (is_expression_attribute(attribute)) {
					const expression = attribute.value[0].expression;
					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 {import('#compiler').RegularElement | import('#compiler').SvelteElement} node
 * @param {import('zimmerframe').Context<import('#compiler').SvelteNode, import('./types.js').AnalysisState>} context
 */
function validate_element(node, context) {
	let has_animate_directive = false;
 
	/** @type {import('#compiler').TransitionDirective | null} */
	let in_transition = null;
 
	/** @type {import('#compiler').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);
 
				if (is_expression) {
					const expression = attribute.value[0].expression;
					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 = attribute.value[0].expression;
				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 {import('#compiler').RegularElement | import('#compiler').SvelteElement | import('#compiler').Component | import('#compiler').SvelteComponent | import('#compiler').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 {import('#compiler').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 {import('#compiler').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 {import('zimmerframe').Context<import('#compiler').SvelteNode, import('./types.js').AnalysisState>} context
 * @param {import('#compiler').Attribute} attribute
 * @param {boolean} is_component
 */
function validate_slot_attribute(context, attribute, is_component = false) {
	let owner = undefined;
 
	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 !== context.path.at(-2)) {
				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 {import('#compiler').Fragment | null | undefined} node
 * @param {import('zimmerframe').Context<import('#compiler').SvelteNode, import('./types.js').AnalysisState>} 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 {import('zimmerframe').Visitors<import('#compiler').SvelteNode, import('./types.js').AnalysisState>}
 */
const validation = {
	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 {import('#compiler').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 {import('#compiler').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) {
			if (!is_tag_valid_with_parent(node.name, context.state.parent_element)) {
				e.node_invalid_placement(node, `<${node.name}>`, context.state.parent_element);
			}
		}
 
		// can't add form to interactive elements because those are also used by the parser
		// to check for the last auto-closing parent.
		if (node.name === 'form') {
			const path = context.path;
			for (let parent of path) {
				if (parent.type === 'RegularElement' && parent.name === 'form') {
					e.node_invalid_placement(node, `<${node.name}>`, parent.name);
				}
			}
		}
 
		if (interactive_elements.has(node.name)) {
			const path = context.path;
			for (let parent of path) {
				if (
					parent.type === 'RegularElement' &&
					parent.name === node.name &&
					interactive_elements.has(parent.name)
				) {
					e.node_invalid_placement(node, `<${node.name}>`, parent.name);
				}
			}
		}
 
		if (disallowed_paragraph_contents.includes(node.name)) {
			const path = context.path;
			for (let parent of path) {
				if (parent.type === 'RegularElement' && parent.name === 'p') {
					e.node_invalid_placement(node, `<${node.name}>`, parent.name);
				}
			}
		}
 
		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) {
		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);
			}
		}
 
		const callee = unwrap_optional(node.expression).callee;
		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);
 
		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 {import('#compiler').SvelteNode} */ (path.at(-1)).type !== 'Program')
		) {
			w.reactive_declaration_invalid_placement(node);
		}
	},
	UpdateExpression(node, { state }) {
		validate_assignment(node, node.argument, state);
	}
});
 
/**
 *
 * @param {import('estree').Node} node
 * @param {import('../scope').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 {import('estree').CallExpression} node
 * @param {Scope} scope
 * @param {import('#compiler').SvelteNode[]} path
 * @returns
 */
function validate_call_expression(node, scope, path) {
	const rune = get_rune(node, scope);
	if (rune === null) return;
 
	const parent = /** @type {import('#compiler').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.active') {
		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 {import('estree').VariableDeclarator} node
 * @param {import('./types.js').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 {import('zimmerframe').Visitors<import('#compiler').SvelteNode, import('./types.js').AnalysisState>}
 */
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 {import('estree').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);
		}
	}
};
 
/**
 * @param {import('../../errors.js').NodeLike} node
 * @param {import('estree').Pattern | import('estree').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);
			}
		}
	}
}
 
/**
 * @param {import('estree').AssignmentExpression | import('estree').UpdateExpression} node
 * @param {import('estree').Pattern | import('estree').Expression} argument
 * @param {import('./types.js').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 {import('estree').Expression | import('estree').Super} */ (argument);
 
	/** @type {import('estree').Expression | import('estree').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);
		}
	},
	Identifier(node, { path, state }) {
		let i = path.length;
		let parent = /** @type {import('estree').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 {import('estree').Expression} */
			let current = node;
			let name = node.name;
 
			while (parent.type === 'MemberExpression') {
				if (parent.computed) e.rune_invalid_computed_property(parent);
				name += `.${/** @type {import('estree').Identifier} */ (parent.property).name}`;
 
				current = parent;
				parent = /** @type {import('estree').Expression} */ (path[--i]);
 
				if (!Runes.includes(/** @type {Runes[number]} */ (name))) {
					e.rune_invalid_name(parent, name);
				}
			}
 
			if (parent.type !== 'CallExpression') {
				e.rune_missing_parentheses(current);
			}
		}
	},
	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 }) {
		const context = node.context;
		if (
			context.type === 'Identifier' &&
			(context.name === '$state' || context.name === '$derived')
		) {
			e.state_invalid_placement(node, context.name);
		}
		next({ ...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 {import('estree').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') {
				e.props_invalid_identifier(node);
			}
 
			if (state.scope !== state.analysis.instance.scope) {
				e.props_invalid_placement(node);
			}
 
			for (const property of node.id.properties) {
				if (property.type === 'Property') {
					if (property.computed) {
						e.props_invalid_pattern(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,
	NewExpression: validation_runes_js.NewExpression
});