@@ -228,31 +228,35 @@ function convertBindingDirective(
228
228
type : "SvelteDirective" ,
229
229
kind : "Binding" ,
230
230
key : null as any ,
231
+ shorthand : false ,
231
232
expression : null ,
232
233
parent,
233
234
...ctx . getConvertLocation ( node ) ,
234
235
}
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
+ } ,
256
260
} )
257
261
return directive
258
262
}
@@ -274,18 +278,15 @@ function convertEventHandlerDirective(
274
278
const isCustomEvent =
275
279
parent . parent . type === "SvelteElement" &&
276
280
( 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 (
282
283
directive ,
283
284
ctx ,
284
285
isCustomEvent
285
286
? "(e:CustomEvent<any>)=>void"
286
287
: `(e:'${ node . name } ' extends keyof HTMLElementEventMap?HTMLElementEventMap['${ node . name } ']:CustomEvent<any>)=>void` ,
287
288
) ,
288
- )
289
+ } )
289
290
return directive
290
291
}
291
292
@@ -299,16 +300,17 @@ function convertClassDirective(
299
300
type : "SvelteDirective" ,
300
301
kind : "Class" ,
301
302
key : null as any ,
303
+ shorthand : false ,
302
304
expression : null ,
303
305
parent,
304
306
...ctx . getConvertLocation ( node ) ,
305
307
}
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
+ } )
312
314
return directive
313
315
}
314
316
@@ -368,18 +370,17 @@ function convertOldStyleDirective(
368
370
}
369
371
processDirectiveKey ( node , directive , ctx )
370
372
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
+ } ,
376
380
} )
377
381
} else {
378
- processDirectiveExpression (
379
- node ,
380
- directive ,
381
- ctx ,
382
- ( expression , shorthand ) => {
382
+ processDirectiveExpression ( node , directive , ctx , {
383
+ processExpression ( expression , shorthand ) {
383
384
; ( directive as any ) . shorthand = shorthand
384
385
return ctx . scriptLet . addExpression (
385
386
expression ,
@@ -400,7 +401,7 @@ function convertOldStyleDirective(
400
401
} ,
401
402
)
402
403
} ,
403
- )
404
+ } )
404
405
}
405
406
406
407
return directive
@@ -463,13 +464,14 @@ function convertTransitionDirective(
463
464
parent,
464
465
...ctx . getConvertLocation ( node ) ,
465
466
}
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
+ } )
473
475
return directive
474
476
}
475
477
@@ -487,13 +489,14 @@ function convertAnimationDirective(
487
489
parent,
488
490
...ctx . getConvertLocation ( node ) ,
489
491
}
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
+ } )
497
500
return directive
498
501
}
499
502
@@ -511,13 +514,14 @@ function convertActionDirective(
511
514
parent,
512
515
...ctx . getConvertLocation ( node ) ,
513
516
}
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
+ } )
521
525
return directive
522
526
}
523
527
@@ -535,16 +539,13 @@ function convertLetDirective(
535
539
parent,
536
540
...ctx . getConvertLocation ( node ) ,
537
541
}
538
- processDirective (
539
- node ,
540
- directive ,
541
- ctx ,
542
- ( pattern ) => {
542
+ processDirective ( node , directive , ctx , {
543
+ processExpression ( pattern ) {
543
544
return ctx . letDirCollections
544
545
. getCollection ( )
545
546
. addPattern ( pattern , directive , "any" )
546
547
} ,
547
- node . expression
548
+ processName : node . expression
548
549
? undefined
549
550
: ( name ) => {
550
551
// shorthand
@@ -555,7 +556,7 @@ function convertLetDirective(
555
556
} )
556
557
return [ ]
557
558
} ,
558
- )
559
+ } )
559
560
return directive
560
561
}
561
562
@@ -568,22 +569,18 @@ function processDirective<
568
569
node : D & { expression : null | E } ,
569
570
directive : S ,
570
571
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
+ } ,
578
581
) {
579
582
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 )
587
584
}
588
585
589
586
/** Common process for directive key */
@@ -657,13 +654,15 @@ function processDirectiveExpression<
657
654
node : D & { expression : null | E } ,
658
655
directive : S ,
659
656
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
+ } ,
667
666
) {
668
667
const key = directive . key
669
668
const keyName = key . name as SvelteName
@@ -679,15 +678,15 @@ function processDirectiveExpression<
679
678
// e.g. bind:value=""
680
679
getWithLoc ( node . expression ) . end = keyName . range [ 1 ]
681
680
}
682
- processExpression ( node . expression , shorthand ) . push ( ( es ) => {
681
+ processors . processExpression ( node . expression , shorthand ) . push ( ( es ) => {
683
682
if ( directive . type === "SvelteDirective" ) {
684
683
directive . expression = es
685
684
}
686
685
} )
687
686
}
688
687
if ( ! shorthand ) {
689
- if ( processName ) {
690
- processName ( keyName ) . push ( ( es ) => {
688
+ if ( processors . processName ) {
689
+ processors . processName ( keyName ) . push ( ( es ) => {
691
690
key . name = es
692
691
} )
693
692
} else {
0 commit comments