Skip to content

Commit 3a91195

Browse files
Fixed erroneous PSUseDeclaredVarsMoreThanAssignments for some globals variables (#2013)
* Fixed erroneous PSUseDeclaredVarsMoreThanAssignments for some globals. * Added unit tests to cover global bug in UseDeclaredVarsMoreThanAssignments. --------- Co-authored-by: Christoph Bergmeister <[email protected]>
1 parent 004c8e0 commit 3a91195

File tree

4 files changed

+47
-11
lines changed

4 files changed

+47
-11
lines changed

Engine/Helper.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -870,19 +870,13 @@ public bool IsUninitialized(VariableExpressionAst varAst, Ast ast)
870870
}
871871

872872
/// <summary>
873-
/// Returns true if varaible is either a global variable or an environment variable
873+
/// Returns true if variable is either a global variable or an environment variable
874874
/// </summary>
875875
/// <param name="varAst"></param>
876-
/// <param name="ast"></param>
877876
/// <returns></returns>
878-
public bool IsVariableGlobalOrEnvironment(VariableExpressionAst varAst, Ast ast)
877+
public bool IsVariableGlobalOrEnvironment(VariableExpressionAst varAst)
879878
{
880-
if (!VariableAnalysisDictionary.ContainsKey(ast) || VariableAnalysisDictionary[ast] == null)
881-
{
882-
return false;
883-
}
884-
885-
return VariableAnalysisDictionary[ast].IsGlobalOrEnvironment(varAst);
879+
return VariableAnalysis.IsGlobalOrEnvironment(varAst);
886880
}
887881

888882

Engine/VariableAnalysis.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ public bool IsUninitialized(VariableExpressionAst varTarget)
375375
/// </summary>
376376
/// <param name="varTarget"></param>
377377
/// <returns></returns>
378-
public bool IsGlobalOrEnvironment(VariableExpressionAst varTarget)
378+
public static bool IsGlobalOrEnvironment(VariableExpressionAst varTarget)
379379
{
380380
if (varTarget != null)
381381
{

Rules/UseDeclaredVarsMoreThanAssignments.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ private IEnumerable<DiagnosticRecord> AnalyzeScriptBlockAst(ScriptBlockAst scrip
143143
if (assignmentVarAst != null)
144144
{
145145
// Ignore if variable is global or environment variable or scope is drive qualified variable
146-
if (!Helper.Instance.IsVariableGlobalOrEnvironment(assignmentVarAst, scriptBlockAst)
146+
if (!Helper.Instance.IsVariableGlobalOrEnvironment(assignmentVarAst)
147147
&& !assignmentVarAst.VariablePath.IsScript
148148
&& assignmentVarAst.VariablePath.DriveName == null)
149149
{

Tests/Rules/UseDeclaredVarsMoreThanAssignments.tests.ps1

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,48 @@ function MyFunc2() {
5858
Should -Be 0
5959
}
6060

61+
It "does not flag global variable" {
62+
Invoke-ScriptAnalyzer -ScriptDefinition '$global:x=$null' -IncludeRule $violationName | `
63+
Get-Count | `
64+
Should -Be 0
65+
}
66+
67+
It "does not flag global variable in block" {
68+
Invoke-ScriptAnalyzer -ScriptDefinition '$global:x=$null;{$global:x=$null}' -IncludeRule $violationName | `
69+
Get-Count | `
70+
Should -Be 0
71+
}
72+
73+
It "does not flag env variable" {
74+
Invoke-ScriptAnalyzer -ScriptDefinition '$env:x=$null' -IncludeRule $violationName | `
75+
Get-Count | `
76+
Should -Be 0
77+
}
78+
79+
It "does not flag env variable in block" {
80+
Invoke-ScriptAnalyzer -ScriptDefinition '$env:x=$null;{$env:x=$null}' -IncludeRule $violationName | `
81+
Get-Count | `
82+
Should -Be 0
83+
}
84+
85+
It "does not flag script variable" {
86+
Invoke-ScriptAnalyzer -ScriptDefinition '$script:x=$null' -IncludeRule $violationName | `
87+
Get-Count | `
88+
Should -Be 0
89+
}
90+
91+
It "does not flag script variable in block" {
92+
Invoke-ScriptAnalyzer -ScriptDefinition '$script:x=$null;{$script:x=$null}' -IncludeRule $violationName | `
93+
Get-Count | `
94+
Should -Be 0
95+
}
96+
97+
It "flags private variable" {
98+
Invoke-ScriptAnalyzer -ScriptDefinition '$private:x=$null' -IncludeRule $violationName | `
99+
Get-Count | `
100+
Should -Be 1
101+
}
102+
61103
It "flags a variable that is defined twice but never used" {
62104
Invoke-ScriptAnalyzer -ScriptDefinition '$myvar=1;$myvar=2' -IncludeRule $violationName | `
63105
Get-Count | `

0 commit comments

Comments
 (0)