@@ -228,31 +228,35 @@ function convertBindingDirective(
228228 type : "SvelteDirective" ,
229229 kind : "Binding" ,
230230 key : null as any ,
231+ shorthand : false ,
231232 expression : null ,
232233 parent,
233234 ...ctx . getConvertLocation ( node ) ,
234235 }
235- processDirective ( node , directive , ctx , ( expression ) => {
236- return ctx . scriptLet . addExpression (
237- expression ,
238- directive ,
239- null ,
240- ( es , { getInnermostScope } ) => {
241- directive . expression = es
242- const scope = getInnermostScope ( es )
243- const reference = scope . references . find (
244- ( ref ) => ref . identifier === es ,
245- )
246- if ( reference ) {
247- // The bind directive does read and write.
248- reference . isWrite = ( ) => true
249- reference . isWriteOnly = ( ) => false
250- reference . isReadWrite = ( ) => true
251- reference . isReadOnly = ( ) => false
252- reference . isRead = ( ) => true
253- }
254- } ,
255- )
236+ processDirective ( node , directive , ctx , {
237+ processExpression ( expression , shorthand ) {
238+ directive . shorthand = shorthand
239+ return ctx . scriptLet . addExpression (
240+ expression ,
241+ directive ,
242+ null ,
243+ ( es , { getInnermostScope } ) => {
244+ directive . expression = es
245+ const scope = getInnermostScope ( es )
246+ const reference = scope . references . find (
247+ ( ref ) => ref . identifier === es ,
248+ )
249+ if ( reference ) {
250+ // The bind directive does read and write.
251+ reference . isWrite = ( ) => true
252+ reference . isWriteOnly = ( ) => false
253+ reference . isReadWrite = ( ) => true
254+ reference . isReadOnly = ( ) => false
255+ reference . isRead = ( ) => true
256+ }
257+ } ,
258+ )
259+ } ,
256260 } )
257261 return directive
258262}
@@ -274,18 +278,15 @@ function convertEventHandlerDirective(
274278 const isCustomEvent =
275279 parent . parent . type === "SvelteElement" &&
276280 ( parent . parent . kind === "component" || parent . parent . kind === "special" )
277- processDirective (
278- node ,
279- directive ,
280- ctx ,
281- buildProcessExpressionForExpression (
281+ processDirective ( node , directive , ctx , {
282+ processExpression : buildProcessExpressionForExpression (
282283 directive ,
283284 ctx ,
284285 isCustomEvent
285286 ? "(e:CustomEvent<any>)=>void"
286287 : `(e:'${ node . name } ' extends keyof HTMLElementEventMap?HTMLElementEventMap['${ node . name } ']:CustomEvent<any>)=>void` ,
287288 ) ,
288- )
289+ } )
289290 return directive
290291}
291292
@@ -299,16 +300,17 @@ function convertClassDirective(
299300 type : "SvelteDirective" ,
300301 kind : "Class" ,
301302 key : null as any ,
303+ shorthand : false ,
302304 expression : null ,
303305 parent,
304306 ...ctx . getConvertLocation ( node ) ,
305307 }
306- processDirective (
307- node ,
308- directive ,
309- ctx ,
310- buildProcessExpressionForExpression ( directive , ctx , null ) ,
311- )
308+ processDirective ( node , directive , ctx , {
309+ processExpression ( expression , shorthand ) {
310+ directive . shorthand = shorthand
311+ return ctx . scriptLet . addExpression ( expression , directive )
312+ } ,
313+ } )
312314 return directive
313315}
314316
@@ -368,18 +370,17 @@ function convertOldStyleDirective(
368370 }
369371 processDirectiveKey ( node , directive , ctx )
370372 if ( processStyleDirectiveValue ( node , ctx ) ) {
371- processDirectiveExpression ( node , directive , ctx , ( expression ) => {
372- directive . value . push (
373- convertTemplateLiteralToLiteral ( expression , directive , ctx ) ,
374- )
375- return [ ]
373+ processDirectiveExpression ( node , directive , ctx , {
374+ processExpression ( expression ) {
375+ directive . value . push (
376+ convertTemplateLiteralToLiteral ( expression , directive , ctx ) ,
377+ )
378+ return [ ]
379+ } ,
376380 } )
377381 } else {
378- processDirectiveExpression (
379- node ,
380- directive ,
381- ctx ,
382- ( expression , shorthand ) => {
382+ processDirectiveExpression ( node , directive , ctx , {
383+ processExpression ( expression , shorthand ) {
383384 ; ( directive as any ) . shorthand = shorthand
384385 return ctx . scriptLet . addExpression (
385386 expression ,
@@ -400,7 +401,7 @@ function convertOldStyleDirective(
400401 } ,
401402 )
402403 } ,
403- )
404+ } )
404405 }
405406
406407 return directive
@@ -463,13 +464,14 @@ function convertTransitionDirective(
463464 parent,
464465 ...ctx . getConvertLocation ( node ) ,
465466 }
466- processDirective (
467- node ,
468- directive ,
469- ctx ,
470- buildProcessExpressionForExpression ( directive , ctx , null ) ,
471- ( name ) => ctx . scriptLet . addExpression ( name , directive . key ) ,
472- )
467+ processDirective ( node , directive , ctx , {
468+ processExpression : buildProcessExpressionForExpression (
469+ directive ,
470+ ctx ,
471+ null ,
472+ ) ,
473+ processName : ( name ) => ctx . scriptLet . addExpression ( name , directive . key ) ,
474+ } )
473475 return directive
474476}
475477
@@ -487,13 +489,14 @@ function convertAnimationDirective(
487489 parent,
488490 ...ctx . getConvertLocation ( node ) ,
489491 }
490- processDirective (
491- node ,
492- directive ,
493- ctx ,
494- buildProcessExpressionForExpression ( directive , ctx , null ) ,
495- ( name ) => ctx . scriptLet . addExpression ( name , directive . key ) ,
496- )
492+ processDirective ( node , directive , ctx , {
493+ processExpression : buildProcessExpressionForExpression (
494+ directive ,
495+ ctx ,
496+ null ,
497+ ) ,
498+ processName : ( name ) => ctx . scriptLet . addExpression ( name , directive . key ) ,
499+ } )
497500 return directive
498501}
499502
@@ -511,13 +514,14 @@ function convertActionDirective(
511514 parent,
512515 ...ctx . getConvertLocation ( node ) ,
513516 }
514- processDirective (
515- node ,
516- directive ,
517- ctx ,
518- buildProcessExpressionForExpression ( directive , ctx , null ) ,
519- ( name ) => ctx . scriptLet . addExpression ( name , directive . key ) ,
520- )
517+ processDirective ( node , directive , ctx , {
518+ processExpression : buildProcessExpressionForExpression (
519+ directive ,
520+ ctx ,
521+ null ,
522+ ) ,
523+ processName : ( name ) => ctx . scriptLet . addExpression ( name , directive . key ) ,
524+ } )
521525 return directive
522526}
523527
@@ -535,16 +539,13 @@ function convertLetDirective(
535539 parent,
536540 ...ctx . getConvertLocation ( node ) ,
537541 }
538- processDirective (
539- node ,
540- directive ,
541- ctx ,
542- ( pattern ) => {
542+ processDirective ( node , directive , ctx , {
543+ processExpression ( pattern ) {
543544 return ctx . letDirCollections
544545 . getCollection ( )
545546 . addPattern ( pattern , directive , "any" )
546547 } ,
547- node . expression
548+ processName : node . expression
548549 ? undefined
549550 : ( name ) => {
550551 // shorthand
@@ -555,7 +556,7 @@ function convertLetDirective(
555556 } )
556557 return [ ]
557558 } ,
558- )
559+ } )
559560 return directive
560561}
561562
@@ -568,22 +569,18 @@ function processDirective<
568569 node : D & { expression : null | E } ,
569570 directive : S ,
570571 ctx : Context ,
571- processExpression : (
572- expression : E ,
573- shorthand : boolean ,
574- ) => ScriptLetCallback < NonNullable < E > > [ ] ,
575- processName ?: (
576- expression : SvelteName ,
577- ) => ScriptLetCallback < ESTree . Identifier > [ ] ,
572+ processors : {
573+ processExpression : (
574+ expression : E ,
575+ shorthand : boolean ,
576+ ) => ScriptLetCallback < NonNullable < E > > [ ]
577+ processName ?: (
578+ expression : SvelteName ,
579+ ) => ScriptLetCallback < ESTree . Identifier > [ ]
580+ } ,
578581) {
579582 processDirectiveKey ( node , directive , ctx )
580- processDirectiveExpression < D , S , E > (
581- node ,
582- directive ,
583- ctx ,
584- processExpression ,
585- processName ,
586- )
583+ processDirectiveExpression < D , S , E > ( node , directive , ctx , processors )
587584}
588585
589586/** Common process for directive key */
@@ -657,13 +654,15 @@ function processDirectiveExpression<
657654 node : D & { expression : null | E } ,
658655 directive : S ,
659656 ctx : Context ,
660- processExpression : (
661- expression : E ,
662- shorthand : boolean ,
663- ) => ScriptLetCallback < NonNullable < E > > [ ] ,
664- processName ?: (
665- expression : SvelteName ,
666- ) => ScriptLetCallback < ESTree . Identifier > [ ] ,
657+ processors : {
658+ processExpression : (
659+ expression : E ,
660+ shorthand : boolean ,
661+ ) => ScriptLetCallback < NonNullable < E > > [ ]
662+ processName ?: (
663+ expression : SvelteName ,
664+ ) => ScriptLetCallback < ESTree . Identifier > [ ]
665+ } ,
667666) {
668667 const key = directive . key
669668 const keyName = key . name as SvelteName
@@ -679,15 +678,15 @@ function processDirectiveExpression<
679678 // e.g. bind:value=""
680679 getWithLoc ( node . expression ) . end = keyName . range [ 1 ]
681680 }
682- processExpression ( node . expression , shorthand ) . push ( ( es ) => {
681+ processors . processExpression ( node . expression , shorthand ) . push ( ( es ) => {
683682 if ( directive . type === "SvelteDirective" ) {
684683 directive . expression = es
685684 }
686685 } )
687686 }
688687 if ( ! shorthand ) {
689- if ( processName ) {
690- processName ( keyName ) . push ( ( es ) => {
688+ if ( processors . processName ) {
689+ processors . processName ( keyName ) . push ( ( es ) => {
691690 key . name = es
692691 } )
693692 } else {
0 commit comments