Skip to content

Commit 01a3259

Browse files
poshAJPoshAJ
and
PoshAJ
authored
Add foreach Assignment to AvoidAssignmentToAutomaticVariable (#2021)
* Add Evaluation for foreach Assignment * Add Tests for foreach Assignment * Update Tests for Consistency * Remove Unnecessary ExcludeRule * Update Test Description --------- Co-authored-by: PoshAJ <[email protected]>
1 parent d13809c commit 01a3259

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

Rules/AvoidAssignmentToAutomaticVariable.cs

+25
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,31 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
7979
}
8080
}
8181

82+
IEnumerable<Ast> forEachStatementAsts = ast.FindAll(testAst => testAst is ForEachStatementAst, searchNestedScriptBlocks: true);
83+
foreach (ForEachStatementAst forEachStatementAst in forEachStatementAsts)
84+
{
85+
var variableExpressionAst = forEachStatementAst.Variable;
86+
var variableName = variableExpressionAst.VariablePath.UserPath;
87+
if (_readOnlyAutomaticVariables.Contains(variableName, StringComparer.OrdinalIgnoreCase))
88+
{
89+
yield return new DiagnosticRecord(DiagnosticRecordHelper.FormatError(Strings.AvoidAssignmentToReadOnlyAutomaticVariableError, variableName),
90+
variableExpressionAst.Extent, GetName(), DiagnosticSeverity.Error, fileName, variableName);
91+
}
92+
93+
if (_readOnlyAutomaticVariablesIntroducedInVersion6_0.Contains(variableName, StringComparer.OrdinalIgnoreCase))
94+
{
95+
var severity = IsPowerShellVersion6OrGreater() ? DiagnosticSeverity.Error : DiagnosticSeverity.Warning;
96+
yield return new DiagnosticRecord(DiagnosticRecordHelper.FormatError(Strings.AvoidAssignmentToReadOnlyAutomaticVariableIntroducedInPowerShell6_0Error, variableName),
97+
variableExpressionAst.Extent, GetName(), severity, fileName, variableName);
98+
}
99+
100+
if (_writableAutomaticVariables.Contains(variableName, StringComparer.OrdinalIgnoreCase))
101+
{
102+
yield return new DiagnosticRecord(DiagnosticRecordHelper.FormatError(Strings.AvoidAssignmentToWritableAutomaticVariableError, variableName),
103+
variableExpressionAst.Extent, GetName(), DiagnosticSeverity.Warning, fileName, variableName);
104+
}
105+
}
106+
82107
IEnumerable<Ast> parameterAsts = ast.FindAll(testAst => testAst is ParameterAst, searchNestedScriptBlocks: true);
83108
foreach (ParameterAst parameterAst in parameterAsts)
84109
{

Tests/Rules/AvoidAssignmentToAutomaticVariable.tests.ps1

+12-3
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,22 @@ Describe "AvoidAssignmentToAutomaticVariables" {
6565
It "Variable <VariableName> produces warning of Severity <ExpectedSeverity>" -TestCases $testCases_AutomaticVariables {
6666
param ($VariableName, $ExpectedSeverity)
6767

68-
$warnings = Invoke-ScriptAnalyzer -ScriptDefinition "`$${VariableName} = 'foo'" -ExcludeRule PSUseDeclaredVarsMoreThanAssignments
68+
[System.Array] $warnings = Invoke-ScriptAnalyzer -ScriptDefinition "`$${VariableName} = 'foo'" -ExcludeRule PSUseDeclaredVarsMoreThanAssignments
6969
$warnings.Count | Should -Be 1
7070
$warnings.Severity | Should -Be $ExpectedSeverity
7171
$warnings.RuleName | Should -Be $ruleName
7272
}
7373

74-
It "Using Variable <VariableName> as parameter name produces warning of Severity error" -TestCases $testCases_AutomaticVariables {
74+
It "Using Variable <VariableName> as foreach assignment produces warning of Severity <ExpectedSeverity>" -TestCases $testCases_AutomaticVariables {
75+
param ($VariableName, $ExpectedSeverity)
76+
77+
[System.Array] $warnings = Invoke-ScriptAnalyzer -ScriptDefinition "foreach (`$$VariableName in `$foo) {}"
78+
$warnings.Count | Should -Be 1
79+
$warnings.Severity | Should -Be $ExpectedSeverity
80+
$warnings.RuleName | Should -Be $ruleName
81+
}
82+
83+
It "Using Variable <VariableName> as parameter name produces warning of Severity <ExpectedSeverity>" -TestCases $testCases_AutomaticVariables {
7584
param ($VariableName, $ExpectedSeverity)
7685

7786
[System.Array] $warnings = Invoke-ScriptAnalyzer -ScriptDefinition "function foo{Param(`$$VariableName)}" -ExcludeRule PSReviewUnusedParameter
@@ -80,7 +89,7 @@ Describe "AvoidAssignmentToAutomaticVariables" {
8089
$warnings.RuleName | Should -Be $ruleName
8190
}
8291

83-
It "Using Variable <VariableName> as parameter name in param block produces warning of Severity error" -TestCases $testCases_AutomaticVariables {
92+
It "Using Variable <VariableName> as parameter name in param block produces warning of Severity <ExpectedSeverity>" -TestCases $testCases_AutomaticVariables {
8493
param ($VariableName, $ExpectedSeverity)
8594

8695
[System.Array] $warnings = Invoke-ScriptAnalyzer -ScriptDefinition "function foo(`$$VariableName){}"

0 commit comments

Comments
 (0)