Skip to content

Commit a19671f

Browse files
committed
added tests and logic for duplicate assignment cases for; foreach and for loops
1 parent 5a01652 commit a19671f

File tree

7 files changed

+112
-1
lines changed

7 files changed

+112
-1
lines changed

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,11 +174,25 @@ internal static Ast GetAstParentScope(Ast node)
174174
{
175175
Ast parent = node;
176176
// Walk backwards up the tree looking for a ScriptBLock of a FunctionDefinition
177-
parent = Utilities.GetAstParentOfType(parent, typeof(ScriptBlockAst), typeof(FunctionDefinitionAst));
177+
parent = Utilities.GetAstParentOfType(parent, typeof(ScriptBlockAst), typeof(FunctionDefinitionAst), typeof(ForEachStatementAst),typeof(ForStatementAst));
178178
if (parent is ScriptBlockAst && parent.Parent != null && parent.Parent is FunctionDefinitionAst)
179179
{
180180
parent = parent.Parent;
181181
}
182+
// Check if the parent of the VariableExpressionAst is a ForEachStatementAst then check if the variable names match
183+
// if so this is probably a variable defined within a foreach loop
184+
else if(parent is ForEachStatementAst ForEachStmnt && node is VariableExpressionAst VarExp &&
185+
ForEachStmnt.Variable.VariablePath.UserPath == VarExp.VariablePath.UserPath) {
186+
parent = ForEachStmnt;
187+
}
188+
// Check if the parent of the VariableExpressionAst is a ForStatementAst then check if the variable names match
189+
// if so this is probably a variable defined within a foreach loop
190+
else if(parent is ForStatementAst ForStmnt && node is VariableExpressionAst ForVarExp &&
191+
ForStmnt.Initializer is AssignmentStatementAst AssignStmnt && AssignStmnt.Left is VariableExpressionAst VarExpStmnt &&
192+
VarExpStmnt.VariablePath.UserPath == ForVarExp.VariablePath.UserPath){
193+
parent = ForStmnt;
194+
}
195+
182196
return parent;
183197
}
184198

test/PowerShellEditorServices.Test.Shared/Refactoring/Variables/RefactorVariablesData.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,5 +149,26 @@ internal static class RenameVariableData
149149
Line = 16,
150150
RenameTo = "Renamed"
151151
};
152+
public static readonly RenameSymbolParams VariableInForeachDuplicateAssignment = new()
153+
{
154+
FileName = "VariableInForeachDuplicateAssignment.ps1",
155+
Column = 18,
156+
Line = 6,
157+
RenameTo = "Renamed"
158+
};
159+
public static readonly RenameSymbolParams VariableInForloopDuplicateAssignment = new()
160+
{
161+
FileName = "VariableInForloopDuplicateAssignment.ps1",
162+
Column = 14,
163+
Line = 7,
164+
RenameTo = "Renamed"
165+
};
166+
public static readonly RenameSymbolParams VariableInWhileDuplicateAssignment = new()
167+
{
168+
FileName = "VariableInWhileDuplicateAssignment.ps1",
169+
Column = 13,
170+
Line = 7,
171+
RenameTo = "Renamed"
172+
};
152173
}
153174
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
$a = 1..5
2+
$b = 6..10
3+
function test {
4+
process {
5+
foreach ($testvar in $a) {
6+
$testvar
7+
}
8+
9+
foreach ($testvar in $b) {
10+
$testvar
11+
}
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
$a = 1..5
2+
$b = 6..10
3+
function test {
4+
process {
5+
foreach ($Renamed in $a) {
6+
$Renamed
7+
}
8+
9+
foreach ($testvar in $b) {
10+
$testvar
11+
}
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
$a = 1..5
2+
$b = 6..10
3+
function test {
4+
process {
5+
6+
for ($i = 0; $i -lt $a.Count; $i++) {
7+
$i
8+
}
9+
10+
for ($i = 0; $i -lt $a.Count; $i++) {
11+
$i
12+
}
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
$a = 1..5
2+
$b = 6..10
3+
function test {
4+
process {
5+
6+
for ($Renamed = 0; $Renamed -lt $a.Count; $Renamed++) {
7+
$Renamed
8+
}
9+
10+
for ($i = 0; $i -lt $a.Count; $i++) {
11+
$i
12+
}
13+
}
14+
}

test/PowerShellEditorServices.Test/Refactoring/RefactorVariableTests.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,28 @@ public void VarableCommandParameterSplattedFromSplat()
271271

272272
string modifiedcontent = TestRenaming(scriptFile, request);
273273

274+
Assert.Equal(expectedContent.Contents, modifiedcontent);
275+
}
276+
[Fact]
277+
public void VariableInForeachDuplicateAssignment()
278+
{
279+
RenameSymbolParams request = RenameVariableData.VariableInForeachDuplicateAssignment;
280+
ScriptFile scriptFile = GetTestScript(request.FileName);
281+
ScriptFile expectedContent = GetTestScript(request.FileName.Substring(0, request.FileName.Length - 4) + "Renamed.ps1");
282+
283+
string modifiedcontent = TestRenaming(scriptFile, request);
284+
285+
Assert.Equal(expectedContent.Contents, modifiedcontent);
286+
}
287+
[Fact]
288+
public void VariableInForloopDuplicateAssignment()
289+
{
290+
RenameSymbolParams request = RenameVariableData.VariableInForloopDuplicateAssignment;
291+
ScriptFile scriptFile = GetTestScript(request.FileName);
292+
ScriptFile expectedContent = GetTestScript(request.FileName.Substring(0, request.FileName.Length - 4) + "Renamed.ps1");
293+
294+
string modifiedcontent = TestRenaming(scriptFile, request);
295+
274296
Assert.Equal(expectedContent.Contents, modifiedcontent);
275297
}
276298
}

0 commit comments

Comments
 (0)