Skip to content

Commit 5acd742

Browse files
author
Kapil Borle
authored
Merge pull request #638 from PowerShell/kapilmb/FixTargetRegex
Fix 'Target' regex in rule suppression
2 parents c23e487 + 60a86ec commit 5acd742

File tree

5 files changed

+125
-43
lines changed

5 files changed

+125
-43
lines changed

CHANGELOG.MD

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
## [1.8.1](https://github.com/PowerShell/PSScriptAnalyzer/tree/1.8.1) - 2016-10-13
1+
## [unreleased](https://github.com/PowerShell/PSScriptAnalyzer/tree/development)
2+
### Fixed
3+
- Regular expression in `target` parameter in SuppressMessageAttribute (#638)
4+
5+
## [1.8.1](https://github.com/PowerShell/PSScriptAnalyzer/tree/1.8.1) - 2016-10-13
26
### Added
37
- Catalog file to play nicely with PowerShellGet, version `1.1.0.0`
48

Engine/Generic/RuleSuppression.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic
2121
{
2222
/// <summary>
23-
///
23+
///
2424
/// </summary>
2525
public class RuleSuppression
2626
{
@@ -185,11 +185,11 @@ public RuleSuppression(AttributeAst attrAst, int start, int end)
185185
case 2:
186186
RuleSuppressionID = (positionalArguments[1] as StringConstantExpressionAst).Value;
187187
goto case 1;
188-
188+
189189
case 1:
190190
RuleName = (positionalArguments[0] as StringConstantExpressionAst).Value;
191191
goto default;
192-
192+
193193
default:
194194
break;
195195
}
@@ -281,7 +281,7 @@ public RuleSuppression(AttributeAst attrAst, int start, int end)
281281
RuleName = String.Empty;
282282
Error = Strings.NullRuleNameError;
283283
}
284-
284+
285285
// Must have scope and target together
286286
if (String.IsNullOrWhiteSpace(Scope) && !String.IsNullOrWhiteSpace(Target))
287287
{
@@ -357,8 +357,10 @@ public static List<RuleSuppression> GetSuppressions(IEnumerable<AttributeAst> at
357357
ruleSupp.Target = "*";
358358
}
359359

360+
// According to documentation 'target' supports regular expression. But to maintain compatibility with
361+
// previous implementation we interpret '*' as a glob and therefore replace '*' with '.*'
360362
// regex for wild card *
361-
Regex reg = new Regex(String.Format("^{0}$", Regex.Escape(ruleSupp.Target).Replace(@"\*", ".*")), RegexOptions.IgnoreCase);
363+
Regex reg = new Regex(String.Format("^{0}$", ruleSupp.Target.Replace(@"*", ".*")), RegexOptions.IgnoreCase);
362364
IEnumerable<Ast> targetAsts = null;
363365

364366
switch (ruleSupp.Scope.ToLower())

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.

Tests/Engine/RuleSuppression.tests.ps1

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ if (!(Get-Module PSScriptAnalyzer) -and !$testingLibraryUsage)
77
}
88

99
$directory = Split-Path -Parent $MyInvocation.MyCommand.Path
10+
$testRootDirectory = Split-Path -Parent $directory
11+
Import-Module (Join-Path $testRootDirectory 'PSScriptAnalyzerTestHelper.psm1')
12+
1013
$violationsUsingScriptDefinition = Invoke-ScriptAnalyzer -ScriptDefinition (Get-Content -Raw "$directory\RuleSuppression.ps1")
1114
$violations = Invoke-ScriptAnalyzer "$directory\RuleSuppression.ps1"
1215

@@ -146,4 +149,70 @@ Describe "RuleSuppressionWithScope" {
146149
$suppression.Count | Should Be 0
147150
}
148151
}
152+
153+
Context "Function scope with regular expression" {
154+
It "suppresses objects that match the regular expression" {
155+
$scriptDef = @'
156+
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingWriteHost', '', Scope='Function', Target='start-ba[rz]')]
157+
param()
158+
function start-foo {
159+
write-host "start-foo"
160+
}
161+
162+
function start-bar {
163+
write-host "start-bar"
164+
}
165+
166+
function start-baz {
167+
write-host "start-baz"
168+
}
169+
170+
function start-bam {
171+
write-host "start-bam"
172+
}
173+
'@
174+
$suppressed = Invoke-ScriptAnalyzer -ScriptDefinition $scriptDef -IncludeRule 'PSAvoidUsingWriteHost' -SuppressedOnly
175+
$suppressed.Count | Should Be 2
176+
}
177+
178+
It "suppresses objects that match glob pattern with glob in the end" {
179+
$scriptDef = @'
180+
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingWriteHost', '', Scope='Function', Target='start-*')]
181+
param()
182+
function start-foo {
183+
write-host "start-foo"
184+
}
185+
186+
function start-bar {
187+
write-host "start-bar"
188+
}
189+
190+
function stop-bar {
191+
write-host "stop-bar"
192+
}
193+
'@
194+
$suppressed = Invoke-ScriptAnalyzer -ScriptDefinition $scriptDef -IncludeRule 'PSAvoidUsingWriteHost' -SuppressedOnly
195+
$suppressed.Count | Should Be 2
196+
}
197+
198+
It "suppresses objects that match glob pattern with glob in the begining" {
199+
$scriptDef = @'
200+
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingWriteHost', '', Scope='Function', Target='*-bar')]
201+
param()
202+
function start-foo {
203+
write-host "start-foo"
204+
}
205+
206+
function start-bar {
207+
write-host "start-bar"
208+
}
209+
210+
function start-baz {
211+
write-host "start-baz"
212+
}
213+
'@
214+
$suppressed = Invoke-ScriptAnalyzer -ScriptDefinition $scriptDef -IncludeRule 'PSAvoidUsingWriteHost' -SuppressedOnly
215+
$suppressed.Count | Should Be 1
216+
}
217+
}
149218
}

