Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix regression caused by PR #963 during development of PSSA 1.18.0 whereby PSAvoidUsingPositionalParameters was not taking aliases into account any more #1175

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions Engine/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -605,18 +605,26 @@ public bool HasSplattedVariable(CommandAst cmdAst)
}

/// <summary>
/// Given a commandast, checks if the command is a Cmdlet.
/// Given a commandast, checks if the command is a known cmdlet, function or ExternalScript.
/// </summary>
/// <param name="cmdAst"></param>
/// <returns></returns>
public bool IsCmdlet(CommandAst cmdAst) {
public bool IsKnownCmdletFunctionOrExternalScript(CommandAst cmdAst)
{
if (cmdAst == null)
{
return false;
}

var commandInfo = GetCommandInfo(cmdAst.GetCommandName());
return (commandInfo != null && commandInfo.CommandType == System.Management.Automation.CommandTypes.Cmdlet);
if (commandInfo == null)
{
return false;
}

return commandInfo.CommandType == CommandTypes.Cmdlet ||
commandInfo.CommandType == CommandTypes.Alias ||
commandInfo.CommandType == CommandTypes.ExternalScript;
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion Rules/AvoidPositionalParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
// MSDN: CommandAst.GetCommandName Method
if (cmdAst.GetCommandName() == null) continue;

if ((Helper.Instance.IsCmdlet(cmdAst) || declaredFunctionNames.Contains(cmdAst.GetCommandName())) &&
if ((Helper.Instance.IsKnownCmdletFunctionOrExternalScript(cmdAst) || declaredFunctionNames.Contains(cmdAst.GetCommandName())) &&
(Helper.Instance.PositionalParameterUsed(cmdAst, true)))
{
PipelineAst parent = cmdAst.Parent as PipelineAst;
Expand Down
2 changes: 1 addition & 1 deletion Rules/UseCmdletCorrectly.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ private bool MandatoryParameterExists(CommandAst cmdAst)
return true;
}

if (mandParams.Count == 0 || (Helper.Instance.IsCmdlet(cmdAst) && Helper.Instance.PositionalParameterUsed(cmdAst)))
if (mandParams.Count == 0 || (Helper.Instance.IsKnownCmdletFunctionOrExternalScript(cmdAst) && Helper.Instance.PositionalParameterUsed(cmdAst)))
{
returnValue = true;
}
Expand Down
7 changes: 7 additions & 0 deletions Tests/Rules/AvoidPositionalParameters.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ Describe "AvoidPositionalParameters" {
$violations[0].Message | Should -Match $violationMessage
}

It "Triggers on alias" {
$violations = Invoke-ScriptAnalyzer -ScriptDefinition "gcm 'abc' 4 4.3"
$violations.Count | Should -Be 2
$violations.RuleName | Should -Contain $violationName
$violations.RuleName | Should -Contain 'PSAvoidUsingCmdletAliases'
}

}

Context "When there are no violations" {
Expand Down