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 | 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 361x 361x 357x 357x 4x 4x 4x 4x 4x 4x 10x 10x 4x 4x 4x 2x 2x 2x 2x 2x 2x 13146x 13146x 13146x 13146x 3466x 3466x 3466x 9680x 13146x 218x 218x 218x 218x 218x 218x 218x 218x 9462x 13146x 26x 26x 26x 9436x 9436x 9436x 2x 2x 2x 2x 2x 908x 908x 798x 798x 798x 798x 93x 93x 93x 93x 798x 798x 110x 110x 110x 2x 2x 2x 2x 1101x 1101x 1101x 2x 2x 2x 2x 2x 2x 2x 1202x 1202x 1202x 1202x 1202x 1202x 1145x 1202x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 114x 114x 114x 114x 114x 57x 57x 41x 41x 41x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 1145x 1202x 1145x 1145x 1145x 1202x 328x 328x 328x 1145x 1202x 44x 44x 1101x 1101x 1202x 1202x 1202x 1202x 1096x 1202x 1096x 1096x 1202x 1202x 1202x 1202x 1202x 1202x 223x 1202x 188x 188x 188x 908x 908x 1195x 798x 42x 42x 756x 1202x 8x 8x 8x 8x 8x 8x 8x 8x 102x 102x 2x 2x 2x 2x 16717x 12864x 29x 29x 12835x 12835x 2x 2x 1088x 2x 2x 143x 143x 143x 143x 9x 9x 9x 9x 9x 9x 9x 9x 134x 134x 2x 2x 1592x 1592x 1592x 1592x 1592x 4x 4x 1588x 1592x 4x 4x 4x 1584x 1592x 1x 1x 1583x 1592x 3x 3x 3x 3x 3x 3x 1580x 1592x 12x 12x 1568x 1568x 1568x 2x 2x 2x 2x 2x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 110x 110x 60x 110x 59x 59x 59x 59x 59x 59x 53x 53x 13x 13x 13x 13x 13x 13x 13x 13x 1x 13x 12x 12x 13x 53x 59x 110x 41x 41x 41x 12x 12x 12x 12x 12x 12x 41x 41x 41x 41x 41x 41x 41x 41x 110x 110x 60x 110x 59x 59x 59x 59x 59x 59x 13x 13x 13x 13x 13x 13x 13x 13x 13x 1x 13x 12x 12x 12x 12x 12x 12x 12x 12x 12x 13x 13x 13x 59x 97x 97x 97x 41x 41x 2x 2x 47x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 6x 2x 2x 831x 831x 831x 832x 832x 832x 235x 235x 235x 597x 832x 114x 114x 114x 58x 58x 28x 58x 24x 24x 24x 24x 24x 58x 114x 114x 114x 114x 483x 483x 483x 832x 832x 832x 6x 6x 6x 6x 6x 477x 817x 471x 471x 471x 6x 804x 4x 4x 4x 2x 2x 2x 831x 831x 831x 831x 831x 2x 2x 397x 397x 213x 213x 213x 52x 52x 161x 161x 213x 213x 113x 213x 18x 18x 213x 327x 2x 2x 425x 27x 27x 1x 1x 27x 424x 424x 424x 2x 2x 2x 2x 2x 2562x 2562x 2562x 2562x 2902x 2902x 2902x 2902x 2902x 500x 500x 500x 2402x 2902x 2043x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 31x 31x 31x 31x 31x 31x 31x 31x 5x 5x 2038x 2038x 2038x 2038x 2043x 2043x 2043x 2043x 2043x 2043x 2043x 987x 987x 987x 987x 987x 2038x 2038x 2038x 2038x 2038x 359x 359x 359x 359x 359x 2902x 2902x 2902x 2902x 2562x 2562x 2562x 2562x 2562x 2x 2x 150x 150x 148x 148x 148x 148x 148x 148x 148x 148x 148x 148x 148x 148x 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 2440x 2440x 2440x 2440x 2440x 2440x 2440x 2440x 2440x 2440x 2440x 2440x 2440x 2440x 2440x 2440x 2440x 2440x 2440x 2440x 2440x 2440x 2440x 2440x 2440x 2440x 2440x 2440x 2440x 2440x 2440x 2440x 2440x 2440x 2440x 2440x 2440x 2440x 2440x 2440x 2440x 858x 858x 2440x 2440x 1909x 1889x 1889x 20x 20x 1909x 2440x 2440x 2440x 2440x 2440x 2440x 2440x 2440x 2440x 2440x 2440x 2440x 2440x 2440x 2440x 2440x 2440x 2440x 2440x 2440x 2440x 148x 148x 148x 148x 148x 112x 148x 95x 90x 90x 67x 67x 90x 95x 148x 148x 148x 2440x 2440x 47x 47x 47x 47x 47x 47x 2440x 2440x 2440x 2440x 81x 81x 293x 1x 1x 81x 81x 81x 293x 1x 1x 81x 81x 81x 81x 81x 81x 81x 81x 81x 81x 81x 81x 81x 81x 81x 81x 81x 81x 81x 81x 81x 81x 81x 81x 81x 2440x 2440x 2440x 2440x 2440x 2440x 89x 89x 89x 89x 89x 2440x 2440x 2440x 2440x 2440x 8352x 2092x 2092x 8352x 2440x 139x 139x 2440x 1128x 1128x 1128x 1128x 2440x 2440x 2440x 2440x 2440x 2440x 2440x 2440x 2440x 2440x 2440x 2440x 570x 570x 570x 2440x 2440x 10x 10x 10x 39x 39x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 2440x 2440x 25x 25x 25x 25x 2440x 2440x 3x 3x 2440x 2440x 2440x 2440x 2x 2x 2x 2x 2x 2x 2440x 2440x 2440x 2440x 2440x 2440x 2440x 2440x 2440x 2440x 2440x 2440x 2440x 2440x 2440x 2440x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 2440x 121x 121x 121x 121x 121x 121x 121x 121x 121x 121x 121x 121x 121x 121x 121x 121x 121x 121x 121x 121x 2436x 2315x 2315x 2440x 2440x 121x 121x 121x 121x 121x 121x 121x 121x 121x 121x 121x 2440x 2440x 2440x 2440x 2440x 2440x 2440x 2x 2x 2x 2x 2x 2x 2x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x | /** @import { AssignmentExpression, BinaryOperator, BlockStatement, CallExpression, Expression, ExpressionStatement, Identifier, MethodDefinition, Node, Pattern, Program, Property, PropertyDefinition, Statement, VariableDeclarator } from 'estree' */ /** @import { Binding, Namespace, SvelteNode, ValidatedCompileOptions, ValidatedModuleCompileOptions } from '#compiler' */ /** @import { ComponentServerTransformState, ComponentVisitors, ServerTransformState, Visitors } from './types.js' */ /** @import { Analysis, ComponentAnalysis } from '../../types.js' */ /** @import { Scope } from '../../scope.js' */ /** @import { StateField } from '../../3-transform/client/types.js' */ // TODO move this type import { walk } from 'zimmerframe'; import { set_scope, get_rune } from '../../scope.js'; import { extract_identifiers, extract_paths, is_expression_async } from '../../../utils/ast.js'; import * as b from '../../../utils/builders.js'; import is_reference from 'is-reference'; import { transform_inspect_rune } from '../utils.js'; import { filename } from '../../../state.js'; import { render_stylesheet } from '../css/index.js'; import { AwaitBlock } from './visitors/template/AwaitBlock.js'; import { Component } from './visitors/template/Component.js'; import { ConstTag } from './visitors/template/ConstTag.js'; import { DebugTag } from './visitors/template/DebugTag.js'; import { EachBlock } from './visitors/template/EachBlock.js'; import { Fragment } from './visitors/template/Fragment.js'; import { HtmlTag } from './visitors/template/HtmlTag.js'; import { IfBlock } from './visitors/template/IfBlock.js'; import { KeyBlock } from './visitors/template/KeyBlock.js'; import { LetDirective } from './visitors/template/LetDirective.js'; import { RegularElement } from './visitors/template/RegularElement.js'; import { RenderTag } from './visitors/template/RenderTag.js'; import { SlotElement } from './visitors/template/SlotElement.js'; import { SnippetBlock } from './visitors/template/SnippetBlock.js'; import { SpreadAttribute } from './visitors/template/SpreadAttribute.js'; import { SvelteComponent } from './visitors/template/SvelteComponent.js'; import { SvelteElement } from './visitors/template/SvelteElement.js'; import { SvelteFragment } from './visitors/template/SvelteFragment.js'; import { SvelteHead } from './visitors/template/SvelteHead.js'; import { SvelteSelf } from './visitors/template/SvelteSelf.js'; import { TitleElement } from './visitors/template/TitleElement.js'; /** * @param {VariableDeclarator} declarator * @param {Scope} scope * @param {Expression} value * @returns {VariableDeclarator[]} */ function create_state_declarators(declarator, scope, value) { if (declarator.id.type === 'Identifier') { return [b.declarator(declarator.id, value)]; } const tmp = scope.generate('tmp'); const paths = extract_paths(declarator.id); return [ b.declarator(b.id(tmp), value), // TODO inject declarator for opts, so we can use it below ...paths.map((path) => { const value = path.expression?.(b.id(tmp)); return b.declarator(path.node, value); }) ]; } /** * @param {Identifier} node * @param {ServerTransformState} state * @returns {Expression} */ function serialize_get_binding(node, state) { const binding = state.scope.get(node.name); if (binding === null || node === binding.node) { // No associated binding or the declaration itself which shouldn't be transformed return node; } if (binding.kind === 'store_sub') { const store_id = b.id(node.name.slice(1)); return b.call( '$.store_get', b.assignment('??=', b.id('$$store_subs'), b.object([])), b.literal(node.name), serialize_get_binding(store_id, state) ); } if (Object.hasOwn(state.getters, node.name)) { const getter = state.getters[node.name]; return typeof getter === 'function' ? getter(node) : getter; } return node; } /** * @param {AssignmentExpression} node * @param {Pick<import('zimmerframe').Context<SvelteNode, ServerTransformState>, 'visit' | 'state'>} context */ function get_assignment_value(node, { state, visit }) { if (node.left.type === 'Identifier') { const operator = node.operator; return operator === '=' ? /** @type {Expression} */ (visit(node.right)) : // turn something like x += 1 into x = x + 1 b.binary( /** @type {BinaryOperator} */ (operator.slice(0, -1)), serialize_get_binding(node.left, state), /** @type {Expression} */ (visit(node.right)) ); } return /** @type {Expression} */ (visit(node.right)); } /** * @param {string} name */ function is_store_name(name) { return name[0] === '$' && /[A-Za-z_]/.test(name[1]); } /** * @param {AssignmentExpression} node * @param {import('zimmerframe').Context<SvelteNode, ServerTransformState>} context * @param {() => any} fallback * @returns {Expression} */ function serialize_set_binding(node, context, fallback) { const { state, visit } = context; if ( node.left.type === 'ArrayPattern' || node.left.type === 'ObjectPattern' || node.left.type === 'RestElement' ) { // Turn assignment into an IIFE, so that `$.set` calls etc don't produce invalid code const tmp_id = context.state.scope.generate('tmp'); /** @type {AssignmentExpression[]} */ const original_assignments = []; /** @type {Expression[]} */ const assignments = []; const paths = extract_paths(node.left); for (const path of paths) { const value = path.expression?.(b.id(tmp_id)); const assignment = b.assignment('=', path.node, value); original_assignments.push(assignment); assignments.push(serialize_set_binding(assignment, context, () => assignment)); } if (assignments.every((assignment, i) => assignment === original_assignments[i])) { // No change to output -> nothing to transform -> we can keep the original assignment return fallback(); } return b.call( b.thunk( b.block([ b.const(tmp_id, /** @type {Expression} */ (visit(node.right))), b.stmt(b.sequence(assignments)), b.return(b.id(tmp_id)) ]) ) ); } if (node.left.type !== 'Identifier' && node.left.type !== 'MemberExpression') { throw new Error(`Unexpected assignment type ${node.left.type}`); } let left = node.left; while (left.type === 'MemberExpression') { // @ts-expect-error left = left.object; } if (left.type !== 'Identifier') { return fallback(); } const is_store = is_store_name(left.name); const left_name = is_store ? left.name.slice(1) : left.name; const binding = state.scope.get(left_name); if (!binding) return fallback(); if (binding.mutation !== null) { return binding.mutation(node, context); } if ( binding.kind !== 'state' && binding.kind !== 'frozen_state' && binding.kind !== 'prop' && binding.kind !== 'bindable_prop' && binding.kind !== 'each' && binding.kind !== 'legacy_reactive' && !is_store ) { // TODO error if it's a computed (or rest prop)? or does that already happen elsewhere? return fallback(); } const value = get_assignment_value(node, { state, visit }); if (left === node.left) { if (is_store) { return b.call('$.store_set', b.id(left_name), /** @type {Expression} */ (visit(node.right))); } return fallback(); } else if (is_store) { return b.call( '$.mutate_store', b.assignment('??=', b.id('$$store_subs'), b.object([])), b.literal(left.name), b.id(left_name), b.assignment(node.operator, /** @type {Pattern} */ (visit(node.left)), value) ); } return fallback(); } /** @type {Visitors} */ const global_visitors = { Identifier(node, { path, state }) { if (is_reference(node, /** @type {Node} */ (path.at(-1)))) { if (node.name === '$$props') { return b.id('$$sanitized_props'); } return serialize_get_binding(node, state); } }, AssignmentExpression(node, context) { return serialize_set_binding(node, context, context.next); }, UpdateExpression(node, context) { const { state, next } = context; const argument = node.argument; if (argument.type === 'Identifier' && state.scope.get(argument.name)?.kind === 'store_sub') { return b.call( node.prefix ? '$.update_store_pre' : '$.update_store', b.assignment('??=', b.id('$$store_subs'), b.object([])), b.literal(argument.name), b.id(argument.name.slice(1)), node.operator === '--' && b.literal(-1) ); } return next(); }, CallExpression(node, context) { const rune = get_rune(node, context.state.scope); if (rune === '$host') { return b.id('undefined'); } if (rune === '$effect.tracking') { return b.literal(false); } if (rune === '$effect.root') { // ignore $effect.root() calls, just return a noop which mimics the cleanup function return b.arrow([], b.block([])); } if (rune === '$state.snapshot') { return b.call('$.snapshot', /** @type {Expression} */ (context.visit(node.arguments[0]))); } if (rune === '$state.is') { return b.call( 'Object.is', /** @type {Expression} */ (context.visit(node.arguments[0])), /** @type {Expression} */ (context.visit(node.arguments[1])) ); } if (rune === '$inspect' || rune === '$inspect().with') { return transform_inspect_rune(node, context); } context.next(); } }; /** @type {Visitors} */ const javascript_visitors_runes = { ClassBody(node, { state, visit }) { /** @type {Map<string, StateField>} */ const public_derived = new Map(); /** @type {Map<string, StateField>} */ const private_derived = new Map(); /** @type {string[]} */ const private_ids = []; for (const definition of node.body) { if ( definition.type === 'PropertyDefinition' && (definition.key.type === 'Identifier' || definition.key.type === 'PrivateIdentifier') ) { const { type, name } = definition.key; const is_private = type === 'PrivateIdentifier'; if (is_private) private_ids.push(name); if (definition.value?.type === 'CallExpression') { const rune = get_rune(definition.value, state.scope); if (rune === '$derived' || rune === '$derived.by') { /** @type {StateField} */ const field = { kind: rune === '$derived.by' ? 'derived_call' : 'derived', // @ts-expect-error this is set in the next pass id: is_private ? definition.key : null }; if (is_private) { private_derived.set(name, field); } else { public_derived.set(name, field); } } } } } // each `foo = $derived()` needs a backing `#foo` field for (const [name, field] of public_derived) { let deconflicted = name; while (private_ids.includes(deconflicted)) { deconflicted = '_' + deconflicted; } private_ids.push(deconflicted); field.id = b.private_id(deconflicted); } /** @type {Array<MethodDefinition | PropertyDefinition>} */ const body = []; const child_state = { ...state, private_derived }; // Replace parts of the class body for (const definition of node.body) { if ( definition.type === 'PropertyDefinition' && (definition.key.type === 'Identifier' || definition.key.type === 'PrivateIdentifier') ) { const name = definition.key.name; const is_private = definition.key.type === 'PrivateIdentifier'; const field = (is_private ? private_derived : public_derived).get(name); if (definition.value?.type === 'CallExpression' && field !== undefined) { const init = /** @type {Expression} **/ ( visit(definition.value.arguments[0], child_state) ); const value = field.kind === 'derived_call' ? b.call('$.once', init) : b.call('$.once', b.thunk(init)); if (is_private) { body.push(b.prop_def(field.id, value)); } else { // #foo; const member = b.member(b.this, field.id); body.push(b.prop_def(field.id, value)); // get foo() { return this.#foo; } body.push(b.method('get', definition.key, [], [b.return(b.call(member))])); if ((field.kind === 'derived' || field.kind === 'derived_call') && state.options.dev) { body.push( b.method( 'set', definition.key, [b.id('_')], [b.throw_error(`Cannot update a derived property ('${name}')`)] ) ); } } continue; } } body.push(/** @type {MethodDefinition} **/ (visit(definition, child_state))); } return { ...node, body }; }, PropertyDefinition(node, { state, next, visit }) { if (node.value != null && node.value.type === 'CallExpression') { const rune = get_rune(node.value, state.scope); if (rune === '$state' || rune === '$state.frozen' || rune === '$derived') { return { ...node, value: node.value.arguments.length === 0 ? null : /** @type {Expression} */ (visit(node.value.arguments[0])) }; } if (rune === '$derived.by') { return { ...node, value: node.value.arguments.length === 0 ? null : b.call(/** @type {Expression} */ (visit(node.value.arguments[0]))) }; } } next(); }, VariableDeclaration(node, { state, visit }) { const declarations = []; for (const declarator of node.declarations) { const init = declarator.init; const rune = get_rune(init, state.scope); if (!rune || rune === '$effect.tracking' || rune === '$inspect' || rune === '$effect.root') { declarations.push(/** @type {VariableDeclarator} */ (visit(declarator))); continue; } if (rune === '$props') { // remove $bindable() from props declaration const id = walk(declarator.id, null, { AssignmentPattern(node) { if ( node.right.type === 'CallExpression' && get_rune(node.right, state.scope) === '$bindable' ) { const right = node.right.arguments.length ? /** @type {Expression} */ (visit(node.right.arguments[0])) : b.id('undefined'); return b.assignment_pattern(node.left, right); } } }); declarations.push(b.declarator(id, b.id('$$props'))); continue; } const args = /** @type {CallExpression} */ (init).arguments; const value = args.length === 0 ? b.id('undefined') : /** @type {Expression} */ (visit(args[0])); if (rune === '$derived.by') { declarations.push( b.declarator(/** @type {Pattern} */ (visit(declarator.id)), b.call(value)) ); continue; } if (declarator.id.type === 'Identifier') { declarations.push(b.declarator(declarator.id, value)); continue; } if (rune === '$derived') { declarations.push(b.declarator(/** @type {Pattern} */ (visit(declarator.id)), value)); continue; } declarations.push(...create_state_declarators(declarator, state.scope, value)); } return { ...node, declarations }; }, ExpressionStatement(node, context) { const expression = node.expression; if (expression.type === 'CallExpression') { const callee = expression.callee; if (callee.type === 'Identifier' && callee.name === '$effect') { return b.empty; } if ( callee.type === 'MemberExpression' && callee.object.type === 'Identifier' && callee.object.name === '$effect' ) { return b.empty; } } context.next(); }, MemberExpression(node, context) { if (node.object.type === 'ThisExpression' && node.property.type === 'PrivateIdentifier') { const field = context.state.private_derived.get(node.property.name); if (field) { return b.call(node); } } context.next(); } }; /** @type {Visitors} */ const javascript_visitors_legacy = { VariableDeclaration(node, { state, visit }) { /** @type {VariableDeclarator[]} */ const declarations = []; for (const declarator of node.declarations) { const bindings = /** @type {Binding[]} */ (state.scope.get_bindings(declarator)); const has_state = bindings.some((binding) => binding.kind === 'state'); const has_props = bindings.some((binding) => binding.kind === 'bindable_prop'); if (!has_state && !has_props) { declarations.push(/** @type {VariableDeclarator} */ (visit(declarator))); continue; } if (has_props) { if (declarator.id.type !== 'Identifier') { // Turn export let into props. It's really really weird because export let { x: foo, z: [bar]} = .. // means that foo and bar are the props (i.e. the leafs are the prop names), not x and z. const tmp = state.scope.generate('tmp'); const paths = extract_paths(declarator.id); declarations.push( b.declarator( b.id(tmp), /** @type {Expression} */ (visit(/** @type {Expression} */ (declarator.init))) ) ); for (const path of paths) { const value = path.expression?.(b.id(tmp)); const name = /** @type {Identifier} */ (path.node).name; const binding = /** @type {Binding} */ (state.scope.get(name)); const prop = b.member(b.id('$$props'), b.literal(binding.prop_alias ?? name), true); declarations.push( b.declarator(path.node, b.call('$.value_or_fallback', prop, b.thunk(value))) ); } continue; } const binding = /** @type {Binding} */ (state.scope.get(declarator.id.name)); const prop = b.member( b.id('$$props'), b.literal(binding.prop_alias ?? declarator.id.name), true ); /** @type {Expression} */ let init = prop; if (declarator.init) { const default_value = /** @type {Expression} */ (visit(declarator.init)); init = is_expression_async(default_value) ? b.await(b.call('$.value_or_fallback_async', prop, b.thunk(default_value, true))) : b.call('$.value_or_fallback', prop, b.thunk(default_value)); } declarations.push(b.declarator(declarator.id, init)); continue; } declarations.push( ...create_state_declarators( declarator, state.scope, /** @type {Expression} */ (declarator.init && visit(declarator.init)) ) ); } return { ...node, declarations }; }, LabeledStatement(node, context) { if (context.path.length > 1) return; if (node.label.name !== '$') return; // TODO bail out if we're in module context // these statements will be topologically ordered later context.state.legacy_reactive_statements.set( node, // people could do "break $" inside, so we need to keep the label b.labeled('$', /** @type {ExpressionStatement} */ (context.visit(node.body))) ); return b.empty; } }; /** @type {ComponentVisitors} */ const template_visitors = { AwaitBlock, Component, ConstTag, DebugTag, EachBlock, Fragment, HtmlTag, IfBlock, KeyBlock, LetDirective, RegularElement, RenderTag, SlotElement, SnippetBlock, SpreadAttribute, SvelteComponent, SvelteElement, SvelteFragment, SvelteHead, SvelteSelf, TitleElement }; /** * @param {ComponentAnalysis} analysis * @param {ValidatedCompileOptions} options * @returns {Program} */ export function server_component(analysis, options) { /** @type {ComponentServerTransformState} */ const state = { analysis, options, scope: analysis.module.scope, scopes: analysis.template.scopes, hoisted: [b.import_all('$', 'svelte/internal/server')], legacy_reactive_statements: new Map(), // these are set inside the `Fragment` visitor, and cannot be used until then init: /** @type {any} */ (null), template: /** @type {any} */ (null), namespace: options.namespace, preserve_whitespace: options.preserveWhitespace, private_derived: new Map(), getters: {}, skip_hydration_boundaries: false }; const module = /** @type {Program} */ ( walk( /** @type {SvelteNode} */ (analysis.module.ast), state, // @ts-expect-error TODO: zimmerframe types { ...set_scope(analysis.module.scopes), ...global_visitors, ...(analysis.runes ? javascript_visitors_runes : javascript_visitors_legacy) } ) ); const instance = /** @type {Program} */ ( walk( /** @type {SvelteNode} */ (analysis.instance.ast), { ...state, scope: analysis.instance.scope }, // @ts-expect-error TODO: zimmerframe types { ...set_scope(analysis.instance.scopes), ...global_visitors, ...(analysis.runes ? javascript_visitors_runes : javascript_visitors_legacy), ImportDeclaration(node) { state.hoisted.push(node); return b.empty; }, ExportNamedDeclaration(node, context) { if (node.declaration) { return context.visit(node.declaration); } return b.empty; } } ) ); const template = /** @type {Program} */ ( walk( /** @type {SvelteNode} */ (analysis.template.ast), { ...state, scope: analysis.template.scope }, // @ts-expect-error TODO: zimmerframe types { ...set_scope(analysis.template.scopes), ...global_visitors, ...template_visitors } ) ); /** @type {VariableDeclarator[]} */ const legacy_reactive_declarations = []; for (const [node] of analysis.reactive_statements) { const statement = [...state.legacy_reactive_statements].find(([n]) => n === node); if (statement === undefined) { throw new Error('Could not find reactive statement'); } if ( node.body.type === 'ExpressionStatement' && node.body.expression.type === 'AssignmentExpression' ) { for (const id of extract_identifiers(node.body.expression.left)) { const binding = analysis.instance.scope.get(id.name); if (binding?.kind === 'legacy_reactive') { legacy_reactive_declarations.push(b.declarator(id)); } } } instance.body.push(statement[1]); } if (legacy_reactive_declarations.length > 0) { instance.body.unshift({ type: 'VariableDeclaration', kind: 'let', declarations: legacy_reactive_declarations }); } // If the component binds to a child, we need to put the template in a loop and repeat until legacy bindings are stable. // We can remove this once the legacy syntax is gone. if (analysis.uses_component_bindings) { const snippets = template.body.filter( (node) => node.type === 'FunctionDeclaration' && // @ts-expect-error node.___snippet ); const rest = template.body.filter( (node) => node.type !== 'FunctionDeclaration' || // @ts-expect-error !node.___snippet ); template.body = [ ...snippets, b.let('$$settled', b.true), b.let('$$inner_payload'), b.stmt( b.function( b.id('$$render_inner'), [b.id('$$payload')], b.block(/** @type {Statement[]} */ (rest)) ) ), b.do_while( b.unary('!', b.id('$$settled')), b.block([ b.stmt(b.assignment('=', b.id('$$settled'), b.true)), b.stmt( b.assignment('=', b.id('$$inner_payload'), b.call('$.copy_payload', b.id('$$payload'))) ), b.stmt(b.call('$$render_inner', b.id('$$inner_payload'))) ]) ), b.stmt(b.call('$.assign_payload', b.id('$$payload'), b.id('$$inner_payload'))) ]; } if ( [...analysis.instance.scope.declarations.values()].some( (binding) => binding.kind === 'store_sub' ) ) { instance.body.unshift(b.var('$$store_subs')); template.body.push( b.if(b.id('$$store_subs'), b.stmt(b.call('$.unsubscribe_stores', b.id('$$store_subs')))) ); } // Propagate values of bound props upwards if they're undefined in the parent and have a value. // Don't do this as part of the props retrieval because people could eagerly mutate the prop in the instance script. /** @type {Property[]} */ const props = []; for (const [name, binding] of analysis.instance.scope.declarations) { if (binding.kind === 'bindable_prop' && !name.startsWith('$$')) { props.push(b.init(binding.prop_alias ?? name, b.id(name))); } } for (const { name, alias } of analysis.exports) { props.push(b.init(alias ?? name, b.id(name))); } if (props.length > 0) { // This has no effect in runes mode other than throwing an error when someone passes // undefined to a binding that has a default value. template.body.push(b.stmt(b.call('$.bind_props', b.id('$$props'), b.object(props)))); } /** @type {Expression[]} */ const push_args = []; if (options.dev) push_args.push(b.id(analysis.name)); const component_block = b.block([ .../** @type {Statement[]} */ (instance.body), .../** @type {Statement[]} */ (template.body) ]); let should_inject_context = analysis.needs_context || options.dev; if (should_inject_context) { component_block.body.unshift(b.stmt(b.call('$.push', ...push_args))); component_block.body.push(b.stmt(b.call('$.pop'))); } if (analysis.uses_rest_props) { /** @type {string[]} */ const named_props = analysis.exports.map(({ name, alias }) => alias ?? name); for (const [name, binding] of analysis.instance.scope.declarations) { if (binding.kind === 'bindable_prop') named_props.push(binding.prop_alias ?? name); } component_block.body.unshift( b.const( '$$restProps', b.call( '$.rest_props', b.id('$$sanitized_props'), b.array(named_props.map((name) => b.literal(name))) ) ) ); } if (analysis.uses_props || analysis.uses_rest_props) { component_block.body.unshift( b.const('$$sanitized_props', b.call('$.sanitize_props', b.id('$$props'))) ); } if (analysis.uses_slots) { component_block.body.unshift(b.const('$$slots', b.call('$.sanitize_slots', b.id('$$props')))); } const body = [...state.hoisted, ...module.body]; if (analysis.css.ast !== null && options.css === 'injected' && !options.customElement) { const hash = b.literal(analysis.css.hash); const code = b.literal(render_stylesheet(analysis.source, analysis, options).code); body.push(b.const('$$css', b.object([b.init('hash', hash), b.init('code', code)]))); component_block.body.unshift(b.stmt(b.call('$$payload.css.add', b.id('$$css')))); } let should_inject_props = should_inject_context || props.length > 0 || analysis.needs_props || analysis.uses_props || analysis.uses_rest_props || analysis.uses_slots || analysis.slot_names.size > 0; const component_function = b.function_declaration( b.id(analysis.name), should_inject_props ? [b.id('$$payload'), b.id('$$props')] : [b.id('$$payload')], component_block ); if (options.compatibility.componentApi === 4) { body.unshift(b.imports([['render', '$$_render']], 'svelte/server')); body.push( component_function, b.stmt( b.assignment( '=', b.member_id(`${analysis.name}.render`), b.function( null, [b.id('$$props'), b.id('$$opts')], b.block([ b.return( b.call( '$$_render', b.id(analysis.name), b.object([ b.init('props', b.id('$$props')), b.init('context', b.member(b.id('$$opts'), b.id('context'), false, true)) ]) ) ) ]) ) ) ), b.export_default(b.id(analysis.name)) ); } else if (options.dev) { body.push( component_function, b.stmt( b.assignment( '=', b.member_id(`${analysis.name}.render`), b.function( null, [], b.block([ b.throw_error( `Component.render(...) is no longer valid in Svelte 5. ` + 'See https://svelte-5-preview.vercel.app/docs/breaking-changes#components-are-no-longer-classes for more information' ) ]) ) ) ), b.export_default(b.id(analysis.name)) ); } else { body.push(b.export_default(component_function)); } if (options.dev && filename) { // add `App[$.FILENAME] = 'App.svelte'` so that we can print useful messages later body.unshift( b.stmt( b.assignment( '=', b.member(b.id(analysis.name), b.id('$.FILENAME'), true), b.literal(filename) ) ) ); } return { type: 'Program', sourceType: 'module', body }; } /** * @param {Analysis} analysis * @param {ValidatedModuleCompileOptions} options * @returns {Program} */ export function server_module(analysis, options) { /** @type {ServerTransformState} */ const state = { analysis, options, scope: analysis.module.scope, scopes: analysis.module.scopes, // this is an anomaly — it can only be used in components, but it needs // to be present for `javascript_visitors_legacy` and so is included in module // transform state as well as component transform state legacy_reactive_statements: new Map(), private_derived: new Map(), getters: {} }; const module = /** @type {Program} */ ( walk(/** @type {SvelteNode} */ (analysis.module.ast), state, { ...set_scope(analysis.module.scopes), ...global_visitors, ...javascript_visitors_runes }) ); return { type: 'Program', sourceType: 'module', body: [b.import_all('$', 'svelte/internal/server'), ...module.body] }; } |