Skip to content

Commit 563e98d

Browse files
committed
adjusting iterative functions to use LookForParentOfType method
1 parent e620586 commit 563e98d

File tree

2 files changed

+9
-26
lines changed

2 files changed

+9
-26
lines changed

src/PowerShellEditorServices/Services/PowerShell/Refactoring/IterativeFunctionVistor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ public static FunctionDefinitionAst GetFunctionDefByCommandAst(string OldName, i
256256

257257
Ast parent = element.Parent;
258258
// walk backwards till we hit a functiondefinition if any
259-
parent = Utilities.LookForParentOfType<FunctionDefinitionAst>(parent);
259+
parent = Utilities.LookForParentOfType(parent,typeof(FunctionDefinitionAst));
260260

261261
// we have hit the global scope of the script file
262262
if (null == parent)

src/PowerShellEditorServices/Services/PowerShell/Refactoring/IterativeVariableVisitor.cs

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,7 @@ public IterativeVariableRename(string NewName, int StartLineNumber, int StartCol
4343
isParam = true;
4444
Ast parent = Node;
4545
// Look for a target function that the parameterAst will be within if it exists
46-
while (parent != null)
47-
{
48-
if (parent is FunctionDefinitionAst)
49-
{
50-
break;
51-
}
52-
parent = parent.Parent;
53-
}
46+
parent = Utilities.LookForParentOfType(parent,typeof(FunctionDefinitionAst));
5447
if (parent != null)
5548
{
5649
TargetFunction = (FunctionDefinitionAst)parent;
@@ -95,18 +88,15 @@ public static Ast GetVariableTopAssignment(int StartLineNumber, int StartColumnN
9588
};
9689

9790
VariableExpressionAst splatAssignment = null;
91+
// A rename of a parameter has been initiated from a splat
9892
if (node is StringConstantExpressionAst)
9993
{
10094
Ast parent = node;
101-
while (parent != null)
95+
parent = Utilities.LookForParentOfType(parent,typeof(AssignmentStatementAst));
96+
if (parent is not null and AssignmentStatementAst assignmentStatementAst)
10297
{
103-
if (parent is AssignmentStatementAst assignmentStatementAst)
104-
{
105-
splatAssignment = (VariableExpressionAst)assignmentStatementAst.Left.Find(ast => ast is VariableExpressionAst, false);
106-
107-
break;
108-
}
109-
parent = parent.Parent;
98+
splatAssignment = (VariableExpressionAst)assignmentStatementAst.Left.Find(
99+
ast => ast is VariableExpressionAst, false);
110100
}
111101
}
112102

@@ -205,15 +195,8 @@ varDef.Parent is CommandAst &&
205195
internal static Ast GetAstParentScope(Ast node)
206196
{
207197
Ast parent = node;
208-
// Walk backwards up the tree lookinf for a ScriptBLock of a FunctionDefinition
209-
while (parent != null)
210-
{
211-
if (parent is ScriptBlockAst or FunctionDefinitionAst)
212-
{
213-
break;
214-
}
215-
parent = parent.Parent;
216-
}
198+
// Walk backwards up the tree looking for a ScriptBLock of a FunctionDefinition
199+
parent = Utilities.LookForParentOfType(parent,typeof(ScriptBlockAst),typeof(FunctionDefinitionAst));
217200
if (parent is ScriptBlockAst && parent.Parent != null && parent.Parent is FunctionDefinitionAst)
218201
{
219202
parent = parent.Parent;

0 commit comments

Comments
 (0)