@@ -495,7 +495,7 @@ function convertLetDirective(
495
495
...ctx . getConvertLocation ( node ) ,
496
496
}
497
497
processDirective ( node , directive , ctx , {
498
- processExpression ( pattern ) {
498
+ processPattern ( pattern ) {
499
499
return ctx . letDirCollections
500
500
. getCollection ( )
501
501
. addPattern ( pattern , directive , "any" )
@@ -515,6 +515,32 @@ function convertLetDirective(
515
515
return directive
516
516
}
517
517
518
+ type DirectiveProcessors <
519
+ D extends SvAST . Directive ,
520
+ S extends SvelteDirective ,
521
+ E extends D [ "expression" ] & S [ "expression" ] ,
522
+ > =
523
+ | {
524
+ processExpression : (
525
+ expression : E ,
526
+ shorthand : boolean ,
527
+ ) => ScriptLetCallback < NonNullable < E > > [ ]
528
+ processPattern ?: undefined
529
+ processName ?: (
530
+ expression : SvelteName ,
531
+ ) => ScriptLetCallback < ESTree . Identifier > [ ]
532
+ }
533
+ | {
534
+ processExpression ?: undefined
535
+ processPattern : (
536
+ expression : E ,
537
+ shorthand : boolean ,
538
+ ) => ScriptLetCallback < NonNullable < E > > [ ]
539
+ processName ?: (
540
+ expression : SvelteName ,
541
+ ) => ScriptLetCallback < ESTree . Identifier > [ ]
542
+ }
543
+
518
544
/** Common process for directive */
519
545
function processDirective <
520
546
D extends SvAST . Directive ,
@@ -524,15 +550,7 @@ function processDirective<
524
550
node : D & { expression : null | E } ,
525
551
directive : S ,
526
552
ctx : Context ,
527
- processors : {
528
- processExpression : (
529
- expression : E ,
530
- shorthand : boolean ,
531
- ) => ScriptLetCallback < NonNullable < E > > [ ]
532
- processName ?: (
533
- expression : SvelteName ,
534
- ) => ScriptLetCallback < ESTree . Identifier > [ ]
535
- } ,
553
+ processors : DirectiveProcessors < D , S , E > ,
536
554
) {
537
555
processDirectiveKey ( node , directive , ctx )
538
556
processDirectiveExpression < D , S , E > ( node , directive , ctx , processors )
@@ -609,15 +627,7 @@ function processDirectiveExpression<
609
627
node : D & { expression : null | E } ,
610
628
directive : S ,
611
629
ctx : Context ,
612
- processors : {
613
- processExpression : (
614
- expression : E ,
615
- shorthand : boolean ,
616
- ) => ScriptLetCallback < NonNullable < E > > [ ]
617
- processName ?: (
618
- expression : SvelteName ,
619
- ) => ScriptLetCallback < ESTree . Identifier > [ ]
620
- } ,
630
+ processors : DirectiveProcessors < D , S , E > ,
621
631
) {
622
632
const key = directive . key
623
633
const keyName = key . name as SvelteName
@@ -633,16 +643,24 @@ function processDirectiveExpression<
633
643
// e.g. bind:value=""
634
644
getWithLoc ( node . expression ) . end = keyName . range [ 1 ]
635
645
}
636
- processors . processExpression ( node . expression , shorthand ) . push ( ( es ) => {
637
- if ( node . expression && es . type !== node . expression . type ) {
638
- throw new ParseError (
639
- `Expected ${ node . expression . type } , but ${ es . type } found.` ,
640
- es . range ! [ 0 ] ,
641
- ctx ,
642
- )
643
- }
644
- directive . expression = es
645
- } )
646
+ if ( processors . processExpression ) {
647
+ processors
648
+ . processExpression ( node . expression , shorthand )
649
+ . push ( ( es ) => {
650
+ if ( node . expression && es . type !== node . expression . type ) {
651
+ throw new ParseError (
652
+ `Expected ${ node . expression . type } , but ${ es . type } found.` ,
653
+ es . range ! [ 0 ] ,
654
+ ctx ,
655
+ )
656
+ }
657
+ directive . expression = es
658
+ } )
659
+ } else {
660
+ processors . processPattern ( node . expression , shorthand ) . push ( ( es ) => {
661
+ directive . expression = es
662
+ } )
663
+ }
646
664
}
647
665
if ( ! shorthand ) {
648
666
if ( processors . processName ) {
0 commit comments