diff --git a/Engine/Helper.cs b/Engine/Helper.cs
index ded37b011..82948a4fc 100644
--- a/Engine/Helper.cs
+++ b/Engine/Helper.cs
@@ -870,19 +870,13 @@ public bool IsUninitialized(VariableExpressionAst varAst, Ast ast)
}
///
- /// Returns true if varaible is either a global variable or an environment variable
+ /// Returns true if variable is either a global variable or an environment variable
///
///
- ///
///
- public bool IsVariableGlobalOrEnvironment(VariableExpressionAst varAst, Ast ast)
+ public bool IsVariableGlobalOrEnvironment(VariableExpressionAst varAst)
{
- if (!VariableAnalysisDictionary.ContainsKey(ast) || VariableAnalysisDictionary[ast] == null)
- {
- return false;
- }
-
- return VariableAnalysisDictionary[ast].IsGlobalOrEnvironment(varAst);
+ return VariableAnalysis.IsGlobalOrEnvironment(varAst);
}
diff --git a/Engine/VariableAnalysis.cs b/Engine/VariableAnalysis.cs
index fd66ea2c4..2870d442f 100644
--- a/Engine/VariableAnalysis.cs
+++ b/Engine/VariableAnalysis.cs
@@ -375,7 +375,7 @@ public bool IsUninitialized(VariableExpressionAst varTarget)
///
///
///
- public bool IsGlobalOrEnvironment(VariableExpressionAst varTarget)
+ public static bool IsGlobalOrEnvironment(VariableExpressionAst varTarget)
{
if (varTarget != null)
{
diff --git a/Rules/UseDeclaredVarsMoreThanAssignments.cs b/Rules/UseDeclaredVarsMoreThanAssignments.cs
index 5a8440ada..b35caafbc 100644
--- a/Rules/UseDeclaredVarsMoreThanAssignments.cs
+++ b/Rules/UseDeclaredVarsMoreThanAssignments.cs
@@ -143,7 +143,7 @@ private IEnumerable AnalyzeScriptBlockAst(ScriptBlockAst scrip
if (assignmentVarAst != null)
{
// Ignore if variable is global or environment variable or scope is drive qualified variable
- if (!Helper.Instance.IsVariableGlobalOrEnvironment(assignmentVarAst, scriptBlockAst)
+ if (!Helper.Instance.IsVariableGlobalOrEnvironment(assignmentVarAst)
&& !assignmentVarAst.VariablePath.IsScript
&& assignmentVarAst.VariablePath.DriveName == null)
{
diff --git a/Tests/Rules/UseDeclaredVarsMoreThanAssignments.tests.ps1 b/Tests/Rules/UseDeclaredVarsMoreThanAssignments.tests.ps1
index 823334afb..592aecc91 100644
--- a/Tests/Rules/UseDeclaredVarsMoreThanAssignments.tests.ps1
+++ b/Tests/Rules/UseDeclaredVarsMoreThanAssignments.tests.ps1
@@ -58,6 +58,48 @@ function MyFunc2() {
Should -Be 0
}
+ It "does not flag global variable" {
+ Invoke-ScriptAnalyzer -ScriptDefinition '$global:x=$null' -IncludeRule $violationName | `
+ Get-Count | `
+ Should -Be 0
+ }
+
+ It "does not flag global variable in block" {
+ Invoke-ScriptAnalyzer -ScriptDefinition '$global:x=$null;{$global:x=$null}' -IncludeRule $violationName | `
+ Get-Count | `
+ Should -Be 0
+ }
+
+ It "does not flag env variable" {
+ Invoke-ScriptAnalyzer -ScriptDefinition '$env:x=$null' -IncludeRule $violationName | `
+ Get-Count | `
+ Should -Be 0
+ }
+
+ It "does not flag env variable in block" {
+ Invoke-ScriptAnalyzer -ScriptDefinition '$env:x=$null;{$env:x=$null}' -IncludeRule $violationName | `
+ Get-Count | `
+ Should -Be 0
+ }
+
+ It "does not flag script variable" {
+ Invoke-ScriptAnalyzer -ScriptDefinition '$script:x=$null' -IncludeRule $violationName | `
+ Get-Count | `
+ Should -Be 0
+ }
+
+ It "does not flag script variable in block" {
+ Invoke-ScriptAnalyzer -ScriptDefinition '$script:x=$null;{$script:x=$null}' -IncludeRule $violationName | `
+ Get-Count | `
+ Should -Be 0
+ }
+
+ It "flags private variable" {
+ Invoke-ScriptAnalyzer -ScriptDefinition '$private:x=$null' -IncludeRule $violationName | `
+ Get-Count | `
+ Should -Be 1
+ }
+
It "flags a variable that is defined twice but never used" {
Invoke-ScriptAnalyzer -ScriptDefinition '$myvar=1;$myvar=2' -IncludeRule $violationName | `
Get-Count | `