File tree Expand file tree Collapse file tree 2 files changed +30
-5
lines changed Expand file tree Collapse file tree 2 files changed +30
-5
lines changed Original file line number Diff line number Diff line change 1
1
using System ;
2
2
using System . Collections . Generic ;
3
3
using System . Linq ;
4
+ using System . Management . Automation ;
4
5
using System . Management . Automation . Language ;
5
6
6
7
namespace Microsoft . Windows . PowerShell . ScriptAnalyzer . Extensions
@@ -42,5 +43,32 @@ public static ParameterAst[] GetParameterAsts(
42
43
43
44
return null ;
44
45
}
46
+
47
+ /// <summary>
48
+ /// Get the CmdletBinding attribute ast
49
+ /// </summary>
50
+ /// <param name="attributeAsts"></param>
51
+ /// <returns>Returns CmdletBinding attribute ast if it exists, otherwise returns null</returns>
52
+ public static AttributeAst GetCmdletBindingAttributeAst ( this ParamBlockAst paramBlockAst )
53
+ {
54
+ var attributeAsts = paramBlockAst . Attributes ;
55
+ if ( attributeAsts == null )
56
+ {
57
+ throw new ArgumentNullException ( "attributeAsts" ) ;
58
+ }
59
+ foreach ( var attributeAst in attributeAsts )
60
+ {
61
+ if ( attributeAst == null || attributeAst . NamedArguments == null )
62
+ {
63
+ continue ;
64
+ }
65
+ if ( attributeAst . TypeName . GetReflectionAttributeType ( )
66
+ == typeof ( CmdletBindingAttribute ) )
67
+ {
68
+ return attributeAst ;
69
+ }
70
+ }
71
+ return null ;
72
+ }
45
73
}
46
74
}
Original file line number Diff line number Diff line change @@ -277,15 +277,12 @@ private CorrectionExtent Normalize(
277
277
correctionExtent . File ,
278
278
correctionExtent . Description ) ;
279
279
}
280
+
280
281
private static bool TryGetCmdletBindingAttribute (
281
282
ParamBlockAst paramBlockAst ,
282
283
out AttributeAst attributeAst )
283
284
{
284
- attributeAst = paramBlockAst . Attributes . FirstOrDefault ( attr =>
285
- {
286
- return attr . TypeName . Name . Equals ( "cmdletbinding" , StringComparison . OrdinalIgnoreCase ) ;
287
- } ) ;
288
-
285
+ attributeAst = paramBlockAst . GetCmdletBindingAttributeAst ( ) ;
289
286
return attributeAst != null ;
290
287
}
291
288
You can’t perform that action at this time.
0 commit comments