FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Fixed erroneous PSUseDeclaredVarsMoreThanAssignments for some globals variables by John-Leitch · Pull Request #2013 · PowerShell/PSScriptAnalyzer · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .cs  (3) .ps1  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
12 changes: 3 additions & 9 deletions Engine/Helper.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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

Back | FazBrowse Home | New Git URL