From 8d91e1e05abea8e3cb243455ced89141f9873c5b Mon Sep 17 00:00:00 2001 From: James Truher Date: Wed, 5 Jun 2019 10:22:32 -0700 Subject: [PATCH 1/2] Fix logic errors in AvoidAlias rule The first fix is that after the yield return, we should move on to the next command The second fix is that if we find _any_ command, we should not move on to the 'Get-' variant These changes mean that we do not run a pipeline (twice!) to find something that was a cache miss. In the pathological case of a script mmade up of only unique aliases, we would run 2 pipelines for each found alias. --- Rules/AvoidAlias.cs | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/Rules/AvoidAlias.cs b/Rules/AvoidAlias.cs index ad35ec4fe..da834b192 100644 --- a/Rules/AvoidAlias.cs +++ b/Rules/AvoidAlias.cs @@ -126,25 +126,29 @@ public IEnumerable AnalyzeScript(Ast ast, string fileName) fileName, commandName, suggestedCorrections: GetCorrectionExtent(cmdAst, cmdletNameIfCommandNameWasAlias)); + // do not continue the search, but go to the next command + continue; + } + + // If we find match of any kind, do not continue with the Get-{commandname} check + if ( Helper.Instance.GetCommandInfo(commandName) != null ) { + continue; } - var isNativeCommand = Helper.Instance.GetCommandInfo(commandName, CommandTypes.Application | CommandTypes.ExternalScript) != null; - if (!isNativeCommand) + var commdNameWithGetPrefix = $"Get-{commandName}"; + var cmdletNameIfCommandWasMissingGetPrefix = Helper.Instance.GetCommandInfo($"Get-{commandName}"); + if (cmdletNameIfCommandWasMissingGetPrefix != null) { - var commdNameWithGetPrefix = $"Get-{commandName}"; - var cmdletNameIfCommandWasMissingGetPrefix = Helper.Instance.GetCommandInfo($"Get-{commandName}"); - if (cmdletNameIfCommandWasMissingGetPrefix != null) - { - yield return new DiagnosticRecord( - string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingCmdletAliasesMissingGetPrefixError, commandName, commdNameWithGetPrefix), - GetCommandExtent(cmdAst), - GetName(), - DiagnosticSeverity.Warning, - fileName, - commandName, - suggestedCorrections: GetCorrectionExtent(cmdAst, commdNameWithGetPrefix)); - } + yield return new DiagnosticRecord( + string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingCmdletAliasesMissingGetPrefixError, commandName, commdNameWithGetPrefix), + GetCommandExtent(cmdAst), + GetName(), + DiagnosticSeverity.Warning, + fileName, + commandName, + suggestedCorrections: GetCorrectionExtent(cmdAst, commdNameWithGetPrefix)); } + } } @@ -264,4 +268,4 @@ public string GetSourceName() return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); } } -} \ No newline at end of file +} From 1781717019388275377e09ed84aa40481ef7e394 Mon Sep 17 00:00:00 2001 From: "James Truher [MSFT]" Date: Wed, 5 Jun 2019 10:59:02 -0700 Subject: [PATCH 2/2] Update Rules/AvoidAlias.cs Co-Authored-By: Christoph Bergmeister [MVP] --- Rules/AvoidAlias.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Rules/AvoidAlias.cs b/Rules/AvoidAlias.cs index da834b192..dd488527f 100644 --- a/Rules/AvoidAlias.cs +++ b/Rules/AvoidAlias.cs @@ -136,7 +136,7 @@ public IEnumerable AnalyzeScript(Ast ast, string fileName) } var commdNameWithGetPrefix = $"Get-{commandName}"; - var cmdletNameIfCommandWasMissingGetPrefix = Helper.Instance.GetCommandInfo($"Get-{commandName}"); + var cmdletNameIfCommandWasMissingGetPrefix = Helper.Instance.GetCommandInfo(commdNameWithGetPrefix); if (cmdletNameIfCommandWasMissingGetPrefix != null) { yield return new DiagnosticRecord(