Skip to content

Commit f87274d

Browse files
author
Kapil Borle
authored
Limit AvoidAlias rule's extent (#667)
For a violation of the rule, the extent will only include the command name and not the entire command statement. For example, Invoke-ScriptAnalyzer -ScriptDefinition "gci -path c:\" with return only the extent of "gci".
1 parent ee2023c commit f87274d

File tree

2 files changed

+38
-7
lines changed

2 files changed

+38
-7
lines changed

Rules/AvoidAlias.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,13 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
116116
{
117117
continue;
118118
}
119+
119120
string cmdletName = Helper.Instance.GetCmdletNameFromAlias(aliasName);
120121
if (!String.IsNullOrEmpty(cmdletName))
121122
{
122123
yield return new DiagnosticRecord(
123124
string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingCmdletAliasesError, aliasName, cmdletName),
124-
cmdAst.Extent,
125+
GetCommandExtent(cmdAst),
125126
GetName(),
126127
DiagnosticSeverity.Warning,
127128
fileName,
@@ -131,6 +132,26 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
131132
}
132133
}
133134

135+
/// <summary>
136+
/// For a command like "gci -path c:", returns the extent of "gci" in the command
137+
/// </summary>
138+
private IScriptExtent GetCommandExtent(CommandAst commandAst)
139+
{
140+
var cmdName = commandAst.GetCommandName();
141+
foreach (var cmdElement in commandAst.CommandElements)
142+
{
143+
var stringConstExpressinAst = cmdElement as StringConstantExpressionAst;
144+
if (stringConstExpressinAst != null)
145+
{
146+
if (stringConstExpressinAst.Value.Equals(cmdName))
147+
{
148+
return stringConstExpressinAst.Extent;
149+
}
150+
}
151+
}
152+
return commandAst.Extent;
153+
}
154+
134155
/// <summary>
135156
/// Creates a list containing suggested correction
136157
/// </summary>

Tests/Rules/AvoidUsingAlias.tests.ps1

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,23 @@ Describe "AvoidUsingAlias" {
1717
$violations[1].Message | Should Match $violationMessage
1818
}
1919

20-
It "suggests correction" {
21-
Test-CorrectionExtent $violationFilepath $violations[0] 1 'iex' 'Invoke-Expression'
22-
$violations[0].SuggestedCorrections[0].Description | Should Be 'Replace iex with Invoke-Expression'
20+
It "suggests correction" {
21+
Test-CorrectionExtent $violationFilepath $violations[0] 1 'iex' 'Invoke-Expression'
22+
$violations[0].SuggestedCorrections[0].Description | Should Be 'Replace iex with Invoke-Expression'
2323

24-
Test-CorrectionExtent $violationFilepath $violations[1] 1 'cls' 'Clear-Host'
25-
$violations[1].SuggestedCorrections[0].Description | Should Be 'Replace cls with Clear-Host'
26-
}
24+
Test-CorrectionExtent $violationFilepath $violations[1] 1 'cls' 'Clear-Host'
25+
$violations[1].SuggestedCorrections[0].Description | Should Be 'Replace cls with Clear-Host'
26+
}
27+
}
28+
29+
Context "Violation Extent" {
30+
It "should return only the cmdlet extent" {
31+
$target = @'
32+
gci -Path C:\
33+
'@
34+
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $target -IncludeRule $violationName
35+
$violations[0].Extent.Text | Should Be "gci"
36+
}
2737
}
2838

2939
Context "When there are no violations" {

0 commit comments

Comments
 (0)