@@ -433,22 +433,26 @@ export class ScriptLetContext {
433
433
434
434
public nestEachBlock (
435
435
expression : ESTree . Expression ,
436
- context : ESTree . Pattern ,
436
+ context : ESTree . Pattern | null ,
437
437
indexRange : { start : number ; end : number } | null ,
438
438
eachBlock : SvelteEachBlock ,
439
439
callback : (
440
440
expr : ESTree . Expression ,
441
- ctx : ESTree . Pattern ,
441
+ ctx : ESTree . Pattern | null ,
442
442
index : ESTree . Identifier | null ,
443
443
) => void ,
444
444
) : void {
445
445
const exprRange = getNodeRange ( expression ) ;
446
- const ctxRange = getNodeRange ( context ) ;
446
+ const ctxRange = context && getNodeRange ( context ) ;
447
447
let source = "Array.from(" ;
448
448
const exprOffset = source . length ;
449
449
source += `${ this . ctx . code . slice ( ...exprRange ) } ).forEach((` ;
450
450
const ctxOffset = source . length ;
451
- source += this . ctx . code . slice ( ...ctxRange ) ;
451
+ if ( ctxRange ) {
452
+ source += this . ctx . code . slice ( ...ctxRange ) ;
453
+ } else {
454
+ source += "__$ctx__" ;
455
+ }
452
456
let idxOffset : number | null = null ;
453
457
if ( indexRange ) {
454
458
source += "," ;
@@ -473,7 +477,7 @@ export class ScriptLetContext {
473
477
const scope = result . getScope ( fn . body ) ;
474
478
475
479
// Process for nodes
476
- callback ( expr , ctx , idx ) ;
480
+ callback ( expr , context ? ctx : null , idx ) ;
477
481
478
482
// Process for scope
479
483
result . registerNodeToScope ( eachBlock , scope ) ;
@@ -484,6 +488,10 @@ export class ScriptLetContext {
484
488
}
485
489
}
486
490
}
491
+ if ( ! context ) {
492
+ // remove `__$ctx__` variable
493
+ removeIdentifierVariable ( ctx , scope ) ;
494
+ }
487
495
// remove Array reference
488
496
const arrayId = ( callArrayFrom . callee as ESTree . MemberExpression )
489
497
. object ;
@@ -512,18 +520,24 @@ export class ScriptLetContext {
512
520
tokens . pop ( ) ; // )
513
521
tokens . pop ( ) ; // ;
514
522
515
- const map = [
523
+ const map : {
524
+ offset : number ;
525
+ range : [ number , number ] ;
526
+ newNode : ESTree . Expression | ESTree . Pattern ;
527
+ } [ ] = [
516
528
{
517
529
offset : exprOffset ,
518
530
range : exprRange ,
519
531
newNode : expr ,
520
532
} ,
521
- {
533
+ ] ;
534
+ if ( ctxRange ) {
535
+ map . push ( {
522
536
offset : ctxOffset ,
523
537
range : ctxRange ,
524
538
newNode : ctx ,
525
- } ,
526
- ] ;
539
+ } ) ;
540
+ }
527
541
if ( indexRange ) {
528
542
map . push ( {
529
543
offset : idxOffset ! ,
0 commit comments