@@ -30,43 +30,38 @@ public class AvoidReservedParams : IScriptRule
30
30
public IEnumerable < DiagnosticRecord > AnalyzeScript ( Ast ast , string fileName ) {
31
31
if ( ast == null ) throw new ArgumentNullException ( Strings . NullAstErrorMessage ) ;
32
32
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 ) ;
35
34
36
35
string paramName ;
37
36
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 ( ) ;
40
38
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
+ }
44
53
}
45
- }
46
-
47
- if ( paramAsts != null ) {
48
- foreach ( ParameterAst paramAst in paramAsts ) {
49
- paramName = paramAst . Name . ToString ( ) ;
50
54
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 ;
56
60
57
- if ( parentAst is FunctionDefinitionAst )
61
+ if ( commonParamNames . Contains ( paramName , StringComparer . OrdinalIgnoreCase ) )
58
62
{
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 ) ;
70
65
}
71
66
}
72
67
}
0 commit comments