Skip to content

Commit 717bccd

Browse files
kalgizSteveL-MSFT
authored andcommitted
Code cleanup for AvoidPositionalParameter rule fix.
1 parent dc17798 commit 717bccd

File tree

4 files changed

+36
-10
lines changed

4 files changed

+36
-10
lines changed

Engine/Helper.cs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -622,26 +622,36 @@ public bool PositionalParameterUsed(CommandAst cmdAst, bool moreThanThreePositio
622622

623623
IEnumerable<ParameterMetadata> switchParams = null;
624624

625+
/// <summary>
626+
/// Given a commandast, checks whether positional parameters are used or not.
627+
/// </summary>
628+
/// <param name="cmdAst"></param>
629+
/// <param name="moreThanTwoPositional">only return true if more than three positional parameters are used</param>
630+
/// <returns></returns>
631+
public bool PositionalParameterUsed(CommandAst cmdAst, bool moreThanTwoPositional = false)
632+
{
625633
if (HasSplattedVariable(cmdAst))
626634
{
627635
return false;
628636
}
629637

630638
// Because of the way we count, we will also count the cmdlet as an argument so we have to -1
631-
int argumentsWithoutParameters = -1;
632-
bool wasParameter = false;
639+
int argumentsWithoutProcedingParameters = -1;
640+
bool parameterPreceding = false;
633641

634642
foreach (CommandElementAst ceAst in cmdAst.CommandElements)
635643
{
636644
var cmdParamAst = ceAst as CommandParameterAst;
637645
if (cmdParamAst != null)
638646
{
639-
wasParameter = true;
640-
} else {
641-
if (!wasParameter) {
642-
argumentsWithoutParameters += 1;
647+
parameterPreceding = true;
648+
} else
649+
{
650+
if (!parameterPreceding)
651+
{
652+
argumentsWithoutProcedingParameters += 1;
643653
}
644-
wasParameter = false;
654+
parameterPreceding = false;
645655
}
646656
}
647657

@@ -650,10 +660,10 @@ public bool PositionalParameterUsed(CommandAst cmdAst, bool moreThanThreePositio
650660

651661
if (parent != null && parent.PipelineElements.Count > 1 && parent.PipelineElements[0] != cmdAst)
652662
{
653-
argumentsWithoutParameters += 1;
663+
argumentsWithoutProcedingParameters += 1;
654664
}
655665

656-
return moreThanThreePositional ? argumentsWithoutParameters >= 3 : argumentsWithoutParameters > 0;
666+
return moreThanTwoPositional ? argumentsWithoutProcedingParameters >= 3 : argumentsWithoutProcedingParameters > 0;
657667
}
658668

659669

Rules/AvoidPositionalParameters.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,18 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
2727
{
2828
if (ast == null) throw new ArgumentNullException(Strings.NullAstErrorMessage);
2929

30+
// Find all function definitions in the script and add them to the set.
31+
IEnumerable<Ast> functionDefinitionAsts = ast.FindAll(testAst => testAst is FunctionDefinitionAst, true);
32+
HashSet<String> declaredFunctionNames = new HashSet<String>();
33+
34+
foreach (FunctionDefinitionAst functionDefinitionAst in functionDefinitionAsts)
35+
{
36+
if (string.IsNullOrEmpty(functionDefinitionAst.Name))
37+
{
38+
continue;
39+
}
40+
declaredFunctionNames.Add(functionDefinitionAst.Name);
41+
}
3042
// Finds all CommandAsts.
3143
IEnumerable<Ast> foundAsts = ast.FindAll(testAst => testAst is CommandAst, true);
3244

Rules/UseCmdletCorrectly.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,11 @@ private bool MandatoryParameterExists(CommandAst cmdAst)
129129
return true;
130130
}
131131

132+
<<<<<<< HEAD
132133
if (mandParams.Count() == 0 || Helper.Instance.PositionalParameterUsed(cmdAst))
134+
=======
135+
if (mandParams.Count == 0 || (Helper.Instance.IsCmdlet(cmdAst) && Helper.Instance.PositionalParameterUsed(cmdAst)))
136+
>>>>>>> d0541d2... Code cleanup for AvoidPositionalParameter rule fix.
133137
{
134138
returnValue = true;
135139
}

Tests/Rules/AvoidPositionalParameters.tests.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ Describe "AvoidPositionalParameters" {
4242
}
4343
$warnings = Invoke-ScriptAnalyzer -ScriptDefinition "$sb"
4444
$warnings.Count | Should -BeGreaterThan 0
45-
$warnings.RuleName | Should -Contain "PSAvoidUsingPositionalParameters"
45+
$warnings.RuleName | Should -Contain $violationName
4646
$warnings.Message | Should -Contain "Cmdlet 'Foo' has positional parameter. Please use named parameters instead of positional parameters when calling a command."
4747
}
4848
}

0 commit comments

Comments
 (0)