@@ -80,6 +80,12 @@ export function analyzeTypeScriptInSvelte(
80
80
81
81
analyzeRenderScopes ( code , ctx ) ;
82
82
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
+ // see: https://github.com/sveltejs/svelte-eslint-parser/issues/557
85
+ if ( ! hasExportDeclaration ( result . ast ) ) {
86
+ appendDummyExport ( ctx ) ;
87
+ }
88
+
83
89
ctx . appendOriginalToEnd ( ) ;
84
90
85
91
return ctx ;
@@ -118,6 +124,18 @@ export function analyzeTypeScript(
118
124
return ctx ;
119
125
}
120
126
127
+ function hasExportDeclaration ( ast : TSESParseForESLintResult [ "ast" ] ) : boolean {
128
+ for ( const node of ast . body ) {
129
+ if (
130
+ node . type === "ExportNamedDeclaration" ||
131
+ node . type === "ExportDefaultDeclaration"
132
+ ) {
133
+ return true ;
134
+ }
135
+ }
136
+ return false ;
137
+ }
138
+
121
139
/**
122
140
* Analyze the store reference names.
123
141
* Insert type definitions code to provide correct type information for variables that begin with `$`.
@@ -319,6 +337,34 @@ function analyzeDollarDollarVariables(
319
337
}
320
338
}
321
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
+
322
368
/**
323
369
* Analyze Runes.
324
370
* Insert type definitions code to provide correct type information for Runes.
@@ -569,24 +615,29 @@ function analyzeRenderScopes(
569
615
) {
570
616
ctx . appendOriginal ( code . script . length ) ;
571
617
const renderFunctionName = ctx . generateUniqueId ( "render" ) ;
572
- ctx . appendVirtualScript ( `function ${ renderFunctionName } (){` ) ;
618
+ ctx . appendVirtualScript ( `export function ${ renderFunctionName } (){` ) ;
573
619
ctx . appendOriginal ( code . script . length + code . render . length ) ;
574
620
ctx . appendVirtualScript ( `}` ) ;
575
621
ctx . restoreContext . addRestoreStatementProcess ( ( node , result ) => {
576
622
if (
577
- node . type !== "FunctionDeclaration" ||
578
- node . id . name !== renderFunctionName
623
+ node . type !== "ExportNamedDeclaration" ||
624
+ node . declaration ?. type !== "FunctionDeclaration" ||
625
+ node . declaration ?. id ?. name !== renderFunctionName
579
626
) {
580
627
return false ;
581
628
}
582
629
const program = result . ast ;
583
- program . body . splice ( program . body . indexOf ( node ) , 1 , ...node . body . body ) ;
584
- 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 ) {
585
636
body . parent = program ;
586
637
}
587
638
588
639
const scopeManager = result . scopeManager as ScopeManager ;
589
- removeFunctionScope ( node , scopeManager ) ;
640
+ removeFunctionScope ( node . declaration , scopeManager ) ;
590
641
return true ;
591
642
} ) ;
592
643
}
@@ -843,7 +894,7 @@ function transformForReactiveStatement(
843
894
const functionId = ctx . generateUniqueId ( "reactiveStatementScopeFunction" ) ;
844
895
const originalBody = statement . body ;
845
896
ctx . appendOriginal ( originalBody . range [ 0 ] ) ;
846
- ctx . appendVirtualScript ( `function ${ functionId } (){` ) ;
897
+ ctx . appendVirtualScript ( `export function ${ functionId } (){` ) ;
847
898
ctx . appendOriginal ( originalBody . range [ 1 ] ) ;
848
899
ctx . appendVirtualScript ( `}` ) ;
849
900
ctx . appendOriginal ( statement . range [ 1 ] ) ;
@@ -854,14 +905,18 @@ function transformForReactiveStatement(
854
905
}
855
906
const reactiveStatement = node as TSESTree . LabeledStatement ;
856
907
const body = reactiveStatement . body ;
857
- 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
+ ) {
858
913
return false ;
859
914
}
860
- reactiveStatement . body = body . body . body [ 0 ] ;
915
+ reactiveStatement . body = body . declaration . body . body [ 0 ] ;
861
916
reactiveStatement . body . parent = reactiveStatement ;
862
917
863
918
const scopeManager = result . scopeManager as ScopeManager ;
864
- removeFunctionScope ( body , scopeManager ) ;
919
+ removeFunctionScope ( body . declaration , scopeManager ) ;
865
920
return true ;
866
921
} ) ;
867
922
}
0 commit comments