Skip to content

Commit 9d19209

Browse files
authored
Merge pull request #1251 from JamesWTruher/avoidaliasfix001
Fix logic errors in AvoidAlias rule
2 parents 6c946c0 + 1781717 commit 9d19209

File tree

1 file changed

+20
-16
lines changed

1 file changed

+20
-16
lines changed

Rules/AvoidAlias.cs

+20-16
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(commdNameWithGetPrefix);
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)