Skip to content

Commit 58ada30

Browse files
author
Kapil Borle
committed
Update "Suppressing Rule" section in README
1 parent a882f23 commit 58ada30

File tree

1 file changed

+22
-27
lines changed

1 file changed

+22
-27
lines changed

README.md

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -172,44 +172,39 @@ function InternalFunction
172172
}
173173
```
174174

175-
The above example demonstrates how to suppress rule violations for internal functions using the `SuppressMessageAttribute`'s `Scope` property.
176-
177-
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.
175+
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 or a glob pattern. Few examples are given below.
178176

177+
Suppress `PSAvoidUsingWriteHost` rule violation in `start-bar` and `start-baz` but not in `start-foo` and `start-bam`:
179178
``` PowerShell
180-
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingPositionalParameters", Scope="Function", Target="PositionalParametersAllowed")]
181-
Param(
182-
)
183-
184-
function PositionalParametersAllowed()
185-
{
186-
Param([string]$Parameter1)
187-
{
188-
Write-Verbose $Parameter1
189-
}
179+
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingWriteHost', '', Scope='Function', Target='start-ba[rz]')]
180+
param()
181+
function start-foo {
182+
write-host "start-foo"
183+
}
190184
185+
function start-bar {
186+
write-host "start-bar"
191187
}
192188
193-
function PositionalParametersNotAllowed()
194-
{
195-
param([string]$Parameter1)
196-
{
197-
Write-Verbose $Parameter1
198-
}
189+
function start-baz {
190+
write-host "start-baz"
199191
}
200192
201-
# PSScriptAnalyzer will skip this violation
202-
PositionalParametersAllowed 'value1'
193+
function start-bam {
194+
write-host "start-bam"
195+
}
196+
```
203197

204-
# PSScriptAnalyzer will report this violation
205-
PositionalParametersNotAllowed 'value1
198+
Suppress violations in all the functions:
199+
``` PowerShell
200+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingWriteHost", Scope="Function", Target="*")]
201+
Param()
206202
```
207203

208-
To match all functions/variables/parameters/objects, use `*` as the value of the Target parameter:
204+
Suppress violation in `start-bar`, `start-baz` and `start-bam` but not in `start-foo`:
209205
``` PowerShell
210-
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingPositionalParameters", Scope="Function", Target="*")]
211-
Param(
212-
)
206+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingWriteHost", Scope="Function", Target="start-b*")]
207+
Param()
213208
```
214209

215210
**Note**: Rule suppression is currently supported only for built-in rules.

0 commit comments

Comments
 (0)