@@ -89,7 +89,7 @@ export class StepperArrowFunctionExpression implements ArrowFunctionExpression,
89
89
90
90
scanAllDeclarationNames ( ) : string [ ] {
91
91
const paramNames = this . params . map ( param => param . name ) ;
92
-
92
+
93
93
let bodyDeclarations : string [ ] = [ ] ;
94
94
// @ts -ignore
95
95
if ( this . body . type === 'BlockStatement' ) {
@@ -103,24 +103,32 @@ export class StepperArrowFunctionExpression implements ArrowFunctionExpression,
103
103
return [ ...paramNames , ...bodyDeclarations ] ;
104
104
}
105
105
106
- substitute ( id : StepperPattern , value : StepperExpression ) : StepperExpression {
106
+ // TODO: Fix name handling for lambda
107
+ substitute ( id : StepperPattern , value : StepperExpression , upperBoundName ?: string [ ] ) : StepperExpression {
107
108
const valueFreeNames = value . freeNames ( )
108
109
const scopeNames = this . scanAllDeclarationNames ( )
109
110
const repeatedNames = valueFreeNames . filter ( name => scopeNames . includes ( name ) )
110
111
111
112
var currentArrowFunction : StepperArrowFunctionExpression = this ;
112
- for ( var index in repeatedNames ) {
113
- const name = repeatedNames [ index ]
114
- currentArrowFunction = currentArrowFunction . rename ( name , getFreshName ( name ) ) as StepperArrowFunctionExpression
113
+ let protectedNamesSet = new Set ( [ this . allNames ( ) , upperBoundName ?? [ ] ] . flat ( ) ) ;
114
+ repeatedNames . forEach ( name => protectedNamesSet . delete ( name ) ) ;
115
+ const protectedNames = Array . from ( protectedNamesSet ) ;
116
+ const newNames = getFreshName ( repeatedNames , protectedNames ) ;
117
+ for ( var index in newNames ) {
118
+ currentArrowFunction = currentArrowFunction
119
+ . rename ( repeatedNames [ index ] , newNames [ index ] ) as StepperArrowFunctionExpression
115
120
}
116
121
122
+
117
123
if ( currentArrowFunction . scanAllDeclarationNames ( ) . includes ( id . name ) ) {
118
124
return currentArrowFunction ;
119
125
}
120
126
121
127
return new StepperArrowFunctionExpression (
122
128
currentArrowFunction . params ,
123
- currentArrowFunction . body . substitute ( id , value ) ,
129
+ currentArrowFunction . body . substitute ( id ,
130
+ value ,
131
+ currentArrowFunction . params . flatMap ( p => p . allNames ( ) ) ) ,
124
132
currentArrowFunction . name
125
133
)
126
134
}
@@ -132,6 +140,13 @@ export class StepperArrowFunctionExpression implements ArrowFunctionExpression,
132
140
return this . body . freeNames ( ) . filter ( name => ! paramNames . includes ( name ) )
133
141
}
134
142
143
+ allNames ( ) : string [ ] {
144
+ const paramNames = this . params
145
+ . filter ( param => param . type === 'Identifier' )
146
+ . map ( param => param . name )
147
+ return Array . from ( new Set ( [ paramNames , this . body . allNames ( ) ] . flat ( ) ) )
148
+ }
149
+
135
150
rename ( before : string , after : string ) : StepperExpression {
136
151
return new StepperArrowFunctionExpression (
137
152
this . params . map ( param => param . rename ( before , after ) ) ,
@@ -143,3 +158,5 @@ export class StepperArrowFunctionExpression implements ArrowFunctionExpression,
143
158
)
144
159
}
145
160
}
161
+
162
+
0 commit comments