Skip to content

Commit 82e9116

Browse files
committed
Merge pull request #159 from GoodOlClint/PSAvoidDefaultTrueValueSwitchParameter
Correct PSAvoidDefaultTrueValueSwitchParameter rule to work with both [switch] and [System.Management.Automation.SwitchParameter] attributes
2 parents 8718dc5 + 42a8f45 commit 82e9116

File tree

3 files changed

+8
-4
lines changed

3 files changed

+8
-4
lines changed

Rules/AvoidDefaultTrueValueSwitchParameter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
3939
// Iterrates all ParamAsts and check if any are switch.
4040
foreach (ParameterAst paramAst in paramAsts)
4141
{
42-
if (paramAst.Attributes.Any(attr => String.Equals(attr.TypeName.FullName, "switch", StringComparison.OrdinalIgnoreCase))
42+
if (paramAst.Attributes.Any(attr => string.Equals(attr.TypeName.GetReflectionType().FullName, "system.management.automation.switchparameter", StringComparison.OrdinalIgnoreCase))
4343
&& paramAst.DefaultValue != null && String.Equals(paramAst.DefaultValue.Extent.Text, "$true", StringComparison.OrdinalIgnoreCase))
4444
{
4545
yield return new DiagnosticRecord(

Tests/Rules/AvoidDefaultTrueValueSwitchParameter.ps1

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@
1313

1414
# Param2 help description
1515
[switch]
16-
$switch=$true
16+
$switch=$true,
17+
18+
# Param3 help description
19+
[System.Management.Automation.SwitchParameter]
20+
$switch2 = $true
1721
)
1822

1923
Begin

Tests/Rules/AvoidDefaultTrueValueSwitchParameter.tests.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ $noViolations = Invoke-ScriptAnalyzer $directory\AvoidDefaultTrueValueSwitchPara
77

88
Describe "AvoidDefaultTrueValueSwitchParameter" {
99
Context "When there are violations" {
10-
It "has 1 avoid using switch parameter default to true violation" {
11-
$violations.Count | Should Be 1
10+
It "has 2 avoid using switch parameter default to true violation" {
11+
$violations.Count | Should Be 2
1212
}
1313

1414
It "has the correct description message" {

0 commit comments

Comments
 (0)