Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Rules/AvoidAlias.cs
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,9 @@ private ScriptBlockExpressionAst GetDscResourceBody(CommandAst cmdAst)
if (!(cmdAst.Parent is PipelineAst commandPipeline)
|| commandPipeline.PipelineElements.Count != 1
|| commandPipeline.PipelineElements[0] != cmdAst
#if CORECLR
|| commandPipeline.Background
#endif
|| !(commandPipeline.Parent is NamedBlockAst || commandPipeline.Parent is StatementBlockAst))
{
return null;
Expand Down Expand Up @@ -346,6 +349,9 @@ private ScriptBlockExpressionAst GetDscResourceBody(CommandAst cmdAst)
if (index >= 0 && index + 1 < statements.Count
&& statements[index + 1] is PipelineAst bodyPipeline
&& bodyPipeline.PipelineElements.Count == 1
#if CORECLR
&& !bodyPipeline.Background
#endif
&& bodyPipeline.PipelineElements[0] is CommandExpressionAst bodyExpression)
{
if (HasStatementSeparator(cmdAst, bodyPipeline)
Expand Down
9 changes: 7 additions & 2 deletions Tests/Rules/AvoidUsingAlias.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ Configuration PackageTest {
Write-Output $(Verb Argument {})
@(Verb Argument {})
Verb Argument {} > $null
Verb Argument {} &
if (Verb Argument {}) {}
return Verb Argument {}
if ($true) {
Expand All @@ -227,7 +228,8 @@ Configuration PackageTest {

$violations = @($rule.AnalyzeScript($configuration, $null))

$violations.Count | Should -Be 5
$expectedViolations = if ($PSVersionTable.PSVersion.Major -ge 7) { 6 } else { 5 }
$violations.Count | Should -Be $expectedViolations
$violations | ForEach-Object {
$_.SuggestedCorrections[0].Text | Should -Be 'Get-Verb'
}
Expand All @@ -241,6 +243,8 @@ Configuration PackageTest {
gci .; {}
gci . &
{}
gci .
{} &
}
'@
$ast = [System.Management.Automation.Language.Parser]::ParseInput($target, [ref]$null, [ref]$null)
Expand All @@ -252,7 +256,8 @@ Configuration PackageTest {

$violations = @($rule.AnalyzeScript($configuration, $null))

$violations.Count | Should -Be 3
$expectedViolations = if ($PSVersionTable.PSVersion.Major -ge 7) { 4 } else { 3 }
$violations.Count | Should -Be $expectedViolations
$violations | ForEach-Object {
$_.Extent.Text | Should -Be 'gci'
$_.SuggestedCorrections[0].Text | Should -Be 'Get-ChildItem'
Expand Down
Loading