Skip to content

Commit d85e978

Browse files
author
Kapil Borle
committed
Fix AvoidAlias correction extent
This implementation correctly handles PowerShell escape sequences that IndexOf cannot.
1 parent c62cdca commit d85e978

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

Rules/AvoidAlias.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,7 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
4848
{
4949
continue;
5050
}
51-
52-
string cmdletName = Microsoft.Windows.PowerShell.ScriptAnalyzer.Helper.Instance.GetCmdletNameFromAlias(aliasName);
53-
51+
string cmdletName = Helper.Instance.GetCmdletNameFromAlias(aliasName);
5452
if (!String.IsNullOrEmpty(cmdletName))
5553
{
5654
yield return new DiagnosticRecord(
@@ -68,24 +66,26 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
6866
/// <summary>
6967
/// Creates a list containing suggested correction
7068
/// </summary>
71-
/// <param name="cmdAst"></param>
72-
/// <param name="cmdletName"></param>
73-
/// <returns>Returns a list of suggested corrections</returns>
69+
/// <param name="cmdAst">Command AST of an alias</param>
70+
/// <param name="cmdletName">Full name of the alias</param>
71+
/// <returns>Retruns a list of suggested corrections</returns>
7472
private List<CorrectionExtent> GetCorrectionExtent(CommandAst cmdAst, string cmdletName)
7573
{
7674
var corrections = new List<CorrectionExtent>();
7775
var ext = cmdAst.Extent;
78-
var alias = cmdAst.GetCommandName();
79-
var startColumnNumber = ext.StartColumnNumber + ext.Text.IndexOf(alias);
80-
var endColumnNumber = startColumnNumber + alias.Length;
81-
string description = string.Format("Replace {0} with {1}", alias, cmdletName);
76+
var alias = cmdAst.GetCommandName();
77+
string description = string.Format(
78+
CultureInfo.CurrentCulture,
79+
Strings.AvoidUsingCmdletAliasesCorrectionDescription,
80+
alias,
81+
cmdletName);
8282
corrections.Add(new CorrectionExtent(
8383
ext.StartLineNumber,
8484
ext.EndLineNumber,
85-
startColumnNumber,
86-
endColumnNumber,
85+
cmdAst.CommandElements[0].Extent.StartColumnNumber,
86+
cmdAst.CommandElements[0].Extent.EndColumnNumber,
8787
cmdletName,
88-
ext.File,
88+
ext.File,
8989
description));
9090
return corrections;
9191
}

0 commit comments

Comments
 (0)