Skip to content
Merged
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
18 changes: 18 additions & 0 deletions Engine/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,24 @@ public bool IsVariableGlobalOrEnvironment(VariableExpressionAst varAst, Ast ast)
return VariableAnalysisDictionary[ast].IsGlobalOrEnvironment(varAst);
}


/// <summary>
/// Checks whether a variable is a global variable.
/// </summary>
/// <param name="ast"></param>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix param name comment field

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

/// <returns></returns>
public bool IsVariableGlobal(VariableExpressionAst varAst)
{
//We ignore the use of built-in variable as global variable
if (varAst.VariablePath.IsGlobal)
{
string varName = varAst.VariablePath.UserPath.Remove(varAst.VariablePath.UserPath.IndexOf("global:", StringComparison.OrdinalIgnoreCase), "global:".Length);
return !SpecialVars.InitializedVariables.Contains(varName, StringComparer.OrdinalIgnoreCase);
}
return false;
}


/// <summary>
/// Checks whether all the code path of ast returns.
/// Runs InitializeVariableAnalysis before calling this method
Expand Down
2 changes: 1 addition & 1 deletion Rules/AvoidGlobalVars.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
{
foreach (VariableExpressionAst varAst in varAsts)
{
if (varAst.VariablePath.IsGlobal)
if (Helper.Instance.IsVariableGlobal(varAst))
{
yield return
new DiagnosticRecord(
Expand Down
5 changes: 4 additions & 1 deletion Tests/Rules/AvoidGlobalOrUnitializedVars.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
$Global:1 = "Globalization?"
$Global:1 = "globalVar“
$Global:DebugPreference



function NotGlobal {
$localVars = "Localization?"
Expand Down
14 changes: 5 additions & 9 deletions Tests/Rules/AvoidGlobalOrUnitializedVarsNoViolations.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
function Test {
$Global:DebugPreference


function Test {
$initialized = "Initialized"
$noglobal = "local"
$env:ShouldNotRaiseError
Expand Down Expand Up @@ -31,18 +34,11 @@ $proc[0]

function Test-PreferenceVariable
{
Param(
[switch]
$a,

[System.Management.Automation.SwitchParameter]
$b
)

if (-not $PSBoundParameters.ContainsKey('Verbose')) {
$VerbosePreference = $PSCmdlet.GetVariableValue('VerbosePreference') -as
[System.Management.Automation.ActionPreference]
}

$VerbosePreference
}
}
Expand Down