@@ -30,43 +30,30 @@ public class AvoidReservedParams : IScriptRule
3030 public IEnumerable < DiagnosticRecord > AnalyzeScript ( Ast ast , string fileName ) {
3131 if ( ast == null ) throw new ArgumentNullException ( Strings . NullAstErrorMessage ) ;
3232
33- IEnumerable < Ast > paramAsts = ast . FindAll ( testAst => testAst is ParameterAst , true ) ;
34- Ast parentAst ;
33+ IEnumerable < Ast > funcAsts = ast . FindAll ( item => item is FunctionDefinitionAst , true ) ;
3534
36- string paramName ;
35+ List < string > commonParamNames = typeof ( CommonParameters ) . GetProperties ( ) . Select ( param => param . Name ) . ToList ( ) ;
3736
38- PropertyInfo [ ] commonParams = typeof ( CommonParameters ) . GetProperties ( ) ;
39- List < string > commonParamNames = new List < string > ( ) ;
40-
41- if ( commonParams != null ) {
42- foreach ( PropertyInfo commonParam in commonParams ) {
43- commonParamNames . Add ( "$" + commonParam . Name ) ;
44- }
45- }
46-
47- if ( paramAsts != null ) {
48- foreach ( ParameterAst paramAst in paramAsts ) {
49- paramName = paramAst . Name . ToString ( ) ;
37+ foreach ( FunctionDefinitionAst funcAst in funcAsts )
38+ {
39+ // this rule only applies to function with param block
40+ if ( funcAst . Body != null && funcAst . Body . ParamBlock != null
41+ && funcAst . Body . ParamBlock . Attributes != null && funcAst . Body . ParamBlock . Parameters != null )
42+ {
43+ // no cmdlet binding
44+ if ( ! funcAst . Body . ParamBlock . Attributes . Any ( attr => attr . TypeName . GetReflectionType ( ) == typeof ( CmdletBindingAttribute ) ) )
45+ {
46+ continue ;
47+ }
5048
51- if ( commonParamNames . Contains ( paramName , StringComparer . OrdinalIgnoreCase ) ) {
52- parentAst = paramAst . Parent ;
53- while ( parentAst != null && ! ( parentAst is FunctionDefinitionAst ) ) {
54- parentAst = parentAst . Parent ;
55- }
49+ foreach ( ParameterAst paramAst in funcAst . Body . ParamBlock . Parameters )
50+ {
51+ string paramName = paramAst . Name . VariablePath . UserPath ;
5652
57- if ( parentAst is FunctionDefinitionAst )
53+ if ( commonParamNames . Contains ( paramName , StringComparer . OrdinalIgnoreCase ) )
5854 {
59- IEnumerable < Ast > attrs = parentAst . FindAll ( testAttr => testAttr is AttributeAst , true ) ;
60- foreach ( AttributeAst attr in attrs )
61- {
62- if ( string . Equals ( attr . Extent . Text , "[CmdletBinding()]" ,
63- StringComparison . OrdinalIgnoreCase ) )
64- {
65- string funcName = string . Format ( CultureInfo . CurrentCulture , Strings . ReservedParamsCmdletPrefix , ( parentAst as FunctionDefinitionAst ) . Name ) ;
66- yield return new DiagnosticRecord ( string . Format ( CultureInfo . CurrentCulture , Strings . ReservedParamsError , funcName , paramName ) , paramAst . Extent , GetName ( ) , DiagnosticSeverity . Warning , fileName ) ;
67-
68- }
69- }
55+ yield return new DiagnosticRecord ( string . Format ( CultureInfo . CurrentCulture , Strings . ReservedParamsError , funcAst . Name , paramName ) ,
56+ paramAst . Extent , GetName ( ) , DiagnosticSeverity . Warning , fileName ) ;
7057 }
7158 }
7259 }
0 commit comments