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
14 changes: 10 additions & 4 deletions Rules/AvoidAlias.cs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,9 @@ private bool IsDscResourceDeclaration(CommandAst cmdAst)
if (parent is FunctionDefinitionAst
|| parent is HashtableAst
|| parent is AssignmentStatementAst
|| parent is ParenExpressionAst)
|| parent is ParenExpressionAst
|| parent is SubExpressionAst
|| parent is ArrayExpressionAst)
{
return false;
}
Expand All @@ -228,7 +230,9 @@ private bool IsDscResourceDeclaration(CommandAst cmdAst)
if (parent is FunctionDefinitionAst
|| parent is HashtableAst
|| parent is AssignmentStatementAst
|| parent is ParenExpressionAst)
|| parent is ParenExpressionAst
|| parent is SubExpressionAst
|| parent is ArrayExpressionAst)
{
return false;
}
Expand Down Expand Up @@ -305,14 +309,16 @@ private ScriptBlockExpressionAst GetDscResourceBody(CommandAst cmdAst)
if (cmdAst.InvocationOperator != TokenKind.Unknown
|| cmdAst.CommandElements.Count < 2
|| !(cmdAst.CommandElements[1] is ExpressionAst)
|| cmdAst.CommandElements[1].Extent.Text == "=")
|| cmdAst.CommandElements[1].Extent.Text == "="
|| cmdAst.Redirections.Count != 0)
{
return null;
}

if (!(cmdAst.Parent is PipelineAst commandPipeline)
|| commandPipeline.PipelineElements.Count != 1
|| commandPipeline.PipelineElements[0] != cmdAst)
|| commandPipeline.PipelineElements[0] != cmdAst
|| !(commandPipeline.Parent is NamedBlockAst || commandPipeline.Parent is StatementBlockAst))
{
return null;
}
Expand Down
31 changes: 31 additions & 0 deletions Tests/Rules/AvoidUsingAlias.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,37 @@ Configuration PackageTest {
$violations[5].Extent.Text | Should -Be 'Verb'
$violations[6].Extent.Text | Should -Be 'gci'
}

It "does not mistake executable pipelines for DSC resources in fallback configuration ASTs" {
$target = @'
{
Write-Output $(Verb Argument {})
@(Verb Argument {})
Verb Argument {} > $null
if (Verb Argument {}) {}
return Verb Argument {}
if ($true) {
Verb SameLineResource {}
Verb NextLineResource
{
}
}
}
'@
$ast = [System.Management.Automation.Language.Parser]::ParseInput($target, [ref]$null, [ref]$null)
$body = $ast.EndBlock.Statements[0].PipelineElements[0].Expression.Copy()
$configuration = [System.Management.Automation.Language.ConfigurationDefinitionAst]::new(
$ast.Extent, $body, 'Resource',
[System.Management.Automation.Language.StringConstantExpressionAst]::new($ast.Extent, 'Test', 'BareWord'))
$rule = [Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules.AvoidAlias]::new()

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

$violations.Count | Should -Be 5
$violations | ForEach-Object {
$_.SuggestedCorrections[0].Text | Should -Be 'Get-Verb'
}
}
}

Context "Settings file provides allowlist" {
Expand Down
Loading