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: 5 additions & 1 deletion Rules/AvoidAlias.cs
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,11 @@ private bool IsFollowingLineDscResourceBody(ScriptBlockExpressionAst body)
return false;
}

return GetDscResourceBody(declaration) == body;
string declarationCommandName = declaration.GetCommandName();
return GetDscResourceBody(declaration) == body
&& String.IsNullOrEmpty(Helper.Instance.GetCmdletNameFromAlias(declarationCommandName))
&& (String.Equals(declarationCommandName, "Node", StringComparison.OrdinalIgnoreCase)
|| Helper.Instance.GetCommandInfo(declarationCommandName) == null);
}

return false;
Expand Down
23 changes: 23 additions & 0 deletions Tests/Rules/AvoidUsingAlias.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,29 @@ Configuration PackageTest {
}
}

It "does not mistake following-line blocks after executable commands for DSC resources" {
$target = @'
{
Write-Output value
{
Verb Argument {}
}
}
'@
$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 1
$violations[0].Extent.Text | Should -Be 'Verb'
$violations[0].SuggestedCorrections[0].Text | Should -Be 'Get-Verb'
}

It "does not pair following-line blocks with commands when the block has redirections or a statement separator intervenes" {
$target = @'
{
Expand Down
Loading