Skip to content

Commit 1177959

Browse files
committed
Merge pull request #113 from GoodOlClint/BugFixes
Only Trigger PSProvideVerboseMessage in Advanced Scripts or Functions
2 parents c60e9d5 + 2523307 commit 1177959

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

Rules/ProvideVerboseMessage.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@
1212

1313
using System;
1414
using System.Collections.Generic;
15+
using System.Linq;
1516
using System.Management.Automation.Language;
1617
using Microsoft.Windows.Powershell.ScriptAnalyzer.Generic;
1718
using System.ComponentModel.Composition;
1819
using System.Globalization;
20+
using System.Management.Automation;
1921

2022
namespace Microsoft.Windows.Powershell.ScriptAnalyzer.BuiltinRules
2123
{
@@ -57,6 +59,15 @@ public override AstVisitAction VisitFunctionDefinition(FunctionDefinitionAst fun
5759
return AstVisitAction.SkipChildren;
5860
}
5961

62+
//Write-Verbose is not required for non-advanced functions
63+
if (funcAst.Body == null || funcAst.Body.ParamBlock == null
64+
|| funcAst.Body.ParamBlock.Attributes == null ||
65+
funcAst.Body.ParamBlock.Parameters == null ||
66+
!funcAst.Body.ParamBlock.Attributes.Any(attr => attr.TypeName.GetReflectionType() == typeof(CmdletBindingAttribute)))
67+
{
68+
return AstVisitAction.Continue;
69+
}
70+
6071
var commandAsts = funcAst.Body.FindAll(testAst => testAst is CommandAst, false);
6172
bool hasVerbose = false;
6273

Tests/Rules/GoodCmdlet.ps1

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,10 @@ function Get-File
8181
if ($pscmdlet.ShouldContinue("Yes", "No")) {
8282
}
8383
}
84+
}
85+
86+
#Write-Verbose should not be required because this is not an advanced function
87+
function Get-SimpleFunc
88+
{
89+
8490
}

0 commit comments

Comments
 (0)