@@ -138,6 +138,8 @@ export class ScriptLetContext {
138
138
139
139
private readonly unique = new UniqueIdGenerator ( ) ;
140
140
141
+ private currentScriptScopeKind : "render" | "snippet" = "render" ;
142
+
141
143
public constructor ( ctx : Context ) {
142
144
this . script = ctx . sourceCode . scripts ;
143
145
this . ctx = ctx ;
@@ -164,7 +166,7 @@ export class ScriptLetContext {
164
166
this . appendScript (
165
167
`(${ part } )${ isTS ? `as (${ typing } )` : "" } ;` ,
166
168
range [ 0 ] - 1 ,
167
- "render" ,
169
+ this . currentScriptScopeKind ,
168
170
( st , tokens , comments , result ) => {
169
171
const exprSt = st as ESTree . ExpressionStatement ;
170
172
const tsAs : TSAsExpression | null = isTS
@@ -220,7 +222,7 @@ export class ScriptLetContext {
220
222
this . appendScript (
221
223
`({${ part } });` ,
222
224
range [ 0 ] - 2 ,
223
- "render" ,
225
+ this . currentScriptScopeKind ,
224
226
( st , tokens , _comments , result ) => {
225
227
const exprSt = st as ESTree . ExpressionStatement ;
226
228
const objectExpression : ESTree . ObjectExpression =
@@ -259,7 +261,7 @@ export class ScriptLetContext {
259
261
this . appendScript (
260
262
`const ${ part } ;` ,
261
263
range [ 0 ] - 6 ,
262
- "render" ,
264
+ this . currentScriptScopeKind ,
263
265
( st , tokens , _comments , result ) => {
264
266
const decl = st as ESTree . VariableDeclaration ;
265
267
const node = decl . declarations [ 0 ] ;
@@ -393,7 +395,7 @@ export class ScriptLetContext {
393
395
const restore = this . appendScript (
394
396
`if(${ part } ){` ,
395
397
range [ 0 ] - 3 ,
396
- "render" ,
398
+ this . currentScriptScopeKind ,
397
399
( st , tokens , _comments , result ) => {
398
400
const ifSt = st as ESTree . IfStatement ;
399
401
const node = ifSt . test ;
@@ -417,7 +419,7 @@ export class ScriptLetContext {
417
419
ifSt . consequent = null as never ;
418
420
} ,
419
421
) ;
420
- this . pushScope ( restore , "}" ) ;
422
+ this . pushScope ( restore , "}" , this . currentScriptScopeKind ) ;
421
423
}
422
424
423
425
public nestEachBlock (
@@ -448,7 +450,7 @@ export class ScriptLetContext {
448
450
const restore = this . appendScript (
449
451
source ,
450
452
exprRange [ 0 ] - exprOffset ,
451
- "render" ,
453
+ this . currentScriptScopeKind ,
452
454
( st , tokens , comments , result ) => {
453
455
const expSt = st as ESTree . ExpressionStatement ;
454
456
const call = expSt . expression as ESTree . CallExpression ;
@@ -525,21 +527,22 @@ export class ScriptLetContext {
525
527
expSt . expression = null as never ;
526
528
} ,
527
529
) ;
528
- this . pushScope ( restore , "});" ) ;
530
+ this . pushScope ( restore , "});" , this . currentScriptScopeKind ) ;
529
531
}
530
532
531
533
public nestSnippetBlock (
532
534
id : ESTree . Identifier ,
533
535
closeParentIndex : number ,
534
536
snippetBlock : SvelteSnippetBlock ,
537
+ kind : "snippet" | "render" ,
535
538
callback : ( id : ESTree . Identifier , params : ESTree . Pattern [ ] ) => void ,
536
539
) : void {
537
540
const idRange = getNodeRange ( id ) ;
538
541
const part = this . ctx . code . slice ( idRange [ 0 ] , closeParentIndex + 1 ) ;
539
542
const restore = this . appendScript (
540
543
`function ${ part } {` ,
541
544
idRange [ 0 ] - 9 ,
542
- "render" ,
545
+ kind ,
543
546
( st , tokens , _comments , result ) => {
544
547
const fnDecl = st as ESTree . FunctionDeclaration ;
545
548
const idNode = fnDecl . id ;
@@ -565,7 +568,7 @@ export class ScriptLetContext {
565
568
fnDecl . params = [ ] ;
566
569
} ,
567
570
) ;
568
- this . pushScope ( restore , "}" ) ;
571
+ this . pushScope ( restore , "}" , kind ) ;
569
572
}
570
573
571
574
public nestBlock (
@@ -588,7 +591,7 @@ export class ScriptLetContext {
588
591
for ( const preparationScript of generatedTypes . preparationScript ) {
589
592
this . appendScriptWithoutOffset (
590
593
preparationScript ,
591
- "render" ,
594
+ this . currentScriptScopeKind ,
592
595
( node , tokens , comments , result ) => {
593
596
tokens . length = 0 ;
594
597
comments . length = 0 ;
@@ -608,7 +611,7 @@ export class ScriptLetContext {
608
611
const restore = this . appendScript (
609
612
`{` ,
610
613
block . range [ 0 ] ,
611
- "render" ,
614
+ this . currentScriptScopeKind ,
612
615
( st , tokens , _comments , result ) => {
613
616
const blockSt = st as ESTree . BlockStatement ;
614
617
@@ -622,7 +625,7 @@ export class ScriptLetContext {
622
625
blockSt . body = null as never ;
623
626
} ,
624
627
) ;
625
- this . pushScope ( restore , "}" ) ;
628
+ this . pushScope ( restore , "}" , this . currentScriptScopeKind ) ;
626
629
} else {
627
630
const sortedParams = [ ...resolvedParams ]
628
631
. map ( ( d ) => {
@@ -664,7 +667,7 @@ export class ScriptLetContext {
664
667
const restore = this . appendScript (
665
668
`(${ source } )=>{` ,
666
669
maps [ 0 ] . range [ 0 ] - 1 ,
667
- "render" ,
670
+ this . currentScriptScopeKind ,
668
671
( st , tokens , comments , result ) => {
669
672
const exprSt = st as ESTree . ExpressionStatement ;
670
673
const fn = exprSt . expression as ESTree . ArrowFunctionExpression ;
@@ -723,7 +726,7 @@ export class ScriptLetContext {
723
726
exprSt . expression = null as never ;
724
727
} ,
725
728
) ;
726
- this . pushScope ( restore , "};" ) ;
729
+ this . pushScope ( restore , "};" , this . currentScriptScopeKind ) ;
727
730
}
728
731
}
729
732
@@ -738,7 +741,7 @@ export class ScriptLetContext {
738
741
private appendScript (
739
742
text : string ,
740
743
offset : number ,
741
- kind : "generics" | "render" ,
744
+ kind : "generics" | "snippet" | " render",
742
745
callback : (
743
746
node : ESTree . Node ,
744
747
tokens : Token [ ] ,
@@ -766,7 +769,7 @@ export class ScriptLetContext {
766
769
767
770
private appendScriptWithoutOffset (
768
771
text : string ,
769
- kind : "generics" | "render" ,
772
+ kind : "generics" | "snippet" | " render",
770
773
callback : (
771
774
node : ESTree . Node ,
772
775
tokens : Token [ ] ,
@@ -788,9 +791,16 @@ export class ScriptLetContext {
788
791
return restoreCallback ;
789
792
}
790
793
791
- private pushScope ( restoreCallback : RestoreCallback , closeToken : string ) {
794
+ private pushScope (
795
+ restoreCallback : RestoreCallback ,
796
+ closeToken : string ,
797
+ kind : "snippet" | "render" ,
798
+ ) {
799
+ const upper = this . currentScriptScopeKind ;
800
+ this . currentScriptScopeKind = kind ;
792
801
this . closeScopeCallbacks . push ( ( ) => {
793
- this . script . addLet ( closeToken , "render" ) ;
802
+ this . script . addLet ( closeToken , kind ) ;
803
+ this . currentScriptScopeKind = upper ;
794
804
restoreCallback . end = this . script . getCurrentVirtualCodeLength ( ) ;
795
805
} ) ;
796
806
}
@@ -825,7 +835,19 @@ export class ScriptLetContext {
825
835
// If we replace the `scope.block` at this time,
826
836
// the scope restore calculation will not work, so we will replace the `scope.block` later.
827
837
postprocessList . push ( ( ) => {
838
+ const beforeBlock = scope . block ;
828
839
scope . block = node ;
840
+
841
+ for ( const variable of [
842
+ ...scope . variables ,
843
+ ...( scope . upper ?. variables ?? [ ] ) ,
844
+ ] ) {
845
+ for ( const def of variable . defs ) {
846
+ if ( def . node === beforeBlock ) {
847
+ def . node = node ;
848
+ }
849
+ }
850
+ }
829
851
} ) ;
830
852
831
853
const scopes = nodeToScope . get ( node ) ;
0 commit comments