Skip to content

Commit 22e639f

Browse files
author
Kapil Borle
committed
Add an exception to PSAvoidUsingCmdletAliases rule
Ignores CommandAsts of the form "[cmdElement0] = [cmdElement1]". DSC configurations use this from.
1 parent 8a02083 commit 22e639f

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

Rules/AvoidAlias.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,13 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
106106
foreach (Ast foundAst in foundAsts)
107107
{
108108
CommandAst cmdAst = (CommandAst)foundAst;
109+
110+
// Check if the command ast should be ignored
111+
if (IgnoreCommandast(cmdAst))
112+
{
113+
continue;
114+
}
115+
109116
string aliasName = cmdAst.GetCommandName();
110117

111118
// Handles the exception caused by commands like, {& $PLINK $args 2> $TempErrorFile}.
@@ -132,6 +139,23 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
132139
}
133140
}
134141

142+
/// <summary>
143+
/// Checks commandast of the form "[commandElement0] = [CommandElement2]". This typically occurs in a DSC configuration.
144+
/// </summary>
145+
private bool IgnoreCommandast(CommandAst cmdAst)
146+
{
147+
if (cmdAst.CommandElements.Count == 3)
148+
{
149+
var element = cmdAst.CommandElements[1] as StringConstantExpressionAst;
150+
if (element != null && element.Value.Equals("="))
151+
{
152+
return true;
153+
}
154+
}
155+
156+
return false;
157+
}
158+
135159
/// <summary>
136160
/// For a command like "gci -path c:", returns the extent of "gci" in the command
137161
/// </summary>

0 commit comments

Comments
 (0)