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 | 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1280x 1280x 1401x 1401x 1401x 1401x 1401x 262x 1401x 4x 4x 1397x 1397x 1401x 60x 1401x 1x 1x 1396x 1401x 888x 174x 174x 2x 2x 2x 2x 1x 2x 2x 174x 887x 887x 887x 888x 17x 17x 888x 1401x 1272x 1272x 1272x 1272x 1272x 1272x 1272x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 8500x 8500x 8500x 8500x 8500x 8500x 8500x 8500x 8500x 8500x 6206x 3876x 3876x 3876x 411x 411x 2x 2x 2x 2x 1x 2x 2x 411x 3875x 3876x 5x 5x 3870x 3876x 378x 1x 1x 377x 377x 377x 378x 378x 20x 378x 2x 2x 378x 3869x 3876x 116x 116x 116x 3859x 3876x 3859x 3859x 3876x 2x 2x 3859x 3859x 6206x 22x 22x 1x 22x 1x 21x 20x 20x 66x 66x 64x 20x 20x 1x 1x 19x 22x 1x 22x 18x 18x 2330x 284x 284x 284x 284x 284x 7x 7x 7x 7x 3x 7x 4x 4x 7x 277x 284x 284x 2308x 967x 967x 967x 36x 1x 1x 1x 36x 2x 33x 11x 11x 36x 2x 2x 2x 2x 2x 2x 36x 964x 6206x 8469x 2x 2x 2x 2x 4746x 4746x 4746x 4746x 4746x 4x 4746x 2x 2x 4746x 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 3732x 3732x 2223x 2223x 3732x 8x 8x 3732x 2x 2x 2x 2x 2x 2x 2102x 2x 2x 917x 917x 917x 917x 917x 917x 911x 911x 911x 911x 917x 917x 917x 510x 510x 510x 508x 508x 508x 508x 508x 508x 508x 510x 2x 2x 508x 510x 1x 1x 507x 510x 1x 1x 506x 510x 1x 1x 510x 906x 917x 126x 126x 906x 917x 5x 5x 906x 906x 906x 906x 917x 917x 917x 917x 258x 917x 648x 1x 1x 647x 648x 644x 644x 3x 3x 3x 3x 3x 3x 641x 644x 360x 360x 360x 360x 3x 1x 1x 2x 2x 357x 360x 1x 1x 356x 360x 360x 637x 644x 68x 68x 82x 82x 82x 6x 68x 68x 1x 1x 68x 636x 644x 1x 1x 1x 1x 1x 1x 635x 643x 20x 20x 20x 20x 1x 20x 1x 1x 20x 648x 3x 3x 1x 1x 1x 1x 1x 2x 2x 648x 2x 2x 3x 2x 2x 257x 257x 257x 257x 257x 257x 257x 257x 257x 257x 257x 14x 257x 257x 2x 2x 2x 2x 1596x 37x 43x 43x 43x 42x 43x 1x 1x 43x 43x 36x 2x 2x 214x 214x 214x 214x 214x 214x 214x 214x 214x 214x 214x 1x 1x 2x 2x 8365x 24x 5x 1x 1x 5x 23x 8364x 8364x 8364x 8365x 8365x 2x 8365x 1x 1x 8364x 8364x 8364x 8365x 1124x 1124x 8333x 8365x 1435x 1435x 2020x 2020x 2020x 1x 2020x 1x 1x 2020x 1434x 8332x 8365x 5003x 5003x 9592x 1x 1x 9592x 5002x 8331x 8331x 8365x 8365x 8365x 98x 8365x 52x 52x 8331x 8331x 8331x 8331x 8331x 2x 2x 105x 105x 105x 105x 39x 39x 105x 105x 105x 105x 105x 9x 105x 1x 1x 104x 104x 166x 166x 166x 166x 166x 104x 105x 2x 2x 620x 620x 2x 2x 930x 930x 2x 2x 171x 171x 171x 2x 2x 30x 2x 2x 89x 89x 89x 89x 89x 10x 10x 10x 86x 10x 86x 89x 1x 89x 9x 9x 9x 9x 9x 1x 1x 9x 2x 2x 64x 1x 1x 2x 2x 54x 54x 1x 1x 2x 2x 136x 136x 136x 136x 136x 2x 2x 69x 69x 2x 2x 67x 67x 109x 60x 60x 60x 109x 109x 2x 2x 471x 340x 330x 159x 1x 1x 158x 159x 1x 1x 159x 340x 340x 2x 2x 2x 2x 2x 24271x 24271x 3744x 3744x 2x 2x 33x 33x 1x 1x 32x 32x 32x 1x 1x 2x 2x 334x 2x 2x 6675x 6588x 4022x 4022x 6675x 2x 2x 2x 2x 6029x 6029x 6029x 347x 347x 347x 5633x 277x 6029x 346x 346x 1x 6029x 2x 2x 1431x 1431x 1431x 2x 2x 305x 305x 301x 301x 305x 2x 2x 2x 2x 96x 96x 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 2297x 2297x 2297x 1348x 1348x 1348x 2297x 188x 1160x 2297x 38x 38x 38x 38x 38x 38x 36x 36x 38x 2x 2x 1122x 1122x 2297x 2297x 2297x 199x 2297x 935x 935x 5x 5x 187x 2297x 142x 2x 2x 140x 140x 2x 2x 142x 183x 2235x 8x 8x 183x 2235x 6x 6x 183x 2235x 22x 22x 183x 2235x 4x 4x 183x 2193x 5x 1x 1x 5x 2297x 2x 2x 2x 2x 2x 7422x 7422x 7422x 7908x 7908x 7174x 7908x 1x 1x 7908x 7421x 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 82x 82x 82x 82x 224x 224x 224x 36x 224x 26x 26x 4x 4x 26x 224x 82x 82x 82x 82x 82x 2x 2x 79x 79x 79x 79x 79x 79x 1x 1x 2x 2x 117x 2x 2x 117x 2x 2x 2x 2x 2x 2x 2x 2x 5231x 5231x 86x 190x 190x 190x 190x 5231x 90x 154x 154x 154x 154x 5145x 3722x 3722x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 6x 18x 12x 12x 18x 3722x 5231x 2x 2x 2x 2x 2x 2x 3970x 3970x 3970x 3970x 2761x 2761x 2761x 564x 2x 2x 562x 564x 1x 1x 564x 2758x 2761x 1x 1x 2761x 3954x 3954x 3954x 3954x 3954x 3954x 3961x 1133x 1133x 1133x 3954x 3970x 36x 2x 2x 36x 3970x 2x 2x 2x 11373x 11373x 11373x 11373x 11373x 11373x 11373x 1315x 11373x 1279x 1279x 1279x 1279x 1279x 84x 84x 84x 84x 84x 84x 84x 84x 1279x 1279x 2x 2x 1279x 2x 2x 2x 2x 36x 13x 9x 9x 9x 9x 9x 9x 9x 9x 8x 36x 23x 7x 1x 1x 1x 2x 2x 10x 2x 2x 2274x 2274x 1x 2274x 1x 1x 1x 1x 1x 2272x 2272x 2x 2x 139x 139x 139x 133x 139x 1x 1x 138x 2x 2x 1393x 1393x 1393x 1393x 1393x 1393x 1003x 1003x 1003x 1003x 1393x 1x 1393x 1x 1002x 193x 1x 1x 192x 192x 192x 193x 1x 1x 191x 193x 191x 193x 1x 1x 190x 193x 286x 264x 264x 264x 264x 264x 264x 264x 286x 190x 998x 1393x 92x 92x 92x 22x 92x 92x 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_event_attribute, 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').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 && 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); } } } 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 && 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 */ 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)) { 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 { 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 (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); } } 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); } 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) { e.tag_invalid_placement( node, '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); context.next({ ...context.state, parent_element: null }); 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()) ) ) { 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'); } } } /** * @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 = { 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, { 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 }); |