Utils/ReleaseMaker.psm1

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,7 @@ Function New-Release
7474
Update-ReleaseNotesInModuleManifest $newVer $oldVer
7575

7676
# build the module
77-
pushd $solutionRoot
78-
remove-item out/ -recurse -force
79-
dotnet restore
80-
.\buildCoreClr.ps1 -Framework net451 -Configuration Release -Build
81-
.\buildCoreClr.ps1 -Framework net451 -Configuration PSV3Release -Build
82-
.\buildCoreClr.ps1 -Framework netstandard1.6 -Configuration Release -Build
83-
.\build.ps1 -BuildDocs
84-
popd
85-
77+
New-ReleaseBuild
8678
}
8779

8880
function Get-VersionsFromChangeLog
@@ -94,6 +86,25 @@ function Get-VersionsFromChangeLog
9486
$versions
9587
}
9688

89+
function New-ReleaseBuild
90+
{
91+
$solutionPath = Get-SolutionPath
92+
pushd $solutionPath
93+
try
94+
{
95+
remove-item out/ -recurse -force
96+
dotnet restore
97+
.\buildCoreClr.ps1 -Framework net451 -Configuration Release -Build
98+
.\buildCoreClr.ps1 -Framework net451 -Configuration PSV3Release -Build
99+
.\buildCoreClr.ps1 -Framework netstandard1.6 -Configuration Release -Build
100+
.\build.ps1 -BuildDocs
101+
}
102+
finally
103+
{
104+
popd
105+
}
106+
}
107+
97108
function Update-ReleaseNotesInModuleManifest
98109
{
99110
[CmdletBinding()]
@@ -173,4 +184,5 @@ function Set-ContentUtf8NoBom {
173184
[System.IO.File]::WriteAllLines($path, $content, $utfNoBom)
174185
}
175186

176-
Export-ModuleMember -Function New-Release
187+
Export-ModuleMember -Function New-Release
188+
Export-ModuleMember -Function New-ReleaseBuild

0 commit comments

Comments
 (0)