@@ -21,18 +21,18 @@ import { Fragment } from './visitors/template/Fragment.js';
21
21
import { HtmlTag } from './visitors/template/HtmlTag.js' ;
22
22
import { IfBlock } from './visitors/template/IfBlock.js' ;
23
23
import { KeyBlock } from './visitors/template/KeyBlock.js' ;
24
+ import { LetDirective } from './visitors/template/LetDirective.js' ;
24
25
import { RegularElement } from './visitors/template/RegularElement.js' ;
25
26
import { RenderTag } from './visitors/template/RenderTag.js' ;
27
+ import { SlotElement } from './visitors/template/SlotElement.js' ;
26
28
import { SnippetBlock } from './visitors/template/SnippetBlock.js' ;
29
+ import { SpreadAttribute } from './visitors/template/SpreadAttribute.js' ;
27
30
import { SvelteComponent } from './visitors/template/SvelteComponent.js' ;
28
31
import { SvelteElement } from './visitors/template/SvelteElement.js' ;
32
+ import { SvelteFragment } from './visitors/template/SvelteFragment.js' ;
33
+ import { SvelteHead } from './visitors/template/SvelteHead.js' ;
29
34
import { SvelteSelf } from './visitors/template/SvelteSelf.js' ;
30
- import {
31
- empty_comment ,
32
- process_children ,
33
- serialize_attribute_value ,
34
- serialize_template
35
- } from './visitors/template/shared/utils.js' ;
35
+ import { TitleElement } from './visitors/template/TitleElement.js' ;
36
36
37
37
/**
38
38
* @param {VariableDeclarator } declarator
@@ -616,124 +616,12 @@ const template_visitors = {
616
616
Component,
617
617
SvelteSelf,
618
618
SvelteComponent,
619
- LetDirective ( node , { state } ) {
620
- if ( node . expression === null || node . expression . type === 'Identifier' ) {
621
- const name = node . expression === null ? node . name : node . expression . name ;
622
- return b . const ( name , b . member ( b . id ( '$$slotProps' ) , b . id ( node . name ) ) ) ;
623
- }
624
-
625
- const name = state . scope . generate ( node . name ) ;
626
- const bindings = state . scope . get_bindings ( node ) ;
627
-
628
- for ( const binding of bindings ) {
629
- state . getters [ binding . node . name ] = b . member ( b . id ( name ) , b . id ( binding . node . name ) ) ;
630
- }
631
-
632
- return b . const (
633
- name ,
634
- b . call (
635
- b . thunk (
636
- b . block ( [
637
- b . let (
638
- node . expression . type === 'ObjectExpression'
639
- ? // @ts -expect-error types don't match, but it can't contain spread elements and the structure is otherwise fine
640
- b . object_pattern ( node . expression . properties )
641
- : // @ts -expect-error types don't match, but it can't contain spread elements and the structure is otherwise fine
642
- b . array_pattern ( node . expression . elements ) ,
643
- b . member ( b . id ( '$$slotProps' ) , b . id ( node . name ) )
644
- ) ,
645
- b . return ( b . object ( bindings . map ( ( binding ) => b . init ( binding . node . name , binding . node ) ) ) )
646
- ] )
647
- )
648
- )
649
- ) ;
650
- } ,
651
- SpreadAttribute ( node , { visit } ) {
652
- return visit ( node . expression ) ;
653
- } ,
654
- SvelteFragment ( node , context ) {
655
- const child_state = {
656
- ...context . state ,
657
- getters : { ...context . state . getters }
658
- } ;
659
-
660
- for ( const attribute of node . attributes ) {
661
- if ( attribute . type === 'LetDirective' ) {
662
- context . state . template . push (
663
- /** @type {ExpressionStatement } */ ( context . visit ( attribute , child_state ) )
664
- ) ;
665
- }
666
- }
667
-
668
- const block = /** @type {BlockStatement } */ ( context . visit ( node . fragment , child_state ) ) ;
669
-
670
- context . state . template . push ( block ) ;
671
- } ,
672
- TitleElement ( node , context ) {
673
- // title is guaranteed to contain only text/expression tag children
674
- const template = [ b . literal ( '<title>' ) ] ;
675
- process_children ( node . fragment . nodes , { ...context , state : { ...context . state , template } } ) ;
676
- template . push ( b . literal ( '</title>' ) ) ;
677
-
678
- context . state . init . push ( ...serialize_template ( template , b . id ( '$$payload.title' ) , '=' ) ) ;
679
- } ,
680
- SlotElement ( node , context ) {
681
- /** @type {Property[] } */
682
- const props = [ ] ;
683
-
684
- /** @type {Expression[] } */
685
- const spreads = [ ] ;
686
-
687
- /** @type {ExpressionStatement[] } */
688
- const lets = [ ] ;
689
-
690
- /** @type {Expression } */
691
- let expression = b . call ( '$.default_slot' , b . id ( '$$props' ) ) ;
692
-
693
- for ( const attribute of node . attributes ) {
694
- if ( attribute . type === 'SpreadAttribute' ) {
695
- spreads . push ( /** @type {Expression } */ ( context . visit ( attribute ) ) ) ;
696
- } else if ( attribute . type === 'Attribute' ) {
697
- const value = serialize_attribute_value ( attribute . value , context , false , true ) ;
698
-
699
- if ( attribute . name === 'name' ) {
700
- expression = b . member ( b . member_id ( '$$props.$$slots' ) , value , true , true ) ;
701
- } else if ( attribute . name !== 'slot' ) {
702
- if ( attribute . metadata . dynamic ) {
703
- props . push ( b . get ( attribute . name , [ b . return ( value ) ] ) ) ;
704
- } else {
705
- props . push ( b . init ( attribute . name , value ) ) ;
706
- }
707
- }
708
- } else if ( attribute . type === 'LetDirective' ) {
709
- lets . push ( /** @type {ExpressionStatement } */ ( context . visit ( attribute ) ) ) ;
710
- }
711
- }
712
-
713
- // Let bindings first, they can be used on attributes
714
- context . state . init . push ( ...lets ) ;
715
-
716
- const props_expression =
717
- spreads . length === 0
718
- ? b . object ( props )
719
- : b . call ( '$.spread_props' , b . array ( [ b . object ( props ) , ...spreads ] ) ) ;
720
-
721
- const fallback =
722
- node . fragment . nodes . length === 0
723
- ? b . literal ( null )
724
- : b . thunk ( /** @type {BlockStatement } */ ( context . visit ( node . fragment ) ) ) ;
725
-
726
- const slot = b . call ( '$.slot' , b . id ( '$$payload' ) , expression , props_expression , fallback ) ;
727
-
728
- context . state . template . push ( empty_comment , b . stmt ( slot ) , empty_comment ) ;
729
- } ,
730
- SvelteHead ( node , context ) {
731
- const block = /** @type {BlockStatement } */ ( context . visit ( node . fragment ) ) ;
732
-
733
- context . state . template . push (
734
- b . stmt ( b . call ( '$.head' , b . id ( '$$payload' ) , b . arrow ( [ b . id ( '$$payload' ) ] , block ) ) )
735
- ) ;
736
- }
619
+ LetDirective,
620
+ SpreadAttribute,
621
+ SvelteFragment,
622
+ TitleElement,
623
+ SlotElement,
624
+ SvelteHead
737
625
} ;
738
626
739
627
/**
0 commit comments