Skip to content

Commit 77a5690

Browse files
author
Kapil Borle
authored
Merge pull request #653 from PowerShell/kapilmb/AddPrefVarsToDeclaredVarsRule
Update special variable entries
2 parents 0fc6447 + efa022c commit 77a5690

File tree

3 files changed

+45
-21
lines changed

3 files changed

+45
-21
lines changed

CHANGELOG.MD

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
+ to check for commands in Microsoft.PowerShell.Core snapin
77
+ to ignore aliases
88
+ to ignore custom defind commands
9-
9+
- PSUseDeclaredVarsMoreThanAssignments rule to ignore the following special variables (#653)
10+
+ `$PSModuleAutoLoadingPreference`
11+
+ `$InformationPreference`
1012

1113
## [1.8.1](https://github.com/PowerShell/PSScriptAnalyzer/tree/1.8.1) - 2016-10-13
1214
### Added

Engine/SpecialVars.cs

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -96,27 +96,30 @@ static SpecialVars()
9696
internal const string WarningPreference = "WarningPreference";
9797
internal const string ConfirmPreference = "ConfirmPreference";
9898
internal const string ProgressPreference = "ProgressPreference";
99+
internal const string InformationPreference = "InformationPreference";
99100

100-
internal static readonly string[] PreferenceVariables = new string[]
101-
{
102-
DebugPreference,
103-
VerbosePreference,
104-
ErrorActionPreference,
105-
WhatIfPreference,
106-
WarningPreference,
101+
internal static readonly string[] PreferenceVariables = new string[]
102+
{
103+
DebugPreference,
104+
VerbosePreference,
105+
ErrorActionPreference,
106+
WhatIfPreference,
107+
WarningPreference,
107108
ConfirmPreference,
108-
ProgressPreference
109+
ProgressPreference,
110+
InformationPreference
109111
};
110112

111-
internal static readonly Type[] PreferenceVariableTypes = new Type[]
112-
{
113-
/* DebugPreference */ typeof(ActionPreference),
114-
/* VerbosePreference */ typeof(ActionPreference),
115-
/* ErrorPreference */ typeof(ActionPreference),
116-
/* WhatIfPreference */ typeof(SwitchParameter),
117-
/* WarningPreference */ typeof(ActionPreference),
118-
/* ConfirmPreference */ typeof(ConfirmImpact),
113+
internal static readonly Type[] PreferenceVariableTypes = new Type[]
114+
{
115+
/* DebugPreference */ typeof(ActionPreference),
116+
/* VerbosePreference */ typeof(ActionPreference),
117+
/* ErrorPreference */ typeof(ActionPreference),
118+
/* WhatIfPreference */ typeof(SwitchParameter),
119+
/* WarningPreference */ typeof(ActionPreference),
120+
/* ConfirmPreference */ typeof(ConfirmImpact),
119121
/* ProgressPreference */ typeof(Enum),
122+
/* InformationPreference */ typeof(ActionPreference),
120123
};
121124

122125
internal enum AutomaticVariable
@@ -156,6 +159,7 @@ internal enum PreferenceVariable
156159
internal const string PathExt = "env:PATHEXT";
157160
internal const string PSEmailServer = "PSEmailServer";
158161
internal const string PSDefaultParameterValues = "PSDefaultParameterValues";
162+
internal const string PSModuleAutoLoadingPreference = "PSModuleAutoLoadingPreference";
159163
internal const string pwd = "PWD";
160164
internal const string Null = "null";
161165
internal const string True = "true";
@@ -176,6 +180,7 @@ internal enum PreferenceVariable
176180
PathExt,
177181
PSEmailServer,
178182
PSDefaultParameterValues,
183+
PSModuleAutoLoadingPreference,
179184
pwd,
180185
Null,
181186
True,

Tests/Rules/UseDeclaredVarsMoreThanAssignments.tests.ps1

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
Import-Module PSScriptAnalyzer
1+
$directory = Split-Path -Parent $MyInvocation.MyCommand.Path
2+
$testRootDirectory = Split-Path -Parent $directory
3+
4+
Import-Module PSScriptAnalyzer
5+
Import-Module (Join-Path $testRootDirectory 'PSScriptAnalyzerTestHelper.psm1')
6+
27
$violationMessage = "The variable 'declaredVar2' is assigned but never used."
38
$violationName = "PSUseDeclaredVarsMoreThanAssignments"
4-
$directory = Split-Path -Parent $MyInvocation.MyCommand.Path
59
$violations = Invoke-ScriptAnalyzer $directory\UseDeclaredVarsMoreThanAssignments.ps1 | Where-Object {$_.RuleName -eq $violationName}
610
$noViolations = Invoke-ScriptAnalyzer $directory\UseDeclaredVarsMoreThanAssignmentsNoViolations.ps1 | Where-Object {$_.RuleName -eq $violationName}
711

@@ -29,8 +33,21 @@ function MyFunc2() {
2933
$a + $a
3034
}
3135
'@
32-
$local:violations = Invoke-ScriptAnalyzer -ScriptDefinition $target -IncludeRule $violationName
33-
$local:violations.Count | Should Be 1
36+
Invoke-ScriptAnalyzer -ScriptDefinition $target -IncludeRule $violationName | `
37+
Get-Count | `
38+
Should Be 1
39+
}
40+
41+
It "does not flag `$InformationPreference variable" {
42+
Invoke-ScriptAnalyzer -ScriptDefinition '$InformationPreference=Stop' -IncludeRule $violationName | `
43+
Get-Count | `
44+
Should Be 0
45+
}
46+
47+
It "does not flag `$PSModuleAutoLoadingPreference variable" {
48+
Invoke-ScriptAnalyzer -ScriptDefinition '$PSModuleAutoLoadingPreference=None' -IncludeRule $violationName | `
49+
Get-Count | `
50+
Should Be 0
3451
}
3552
}
3653

0 commit comments

Comments
 (0)