@@ -25,7 +25,7 @@ internal class IterativeVariableRename
25
25
internal FunctionDefinitionAst TargetFunction ;
26
26
internal RenameSymbolOptions options ;
27
27
28
- public IterativeVariableRename ( string NewName , int StartLineNumber , int StartColumnNumber , Ast ScriptAst , RenameSymbolOptions options = null )
28
+ public IterativeVariableRename ( string NewName , int StartLineNumber , int StartColumnNumber , Ast ScriptAst , RenameSymbolOptions options = null )
29
29
{
30
30
this . NewName = NewName ;
31
31
this . StartLineNumber = StartLineNumber ;
@@ -185,38 +185,75 @@ internal static Ast GetAstParentScope(Ast node)
185
185
{
186
186
Ast parent = node ;
187
187
// Walk backwards up the tree looking for a ScriptBLock of a FunctionDefinition
188
- parent = Utilities . GetAstParentOfType ( parent , typeof ( ScriptBlockAst ) , typeof ( FunctionDefinitionAst ) , typeof ( ForEachStatementAst ) , typeof ( ForStatementAst ) ) ;
188
+ parent = Utilities . GetAstParentOfType ( parent , typeof ( ScriptBlockAst ) , typeof ( FunctionDefinitionAst ) , typeof ( ForEachStatementAst ) , typeof ( ForStatementAst ) ) ;
189
189
if ( parent is ScriptBlockAst && parent . Parent != null && parent . Parent is FunctionDefinitionAst )
190
190
{
191
191
parent = parent . Parent ;
192
192
}
193
193
// Check if the parent of the VariableExpressionAst is a ForEachStatementAst then check if the variable names match
194
194
// if so this is probably a variable defined within a foreach loop
195
- else if ( parent is ForEachStatementAst ForEachStmnt && node is VariableExpressionAst VarExp &&
196
- ForEachStmnt . Variable . VariablePath . UserPath == VarExp . VariablePath . UserPath ) {
195
+ else if ( parent is ForEachStatementAst ForEachStmnt && node is VariableExpressionAst VarExp &&
196
+ ForEachStmnt . Variable . VariablePath . UserPath == VarExp . VariablePath . UserPath )
197
+ {
197
198
parent = ForEachStmnt ;
198
199
}
199
200
// Check if the parent of the VariableExpressionAst is a ForStatementAst then check if the variable names match
200
201
// if so this is probably a variable defined within a foreach loop
201
- else if ( parent is ForStatementAst ForStmnt && node is VariableExpressionAst ForVarExp &&
202
+ else if ( parent is ForStatementAst ForStmnt && node is VariableExpressionAst ForVarExp &&
202
203
ForStmnt . Initializer is AssignmentStatementAst AssignStmnt && AssignStmnt . Left is VariableExpressionAst VarExpStmnt &&
203
- VarExpStmnt . VariablePath . UserPath == ForVarExp . VariablePath . UserPath ) {
204
+ VarExpStmnt . VariablePath . UserPath == ForVarExp . VariablePath . UserPath )
205
+ {
204
206
parent = ForStmnt ;
205
207
}
206
208
207
209
return parent ;
208
210
}
209
211
212
+ internal static bool IsVariableExpressionAssignedInTargetScope ( VariableExpressionAst node , Ast scope )
213
+ {
214
+ bool r = false ;
215
+
216
+ List < VariableExpressionAst > VariableAssignments = node . FindAll ( ast =>
217
+ {
218
+ return ast is VariableExpressionAst VarDef &&
219
+ VarDef . Parent is AssignmentStatementAst or ParameterAst &&
220
+ VarDef . VariablePath . UserPath . ToLower ( ) == node . VariablePath . UserPath . ToLower ( ) &&
221
+ // Look Backwards from the node above
222
+ ( VarDef . Extent . EndLineNumber < node . Extent . StartLineNumber ||
223
+ ( VarDef . Extent . EndColumnNumber <= node . Extent . StartColumnNumber &&
224
+ VarDef . Extent . EndLineNumber <= node . Extent . StartLineNumber ) ) &&
225
+ // Must be within the the designated scope
226
+ VarDef . Extent . StartLineNumber >= scope . Extent . StartLineNumber ;
227
+ } , true ) . Cast < VariableExpressionAst > ( ) . ToList ( ) ;
228
+
229
+ if ( VariableAssignments . Count > 0 )
230
+ {
231
+ r = true ;
232
+ }
233
+ // Node is probably the first Assignment Statement within scope
234
+ if ( node . Parent is AssignmentStatementAst && node . Extent . StartLineNumber >= scope . Extent . StartLineNumber )
235
+ {
236
+ r = true ;
237
+ }
238
+
239
+ return r ;
240
+ }
241
+
210
242
internal static bool WithinTargetsScope ( Ast Target , Ast Child )
211
243
{
212
244
bool r = false ;
213
245
Ast childParent = Child . Parent ;
214
246
Ast TargetScope = GetAstParentScope ( Target ) ;
215
247
while ( childParent != null )
216
248
{
217
- if ( childParent is FunctionDefinitionAst )
249
+ if ( childParent is FunctionDefinitionAst FuncDefAst )
218
250
{
219
- break ;
251
+ if ( Child is VariableExpressionAst VarExpAst && ! IsVariableExpressionAssignedInTargetScope ( VarExpAst , FuncDefAst ) )
252
+ {
253
+
254
+ } else {
255
+ break ;
256
+ }
220
257
}
221
258
if ( childParent == TargetScope )
222
259
{
0 commit comments