Skip to content

Commit c61b40e

Browse files
author
Kapil Borle
authored
Merge pull request #678 from PowerShell/kapilmb/FixAvoidAliasRule
Fix PSAvoidUsingCmdletAliases false positives in DSC configuration
2 parents 3e9e5c5 + 9d3daab commit c61b40e

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
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>

Tests/Rules/AvoidUsingAlias.tests.ps1

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
Import-Module PSScriptAnalyzer
2-
$violationMessage = "'cls' is an alias of 'Clear-Host'. Alias can introduce possible problems and make scripts hard to maintain. Please consider changing alias to its full content."
1+
$violationMessage = "'cls' is an alias of 'Clear-Host'. Alias can introduce possible problems and make scripts hard to maintain. Please consider changing alias to its full content."
32
$violationName = "PSAvoidUsingCmdletAliases"
43
$directory = Split-Path -Parent $MyInvocation.MyCommand.Path
4+
$testRootDirectory = Split-Path -Parent $directory
55
$violationFilepath = Join-Path $directory 'AvoidUsingAlias.ps1'
66
$violations = Invoke-ScriptAnalyzer $violationFilepath | Where-Object {$_.RuleName -eq $violationName}
77
$noViolations = Invoke-ScriptAnalyzer $directory\AvoidUsingAliasNoViolations.ps1 | Where-Object {$_.RuleName -eq $violationName}
8-
Import-Module (Join-Path $directory "PSScriptAnalyzerTestHelper.psm1")
8+
9+
Import-Module PSScriptAnalyzer
10+
Import-Module (Join-Path $testRootDirectory "PSScriptAnalyzerTestHelper.psm1")
911

1012
Describe "AvoidUsingAlias" {
1113
Context "When there are violations" {
@@ -40,6 +42,22 @@ gci -Path C:\
4042
It "returns no violations" {
4143
$noViolations.Count | Should Be 0
4244
}
45+
46+
It "should return no violation for assignment statement-like command in dsc configuration" {
47+
$target = @'
48+
Configuration MyDscConfiguration {
49+
Node "NodeA" {
50+
SomeResource MyResourceInstance {
51+
Type = "Present"
52+
Name = "RSAT"
53+
}
54+
}
55+
}
56+
'@
57+
Invoke-ScriptAnalyzer -ScriptDefinition $target -IncludeRule $violationName | `
58+
Get-Count | `
59+
Should Be 0
60+
}
4361
}
4462

4563
Context "Settings file provides whitelist" {

0 commit comments

Comments
 (0)