Skip to content

Commit 45a2c61

Browse files
authored
Fix stepper: infinite loop in findMain (#1460)
1 parent b8a7b7d commit 45a2c61

File tree

1 file changed

+21
-18
lines changed

1 file changed

+21
-18
lines changed

src/stepper/stepper.ts

+21-18
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,8 @@ function findMain(
177177
| es.BlockStatement
178178
| BlockExpression
179179
| es.FunctionDeclaration
180-
| es.Program
180+
| es.Program,
181+
seenBefore: Map<substituterNodes, substituterNodes>
181182
): string[] {
182183
const params: string[] = []
183184
if (
@@ -194,7 +195,6 @@ function findMain(
194195
}
195196

196197
const freeNames: any[] = []
197-
const seenBefore: Map<substituterNodes, substituterNodes> = new Map()
198198

199199
const finders = {
200200
Identifier(target: es.Identifier): void {
@@ -250,7 +250,7 @@ function findMain(
250250

251251
FunctionDeclaration(target: es.FunctionDeclaration): void {
252252
seenBefore.set(target, target)
253-
const freeInNested = findMain(target)
253+
const freeInNested = findMain(target, seenBefore)
254254
for (const free of freeInNested) {
255255
let bound = false
256256
for (const param of params) {
@@ -266,7 +266,7 @@ function findMain(
266266

267267
ArrowFunctionExpression(target: es.ArrowFunctionExpression): void {
268268
seenBefore.set(target, target)
269-
const freeInNested = findMain(target)
269+
const freeInNested = findMain(target, seenBefore)
270270
for (const free of freeInNested) {
271271
let bound = false
272272
for (const param of params) {
@@ -585,10 +585,10 @@ function substituteMain(
585585
replacement.type == 'FunctionExpression' ||
586586
replacement.type == 'ArrowFunctionExpression'
587587
) {
588-
freeReplacement = findMain(replacement)
588+
freeReplacement = findMain(replacement, new Map())
589589
boundReplacement = scanOutBoundNames(replacement.body)
590590
}
591-
const freeTarget = findMain(target)
591+
const freeTarget = findMain(target, new Map())
592592
const boundTarget = scanOutBoundNames(target.body)
593593
for (let i = 0; i < target.params.length; i++) {
594594
const param = target.params[i]
@@ -664,10 +664,10 @@ function substituteMain(
664664
replacement.type == 'FunctionExpression' ||
665665
replacement.type == 'ArrowFunctionExpression'
666666
) {
667-
freeReplacement = findMain(replacement)
667+
freeReplacement = findMain(replacement, new Map())
668668
boundReplacement = scanOutBoundNames(replacement.body)
669669
}
670-
const freeTarget = findMain(target)
670+
const freeTarget = findMain(target, new Map())
671671
const boundTarget = scanOutBoundNames(target.body)
672672
for (let i = 0; i < target.params.length; i++) {
673673
const param = target.params[i]
@@ -741,9 +741,9 @@ function substituteMain(
741741
replacement.type == 'ArrowFunctionExpression') &&
742742
!re.test(name.name)
743743
) {
744-
const freeTarget: string[] = findMain(target)
744+
const freeTarget: string[] = findMain(target, new Map())
745745
const declaredIds: es.Identifier[] = scanOutDeclarations(target)
746-
const freeReplacement: string[] = findMain(replacement)
746+
const freeReplacement: string[] = findMain(replacement, new Map())
747747
const boundReplacement: es.Identifier[] = scanOutDeclarations(replacement.body)
748748
for (const declaredId of declaredIds) {
749749
if (freeReplacement.includes(declaredId.name)) {
@@ -826,9 +826,9 @@ function substituteMain(
826826
replacement.type == 'ArrowFunctionExpression') &&
827827
!re.test(name.name)
828828
) {
829-
const freeTarget: string[] = findMain(target)
829+
const freeTarget: string[] = findMain(target, new Map())
830830
const declaredIds: es.Identifier[] = scanOutDeclarations(target)
831-
const freeReplacement: string[] = findMain(replacement)
831+
const freeReplacement: string[] = findMain(replacement, new Map())
832832
const boundReplacement: es.Identifier[] = scanOutDeclarations(replacement.body)
833833
for (const declaredId of declaredIds) {
834834
if (freeReplacement.includes(declaredId.name)) {
@@ -911,9 +911,9 @@ function substituteMain(
911911
replacement.type == 'ArrowFunctionExpression') &&
912912
!re.test(name.name)
913913
) {
914-
const freeTarget: string[] = findMain(target)
914+
const freeTarget: string[] = findMain(target, new Map())
915915
const declaredIds: es.Identifier[] = scanOutDeclarations(target)
916-
const freeReplacement: string[] = findMain(replacement)
916+
const freeReplacement: string[] = findMain(replacement, new Map())
917917
const boundReplacement: es.Identifier[] = scanOutDeclarations(replacement.body)
918918
for (const declaredId of declaredIds) {
919919
if (freeReplacement.includes(declaredId.name)) {
@@ -1015,7 +1015,7 @@ function substituteMain(
10151015
replacement.type == 'FunctionExpression' ||
10161016
replacement.type == 'ArrowFunctionExpression'
10171017
) {
1018-
freeReplacement = findMain(replacement)
1018+
freeReplacement = findMain(replacement, new Map())
10191019
boundReplacement = scanOutBoundNames(replacement.body)
10201020
}
10211021
for (let i = 0; i < target.params.length; i++) {
@@ -1025,7 +1025,7 @@ function substituteMain(
10251025
substedArrow.expression = target.body.type !== 'BlockStatement'
10261026
return substedArrow
10271027
}
1028-
const freeTarget = findMain(target)
1028+
const freeTarget = findMain(target, new Map())
10291029
const boundTarget = scanOutBoundNames(target.body)
10301030
if (param.type == 'Identifier') {
10311031
if (freeReplacement.includes(param.name)) {
@@ -1232,9 +1232,12 @@ function apply(
12321232
const arg = args[i]
12331233

12341234
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+
)
12361239
const declaredIds: es.Identifier[] = substedParams as es.Identifier[]
1237-
const freeReplacement: string[] = findMain(arg)
1240+
const freeReplacement: string[] = findMain(arg, new Map())
12381241
const boundReplacement: es.Identifier[] = scanOutDeclarations(arg.body)
12391242
for (const declaredId of declaredIds) {
12401243
if (freeReplacement.includes(declaredId.name)) {

0 commit comments

Comments
 (0)