Skip to content

Commit a2f92d3

Browse files
committed
Merge pull request #274 from splatteredbits/master
Adding documentation about suppressing rule violations.
2 parents 4042839 + d21154c commit a2f92d3

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

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

0 commit comments

Comments
 (0)