From f50d393e2dcc83d2cf9f6deb765602ef4e323e3b Mon Sep 17 00:00:00 2001 From: Christoph Bergmeister Date: Thu, 14 Mar 2019 20:00:31 +0000 Subject: [PATCH 1/2] Allow aliases or external script definitions as well so that positionalparameters triggers on them as well --- Engine/Helper.cs | 14 +++++++++++--- Rules/AvoidPositionalParameters.cs | 2 +- Rules/UseCmdletCorrectly.cs | 2 +- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/Engine/Helper.cs b/Engine/Helper.cs index 24c5a12c2..a3009a7ef 100644 --- a/Engine/Helper.cs +++ b/Engine/Helper.cs @@ -605,18 +605,26 @@ public bool HasSplattedVariable(CommandAst cmdAst) } /// - /// Given a commandast, checks if the command is a Cmdlet. + /// Given a commandast, checks if the command is a known cmdlet, function or ExternalScript. /// /// /// - 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; } /// diff --git a/Rules/AvoidPositionalParameters.cs b/Rules/AvoidPositionalParameters.cs index 0fa54845d..d1c552957 100644 --- a/Rules/AvoidPositionalParameters.cs +++ b/Rules/AvoidPositionalParameters.cs @@ -52,7 +52,7 @@ public IEnumerable 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; diff --git a/Rules/UseCmdletCorrectly.cs b/Rules/UseCmdletCorrectly.cs index 8138e6dec..5293eaea9 100644 --- a/Rules/UseCmdletCorrectly.cs +++ b/Rules/UseCmdletCorrectly.cs @@ -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; } From 81602c59a315399d6daff2c390bac4f2c5ea0cdb Mon Sep 17 00:00:00 2001 From: Christoph Bergmeister Date: Thu, 14 Mar 2019 20:09:18 +0000 Subject: [PATCH 2/2] add test --- Tests/Rules/AvoidPositionalParameters.tests.ps1 | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Tests/Rules/AvoidPositionalParameters.tests.ps1 b/Tests/Rules/AvoidPositionalParameters.tests.ps1 index 1e0000a54..ebc11b144 100644 --- a/Tests/Rules/AvoidPositionalParameters.tests.ps1 +++ b/Tests/Rules/AvoidPositionalParameters.tests.ps1 @@ -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" {