Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,86 @@ 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!"

}

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)
{
}
}

To suppress a rule for an entire function/script, decorate the `param` block of the script/function and set the `SuppressMessageAttribute's` `Scope` property to `Function`:

[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSProvideCommentHelp", "", Scope="Function")]
param(
)

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!



You can also suppress a rule for an entire class using `Class` as the value of the `Scope` property:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can actually do this by simply decorating the class with the SuppressMessageAttribute (the scope is unnecessary in this case). A case where the use of scope is more meaningful is if you want to suppress a rule for all the classes within a script, then you can decorate the script with a SuppressMessageAttribute with the Scope as Class.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the scope reflects the kind of objects in the scope where the suppress
attribute is? So a scope of "function" would apply to all functions in the
scope of the attribute?

On Mon, Jul 13, 2015 at 3:40 PM, Quoc Truong notifications@github.com
wrote:

In README.md
#274 (comment)
:

  • {
  •    [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSProvideDefaultParameterValue", "b")]
    
  •    [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSProvideDefaultParameterValue", "a")]
    
  •    param([string]$a, [int]$b)
    
  •    {
    
  •    }
    
  • }

+To suppress a rule for an entire function/script, decorate the param block of the script/function and set the SuppressMessageAttribute's Scope property to Function:
+

  • [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSProvideCommentHelp", "", Scope="Function")]
  • param(
  • )

+You can also suppress a rule for an entire class using Class as the value of the Scope property:

You can actually do this by simply decorating the class with the
SuppressMessageAttribute (the scope is unnecessary in this case). A case
where the use of scope is more meaningful is if you want to suppress a rule
for all the classes within a script, then you can decorate the script with
a SuppressMessageAttribute with the Scope as Class.


Reply to this email directly or view it on GitHub
https://github.com/PowerShell/PSScriptAnalyzer/pull/274/files#r34520293.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes that is correct.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

On Mon, Jul 13, 2015 at 3:45 PM, Quoc Truong notifications@github.com
wrote:

In README.md
#274 (comment)
:

  • {
  •    [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSProvideDefaultParameterValue", "b")]
    
  •    [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSProvideDefaultParameterValue", "a")]
    
  •    param([string]$a, [int]$b)
    
  •    {
    
  •    }
    
  • }

+To suppress a rule for an entire function/script, decorate the param block of the script/function and set the SuppressMessageAttribute's Scope property to Function:
+

  • [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSProvideCommentHelp", "", Scope="Function")]
  • param(
  • )

+You can also suppress a rule for an entire class using Class as the value of the Scope property:

Yes that is correct.


Reply to this email directly or view it on GitHub
https://github.com/PowerShell/PSScriptAnalyzer/pull/274/files#r34520695.


[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingConvertToSecureStringWithPlainText", "", Scope="Class")]
class TestClass
{
<#
... snip ...
#>
}

Finally, you can restrict suppression inside a scope by setting the `SuppressMessageAttribute's` `Target` property to a regular expression that causes the script analyzer to skip functions/variables/parameters/objects whose names match the regular expression.

[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingPositionalParameters", Scope="Function", Target="PositionalParametersAllowed")]
Param(
)

Choose a reason for hiding this comment

The 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
=================

Expand Down