Skip to content

Commit 3af1f03

Browse files
committed
Counting positional parameters fix.
1 parent 27ca3a7 commit 3af1f03

File tree

2 files changed

+12
-13
lines changed

2 files changed

+12
-13
lines changed

Engine/Helper.cs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -629,16 +629,21 @@ public bool PositionalParameterUsed(CommandAst cmdAst, bool moreThanThreePositio
629629
return false;
630630
}
631631

632-
int parameters = 0;
633632
// Because of the way we count, we will also count the cmdlet as an argument so we have to -1
634-
int arguments = -1;
633+
int argumentsWithoutParameters = -1;
634+
bool wasParameter = false;
635635

636636
foreach (CommandElementAst ceAst in cmdAst.CommandElements)
637637
{
638638
var cmdParamAst = ceAst as CommandParameterAst;
639-
if (cmdParamAst == null)
639+
if (cmdParamAst != null)
640640
{
641-
arguments += 1;
641+
wasParameter = true;
642+
} else {
643+
if (!wasParameter) {
644+
argumentsWithoutParameters += 1;
645+
}
646+
wasParameter = false;
642647
}
643648
}
644649

@@ -647,16 +652,10 @@ public bool PositionalParameterUsed(CommandAst cmdAst, bool moreThanThreePositio
647652

648653
if (parent != null && parent.PipelineElements.Count > 1 && parent.PipelineElements[0] != cmdAst)
649654
{
650-
arguments += 1;
651-
}
652-
653-
// if we are only checking for 3 or more positional parameters, check that arguments < parameters + 3
654-
if (moreThanThreePositional && (arguments - parameters) < 3)
655-
{
656-
return false;
655+
argumentsWithoutParameters += 1;
657656
}
658657

659-
return arguments > parameters;
658+
return moreThanThreePositional ? argumentsWithoutParameters >= 3 : argumentsWithoutParameters > 0;
660659
}
661660

662661

Rules/AvoidPositionalParameters.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
5252
if (cmdAst.GetCommandName() == null) continue;
5353

5454
if ((Helper.Instance.IsCmdlet(cmdAst) || declaredFunctionNames.Contains(cmdAst.GetCommandName())) &&
55-
(Helper.Instance.PositionalParameterUsed(cmdAst)))
55+
(Helper.Instance.PositionalParameterUsed(cmdAst, true)))
5656
{
5757
PipelineAst parent = cmdAst.Parent as PipelineAst;
5858

0 commit comments

Comments
 (0)