@@ -337,7 +337,7 @@ export class ScriptLetContext {
337
337
| string [ ]
338
338
| ( ( helper : TypeGenHelper ) => {
339
339
typings : string [ ]
340
- preparationScript ?: string
340
+ preparationScript ?: string [ ]
341
341
} ) ,
342
342
) : void
343
343
@@ -353,7 +353,7 @@ export class ScriptLetContext {
353
353
| string [ ]
354
354
| ( ( helper : TypeGenHelper ) => {
355
355
typings : string [ ]
356
- preparationScript ?: string
356
+ preparationScript ?: string [ ]
357
357
} ) ,
358
358
) : void {
359
359
let arrayTypings : string [ ] = [ ]
@@ -366,14 +366,16 @@ export class ScriptLetContext {
366
366
} )
367
367
arrayTypings = generatedTypes . typings
368
368
if ( generatedTypes . preparationScript ) {
369
- this . appendScriptWithoutOffset (
370
- generatedTypes . preparationScript ,
371
- ( node , tokens , comments , result ) => {
372
- tokens . length = 0
373
- comments . length = 0
374
- removeAllReference ( node , result )
375
- } ,
376
- )
369
+ for ( const preparationScript of generatedTypes . preparationScript ) {
370
+ this . appendScriptWithoutOffset (
371
+ preparationScript ,
372
+ ( node , tokens , comments , result ) => {
373
+ tokens . length = 0
374
+ comments . length = 0
375
+ removeAllScope ( node , result )
376
+ } ,
377
+ )
378
+ }
377
379
}
378
380
}
379
381
}
@@ -445,7 +447,7 @@ export class ScriptLetContext {
445
447
column : typeAnnotation . loc . start . column ,
446
448
}
447
449
448
- removeAllReference ( typeAnnotation , result )
450
+ removeAllScope ( typeAnnotation , result )
449
451
}
450
452
}
451
453
@@ -869,23 +871,55 @@ function applyLocs(target: Locations | ESTree.Node, locs: Locations) {
869
871
}
870
872
871
873
/** Remove all reference */
872
- function removeAllReference (
873
- target : ESTree . Node ,
874
- result : ScriptLetCallbackOption ,
875
- ) {
874
+ function removeAllScope ( target : ESTree . Node , result : ScriptLetCallbackOption ) {
875
+ const targetScopes = new Set < Scope > ( )
876
876
traverseNodes ( target , {
877
877
visitorKeys : result . visitorKeys ,
878
878
enterNode ( node ) {
879
+ const scope = result . scopeManager . acquire ( node )
880
+ if ( scope ) {
881
+ targetScopes . add ( scope )
882
+ return
883
+ }
879
884
if ( node . type === "Identifier" ) {
880
- const scope = result . getScope ( node )
885
+ let scope = result . getScope ( node )
886
+ if (
887
+ ( scope . block as any ) . type === "TSTypeAliasDeclaration" &&
888
+ ( scope . block as any ) . id === node
889
+ ) {
890
+ scope = scope . upper !
891
+ }
892
+ if ( targetScopes . has ( scope ) ) {
893
+ return
894
+ }
881
895
896
+ removeIdentifierVariable ( node , scope )
882
897
removeIdentifierReference ( node , scope )
883
898
}
884
899
} ,
885
900
leaveNode ( ) {
886
901
// noop
887
902
} ,
888
903
} )
904
+
905
+ for ( const scope of targetScopes ) {
906
+ removeScope ( result . scopeManager , scope )
907
+ }
908
+ }
909
+
910
+ /** Remove variable */
911
+ function removeIdentifierVariable ( node : ESTree . Identifier , scope : Scope ) : void {
912
+ const varIndex = scope . variables . findIndex ( ( v ) =>
913
+ v . defs . some ( ( def ) => def . name === node ) ,
914
+ )
915
+ if ( varIndex >= 0 ) {
916
+ const variable = scope . variables [ varIndex ]
917
+ scope . variables . splice ( varIndex , 1 )
918
+ const name = node . name
919
+ if ( variable === scope . set . get ( name ) ) {
920
+ scope . set . delete ( name )
921
+ }
922
+ }
889
923
}
890
924
891
925
/** Remove reference */
0 commit comments