diff --git a/Rules/AvoidAlias.cs b/Rules/AvoidAlias.cs index fa6f94efa..d1ab8b780 100644 --- a/Rules/AvoidAlias.cs +++ b/Rules/AvoidAlias.cs @@ -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; diff --git a/Tests/Rules/AvoidUsingAlias.tests.ps1 b/Tests/Rules/AvoidUsingAlias.tests.ps1 index 7dc377774..60b4c7a6f 100644 --- a/Tests/Rules/AvoidUsingAlias.tests.ps1 +++ b/Tests/Rules/AvoidUsingAlias.tests.ps1 @@ -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 = @' {