Skip to content

Commit 8d531d9

Browse files
author
Quoc Truong
committed
Check for cmdletbinding attribute before applying the rule
1 parent e6e2ee5 commit 8d531d9

File tree

1 file changed

+8
-16
lines changed

1 file changed

+8
-16
lines changed

Rules/AvoidReservedParams.cs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,31 +32,23 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName) {
3232

3333
IEnumerable<Ast> funcAsts = ast.FindAll(item => item is FunctionDefinitionAst, true);
3434

35-
string paramName;
36-
3735
List<string> commonParamNames = typeof(CommonParameters).GetProperties().Select(param => param.Name).ToList();
3836

3937
foreach (FunctionDefinitionAst funcAst in funcAsts)
4038
{
41-
IEnumerable<ParameterAst> parameters = null;
42-
if (funcAst.Parameters != null)
43-
{
44-
parameters = funcAst.Parameters;
45-
}
46-
// Check param block
47-
else
39+
// this rule only applies to function with param block
40+
if (funcAst.Body != null && funcAst.Body.ParamBlock != null
41+
&& funcAst.Body.ParamBlock.Attributes != null && funcAst.Body.ParamBlock.Parameters != null)
4842
{
49-
if (funcAst.Body != null && funcAst.Body.ParamBlock != null && funcAst.Body.ParamBlock.Parameters != null)
43+
// no cmdlet binding
44+
if (!funcAst.Body.ParamBlock.Attributes.Any(attr => attr.TypeName.GetReflectionType() == typeof(CmdletBindingAttribute)))
5045
{
51-
parameters = funcAst.Body.ParamBlock.Parameters;
46+
continue;
5247
}
53-
}
5448

55-
if (parameters != null)
56-
{
57-
foreach (ParameterAst paramAst in parameters)
49+
foreach (ParameterAst paramAst in funcAst.Body.ParamBlock.Parameters)
5850
{
59-
paramName = paramAst.Name.VariablePath.UserPath;
51+
string paramName = paramAst.Name.VariablePath.UserPath;
6052

6153
if (commonParamNames.Contains(paramName, StringComparer.OrdinalIgnoreCase))
6254
{

0 commit comments

Comments
 (0)