22
22
namespace Microsoft . Windows . PowerShell . ScriptAnalyzer . BuiltinRules
23
23
{
24
24
/// <summary>
25
- /// ProvideVerboseMessage: Analyzes the ast to check that Write-Verbose is called at least once in every cmdlet or script.
25
+ /// ProvideVerboseMessage: Analyzes the ast to check that Write-Verbose is called for DSC Resources
26
26
/// </summary>
27
- [ Export ( typeof ( IScriptRule ) ) ]
28
- public class ProvideVerboseMessage : SkipNamedBlock , IScriptRule
27
+ [ Export ( typeof ( IDSCResourceRule ) ) ]
28
+ public class UseVerboseMessageInDSCResouce : SkipNamedBlock , IDSCResourceRule
29
29
{
30
30
/// <summary>
31
- /// AnalyzeScript : Analyzes the ast to check that Write-Verbose is called at least once in every cmdlet or script.
31
+ /// AnalyzeDSCResource : Analyzes the ast to check that Write-Verbose is called for DSC Resources
32
32
/// <param name="ast">The script's ast</param>
33
33
/// <param name="fileName">The script's file name</param>
34
34
/// </summary>
35
- public IEnumerable < DiagnosticRecord > AnalyzeScript ( Ast ast , string fileName )
35
+ public IEnumerable < DiagnosticRecord > AnalyzeDSCResource ( Ast ast , string fileName )
36
36
{
37
- if ( ast == null ) throw new ArgumentNullException ( Strings . NullAstErrorMessage ) ;
38
-
39
- ClearList ( ) ;
40
- this . AddNames ( new List < string > ( ) { "Configuration" , "Workflow" } ) ;
41
- DiagnosticRecords . Clear ( ) ;
42
-
43
- this . fileName = fileName ;
44
- //We only check that advanced functions should have Write-Verbose
45
- ast . Visit ( this ) ;
46
-
47
- return DiagnosticRecords ;
48
- }
49
-
50
- /// <summary>
51
- /// Visit function and checks that it has write verbose
52
- /// </summary>
53
- /// <param name="funcAst"></param>
54
- /// <returns></returns>
55
- public override AstVisitAction VisitFunctionDefinition ( FunctionDefinitionAst funcAst )
56
- {
57
- if ( funcAst == null )
37
+ if ( ast == null )
58
38
{
59
- return AstVisitAction . SkipChildren ;
39
+ throw new ArgumentNullException ( Strings . NullAstErrorMessage ) ;
60
40
}
61
41
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
- }
42
+ List < string > expectedTargetResourceFunctionNames = new List < string > ( new string [ ] { "Set-TargetResource" , "Test-TargetResource" , "Get-TargetResource" } ) ;
70
43
71
- var commandAsts = funcAst . Body . FindAll ( testAst => testAst is CommandAst , false ) ;
72
- bool hasVerbose = false ;
44
+ IEnumerable < Ast > functionDefinitionAsts = Helper . Instance . DscResourceFunctions ( ast ) ;
73
45
74
- if ( commandAsts != null )
46
+ foreach ( FunctionDefinitionAst functionDefinitionAst in functionDefinitionAsts )
75
47
{
76
- foreach ( CommandAst commandAst in commandAsts )
48
+ var commandAsts = functionDefinitionAst . Body . FindAll ( testAst => testAst is CommandAst , false ) ;
49
+ bool hasVerbose = false ;
50
+
51
+ if ( null != commandAsts )
77
52
{
78
- hasVerbose |= String . Equals ( commandAst . GetCommandName ( ) , "Write-Verbose" , StringComparison . OrdinalIgnoreCase ) ;
53
+ foreach ( CommandAst commandAst in commandAsts )
54
+ {
55
+ hasVerbose |= String . Equals ( commandAst . GetCommandName ( ) , "Write-Verbose" , StringComparison . OrdinalIgnoreCase ) ;
56
+ }
79
57
}
80
- }
81
58
82
- if ( ! hasVerbose )
83
- {
84
- DiagnosticRecords . Add ( new DiagnosticRecord ( string . Format ( CultureInfo . CurrentCulture , Strings . ProvideVerboseMessageErrorFunction , funcAst . Name ) ,
85
- funcAst . Extent , GetName ( ) , DiagnosticSeverity . Information , fileName ) ) ;
86
- }
59
+ if ( ! hasVerbose )
60
+ {
61
+ yield return new DiagnosticRecord ( string . Format ( CultureInfo . CurrentCulture , Strings . UseVerboseMessageInDSCResourceErrorFunction , functionDefinitionAst . Name ) ,
62
+ functionDefinitionAst . Extent , GetName ( ) , DiagnosticSeverity . Information , fileName ) ;
63
+ }
87
64
88
- return AstVisitAction . Continue ;
65
+ }
66
+ }
67
+
68
+ /// <summary>
69
+ /// AnalyzeDSCClass: This function returns nothing in the case of dsc class.
70
+ /// </summary>
71
+ /// <param name="ast"></param>
72
+ /// <param name="fileName"></param>
73
+ /// <returns></returns>
74
+ public IEnumerable < DiagnosticRecord > AnalyzeDSCClass ( Ast ast , string fileName )
75
+ {
76
+ return Enumerable . Empty < DiagnosticRecord > ( ) ;
89
77
}
90
78
91
79
/// <summary>
92
80
/// Method: Retrieves the name of this rule.
93
81
/// </summary>
94
82
public string GetName ( )
95
83
{
96
- return string . Format ( CultureInfo . CurrentCulture , Strings . NameSpaceFormat , GetSourceName ( ) , Strings . ProvideVerboseMessageName ) ;
84
+ return string . Format ( CultureInfo . CurrentCulture , Strings . NameSpaceFormat , GetSourceName ( ) , Strings . UseVerboseMessageInDSCResourceName ) ;
97
85
}
98
86
99
87
/// <summary>
@@ -102,7 +90,7 @@ public string GetName()
102
90
/// <returns>The common name of this rule</returns>
103
91
public string GetCommonName ( )
104
92
{
105
- return string . Format ( CultureInfo . CurrentCulture , Strings . ProvideVerboseMessageCommonName ) ;
93
+ return string . Format ( CultureInfo . CurrentCulture , Strings . UseVerboseMessageInDSCResourceCommonName ) ;
106
94
}
107
95
108
96
/// <summary>
@@ -111,7 +99,7 @@ public string GetCommonName()
111
99
/// <returns>The description of this rule</returns>
112
100
public string GetDescription ( )
113
101
{
114
- return string . Format ( CultureInfo . CurrentCulture , Strings . ProvideVerboseMessageDescription ) ;
102
+ return string . Format ( CultureInfo . CurrentCulture , Strings . UseVerboseMessageInDSCResourceDescription ) ;
115
103
}
116
104
117
105
/// <summary>
@@ -136,7 +124,7 @@ public RuleSeverity GetSeverity()
136
124
/// </summary>
137
125
public string GetSourceName ( )
138
126
{
139
- return string . Format ( CultureInfo . CurrentCulture , Strings . SourceName ) ;
127
+ return string . Format ( CultureInfo . CurrentCulture , Strings . DSCSourceName ) ;
140
128
}
141
129
}
142
- }
130
+ }
0 commit comments