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 11using System ;
22using System . Collections . Generic ;
33using System . Linq ;
4+ using System . Management . Automation ;
45using System . Management . Automation . Language ;
56
67namespace Microsoft . Windows . PowerShell . ScriptAnalyzer . Extensions
@@ -42,5 +43,32 @@ public static ParameterAst[] GetParameterAsts(
4243
4344 return null ;
4445 }
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+ }
4573 }
4674}
Original file line number Diff line number Diff line change @@ -277,15 +277,12 @@ private CorrectionExtent Normalize(
277277 correctionExtent . File ,
278278 correctionExtent . Description ) ;
279279 }
280+
280281 private static bool TryGetCmdletBindingAttribute (
281282 ParamBlockAst paramBlockAst ,
282283 out AttributeAst attributeAst )
283284 {
284- attributeAst = paramBlockAst . Attributes . FirstOrDefault ( attr =>
285- {
286- return attr . TypeName . Name . Equals ( "cmdletbinding" , StringComparison . OrdinalIgnoreCase ) ;
287- } ) ;
288-
285+ attributeAst = paramBlockAst . GetCmdletBindingAttributeAst ( ) ;
289286 return attributeAst != null ;
290287 }
291288
You can’t perform that action at this time.
0 commit comments