@@ -30,43 +30,38 @@ 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
3635 string paramName ;
3736
38- PropertyInfo [ ] commonParams = typeof ( CommonParameters ) . GetProperties ( ) ;
39- List < string > commonParamNames = new List < string > ( ) ;
37+ List < string > commonParamNames = typeof ( CommonParameters ) . GetProperties ( ) . Select ( param => param . Name ) . ToList ( ) ;
4038
41- if ( commonParams != null ) {
42- foreach ( PropertyInfo commonParam in commonParams ) {
43- commonParamNames . Add ( "$" + commonParam . Name ) ;
39+ foreach ( FunctionDefinitionAst funcAst in funcAsts )
40+ {
41+ IEnumerable < ParameterAst > parameters = null ;
42+ if ( funcAst . Parameters != null )
43+ {
44+ parameters = funcAst . Parameters ;
45+ }
46+ // Check param block
47+ else
48+ {
49+ if ( funcAst . Body != null && funcAst . Body . ParamBlock != null && funcAst . Body . ParamBlock . Parameters != null )
50+ {
51+ parameters = funcAst . Body . ParamBlock . Parameters ;
52+ }
4453 }
45- }
46-
47- if ( paramAsts != null ) {
48- foreach ( ParameterAst paramAst in paramAsts ) {
49- paramName = paramAst . Name . ToString ( ) ;
5054
51- if ( commonParamNames . Contains ( paramName , StringComparer . OrdinalIgnoreCase ) ) {
52- parentAst = paramAst . Parent ;
53- while ( parentAst != null && ! ( parentAst is FunctionDefinitionAst ) ) {
54- parentAst = parentAst . Parent ;
55- }
55+ if ( parameters != null )
56+ {
57+ foreach ( ParameterAst paramAst in parameters )
58+ {
59+ paramName = paramAst . Name . VariablePath . UserPath ;
5660
57- if ( parentAst is FunctionDefinitionAst )
61+ if ( commonParamNames . Contains ( paramName , StringComparer . OrdinalIgnoreCase ) )
5862 {
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- }
63+ yield return new DiagnosticRecord ( string . Format ( CultureInfo . CurrentCulture , Strings . ReservedParamsError , funcAst . Name , paramName ) ,
64+ paramAst . Extent , GetName ( ) , DiagnosticSeverity . Warning , fileName ) ;
7065 }
7166 }
7267 }
0 commit comments