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

95.38% Statements 1179/1236
94.11% Branches 544/578
96.36% Functions 53/55
95.34% Lines 1167/1224

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 12252x 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 1237x 1237x 1357x 1357x 1357x 1357x 1357x 258x 1357x 4x 4x 1353x 1353x 1357x 41x 1357x 1x 1x 1352x 1357x 877x 157x 157x 2x 2x 2x 2x 1x 2x 2x 157x 876x 876x 876x 877x 17x 17x 877x 1357x 1229x 1229x 1229x 1229x 1229x 1229x 1229x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 8343x 8343x 8343x 8343x 8343x 8343x 6098x 3826x 3826x 3826x 376x 376x 2x 2x 2x 2x 1x 2x 2x 376x 3825x 3826x 5x 5x 3820x 3826x 348x 1x 1x 347x 347x 347x 348x 348x 18x 348x 2x 2x 2x 2x 2x 2x 2x 2x 348x 3819x 3826x 116x 116x 116x 3809x 3826x     3809x 3809x 3826x 2x 2x 2x 2x 2x 2x 2x 2x 2x 3809x 3809x 6098x 22x 22x 1x 22x 1x 21x 20x 20x 66x 66x 64x 20x 20x 1x 1x 19x 22x 1x 22x 18x 18x 2272x 284x 7x 7x 7x 7x 7x 7x 7x 7x 7x 277x 284x 284x 2250x 937x 937x 937x 29x 1x 1x 29x 2x 26x 8x 8x 29x 2x 2x 2x 2x 2x 2x 2x 29x 934x 6098x 8312x 2x 2x 2x 2x 2x 4685x 4685x 4685x 4685x 4685x 4x 4685x 2x 2x 4685x 2x 2x 2x 2x 2x 193x 193x 193x 193x 193x 629x 629x 629x 407x 407x 407x 407x 407x 629x 192x 192x 629x 193x 193x 192x 1x 1x 191x 191x 192x 192x 8x 192x 183x 6x 6x 177x 177x 177x 183x 5x 5x 172x 172x 172x 183x 1x 4x 2x 2x 2x 4x 2x 1x 1x 2x 1x 1x 1x   183x 193x 1x 1x 193x 2x 2x 2x 2x 2x 3681x 3681x 2194x 2194x 3681x 8x 8x 3681x 2x 2x 2x 2x 2x 2x 2023x 2x 2x 900x 900x 900x 900x 900x 900x     894x 894x 894x 894x 900x 900x 900x 496x 496x 496x 494x 494x 494x 494x 494x 494x 494x 496x 2x 2x 494x 496x 1x 1x 493x 496x 1x 1x 492x 496x 1x 1x 496x 889x 900x 126x     126x 889x 900x 5x 5x 5x 5x 5x 5x 5x 5x 889x 889x 889x 889x 900x 900x 900x 900x 254x 900x 635x 1x 1x 1x 1x 1x 1x 1x 1x 634x 635x 631x 631x 3x 3x 3x 3x 3x 3x 3x 628x 631x 352x 352x 352x 352x 3x 1x 1x 2x 2x 349x 352x 1x 1x 348x 352x     352x 624x 630x 68x 68x 82x 82x 82x 6x 68x 68x 1x 1x 68x 623x 631x 1x 1x 1x 1x 1x 1x 1x 622x 630x 20x 20x 20x 20x 1x 20x 1x 1x 20x 635x 3x 3x 1x 1x 1x 1x 1x 2x 2x 635x 2x 2x 3x 2x 2x 257x 257x 257x 257x 257x 257x 257x 257x 257x 257x 257x 14x 257x 257x 2x 2x 2x 2x 1658x 35x 39x 39x 39x 38x 39x 1x 1x 39x 39x 34x 2x 2x 214x 214x 214x 214x 214x 214x 214x 214x 214x 214x 214x 1x 1x 2x 2x 8216x 24x 5x 1x 1x 5x 23x 8215x 8215x 8215x 8216x 8216x 2x 8216x 1x 1x 1x 1x 1x 1x 1x 1x 8215x 8215x 8215x 8216x 1111x     1111x 8184x 8216x 1367x 1367x 1948x 1948x 1948x 1x 1948x 1x 1x 1948x 1366x 8183x 8216x 2921x 2921x 5358x 1x 1x 5358x 2920x 8182x 8182x 8216x 8216x 8216x 98x 8216x 52x 52x 52x 52x 52x 52x 52x 52x 8182x 8182x 8182x 8182x 8182x 2x 2x 90x 90x 90x 90x 38x     38x 90x 90x 90x 90x 90x 9x 90x 1x 1x 89x 89x 125x 125x 125x 125x   125x 89x 90x               2x 2x 605x 605x 2x 2x 923x 923x 2x 2x 171x 171x 171x 2x 2x 28x 2x 2x 84x 84x 84x 10x 10x 10x 84x 10x 82x 84x 1x 84x 9x 9x 9x 9x 9x 1x 1x 9x 2x 2x 60x 1x 1x 2x 2x 54x 54x 1x 1x 2x 2x 128x 128x 128x 128x 128x 2x 2x 69x 69x 2x 2x 67x 67x 109x 60x 60x 60x 109x     109x 2x 2x 468x 337x 327x 157x 1x 1x 156x 157x 1x 1x 157x 337x     337x 2x 2x 2x 2x 2x 23649x 23649x 3658x     3658x 2x 2x 33x 33x 1x 1x 32x 32x 32x 1x 1x 2x 2x 318x 2x 2x 6568x 6482x 3996x     3996x 6568x 2x 2x 2x 2x 6021x 6021x 6021x 347x 347x 347x 5637x 277x 6021x 346x 346x 1x 6021x     2x 2x 1410x 1410x 1410x 2x 2x 291x 291x 287x 287x 291x 2x 2x 2x 2x 92x 92x 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 2117x 2117x 2117x 1234x 1234x 1234x 2117x 173x     1061x 2117x 36x 36x 36x 36x 36x 36x 34x 34x 36x 2x 2x 1025x 1025x 2117x 2117x 2117x 175x 2117x 856x 856x 5x 5x 169x 2117x 134x 2x 2x 132x 132x 2x 2x 134x 165x 2056x 6x     6x 165x 2056x 6x     6x 165x 2056x 16x     16x 165x 2056x 4x     4x 165x 2013x 3x 1x 1x 3x 2117x 2x 2x 2x 2x 2x 7299x 7299x 7299x 7777x 7777x 7061x 7777x 1x 1x 7777x 7298x 2x 2x 2x 2x 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 78x 78x 78x 78x 212x 212x 212x 34x 212x 24x 24x 4x 4x 24x 212x 78x 78x 78x 78x 78x 2x 2x 75x 75x 75x 75x 75x 75x 1x 1x 2x 2x 111x 2x 2x 111x 2x 2x 2x 2x 2x 2x 2x 2x 5092x 5092x 86x 190x 190x 190x 190x 5092x 88x 152x 152x 152x 152x 5006x 3615x 3615x 18x 18x 18x 18x 18x 18x 18x 18x 15x 18x 18x 3615x 5092x 2x 2x 2x 2x 2x 2x 3850x 3850x 3850x 3850x 2673x 2673x 2673x 508x 2x 2x 506x 508x 1x 1x 508x 2670x 2673x 1x 1x 2673x 3834x 3834x 3834x 3834x 3834x 3834x 3841x 1103x 1103x 1103x 3834x 3850x 36x 2x 2x 36x 3850x 2x 2x 2x     2x 2x 37x 13x 9x 9x 9x 9x 9x 9x 9x 9x 8x 37x 24x 8x 2x 1x 1x 2x 2x 10x     2x 2x 2094x 2094x 1x 2094x 1x   1x 1x 1x 1x 2092x 2092x 2x 2x 135x 135x 135x 129x 135x 1x 1x 134x 2x 2x 1278x 1278x 1278x 1278x 1278x 1278x 368x 1x 1x 368x 368x 910x 910x 910x 910x 1278x 1x 1278x 1x 909x 177x 1x 1x 176x 176x 176x 177x 1x 1x 175x 177x     175x 177x 1x 1x 174x 177x 262x 246x     246x 246x 246x 246x 246x     246x 262x 174x 905x 1278x 80x 80x 80x 22x 80x     80x 2x 2x 141x 141x 141x 1x 141x 1x 1x 2x 2x 15x 15x 15x 2x 2x 290x 290x 290x 287x 287x 2x 2x 2x 2x 2x 2x  
import {
	disallowed_paragraph_contents,
	interactive_elements,
	is_tag_valid_with_parent
} from '../../../constants.js';
import { error } from '../../errors.js';
import {
	extract_identifiers,
	get_parent,
	is_expression_attribute,
	is_text_attribute,
	object,
	unwrap_optional
} from '../../utils/ast.js';
import { warn } from '../../warnings.js';
import fuzzymatch from '../1-parse/utils/fuzzymatch.js';
import { binding_properties } from '../bindings.js';
import {
	ContentEditableBindings,
	EventModifiers,
	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').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'
		) {
			error(attribute, 'invalid-component-directive');
		}
 
		if (
			attribute.type === 'OnDirective' &&
			(attribute.modifiers.length > 1 || attribute.modifiers.some((m) => m !== 'once'))
		) {
			error(attribute, 'invalid-event-modifier');
		}
 
		if (attribute.type === 'Attribute') {
			if (context.state.analysis.runes && 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 === '{') error(expression, 'invalid-sequence-expression');
					}
				}
			}
 
			validate_attribute_name(attribute, context);
 
			if (attribute.name === 'slot') {
				validate_slot_attribute(context, attribute);
			}
		}
	}
 
	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;
	let has_in_transition = false;
	let has_out_transition = false;
 
	for (const attribute of node.attributes) {
		if (attribute.type === 'Attribute') {
			const is_expression = is_expression_attribute(attribute);
 
			if (context.state.analysis.runes && 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 === '{') error(expression, 'invalid-sequence-expression');
					}
				}
			}
 
			if (regex_illegal_attribute_character.test(attribute.name)) {
				error(attribute, 'invalid-attribute-name', attribute.name);
			}
 
			if (attribute.name.startsWith('on') && attribute.name.length > 2) {
				if (!is_expression) {
					error(attribute, 'invalid-event-attribute-value');
				}
 
				const value = attribute.value[0].expression;
				if (
					value.type === 'Identifier' &&
					value.name === attribute.name &&
					!context.state.scope.get(value.name)
				) {
					warn(
						context.state.analysis.warnings,
						attribute,
						context.path,
						'global-event-reference',
						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') {
				warn(context.state.analysis.warnings, attribute, context.path, 'avoid-is');
			}
 
			const correct_name = react_attributes.get(attribute.name);
			if (correct_name) {
				warn(
					context.state.analysis.warnings,
					attribute,
					context.path,
					'invalid-html-attribute',
					attribute.name,
					correct_name
				);
			}
 
			validate_attribute_name(attribute, context);
		} else if (attribute.type === 'AnimateDirective') {
			const parent = context.path.at(-2);
			if (parent?.type !== 'EachBlock') {
				error(attribute, 'invalid-animation', 'no-each');
			} else if (!parent.key) {
				error(attribute, 'invalid-animation', 'each-key');
			} else if (
				parent.body.nodes.filter(
					(n) =>
						n.type !== 'Comment' &&
						n.type !== 'ConstTag' &&
						(n.type !== 'Text' || n.data.trim() !== '')
				).length > 1
			) {
				error(attribute, 'invalid-animation', 'child');
			}
 
			if (has_animate_directive) {
				error(attribute, 'duplicate-animation');
			} else {
				has_animate_directive = true;
			}
		} else if (attribute.type === 'TransitionDirective') {
			if ((attribute.outro && has_out_transition) || (attribute.intro && has_in_transition)) {
				/** @param {boolean} _in @param {boolean} _out */
				const type = (_in, _out) => (_in && _out ? 'transition' : _in ? 'in' : 'out');
				error(
					attribute,
					'duplicate-transition',
					type(has_in_transition, has_out_transition),
					type(attribute.intro, attribute.outro)
				);
			}
 
			has_in_transition = has_in_transition || attribute.intro;
			has_out_transition = has_out_transition || attribute.outro;
		} else if (attribute.type === 'OnDirective') {
			let has_passive_modifier = false;
			let conflicting_passive_modifier = '';
			for (const modifier of attribute.modifiers) {
				if (!EventModifiers.includes(modifier)) {
					error(attribute, 'invalid-event-modifier', EventModifiers);
				}
				if (modifier === 'passive') {
					has_passive_modifier = true;
				} else if (modifier === 'nonpassive' || modifier === 'preventDefault') {
					conflicting_passive_modifier = modifier;
				}
				if (has_passive_modifier && conflicting_passive_modifier) {
					error(
						attribute,
						'invalid-event-modifier-combination',
						'passive',
						conflicting_passive_modifier
					);
				}
			}
		}
	}
}
 
