@@ -138,6 +138,8 @@ export class ScriptLetContext {
138138
139139 private readonly unique = new UniqueIdGenerator ( ) ;
140140
141+ private currentScriptScopeKind : "render" | "snippet" = "render" ;
142+
141143 public constructor ( ctx : Context ) {
142144 this . script = ctx . sourceCode . scripts ;
143145 this . ctx = ctx ;
@@ -164,7 +166,7 @@ export class ScriptLetContext {
164166 this . appendScript (
165167 `(${ part } )${ isTS ? `as (${ typing } )` : "" } ;` ,
166168 range [ 0 ] - 1 ,
167- "render" ,
169+ this . currentScriptScopeKind ,
168170 ( st , tokens , comments , result ) => {
169171 const exprSt = st as ESTree . ExpressionStatement ;
170172 const tsAs : TSAsExpression | null = isTS
@@ -220,7 +222,7 @@ export class ScriptLetContext {
220222 this . appendScript (
221223 `({${ part } });` ,
222224 range [ 0 ] - 2 ,
223- "render" ,
225+ this . currentScriptScopeKind ,
224226 ( st , tokens , _comments , result ) => {
225227 const exprSt = st as ESTree . ExpressionStatement ;
226228 const objectExpression : ESTree . ObjectExpression =
@@ -259,7 +261,7 @@ export class ScriptLetContext {
259261 this . appendScript (
260262 `const ${ part } ;` ,
261263 range [ 0 ] - 6 ,
262- "render" ,
264+ this . currentScriptScopeKind ,
263265 ( st , tokens , _comments , result ) => {
264266 const decl = st as ESTree . VariableDeclaration ;
265267 const node = decl . declarations [ 0 ] ;
@@ -393,7 +395,7 @@ export class ScriptLetContext {
393395 const restore = this . appendScript (
394396 `if(${ part } ){` ,
395397 range [ 0 ] - 3 ,
396- "render" ,
398+ this . currentScriptScopeKind ,
397399 ( st , tokens , _comments , result ) => {
398400 const ifSt = st as ESTree . IfStatement ;
399401 const node = ifSt . test ;
@@ -417,7 +419,7 @@ export class ScriptLetContext {
417419 ifSt . consequent = null as never ;
418420 } ,
419421 ) ;
420- this . pushScope ( restore , "}" ) ;
422+ this . pushScope ( restore , "}" , this . currentScriptScopeKind ) ;
421423 }
422424
423425 public nestEachBlock (
@@ -448,7 +450,7 @@ export class ScriptLetContext {
448450 const restore = this . appendScript (
449451 source ,
450452 exprRange [ 0 ] - exprOffset ,
451- "render" ,
453+ this . currentScriptScopeKind ,
452454 ( st , tokens , comments , result ) => {
453455 const expSt = st as ESTree . ExpressionStatement ;
454456 const call = expSt . expression as ESTree . CallExpression ;
@@ -525,21 +527,22 @@ export class ScriptLetContext {
525527 expSt . expression = null as never ;
526528 } ,
527529 ) ;
528- this . pushScope ( restore , "});" ) ;
530+ this . pushScope ( restore , "});" , this . currentScriptScopeKind ) ;
529531 }
530532
531533 public nestSnippetBlock (
532534 id : ESTree . Identifier ,
533535 closeParentIndex : number ,
534536 snippetBlock : SvelteSnippetBlock ,
537+ kind : "snippet" | "render" ,
535538 callback : ( id : ESTree . Identifier , params : ESTree . Pattern [ ] ) => void ,
536539 ) : void {
537540 const idRange = getNodeRange ( id ) ;
538541 const part = this . ctx . code . slice ( idRange [ 0 ] , closeParentIndex + 1 ) ;
539542 const restore = this . appendScript (
540543 `function ${ part } {` ,
541544 idRange [ 0 ] - 9 ,
542- "render" ,
545+ kind ,
543546 ( st , tokens , _comments , result ) => {
544547 const fnDecl = st as ESTree . FunctionDeclaration ;
545548 const idNode = fnDecl . id ;
@@ -565,7 +568,7 @@ export class ScriptLetContext {
565568 fnDecl . params = [ ] ;
566569 } ,
567570 ) ;
568- this . pushScope ( restore , "}" ) ;
571+ this . pushScope ( restore , "}" , kind ) ;
569572 }
570573
571574 public nestBlock (
@@ -588,7 +591,7 @@ export class ScriptLetContext {
588591 for ( const preparationScript of generatedTypes . preparationScript ) {
589592 this . appendScriptWithoutOffset (
590593 preparationScript ,
591- "render" ,
594+ this . currentScriptScopeKind ,
592595 ( node , tokens , comments , result ) => {
593596 tokens . length = 0 ;
594597 comments . length = 0 ;
@@ -608,7 +611,7 @@ export class ScriptLetContext {
608611 const restore = this . appendScript (
609612 `{` ,
610613 block . range [ 0 ] ,
611- "render" ,
614+ this . currentScriptScopeKind ,
612615 ( st , tokens , _comments , result ) => {
613616 const blockSt = st as ESTree . BlockStatement ;
614617
@@ -622,7 +625,7 @@ export class ScriptLetContext {
622625 blockSt . body = null as never ;
623626 } ,
624627 ) ;
625- this . pushScope ( restore , "}" ) ;
628+ this . pushScope ( restore , "}" , this . currentScriptScopeKind ) ;
626629 } else {
627630 const sortedParams = [ ...resolvedParams ]
628631 . map ( ( d ) => {
@@ -664,7 +667,7 @@ export class ScriptLetContext {
664667 const restore = this . appendScript (
665668 `(${ source } )=>{` ,
666669 maps [ 0 ] . range [ 0 ] - 1 ,
667- "render" ,
670+ this . currentScriptScopeKind ,
668671 ( st , tokens , comments , result ) => {
669672 const exprSt = st as ESTree . ExpressionStatement ;
670673 const fn = exprSt . expression as ESTree . ArrowFunctionExpression ;
@@ -723,7 +726,7 @@ export class ScriptLetContext {
723726 exprSt . expression = null as never ;
724727 } ,
725728 ) ;
726- this . pushScope ( restore , "};" ) ;
729+ this . pushScope ( restore , "};" , this . currentScriptScopeKind ) ;
727730 }
728731 }
729732
@@ -738,7 +741,7 @@ export class ScriptLetContext {
738741 private appendScript (
739742 text : string ,
740743 offset : number ,
741- kind : "generics" | "render" ,
744+ kind : "generics" | "snippet" | " render",
742745 callback : (
743746 node : ESTree . Node ,
744747 tokens : Token [ ] ,
@@ -766,7 +769,7 @@ export class ScriptLetContext {
766769
767770 private appendScriptWithoutOffset (
768771 text : string ,
769- kind : "generics" | "render" ,
772+ kind : "generics" | "snippet" | " render",
770773 callback : (
771774 node : ESTree . Node ,
772775 tokens : Token [ ] ,
@@ -788,9 +791,16 @@ export class ScriptLetContext {
788791 return restoreCallback ;
789792 }
790793
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 ;
792801 this . closeScopeCallbacks . push ( ( ) => {
793- this . script . addLet ( closeToken , "render" ) ;
802+ this . script . addLet ( closeToken , kind ) ;
803+ this . currentScriptScopeKind = upper ;
794804 restoreCallback . end = this . script . getCurrentVirtualCodeLength ( ) ;
795805 } ) ;
796806 }
@@ -825,7 +835,19 @@ export class ScriptLetContext {
825835 // If we replace the `scope.block` at this time,
826836 // the scope restore calculation will not work, so we will replace the `scope.block` later.
827837 postprocessList . push ( ( ) => {
838+ const beforeBlock = scope . block ;
828839 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+ }
829851 } ) ;
830852
831853 const scopes = nodeToScope . get ( node ) ;
0 commit comments