@@ -83,7 +83,7 @@ export function analyzeTypeScriptInSvelte(
83
83
// When performing type checking on TypeScript code that is not a module, the error `Cannot redeclare block-scoped variable 'xxx'`. occurs. To fix this, add an `export`.
84
84
// see: https://github.com/sveltejs/svelte-eslint-parser/issues/557
85
85
if ( ! hasExportDeclaration ( result . ast ) ) {
86
- ctx . appendVirtualScript ( "export {};" ) ;
86
+ appendDummyExport ( ctx ) ;
87
87
}
88
88
89
89
ctx . appendOriginalToEnd ( ) ;
@@ -337,6 +337,34 @@ function analyzeDollarDollarVariables(
337
337
}
338
338
}
339
339
340
+ /** Append dummy export */
341
+ function appendDummyExport ( ctx : VirtualTypeScriptContext ) {
342
+ ctx . appendVirtualScript ( `export namespace SvelteEslintParserModuleMarker {}` ) ;
343
+ ctx . restoreContext . addRestoreStatementProcess ( ( node , result ) => {
344
+ if (
345
+ node . type !== "ExportNamedDeclaration" ||
346
+ node . declaration ?. type !== "TSModuleDeclaration" ||
347
+ node . declaration . kind !== "namespace" ||
348
+ node . declaration . id . type !== "Identifier" ||
349
+ node . declaration . id . name !== "SvelteEslintParserModuleMarker"
350
+ ) {
351
+ return false ;
352
+ }
353
+ const program = result . ast ;
354
+ program . body . splice ( program . body . indexOf ( node ) , 1 ) ;
355
+
356
+ const scopeManager = result . scopeManager as ScopeManager ;
357
+
358
+ // Remove `declare` variable
359
+ removeAllScopeAndVariableAndReference ( node , {
360
+ visitorKeys : result . visitorKeys ,
361
+ scopeManager,
362
+ } ) ;
363
+
364
+ return true ;
365
+ } ) ;
366
+ }
367
+
340
368
/**
341
369
* Analyze Runes.
342
370
* Insert type definitions code to provide correct type information for Runes.
@@ -398,7 +426,7 @@ function analyzeRuneVariables(
398
426
// See https://github.com/sveltejs/svelte/blob/41b5cd6f5daae3970a9927e062f42b6b62440d16/packages/svelte/types/index.d.ts#L2615
399
427
case "$props" : {
400
428
// Use type parameters to avoid `@typescript-eslint/no-unsafe-assignment` errors.
401
- appendDeclareFunctionVirtualScripts ( globalName , [ "(): any " ] ) ;
429
+ appendDeclareFunctionVirtualScripts ( globalName , [ "<T> (): T " ] ) ;
402
430
break ;
403
431
}
404
432
// See https://github.com/sveltejs/svelte/blob/41b5cd6f5daae3970a9927e062f42b6b62440d16/packages/svelte/types/index.d.ts#L2626
@@ -587,24 +615,29 @@ function analyzeRenderScopes(
587
615
) {
588
616
ctx . appendOriginal ( code . script . length ) ;
589
617
const renderFunctionName = ctx . generateUniqueId ( "render" ) ;
590
- ctx . appendVirtualScript ( `function ${ renderFunctionName } (){` ) ;
618
+ ctx . appendVirtualScript ( `export function ${ renderFunctionName } (){` ) ;
591
619
ctx . appendOriginal ( code . script . length + code . render . length ) ;
592
620
ctx . appendVirtualScript ( `}` ) ;
593
621
ctx . restoreContext . addRestoreStatementProcess ( ( node , result ) => {
594
622
if (
595
- node . type !== "FunctionDeclaration" ||
596
- node . id . name !== renderFunctionName
623
+ node . type !== "ExportNamedDeclaration" ||
624
+ node . declaration ?. type !== "FunctionDeclaration" ||
625
+ node . declaration ?. id ?. name !== renderFunctionName
597
626
) {
598
627
return false ;
599
628
}
600
629
const program = result . ast ;
601
- program . body . splice ( program . body . indexOf ( node ) , 1 , ...node . body . body ) ;
602
- for ( const body of node . body . body ) {
630
+ program . body . splice (
631
+ program . body . indexOf ( node ) ,
632
+ 1 ,
633
+ ...node . declaration . body . body ,
634
+ ) ;
635
+ for ( const body of node . declaration . body . body ) {
603
636
body . parent = program ;
604
637
}
605
638
606
639
const scopeManager = result . scopeManager as ScopeManager ;
607
- removeFunctionScope ( node , scopeManager ) ;
640
+ removeFunctionScope ( node . declaration , scopeManager ) ;
608
641
return true ;
609
642
} ) ;
610
643
}
@@ -861,7 +894,7 @@ function transformForReactiveStatement(
861
894
const functionId = ctx . generateUniqueId ( "reactiveStatementScopeFunction" ) ;
862
895
const originalBody = statement . body ;
863
896
ctx . appendOriginal ( originalBody . range [ 0 ] ) ;
864
- ctx . appendVirtualScript ( `function ${ functionId } (){` ) ;
897
+ ctx . appendVirtualScript ( `export function ${ functionId } (){` ) ;
865
898
ctx . appendOriginal ( originalBody . range [ 1 ] ) ;
866
899
ctx . appendVirtualScript ( `}` ) ;
867
900
ctx . appendOriginal ( statement . range [ 1 ] ) ;
@@ -872,14 +905,18 @@ function transformForReactiveStatement(
872
905
}
873
906
const reactiveStatement = node as TSESTree . LabeledStatement ;
874
907
const body = reactiveStatement . body ;
875
- if ( body . type !== "FunctionDeclaration" || body . id . name !== functionId ) {
908
+ if (
909
+ body . type !== "ExportNamedDeclaration" ||
910
+ body . declaration ?. type !== "FunctionDeclaration" ||
911
+ body . declaration ?. id ?. name !== functionId
912
+ ) {
876
913
return false ;
877
914
}
878
- reactiveStatement . body = body . body . body [ 0 ] ;
915
+ reactiveStatement . body = body . declaration . body . body [ 0 ] ;
879
916
reactiveStatement . body . parent = reactiveStatement ;
880
917
881
918
const scopeManager = result . scopeManager as ScopeManager ;
882
- removeFunctionScope ( body , scopeManager ) ;
919
+ removeFunctionScope ( body . declaration , scopeManager ) ;
883
920
return true ;
884
921
} ) ;
885
922
}
0 commit comments