/**
 * @param {import('#compiler').Attribute} attribute
 * @param {import('zimmerframe').Context<import('#compiler').SvelteNode, import('./types.js').AnalysisState>} context
 */
function validate_attribute_name(attribute, context) {
	if (
		attribute.name.includes(':') &&
		!attribute.name.startsWith('xmlns:') &&
		!attribute.name.startsWith('xlink:') &&
		!attribute.name.startsWith('xml:')
	) {
		warn(context.state.analysis.warnings, attribute, context.path, 'illegal-attribute-character');
	}
}
 
/**
 * @param {import('zimmerframe').Context<import('#compiler').SvelteNode, import('./types.js').AnalysisState>} context
 * @param {import('#compiler').Attribute} attribute
 */
function validate_slot_attribute(context, attribute) {
	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)) {
			error(attribute, 'invalid-slot-attribute');
		}
 
		if (
			owner.type === 'Component' ||
			owner.type === 'SvelteComponent' ||
			owner.type === 'SvelteSelf'
		) {
			if (owner !== context.path.at(-2)) {
				error(attribute, 'invalid-slot-placement');
			}
 
			const name = attribute.value[0].data;
 
			if (context.state.component_slots.has(name)) {
				error(attribute, 'duplicate-slot-name', 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;
						}
					}
 
					error(node, 'invalid-default-slot-content');
				}
			}
		}
	} else {
		error(attribute, 'invalid-slot-placement');
	}
}
 
