Skip to content

Commit ff6fc70

Browse files
committed
Merge pull request #287 from PowerShell/master
Take latest Master to Development
2 parents 96652e2 + 1c579c3 commit ff6fc70

File tree

5 files changed

+97
-2
lines changed

5 files changed

+97
-2
lines changed

Engine/Helper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ public bool PositionalParameterUsed(CommandAst cmdAst)
349349
/// <param name="name"></param>
350350
/// <param name="commandType"></param>
351351
/// <returns></returns>
352-
public CommandInfo GetCommandInfo(string name, CommandTypes commandType = CommandTypes.All)
352+
public CommandInfo GetCommandInfo(string name, CommandTypes commandType = CommandTypes.Alias | CommandTypes.Cmdlet | CommandTypes.Configuration | CommandTypes.ExternalScript | CommandTypes.Filter | CommandTypes.Function | CommandTypes.Script | CommandTypes.Workflow)
353353
{
354354
return this.invokeCommand.GetCommand(name, commandType);
355355
}

Engine/SpecialVars.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ internal enum PreferenceVariable
140140
Confirm = 14,
141141
}
142142

143+
internal const string Host = "Host";
143144
internal const string HistorySize = "MaximumHistoryCount";
144145
internal const string OutputEncoding = "OutputEncoding";
145146
internal const string NestedPromptLevel = "NestedPromptLevel";
@@ -159,6 +160,7 @@ internal enum PreferenceVariable
159160

160161
internal static readonly string[] OtherInitializedVariables = new string[]
161162
{
163+
Host,
162164
HistorySize,
163165
OutputEncoding,
164166
NestedPromptLevel,

README.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,87 @@ If you have previous version of PSScriptAnalyzer installed on your machine, you
3939

4040
To confirm installation: run ```Get-ScriptAnalyzerRule``` in the PowerShell console to obtain the built-in rules
4141

42+
Suppressing Rules
43+
=================
44+
45+
You can suppress a rule by decorating a script/function or script/function parameter with .NET's [SuppressMessageAttribute](https://msdn.microsoft.com/en-us/library/system.diagnostics.codeanalysis.suppressmessageattribute.aspx). `SuppressMessageAttribute`'s constructor takes two parameters: a category and a check ID. Set the `categoryID` parameter to the name of the rule you want to suppress (you may omit the `checkID` parameter):
46+
47+
function SuppressMe()
48+
{
49+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSProvideCommentHelp")]
50+
param()
51+
52+
Write-Verbose -Message "I'm making a difference!"
53+
54+
}
55+
56+
All rule violations within the scope of the script/function/parameter you decorate will be suppressed.
57+
58+
To suppress a message on a specific parameter, set the `SuppressMessageAttribute`'s `CheckId` parameter to the name of the parameter:
59+
60+
function SuppressTwoVariables()
61+
{
62+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSProvideDefaultParameterValue", "b")]
63+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSProvideDefaultParameterValue", "a")]
64+
param([string]$a, [int]$b)
65+
{
66+
}
67+
}
68+
69+
Use the `SuppressMessageAttribute`'s `Scope` property to limit rule suppression to functions or classes within the attribute's scope. Use the value `Function` to suppress violations on all functions within the attribute's scope. Use the value `Class` to suppress violoations on all classes within the attribute's scope:
70+
71+
72+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSProvideCommentHelp", "", Scope="Function")]
73+
param(
74+
)
75+
76+
function InternalFunction
77+
{
78+
param()
79+
80+
Write-Verbose -Message "I am invincible!"
81+
}
82+
83+
The above example demonstrates how to suppress rule violations for internal functions using the `SuppressMessageAttribute`'s `Scope` property.
84+
85+
You can further restrict suppression based on a function/parameter/class/variable/object's name by setting the `SuppressMessageAttribute's` `Target` property to a regular expression. Any function/parameter/class/variable/object whose name matches the regular expression is skipped.
86+
87+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingPositionalParameters", Scope="Function", Target="PositionalParametersAllowed")]
88+
Param(
89+
)
90+
91+
function PositionalParametersAllowed()
92+
{
93+
Param([string]$Parameter1)
94+
{
95+
Write-Verbose $Parameter1
96+
}
97+
98+
}
99+
100+
function PositionalParametersNotAllowed()
101+
{
102+
param([string]$Parameter1)
103+
{
104+
Write-Verbose $Parameter1
105+
}
106+
}
107+
108+
# The script analyzer will skip this violation
109+
PositionalParametersAllowed 'value1'
110+
111+
# The script analyzer will report this violation
112+
PositionalParametersNotAllowed 'value1
113+
114+
To match all functions/variables/parameters/objects, use `*` as the value of the Target parameter:
115+
116+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingPositionalParameters", Scope="Function", Target="*")]
117+
Param(
118+
)
119+
120+
121+
122+
42123
Building the Code
43124
=================
44125

Tests/Rules/AvoidGlobalOrUnitializedVarsNoViolations.ps1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ function Test {
99

1010
$a = 3;
1111

12+
#should not raise error
13+
$Host
14+
1215
"hi there!" -match "hi" | Out-Null;
1316
$matches[0];
1417

Tests/Rules/AvoidPositionalParametersNoViolations.ps1

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,13 @@ Clear-Host
44
Split-Path -Path "Random" -leaf
55
Get-Process | Where-Object {$_.handles -gt 200}
66
get-service-computername localhost | where {($_.status -eq "Running") -and ($_.CanStop -eq $true)}
7-
1, 2, $null, 4 | ForEach-Object {"Hello"}
7+
1, 2, $null, 4 | ForEach-Object {"Hello"}
8+
& "$env:Windir\System32\Calc.exe" Parameter1 Parameter2
9+
10+
# There was a bug in Positional Parameter rule that resulted in the rule being fired
11+
# when using external application with absolute paths
12+
# The below function is to validate the fix - rule must not get triggered
13+
function TestExternalApplication
14+
{
15+
& "c:\Windows\System32\Calc.exe" parameter1
16+
}

0 commit comments

Comments
 (0)