@@ -102,9 +102,12 @@ function getNodeRange(
102102 ] ;
103103}
104104
105+ type StatementNodeType = `${TSESTree . Statement [ "type" ] } `;
106+
105107type RestoreCallback = {
106108 start : number ;
107109 end : number ;
110+ nodeType : StatementNodeType ;
108111 callback : ScriptLetRestoreCallback ;
109112} ;
110113
@@ -167,6 +170,7 @@ export class ScriptLetContext {
167170 `(${ part } )${ isTS ? `as (${ typing } )` : "" } ;` ,
168171 range [ 0 ] - 1 ,
169172 this . currentScriptScopeKind ,
173+ "ExpressionStatement" ,
170174 ( st , tokens , comments , result ) => {
171175 const exprSt = st as ESTree . ExpressionStatement ;
172176 const tsAs : TSAsExpression | null = isTS
@@ -223,6 +227,7 @@ export class ScriptLetContext {
223227 `({${ part } });` ,
224228 range [ 0 ] - 2 ,
225229 this . currentScriptScopeKind ,
230+ "ExpressionStatement" ,
226231 ( st , tokens , _comments , result ) => {
227232 const exprSt = st as ESTree . ExpressionStatement ;
228233 const objectExpression : ESTree . ObjectExpression =
@@ -262,6 +267,7 @@ export class ScriptLetContext {
262267 `const ${ part } ;` ,
263268 range [ 0 ] - 6 ,
264269 this . currentScriptScopeKind ,
270+ "VariableDeclaration" ,
265271 ( st , tokens , _comments , result ) => {
266272 const decl = st as ESTree . VariableDeclaration ;
267273 const node = decl . declarations [ 0 ] ;
@@ -309,6 +315,7 @@ export class ScriptLetContext {
309315 scriptLet ,
310316 ranges . idRange [ 0 ] - 5 ,
311317 "generics" ,
318+ "TSTypeAliasDeclaration" ,
312319 ( st , tokens , _comments , result ) => {
313320 const decl = st as any as TSESTree . TSTypeAliasDeclaration ;
314321 const id = decl . id ;
@@ -361,6 +368,7 @@ export class ScriptLetContext {
361368 scriptLet ,
362369 ranges . defaultRange [ 0 ] - 5 - id . length - 1 ,
363370 "generics" ,
371+ "TSTypeAliasDeclaration" ,
364372 ( st , tokens , _comments , result ) => {
365373 const decl = st as any as TSESTree . TSTypeAliasDeclaration ;
366374 const typeAnnotation = decl . typeAnnotation ;
@@ -396,6 +404,7 @@ export class ScriptLetContext {
396404 `if(${ part } ){` ,
397405 range [ 0 ] - 3 ,
398406 this . currentScriptScopeKind ,
407+ "IfStatement" ,
399408 ( st , tokens , _comments , result ) => {
400409 const ifSt = st as ESTree . IfStatement ;
401410 const node = ifSt . test ;
@@ -451,6 +460,7 @@ export class ScriptLetContext {
451460 source ,
452461 exprRange [ 0 ] - exprOffset ,
453462 this . currentScriptScopeKind ,
463+ "ExpressionStatement" ,
454464 ( st , tokens , comments , result ) => {
455465 const expSt = st as ESTree . ExpressionStatement ;
456466 const call = expSt . expression as ESTree . CallExpression ;
@@ -534,15 +544,18 @@ export class ScriptLetContext {
534544 id : ESTree . Identifier ,
535545 closeParentIndex : number ,
536546 snippetBlock : SvelteSnippetBlock ,
537- kind : "snippet" | "render" ,
547+ // If set to null, `currentScriptScopeKind` will be used.
548+ kind : "snippet" | null ,
538549 callback : ( id : ESTree . Identifier , params : ESTree . Pattern [ ] ) => void ,
539550 ) : void {
551+ const scopeKind = kind || this . currentScriptScopeKind ;
540552 const idRange = getNodeRange ( id ) ;
541553 const part = this . ctx . code . slice ( idRange [ 0 ] , closeParentIndex + 1 ) ;
542554 const restore = this . appendScript (
543555 `function ${ part } {` ,
544556 idRange [ 0 ] - 9 ,
545- kind ,
557+ scopeKind ,
558+ "FunctionDeclaration" ,
546559 ( st , tokens , _comments , result ) => {
547560 const fnDecl = st as ESTree . FunctionDeclaration ;
548561 const idNode = fnDecl . id ;
@@ -568,7 +581,7 @@ export class ScriptLetContext {
568581 fnDecl . params = [ ] ;
569582 } ,
570583 ) ;
571- this . pushScope ( restore , "}" , kind ) ;
584+ this . pushScope ( restore , "}" , scopeKind ) ;
572585 }
573586
574587 public nestBlock (
@@ -577,7 +590,7 @@ export class ScriptLetContext {
577590 | ScriptLetBlockParam [ ]
578591 | ( ( helper : TypeGenHelper | null ) => {
579592 param : ScriptLetBlockParam ;
580- preparationScript ?: string [ ] ;
593+ preparationScript ?: { script : string ; nodeType : StatementNodeType } [ ] ;
581594 } ) ,
582595 ) : void {
583596 let resolvedParams ;
@@ -590,8 +603,9 @@ export class ScriptLetContext {
590603 if ( generatedTypes . preparationScript ) {
591604 for ( const preparationScript of generatedTypes . preparationScript ) {
592605 this . appendScriptWithoutOffset (
593- preparationScript ,
606+ preparationScript . script ,
594607 this . currentScriptScopeKind ,
608+ preparationScript . nodeType ,
595609 ( node , tokens , comments , result ) => {
596610 tokens . length = 0 ;
597611 comments . length = 0 ;
@@ -612,6 +626,7 @@ export class ScriptLetContext {
612626 `{` ,
613627 block . range [ 0 ] ,
614628 this . currentScriptScopeKind ,
629+ "BlockStatement" ,
615630 ( st , tokens , _comments , result ) => {
616631 const blockSt = st as ESTree . BlockStatement ;
617632
@@ -668,6 +683,7 @@ export class ScriptLetContext {
668683 `(${ source } )=>{` ,
669684 maps [ 0 ] . range [ 0 ] - 1 ,
670685 this . currentScriptScopeKind ,
686+ "ExpressionStatement" ,
671687 ( st , tokens , comments , result ) => {
672688 const exprSt = st as ESTree . ExpressionStatement ;
673689 const fn = exprSt . expression as ESTree . ArrowFunctionExpression ;
@@ -742,6 +758,7 @@ export class ScriptLetContext {
742758 text : string ,
743759 offset : number ,
744760 kind : "generics" | "snippet" | "render" ,
761+ nodeType : StatementNodeType ,
745762 callback : (
746763 node : ESTree . Node ,
747764 tokens : Token [ ] ,
@@ -752,6 +769,7 @@ export class ScriptLetContext {
752769 const resultCallback = this . appendScriptWithoutOffset (
753770 text ,
754771 kind ,
772+ nodeType ,
755773 ( node , tokens , comments , result ) => {
756774 fixLocations (
757775 node ,
@@ -770,6 +788,7 @@ export class ScriptLetContext {
770788 private appendScriptWithoutOffset (
771789 text : string ,
772790 kind : "generics" | "snippet" | "render" ,
791+ nodeType : StatementNodeType ,
773792 callback : (
774793 node : ESTree . Node ,
775794 tokens : Token [ ] ,
@@ -785,6 +804,7 @@ export class ScriptLetContext {
785804 const restoreCallback : RestoreCallback = {
786805 start : startOffset ,
787806 end : endOffset ,
807+ nodeType,
788808 callback,
789809 } ;
790810 this . restoreCallbacks . push ( restoreCallback ) ;
@@ -918,6 +938,7 @@ export class ScriptLetContext {
918938 return ;
919939 }
920940 if (
941+ orderedRestoreCallback . nodeType === node . type &&
921942 orderedRestoreCallback . start <= node . range ! [ 0 ] &&
922943 node . range ! [ 1 ] <= orderedRestoreCallback . end
923944 ) {
0 commit comments