/**
 * @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()) {
		warn(context.state.analysis.warnings, node.nodes[0], context.path, 'empty-block');
	}
}
 
/**
 * @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) {
			error(node, 'invalid-binding-expression');
		}
 
		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)
			) {
				error(node.expression, 'invalid-binding-value');
			}
 
			if (binding.kind === 'derived') {
				error(node.expression, 'invalid-derived-binding');
			}
 
			if (context.state.analysis.runes && binding.kind === 'each') {
				error(node, 'invalid-each-assignment');
			}
 
			if (binding.kind === 'snippet') {
				error(node, 'invalid-snippet-assignment');
			}
		}
 
		if (node.name === 'group') {
			if (!binding) {
				error(node, 'INTERNAL', 'Cannot find declaration for bind:group');
			}
		}
 
		if (binding?.kind === 'each' && binding.metadata?.inside_rest) {
			warn(
				context.state.analysis.warnings,
				binding.node,
				context.path,
				'invalid-rest-eachblock-binding',
				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') {
				error(
					node,
					'invalid-binding',
					node.name,
					undefined,
					'. 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)) {
					error(
						node,
						'invalid-binding',
						node.name,
						property.valid_elements.map((valid_element) => `<${valid_element}>`).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) {
							error(type, 'invalid-type-attribute');
						}
						return; // bind:value can handle dynamic `type` attributes
					}
 
					if (node.name === 'checked' && type?.value[0].data !== 'checkbox') {
						error(node, 'invalid-binding', node.name, '<input type="checkbox">');
					}
 
					if (node.name === 'files' && type?.value[0].data !== 'file') {
						error(node, 'invalid-binding', node.name, '<input type="file">');
					}
				}
 
				if (parent.name === 'select') {
					const multiple = parent.attributes.find(
						(a) =>
							a.type === 'Attribute' &&
							a.name === 'multiple' &&
							!is_text_attribute(a) &&
							a.value !== true
					);
					if (multiple) {
						error(multiple, 'invalid-multiple-attribute');
					}
				}
 
				if (node.name === 'offsetWidth' && SVGElements.includes(parent.name)) {
					error(
						node,
						'invalid-binding',
						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) {
						error(node, 'missing-contenteditable-attribute');
					} else if (!is_text_attribute(contenteditable) && contenteditable.value !== true) {
						error(contenteditable, 'dynamic-contenteditable-attribute');
					}
				}
			} 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)) {
						error(node, 'invalid-binding', node.name, undefined, ` (did you mean '${match}'?)`);
					}
				}
				error(node, 'invalid-binding', node.name);
			}
		}
	},
	ExportDefaultDeclaration(node) {
		error(node, 'default-export');
	},
	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')))
		) {
			error(node, 'invalid-const-placement');
		}
	},
	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'
					) {
						error(specifier, 'invalid-runes-mode-import', 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')
		) {
			error(node, 'invalid-let-directive-placement');
		}
	},
	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') {
					error(node, 'invalid-textarea-content');
				}
			}
		}
 
		const binding = context.state.scope.get(node.name);
		if (
			binding !== null &&
			binding.declaration_kind === 'import' &&
			binding.references.length === 0
		) {
			warn(
				context.state.analysis.warnings,
				node,
				context.path,
				'component-name-lowercase',
				node.name
			);
		}
 
		validate_element(node, context);
 
		if (context.state.parent_element) {
			if (!is_tag_valid_with_parent(node.name, context.state.parent_element)) {
				error(node, 'invalid-node-placement', `<${node.name}>`, context.state.parent_element);
			}
		}
 
		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)
				) {
					error(node, 'invalid-node-placement', `<${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') {
					error(node, 'invalid-node-placement', `<${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)
		) {
			warn(
				context.state.analysis.warnings,
				node,
				context.path,
				'invalid-self-closing-tag',
				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') {
				error(arg, 'invalid-render-spread-argument');
			}
		}
 
		const callee = unwrap_optional(node.expression).callee;
		if (
			callee.type === 'MemberExpression' &&
			callee.property.type === 'Identifier' &&
			['bind', 'apply', 'call'].includes(callee.property.name)
		) {
			error(node, 'invalid-render-call');
		}
 
		const is_inside_textarea = context.path.find((n) => {
			return (
				n.type === 'SvelteElement' &&
				n.name === 'svelte:element' &&
				n.tag.type === 'Literal' &&
				n.tag.value === 'textarea'
			);
		});
		if (is_inside_textarea) {
			error(
				node,
				'invalid-tag-placement',
				'inside <textarea> or <svelte:element this="textarea">',
				'render'
			);
		}
	},
	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);
 
		if (node.expression.name !== 'children') return;
 
		const { path } = context;
		const parent = path.at(-2);
		if (!parent) 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())
				)
			) {
				error(node, 'conflicting-children-snippet');
			}
		}
	},
	StyleDirective(node) {
		if (node.modifiers.length > 1 || (node.modifiers.length && node.modifiers[0] !== 'important')) {
			error(node, 'invalid-style-directive-modifier');
		}
	},
	SvelteHead(node) {
		const attribute = node.attributes[0];
		if (attribute) {
			error(attribute, 'illegal-svelte-head-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') {
			error(node, 'invalid-svelte-fragment-placement');
		}
 
		for (const attribute of node.attributes) {
			if (attribute.type === 'Attribute') {
				if (attribute.name === 'slot') {
					validate_slot_attribute(context, attribute);
				}
			} else if (attribute.type !== 'LetDirective') {
				error(attribute, 'invalid-svelte-fragment-attribute');
			}
		}
	},
	SlotElement(node) {
		for (const attribute of node.attributes) {
			if (attribute.type === 'Attribute') {
				if (attribute.name === 'name') {
					if (!is_text_attribute(attribute)) {
						error(attribute, 'invalid-slot-name', false);
					}
					const slot_name = attribute.value[0].data;
					if (slot_name === 'default') {
						error(attribute, 'invalid-slot-name', true);
					}
				}
			} else if (attribute.type !== 'SpreadAttribute' && attribute.type !== 'LetDirective') {
				error(attribute, 'invalid-slot-element-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)) {
				error(node, 'invalid-node-placement', 'Text node', context.state.parent_element);
			}
		}
	},
	TitleElement(node) {
		const attribute = node.attributes[0];
		if (attribute) {
			error(attribute, 'illegal-title-attribute');
		}
 
		const child = node.fragment.nodes.find((n) => n.type !== 'Text' && n.type !== 'ExpressionTag');
		if (child) {
			error(child, 'invalid-title-content');
		}
	},
	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)) {
				error(node, 'invalid-node-placement', '{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') {
			error(node.init, 'invalid-rune-usage', 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')
		) {
			warn(state.analysis.warnings, node, path, 'no-reactive-declaration');
		}
	},
	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') {
		error(node, 'invalid-derived-export');
	}
 
	if ((binding.kind === 'state' || binding.kind === 'frozen_state') && binding.reassigned) {
		error(node, 'invalid-state-export');
	}
}
 
/**
 * @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;
		error(node, 'invalid-props-location');
	}
 
	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;
			}
		}
		error(node, 'invalid-bindable-location');
	}
 
	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;
		error(node, 'invalid-state-location', rune);
	}
 
	if (rune === '$effect' || rune === '$effect.pre') {
		if (parent.type !== 'ExpressionStatement') {
			error(node, 'invalid-effect-location');
		}
 
		if (node.arguments.length !== 1) {
			error(node, 'invalid-rune-args-length', rune, [1]);
		}
	}
 
	if (rune === '$effect.active') {
		if (node.arguments.length !== 0) {
			error(node, 'invalid-rune-args-length', rune, [0]);
		}
	}
 
	if (rune === '$effect.root') {
		if (node.arguments.length !== 1) {
			error(node, 'invalid-rune-args-length', rune, [1]);
		}
	}
 
	if (rune === '$inspect') {
		if (node.arguments.length < 1) {
			error(node, 'invalid-rune-args-length', rune, [1, 'more']);
		}
	}
 
	if (rune === '$inspect().with') {
		if (node.arguments.length !== 1) {
			error(node, 'invalid-rune-args-length', rune, [1]);
		}
	}
 
	if (rune === '$state.snapshot') {
		if (node.arguments.length !== 1) {
			error(node, 'invalid-rune-args-length', rune, [1]);
		}
	}
}
 
/**
 * @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'
		) {
			error(node.id, 'illegal-variable-declaration');
		}
	}
}
 
/**
 * @type {import('zimmerframe').Visitors<import('#compiler').SvelteNode, import('./types.js').AnalysisState>}
 */
