Skip to content

Commit c8fe6d6

Browse files
author
Kapil Borle
committed
Set SuggestedCorrection property in AvoidAlias rule
1 parent d8a2a67 commit c8fe6d6

2 files changed

Lines changed: 18 additions & 3 deletions

File tree

Rules/AvoidAlias.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,27 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
4040
{
4141
CommandAst cmdAst = (CommandAst)foundAst;
4242
string aliasName = cmdAst.GetCommandName();
43+
4344
// Handles the exception caused by commands like, {& $PLINK $args 2> $TempErrorFile}.
4445
// You can also review the remark section in following document,
4546
// MSDN: CommandAst.GetCommandName Method
46-
if (aliasName == null) continue;
47+
if (aliasName == null)
48+
{
49+
continue;
50+
}
4751

4852
string cmdletName = Microsoft.Windows.PowerShell.ScriptAnalyzer.Helper.Instance.GetCmdletNameFromAlias(aliasName);
4953

5054
if (!String.IsNullOrEmpty(cmdletName))
5155
{
52-
yield return new DiagnosticRecord(string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingCmdletAliasesError, aliasName, cmdletName),
53-
cmdAst.Extent, GetName(), DiagnosticSeverity.Warning, fileName, aliasName);
56+
yield return new DiagnosticRecord(
57+
string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingCmdletAliasesError, aliasName, cmdletName),
58+
cmdAst.Extent,
59+
GetName(),
60+
DiagnosticSeverity.Warning,
61+
fileName,
62+
aliasName,
63+
cmdletName);
5464
}
5565
}
5666
}

Tests/Rules/AvoidUsingAlias.tests.ps1

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ Describe "AvoidUsingAlias" {
1414
It "has the correct description message" {
1515
$violations[1].Message | Should Match $violationMessage
1616
}
17+
18+
It "suggests correction" {
19+
$violations[0].SuggestedCorrection | Should Be 'Invoke-Expression'
20+
$violations[1].SuggestedCorrection | Should Be 'Clear-Host'
21+
}
1722
}
1823

1924
Context "When there are no violations" {

0 commit comments

Comments
 (0)