1616namespace Microsoft . Windows . Powershell . ScriptAnalyzer . BuiltinRules
1717{
1818 /// <summary>
19- /// UseApprovedVerbs: Analyzes CommandInfos to check that all defined cmdlets use approved verbs.
19+ /// UseApprovedVerbs: Analyzes scripts to check that all defined functions use approved verbs.
2020 /// </summary>
21- [ Export ( typeof ( ICommandRule ) ) ]
22- public class UseApprovedVerbs : ICommandRule {
21+ [ Export ( typeof ( IScriptRule ) ) ]
22+ public class UseApprovedVerbs : IScriptRule {
2323 /// <summary>
24- /// AnalyzeCommand: Analyzes command infos to check that all defined cmdlets use approved verbs.
24+ /// Analyze script to check that all defined functions use approved verbs
2525 /// </summary>
26- /// <param name="commandInfo">The current command info from the script</param>
27- /// <param name="extent">The current position in the script</param>
28- /// <param name="fileName">The name of the script</param>
29- /// <returns>A List of diagnostic results of this rule</returns>
30- public IEnumerable < DiagnosticRecord > AnalyzeCommand ( CommandInfo commandInfo , IScriptExtent extent , string fileName ) {
31- if ( commandInfo == null ) throw new ArgumentNullException ( Strings . NullCommandInfoError ) ;
26+ /// <param name="ast"></param>
27+ /// <param name="fileName"></param>
28+ /// <returns></returns>
29+ public IEnumerable < DiagnosticRecord > AnalyzeScript ( Ast ast , string fileName ) {
30+ if ( ast == null ) throw new ArgumentNullException ( Strings . NullAstErrorMessage ) ;
3231
3332 List < string > approvedVerbs = typeof ( VerbsCommon ) . GetFields ( ) . Concat < FieldInfo > (
3433 typeof ( VerbsCommunications ) . GetFields ( ) ) . Concat < FieldInfo > (
@@ -44,14 +43,22 @@ public IEnumerable<DiagnosticRecord> AnalyzeCommand(CommandInfo commandInfo, ISc
4443 string [ ] funcNamePieces = new string [ 2 ] ;
4544 string verb ;
4645
47- funcName = commandInfo . Name ;
46+ IEnumerable < Ast > funcAsts = ast . FindAll ( item => item is FunctionDefinitionAst , true ) ;
4847
49- if ( funcName != null && funcName . Contains ( '-' ) ) {
50- funcNamePieces = funcName . Split ( funcSeperator ) ;
51- verb = funcNamePieces [ 0 ] ;
48+ foreach ( FunctionDefinitionAst funcAst in funcAsts )
49+ {
50+ funcName = funcAst . Name ;
5251
53- if ( ! approvedVerbs . Contains ( verb , StringComparer . OrdinalIgnoreCase ) ) {
54- yield return new DiagnosticRecord ( string . Format ( CultureInfo . CurrentCulture , Strings . UseApprovedVerbsError , funcName ) , extent , GetName ( ) , DiagnosticSeverity . Warning , fileName ) ;
52+ if ( funcName != null && funcName . Contains ( '-' ) )
53+ {
54+ funcNamePieces = funcName . Split ( funcSeperator ) ;
55+ verb = funcNamePieces [ 0 ] ;
56+
57+ if ( ! approvedVerbs . Contains ( verb , StringComparer . OrdinalIgnoreCase ) )
58+ {
59+ yield return new DiagnosticRecord ( string . Format ( CultureInfo . CurrentCulture , Strings . UseApprovedVerbsError , funcName ) ,
60+ funcAst . Extent , GetName ( ) , DiagnosticSeverity . Warning , fileName ) ;
61+ }
5562 }
5663 }
5764 }
0 commit comments