12
12
13
13
using System ;
14
14
using System . Collections . Generic ;
15
+ using System . Linq ;
15
16
using System . Management . Automation . Language ;
16
17
using Microsoft . Windows . Powershell . ScriptAnalyzer . Generic ;
17
18
using System . ComponentModel . Composition ;
18
19
using System . Globalization ;
20
+ using System . Management . Automation ;
19
21
20
22
namespace Microsoft . Windows . Powershell . ScriptAnalyzer . BuiltinRules
21
23
{
@@ -33,11 +35,11 @@ public class ProvideVerboseMessage : SkipNamedBlock, IScriptRule
33
35
public IEnumerable < DiagnosticRecord > AnalyzeScript ( Ast ast , string fileName )
34
36
{
35
37
if ( ast == null ) throw new ArgumentNullException ( Strings . NullAstErrorMessage ) ;
36
-
38
+
37
39
ClearList ( ) ;
38
40
this . AddNames ( new List < string > ( ) { "Configuration" , "Workflow" } ) ;
39
41
DiagnosticRecords . Clear ( ) ;
40
-
42
+
41
43
this . fileName = fileName ;
42
44
//We only check that advanced functions should have Write-Verbose
43
45
ast . Visit ( this ) ;
@@ -57,6 +59,17 @@ public override AstVisitAction VisitFunctionDefinition(FunctionDefinitionAst fun
57
59
return AstVisitAction . SkipChildren ;
58
60
}
59
61
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
+ {
67
+ if ( ! funcAst . Body . ParamBlock . Attributes . Any ( attr => attr . TypeName . GetReflectionType ( ) == typeof ( CmdletBindingAttribute ) ) )
68
+ {
69
+ return AstVisitAction . Continue ;
70
+ }
71
+ }
72
+
60
73
var commandAsts = funcAst . Body . FindAll ( testAst => testAst is CommandAst , false ) ;
61
74
bool hasVerbose = false ;
62
75
0 commit comments