@@ -177,7 +177,8 @@ function findMain(
177
177
| es . BlockStatement
178
178
| BlockExpression
179
179
| es . FunctionDeclaration
180
- | es . Program
180
+ | es . Program ,
181
+ seenBefore : Map < substituterNodes , substituterNodes >
181
182
) : string [ ] {
182
183
const params : string [ ] = [ ]
183
184
if (
@@ -194,7 +195,6 @@ function findMain(
194
195
}
195
196
196
197
const freeNames : any [ ] = [ ]
197
- const seenBefore : Map < substituterNodes , substituterNodes > = new Map ( )
198
198
199
199
const finders = {
200
200
Identifier ( target : es . Identifier ) : void {
@@ -250,7 +250,7 @@ function findMain(
250
250
251
251
FunctionDeclaration ( target : es . FunctionDeclaration ) : void {
252
252
seenBefore . set ( target , target )
253
- const freeInNested = findMain ( target )
253
+ const freeInNested = findMain ( target , seenBefore )
254
254
for ( const free of freeInNested ) {
255
255
let bound = false
256
256
for ( const param of params ) {
@@ -266,7 +266,7 @@ function findMain(
266
266
267
267
ArrowFunctionExpression ( target : es . ArrowFunctionExpression ) : void {
268
268
seenBefore . set ( target , target )
269
- const freeInNested = findMain ( target )
269
+ const freeInNested = findMain ( target , seenBefore )
270
270
for ( const free of freeInNested ) {
271
271
let bound = false
272
272
for ( const param of params ) {
@@ -585,10 +585,10 @@ function substituteMain(
585
585
replacement . type == 'FunctionExpression' ||
586
586
replacement . type == 'ArrowFunctionExpression'
587
587
) {
588
- freeReplacement = findMain ( replacement )
588
+ freeReplacement = findMain ( replacement , new Map ( ) )
589
589
boundReplacement = scanOutBoundNames ( replacement . body )
590
590
}
591
- const freeTarget = findMain ( target )
591
+ const freeTarget = findMain ( target , new Map ( ) )
592
592
const boundTarget = scanOutBoundNames ( target . body )
593
593
for ( let i = 0 ; i < target . params . length ; i ++ ) {
594
594
const param = target . params [ i ]
@@ -664,10 +664,10 @@ function substituteMain(
664
664
replacement . type == 'FunctionExpression' ||
665
665
replacement . type == 'ArrowFunctionExpression'
666
666
) {
667
- freeReplacement = findMain ( replacement )
667
+ freeReplacement = findMain ( replacement , new Map ( ) )
668
668
boundReplacement = scanOutBoundNames ( replacement . body )
669
669
}
670
- const freeTarget = findMain ( target )
670
+ const freeTarget = findMain ( target , new Map ( ) )
671
671
const boundTarget = scanOutBoundNames ( target . body )
672
672
for ( let i = 0 ; i < target . params . length ; i ++ ) {
673
673
const param = target . params [ i ]
@@ -741,9 +741,9 @@ function substituteMain(
741
741
replacement . type == 'ArrowFunctionExpression' ) &&
742
742
! re . test ( name . name )
743
743
) {
744
- const freeTarget : string [ ] = findMain ( target )
744
+ const freeTarget : string [ ] = findMain ( target , new Map ( ) )
745
745
const declaredIds : es . Identifier [ ] = scanOutDeclarations ( target )
746
- const freeReplacement : string [ ] = findMain ( replacement )
746
+ const freeReplacement : string [ ] = findMain ( replacement , new Map ( ) )
747
747
const boundReplacement : es . Identifier [ ] = scanOutDeclarations ( replacement . body )
748
748
for ( const declaredId of declaredIds ) {
749
749
if ( freeReplacement . includes ( declaredId . name ) ) {
@@ -826,9 +826,9 @@ function substituteMain(
826
826
replacement . type == 'ArrowFunctionExpression' ) &&
827
827
! re . test ( name . name )
828
828
) {
829
- const freeTarget : string [ ] = findMain ( target )
829
+ const freeTarget : string [ ] = findMain ( target , new Map ( ) )
830
830
const declaredIds : es . Identifier [ ] = scanOutDeclarations ( target )
831
- const freeReplacement : string [ ] = findMain ( replacement )
831
+ const freeReplacement : string [ ] = findMain ( replacement , new Map ( ) )
832
832
const boundReplacement : es . Identifier [ ] = scanOutDeclarations ( replacement . body )
833
833
for ( const declaredId of declaredIds ) {
834
834
if ( freeReplacement . includes ( declaredId . name ) ) {
@@ -911,9 +911,9 @@ function substituteMain(
911
911
replacement . type == 'ArrowFunctionExpression' ) &&
912
912
! re . test ( name . name )
913
913
) {
914
- const freeTarget : string [ ] = findMain ( target )
914
+ const freeTarget : string [ ] = findMain ( target , new Map ( ) )
915
915
const declaredIds : es . Identifier [ ] = scanOutDeclarations ( target )
916
- const freeReplacement : string [ ] = findMain ( replacement )
916
+ const freeReplacement : string [ ] = findMain ( replacement , new Map ( ) )
917
917
const boundReplacement : es . Identifier [ ] = scanOutDeclarations ( replacement . body )
918
918
for ( const declaredId of declaredIds ) {
919
919
if ( freeReplacement . includes ( declaredId . name ) ) {
@@ -1015,7 +1015,7 @@ function substituteMain(
1015
1015
replacement . type == 'FunctionExpression' ||
1016
1016
replacement . type == 'ArrowFunctionExpression'
1017
1017
) {
1018
- freeReplacement = findMain ( replacement )
1018
+ freeReplacement = findMain ( replacement , new Map ( ) )
1019
1019
boundReplacement = scanOutBoundNames ( replacement . body )
1020
1020
}
1021
1021
for ( let i = 0 ; i < target . params . length ; i ++ ) {
@@ -1025,7 +1025,7 @@ function substituteMain(
1025
1025
substedArrow . expression = target . body . type !== 'BlockStatement'
1026
1026
return substedArrow
1027
1027
}
1028
- const freeTarget = findMain ( target )
1028
+ const freeTarget = findMain ( target , new Map ( ) )
1029
1029
const boundTarget = scanOutBoundNames ( target . body )
1030
1030
if ( param . type == 'Identifier' ) {
1031
1031
if ( freeReplacement . includes ( param . name ) ) {
@@ -1232,9 +1232,12 @@ function apply(
1232
1232
const arg = args [ i ]
1233
1233
1234
1234
if ( arg . type === 'ArrowFunctionExpression' || arg . type === 'FunctionExpression' ) {
1235
- const freeTarget : string [ ] = findMain ( ast . arrowFunctionExpression ( substedParams , substedBody ) )
1235
+ const freeTarget : string [ ] = findMain (
1236
+ ast . arrowFunctionExpression ( substedParams , substedBody ) ,
1237
+ new Map ( )
1238
+ )
1236
1239
const declaredIds : es . Identifier [ ] = substedParams as es . Identifier [ ]
1237
- const freeReplacement : string [ ] = findMain ( arg )
1240
+ const freeReplacement : string [ ] = findMain ( arg , new Map ( ) )
1238
1241
const boundReplacement : es . Identifier [ ] = scanOutDeclarations ( arg . body )
1239
1242
for ( const declaredId of declaredIds ) {
1240
1243
if ( freeReplacement . includes ( declaredId . name ) ) {
0 commit comments