Skip to content

Commit 49b5e0a

Browse files
author
Kapil Borle
committed
Use extension method to get cmdletbinding attribute
1 parent 71ac64a commit 49b5e0a

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

Engine/Extensions.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using System.Management.Automation;
45
using System.Management.Automation.Language;
56

67
namespace 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
}

rules/UseSupportsShouldProcess.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff 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

0 commit comments

Comments
 (0)