Skip to content

Commit dc17798

Browse files
kalgizSteveL-MSFT
authored andcommitted
Counting positional parameters fix.
1 parent b1ca186 commit dc17798

File tree

2 files changed

+12
-45
lines changed

2 files changed

+12
-45
lines changed

Engine/Helper.cs

Lines changed: 9 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -627,48 +627,21 @@ public bool PositionalParameterUsed(CommandAst cmdAst, bool moreThanThreePositio
627627
return false;
628628
}
629629

630-
if (commandInfo != null && commandInfo.CommandType == System.Management.Automation.CommandTypes.Cmdlet)
631-
{
632-
try
633-
{
634-
switchParams = commandInfo.Parameters.Values.Where<ParameterMetadata>(pm => pm.SwitchParameter);
635-
}
636-
catch (Exception)
637-
{
638-
switchParams = null;
639-
}
640-
}
641-
642-
int parameters = 0;
643630
// Because of the way we count, we will also count the cmdlet as an argument so we have to -1
644-
int arguments = -1;
631+
int argumentsWithoutParameters = -1;
632+
bool wasParameter = false;
645633

646634
foreach (CommandElementAst ceAst in cmdAst.CommandElements)
647635
{
648636
var cmdParamAst = ceAst as CommandParameterAst;
649637
if (cmdParamAst != null)
650638
{
651-
// Skip if it's a switch parameter
652-
if (switchParams != null &&
653-
switchParams.Any(
654-
pm => String.Equals(
655-
pm.Name,
656-
cmdParamAst.ParameterName, StringComparison.OrdinalIgnoreCase)))
657-
{
658-
continue;
639+
wasParameter = true;
640+
} else {
641+
if (!wasParameter) {
642+
argumentsWithoutParameters += 1;
659643
}
660-
661-
parameters += 1;
662-
663-
if (cmdParamAst.Argument != null)
664-
{
665-
arguments += 1;
666-
}
667-
668-
}
669-
else
670-
{
671-
arguments += 1;
644+
wasParameter = false;
672645
}
673646
}
674647

@@ -677,16 +650,10 @@ public bool PositionalParameterUsed(CommandAst cmdAst, bool moreThanThreePositio
677650

678651
if (parent != null && parent.PipelineElements.Count > 1 && parent.PipelineElements[0] != cmdAst)
679652
{
680-
arguments += 1;
681-
}
682-
683-
// if we are only checking for 3 or more positional parameters, check that arguments < parameters + 3
684-
if (moreThanThreePositional && (arguments - parameters) < 3)
685-
{
686-
return false;
653+
argumentsWithoutParameters += 1;
687654
}
688655

689-
return arguments > parameters;
656+
return moreThanThreePositional ? argumentsWithoutParameters >= 3 : argumentsWithoutParameters > 0;
690657
}
691658

692659

Rules/AvoidPositionalParameters.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
3838
// You can also review the remark section in following document,
3939
// MSDN: CommandAst.GetCommandName Method
4040
if (cmdAst.GetCommandName() == null) continue;
41-
42-
if (Helper.Instance.GetCommandInfoLegacy(cmdAst.GetCommandName()) != null
43-
&& Helper.Instance.PositionalParameterUsed(cmdAst, true))
41+
42+
if ((Helper.Instance.IsCmdlet(cmdAst) || declaredFunctionNames.Contains(cmdAst.GetCommandName())) &&
43+
(Helper.Instance.PositionalParameterUsed(cmdAst, true)))
4444
{
4545
PipelineAst parent = cmdAst.Parent as PipelineAst;
4646

0 commit comments

Comments
 (0)