From 1e78842e0a11a56ff0b6abe282f40072d58e3b31 Mon Sep 17 00:00:00 2001 From: Yuting Chen Date: Thu, 21 May 2015 15:25:52 -0700 Subject: [PATCH 1/2] Add check for builtin variables for GlobalVar rule --- Engine/Helper.cs | 17 +++++++++++++++++ Rules/AvoidGlobalVars.cs | 2 +- Tests/Rules/AvoidGlobalOrUnitializedVars.ps1 | 5 ++++- ...AvoidGlobalOrUnitializedVarsNoViolations.ps1 | 14 +++++--------- 4 files changed, 27 insertions(+), 11 deletions(-) diff --git a/Engine/Helper.cs b/Engine/Helper.cs index 8864ac136..92e8ed358 100644 --- a/Engine/Helper.cs +++ b/Engine/Helper.cs @@ -494,6 +494,23 @@ public bool IsVariableGlobalOrEnvironment(VariableExpressionAst varAst, Ast ast) return VariableAnalysisDictionary[ast].IsGlobalOrEnvironment(varAst); } + + /// + /// Checks whether a variable is a built-in variable. + /// + /// + /// + public bool IsVariableGlobal(VariableExpressionAst varAst) + { + 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; + } + + /// /// Checks whether all the code path of ast returns. /// Runs InitializeVariableAnalysis before calling this method diff --git a/Rules/AvoidGlobalVars.cs b/Rules/AvoidGlobalVars.cs index 6da1cdb91..109da5ee5 100644 --- a/Rules/AvoidGlobalVars.cs +++ b/Rules/AvoidGlobalVars.cs @@ -41,7 +41,7 @@ public IEnumerable AnalyzeScript(Ast ast, string fileName) { foreach (VariableExpressionAst varAst in varAsts) { - if (varAst.VariablePath.IsGlobal) + if (Helper.Instance.IsVariableGlobal(varAst)) { yield return new DiagnosticRecord( diff --git a/Tests/Rules/AvoidGlobalOrUnitializedVars.ps1 b/Tests/Rules/AvoidGlobalOrUnitializedVars.ps1 index ef6988ad9..e606404f3 100644 --- a/Tests/Rules/AvoidGlobalOrUnitializedVars.ps1 +++ b/Tests/Rules/AvoidGlobalOrUnitializedVars.ps1 @@ -1,4 +1,7 @@ -$Global:1 = "Globalization?" +$Global:1 = "globalVar“ +$Global:DebugPreference + + function NotGlobal { $localVars = "Localization?" diff --git a/Tests/Rules/AvoidGlobalOrUnitializedVarsNoViolations.ps1 b/Tests/Rules/AvoidGlobalOrUnitializedVarsNoViolations.ps1 index ea436bb4f..6a9ce8077 100644 --- a/Tests/Rules/AvoidGlobalOrUnitializedVarsNoViolations.ps1 +++ b/Tests/Rules/AvoidGlobalOrUnitializedVarsNoViolations.ps1 @@ -1,4 +1,7 @@ -function Test { +$Global:DebugPreference + + +function Test { $initialized = "Initialized" $noglobal = "local" $env:ShouldNotRaiseError @@ -31,13 +34,6 @@ $proc[0] function Test-PreferenceVariable { - Param( - [switch] - $a, - - [System.Management.Automation.SwitchParameter] - $b - ) if (-not $PSBoundParameters.ContainsKey('Verbose')) { $VerbosePreference = $PSCmdlet.GetVariableValue('VerbosePreference') -as @@ -45,4 +41,4 @@ function Test-PreferenceVariable } $VerbosePreference -} \ No newline at end of file + } \ No newline at end of file From 7a1215a6b123c1ac2c1a225703e87a064d7bcb03 Mon Sep 17 00:00:00 2001 From: Yuting Chen Date: Thu, 21 May 2015 15:54:57 -0700 Subject: [PATCH 2/2] Modify the comment --- Engine/Helper.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Engine/Helper.cs b/Engine/Helper.cs index 92e8ed358..2f334241d 100644 --- a/Engine/Helper.cs +++ b/Engine/Helper.cs @@ -496,12 +496,13 @@ public bool IsVariableGlobalOrEnvironment(VariableExpressionAst varAst, Ast ast) /// - /// Checks whether a variable is a built-in variable. + /// Checks whether a variable is a global variable. /// /// /// 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);