Skip to content

Commit dfea64a

Browse files
authored
Fix regression caused by PR #963 during development of PSSA 1.18.0 whereby PSAvoidUsingPositionalParameters was not taking aliases into account any more (#1175)
* Allow aliases or external script definitions as well so that positionalparameters triggers on them as well * add test
1 parent aaa24d9 commit dfea64a

File tree

4 files changed

+20
-5
lines changed

4 files changed

+20
-5
lines changed

Engine/Helper.cs

+11-3
Original file line numberDiff line numberDiff line change
@@ -605,18 +605,26 @@ public bool HasSplattedVariable(CommandAst cmdAst)
605605
}
606606

607607
/// <summary>
608-
/// Given a commandast, checks if the command is a Cmdlet.
608+
/// Given a commandast, checks if the command is a known cmdlet, function or ExternalScript.
609609
/// </summary>
610610
/// <param name="cmdAst"></param>
611611
/// <returns></returns>
612-
public bool IsCmdlet(CommandAst cmdAst) {
612+
public bool IsKnownCmdletFunctionOrExternalScript(CommandAst cmdAst)
613+
{
613614
if (cmdAst == null)
614615
{
615616
return false;
616617
}
617618

618619
var commandInfo = GetCommandInfo(cmdAst.GetCommandName());
619-
return (commandInfo != null && commandInfo.CommandType == System.Management.Automation.CommandTypes.Cmdlet);
620+
if (commandInfo == null)
621+
{
622+
return false;
623+
}
624+
625+
return commandInfo.CommandType == CommandTypes.Cmdlet ||
626+
commandInfo.CommandType == CommandTypes.Alias ||
627+
commandInfo.CommandType == CommandTypes.ExternalScript;
620628
}
621629

622630
/// <summary>

Rules/AvoidPositionalParameters.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
5252
// MSDN: CommandAst.GetCommandName Method
5353
if (cmdAst.GetCommandName() == null) continue;
5454

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

Rules/UseCmdletCorrectly.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ private bool MandatoryParameterExists(CommandAst cmdAst)
129129
return true;
130130
}
131131

132-
if (mandParams.Count == 0 || (Helper.Instance.IsCmdlet(cmdAst) && Helper.Instance.PositionalParameterUsed(cmdAst)))
132+
if (mandParams.Count == 0 || (Helper.Instance.IsKnownCmdletFunctionOrExternalScript(cmdAst) && Helper.Instance.PositionalParameterUsed(cmdAst)))
133133
{
134134
returnValue = true;
135135
}

Tests/Rules/AvoidPositionalParameters.tests.ps1

+7
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ Describe "AvoidPositionalParameters" {
1515
$violations[0].Message | Should -Match $violationMessage
1616
}
1717

18+
It "Triggers on alias" {
19+
$violations = Invoke-ScriptAnalyzer -ScriptDefinition "gcm 'abc' 4 4.3"
20+
$violations.Count | Should -Be 2
21+
$violations.RuleName | Should -Contain $violationName
22+
$violations.RuleName | Should -Contain 'PSAvoidUsingCmdletAliases'
23+
}
24+
1825
}
1926

2027
Context "When there are no violations" {

0 commit comments

Comments
 (0)