-
Notifications
You must be signed in to change notification settings - Fork 395
Adding documentation about suppressing rule violations. #274
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,6 +39,87 @@ If you have previous version of PSScriptAnalyzer installed on your machine, you | |
|
||
To confirm installation: run ```Get-ScriptAnalyzerRule``` in the PowerShell console to obtain the built-in rules | ||
|
||
Suppressing Rules | ||
================= | ||
|
||
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): | ||
|
||
function SuppressMe() | ||
{ | ||
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSProvideCommentHelp")] | ||
param() | ||
|
||
Write-Verbose -Message "I'm making a difference!" | ||
|
||
} | ||
|
||
All rule violations within the scope of the script/function/parameter you decorate will be suppressed. | ||
|
||
To suppress a message on a specific parameter, set the `SuppressMessageAttribute`'s `CheckId` parameter to the name of the parameter: | ||
|
||
function SuppressTwoVariables() | ||
{ | ||
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSProvideDefaultParameterValue", "b")] | ||
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSProvideDefaultParameterValue", "a")] | ||
param([string]$a, [int]$b) | ||
{ | ||
} | ||
} | ||
|
||
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: | ||
|
||
|
||
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSProvideCommentHelp", "", Scope="Function")] | ||
param( | ||
) | ||
|
||
function InternalFunction | ||
{ | ||
param() | ||
|
||
Write-Verbose -Message "I am invincible!" | ||
} | ||
|
||
The above example demonstrates how to suppress rule violations for internal functions using the `SuppressMessageAttribute`'s `Scope` property. | ||
|
||
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. | ||
|
||
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingPositionalParameters", Scope="Function", Target="PositionalParametersAllowed")] | ||
Param( | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This param is also not attached to a function or script or class. |
||
|
||
function PositionalParametersAllowed() | ||
{ | ||
Param([string]$Parameter1) | ||
{ | ||
Write-Verbose $Parameter1 | ||
} | ||
|
||
} | ||
|
||
function PositionalParametersNotAllowed() | ||
{ | ||
param([string]$Parameter1) | ||
{ | ||
Write-Verbose $Parameter1 | ||
} | ||
} | ||
|
||
# The script analyzer will skip this violation | ||
PositionalParametersAllowed 'value1' | ||
|
||
# The script analyzer will report this violation | ||
PositionalParametersNotAllowed 'value1 | ||
|
||
To match all functions/variables/parameters/objects, use `*` as the value of the Target parameter: | ||
|
||
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingPositionalParameters", Scope="Function", Target="*")] | ||
Param( | ||
) | ||
|
||
|
||
|
||
|
||
Building the Code | ||
================= | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This param is not attached to any function or script. You can look at https://github.com/PowerShell/PSScriptAnalyzer/blob/master/Tests/Engine/RuleSuppression.ps1 for an example of a param block of a function, script or class. Thanks!