export const validation_runes_js = {
	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') {
			error(node, 'invalid-host-location');
		}
		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) {
			error(node, 'invalid-rune-args-length', rune, [1]);
		} else if (rune === '$state' && args.length > 1) {
			error(node, 'invalid-rune-args-length', rune, [0, 1]);
		} else if (rune === '$props') {
			error(node, 'invalid-props-location');
		} else if (rune === '$bindable') {
			error(node, 'invalid-bindable-location');
		}
	},
	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) {
			warn(context.state.analysis.warnings, node, context.path, 'avoid-nested-class');
		}
	},
	NewExpression(node, context) {
		if (node.callee.type === 'ClassExpression' && context.state.scope.function_depth > 0) {
			warn(context.state.analysis.warnings, node, context.path, 'avoid-inline-class');
		}
	}
};
 
/**
 * @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') {
			error(
				node,
				'invalid-const-assignment',
				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)
			);
		}
	}
}
 
/**
 * @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') {
				error(node, 'invalid-derived-assignment');
			}
 
			if (binding?.kind === 'each') {
				error(node, 'invalid-each-assignment');
			}
		}
 
		if (binding?.kind === 'snippet') {
			error(node, 'invalid-snippet-assignment');
		}
	}
 
	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)) {
			error(node, 'invalid-derived-assignment');
		}
	}
}
 
export const validation_runes = merge(validation, a11y_validators, {
	LabeledStatement(node, { path }) {
		if (node.label.name !== '$' || path.at(-1)?.type !== 'Program') return;
		error(node, 'invalid-legacy-reactive-statement');
	},
	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;
			error(node, 'invalid-legacy-export');
		}
	},
	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) {
			error(node, 'invalid-rune-args-length', '$bindable', [0, 1]);
		} else if (rune === '$host') {
			if (node.arguments.length > 0) {
				error(node, 'invalid-rune-args-length', '$host', [0]);
			} else if (state.ast_type === 'module' || !state.analysis.custom_element) {
				error(node, 'invalid-host-location');
			}
		}
 
		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')
		) {
			error(node, 'invalid-state-location', context.name);
		}
		next({ ...state });
	},
	VariableDeclarator(node, { state, path }) {
		ensure_no_module_import_conflict(node, state);
 
		const init = node.init;
		const rune = get_rune(init, state.scope);
 
		if (rune === null) {
			if (init?.type === 'Identifier' && init.name === '$props' && !state.scope.get('props')) {
				warn(state.analysis.warnings, node, path, 'invalid-props-declaration');
			}
			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) {
			error(node, 'invalid-rune-args-length', rune, [1]);
		} else if (rune === '$state' && args.length > 1) {
			error(node, 'invalid-rune-args-length', rune, [0, 1]);
		} else if (rune === '$props') {
			if (state.has_props_rune) {
				error(node, 'duplicate-props-rune');
			}
 
			state.has_props_rune = true;
 
			if (args.length > 0) {
				error(node, 'invalid-rune-args-length', rune, [0]);
			}
 
			if (node.id.type !== 'ObjectPattern') {
				error(node, 'invalid-props-id');
			}
 
			if (state.scope !== state.analysis.instance.scope) {
				error(node, 'invalid-props-location');
			}
 
			for (const property of node.id.properties) {
				if (property.type === 'Property') {
					if (property.computed) {
						error(property, 'invalid-props-pattern');
					}
 
					const value =
						property.value.type === 'AssignmentPattern' ? property.value.left : property.value;
 
					if (value.type !== 'Identifier') {
						error(property, 'invalid-props-pattern');
					}
				}
			}
		}
 
		if (rune === '$derived') {
			const arg = args[0];
			if (
				arg.type === 'CallExpression' &&
				(arg.callee.type === 'ArrowFunctionExpression' || arg.callee.type === 'FunctionExpression')
			) {
				warn(state.analysis.warnings, node, path, 'derived-iife');
			}
		}
	},
	AssignmentPattern(node, { state, path }) {
		if (
			node.right.type === 'Identifier' &&
			node.right.name === '$bindable' &&
			!state.scope.get('bindable')
		) {
			warn(state.analysis.warnings, node, path, 'invalid-bindable-declaration');
		}
	},
	SlotElement(node, { state, path }) {
		if (!state.analysis.custom_element) {
			warn(state.analysis.warnings, node, path, 'deprecated-slot-element');
		}
	},
	OnDirective(node, { state, 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') {
			warn(state.analysis.warnings, node, path, 'deprecated-event-handler', 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
});