Skip to content

Commit a9d141c

Browse files
Copilotbergmeister
andauthored
Preserve DSC Node resource declarations
Co-authored-by: bergmeister <9250262+bergmeister@users.noreply.github.com>
1 parent 74c856e commit a9d141c

2 files changed

Lines changed: 18 additions & 1 deletion

File tree

‎Rules/AvoidAlias.cs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,8 @@ private bool IsDscResourceDeclaration(CommandAst cmdAst)
247247
{
248248
if (GetDscResourceBody(enclosingCommand) != scriptBlock
249249
|| !String.IsNullOrEmpty(Helper.Instance.GetCmdletNameFromAlias(enclosingCommand.GetCommandName()))
250-
|| Helper.Instance.GetCommandInfo(enclosingCommand.GetCommandName()) != null)
250+
|| (!String.Equals(enclosingCommand.GetCommandName(), "Node", StringComparison.OrdinalIgnoreCase)
251+
&& Helper.Instance.GetCommandInfo(enclosingCommand.GetCommandName()) != null))
251252
{
252253
return false;
253254
}

‎Tests/Rules/AvoidUsingAlias.tests.ps1‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,22 @@ Configuration PackageTest {
9797
}
9898

9999
Context "DSC resource ASTs" {
100+
It "does not expand nested resource names in a same-line Node declaration when node is discoverable" -skip:(!(Get-Command node -CommandType Application -ErrorAction Ignore)) {
101+
$target = @'
102+
{
103+
Node localhost { Package PackageExample {} }
104+
}
105+
'@
106+
$ast = [System.Management.Automation.Language.Parser]::ParseInput($target, [ref]$null, [ref]$null)
107+
$body = $ast.EndBlock.Statements[0].PipelineElements[0].Expression.Copy()
108+
$configuration = [System.Management.Automation.Language.ConfigurationDefinitionAst]::new(
109+
$ast.Extent, $body, 'Resource',
110+
[System.Management.Automation.Language.StringConstantExpressionAst]::new($ast.Extent, 'Test', 'BareWord'))
111+
$rule = [Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules.AvoidAlias]::new()
112+
113+
@($rule.AnalyzeScript($configuration, $null)).Count | Should -Be 0
114+
}
115+
100116
It "ignores resource declarations but not executable commands" {
101117
$target = @'
102118
{

0 commit comments

Comments
 (0)