Skip to content

Commit 8d91e1e

Browse files
committed
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-<command>' 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.
1 parent 6c946c0 commit 8d91e1e

File tree

1 file changed

+20
-16
lines changed

1 file changed

+20
-16
lines changed

Rules/AvoidAlias.cs

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -126,25 +126,29 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
126126
fileName,
127127
commandName,
128128
suggestedCorrections: GetCorrectionExtent(cmdAst, cmdletNameIfCommandNameWasAlias));
129+
// do not continue the search, but go to the next command
130+
continue;
131+
}
132+
133+
// If we find match of any kind, do not continue with the Get-{commandname} check
134+
if ( Helper.Instance.GetCommandInfo(commandName) != null ) {
135+
continue;
129136
}
130137

131-
var isNativeCommand = Helper.Instance.GetCommandInfo(commandName, CommandTypes.Application | CommandTypes.ExternalScript) != null;
132-
if (!isNativeCommand)
138+
var commdNameWithGetPrefix = $"Get-{commandName}";
139+
var cmdletNameIfCommandWasMissingGetPrefix = Helper.Instance.GetCommandInfo($"Get-{commandName}");
140+
if (cmdletNameIfCommandWasMissingGetPrefix != null)
133141
{
134-
var commdNameWithGetPrefix = $"Get-{commandName}";
135-
var cmdletNameIfCommandWasMissingGetPrefix = Helper.Instance.GetCommandInfo($"Get-{commandName}");
136-
if (cmdletNameIfCommandWasMissingGetPrefix != null)
137-
{
138-
yield return new DiagnosticRecord(
139-
string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingCmdletAliasesMissingGetPrefixError, commandName, commdNameWithGetPrefix),
140-
GetCommandExtent(cmdAst),
141-
GetName(),
142-
DiagnosticSeverity.Warning,
143-
fileName,
144-
commandName,
145-
suggestedCorrections: GetCorrectionExtent(cmdAst, commdNameWithGetPrefix));
146-
}
142+
yield return new DiagnosticRecord(
143+
string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingCmdletAliasesMissingGetPrefixError, commandName, commdNameWithGetPrefix),
144+
GetCommandExtent(cmdAst),
145+
GetName(),
146+
DiagnosticSeverity.Warning,
147+
fileName,
148+
commandName,
149+
suggestedCorrections: GetCorrectionExtent(cmdAst, commdNameWithGetPrefix));
147150
}
151+
148152
}
149153
}
150154

@@ -264,4 +268,4 @@ public string GetSourceName()
264268
return string.Format(CultureInfo.CurrentCulture, Strings.SourceName);
265269
}
266270
}
267-
}
271+
}

0 commit comments

Comments
 (0)