Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed erroneous PSUseDeclaredVarsMoreThanAssignments for some globals variables #2013

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 3 additions & 9 deletions Engine/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -870,19 +870,13 @@ public bool IsUninitialized(VariableExpressionAst varAst, Ast ast)
}

/// <summary>
/// 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
/// </summary>
/// <param name="varAst"></param>
/// <param name="ast"></param>
/// <returns></returns>
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);
}


Expand Down
2 changes: 1 addition & 1 deletion Engine/VariableAnalysis.cs
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ public bool IsUninitialized(VariableExpressionAst varTarget)
/// </summary>
/// <param name="varTarget"></param>
/// <returns></returns>
public bool IsGlobalOrEnvironment(VariableExpressionAst varTarget)
public static bool IsGlobalOrEnvironment(VariableExpressionAst varTarget)
{
if (varTarget != null)
{
Expand Down
2 changes: 1 addition & 1 deletion Rules/UseDeclaredVarsMoreThanAssignments.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ private IEnumerable<DiagnosticRecord> 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)
{
Expand Down
42 changes: 42 additions & 0 deletions Tests/Rules/UseDeclaredVarsMoreThanAssignments.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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 | `
Expand Down