Skip to content

Commit 5eafbae

Browse files
Copilotbergmeister
andauthored
Guard following-line DSC resource detection
Co-authored-by: bergmeister <9250262+bergmeister@users.noreply.github.com>
1 parent 52ca729 commit 5eafbae

2 files changed

Lines changed: 28 additions & 1 deletion

File tree

‎Rules/AvoidAlias.cs‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,11 @@ private bool IsFollowingLineDscResourceBody(ScriptBlockExpressionAst body)
298298
return false;
299299
}
300300

301-
return GetDscResourceBody(declaration) == body;
301+
string declarationCommandName = declaration.GetCommandName();
302+
return GetDscResourceBody(declaration) == body
303+
&& String.IsNullOrEmpty(Helper.Instance.GetCmdletNameFromAlias(declarationCommandName))
304+
&& (String.Equals(declarationCommandName, "Node", StringComparison.OrdinalIgnoreCase)
305+
|| Helper.Instance.GetCommandInfo(declarationCommandName) == null);
302306
}
303307

304308
return false;

‎Tests/Rules/AvoidUsingAlias.tests.ps1‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,29 @@ Configuration PackageTest {
235235
}
236236
}
237237

238+
It "does not mistake following-line blocks after executable commands for DSC resources" {
239+
$target = @'
240+
{
241+
Write-Output value
242+
{
243+
Verb Argument {}
244+
}
245+
}
246+
'@
247+
$ast = [System.Management.Automation.Language.Parser]::ParseInput($target, [ref]$null, [ref]$null)
248+
$body = $ast.EndBlock.Statements[0].PipelineElements[0].Expression.Copy()
249+
$configuration = [System.Management.Automation.Language.ConfigurationDefinitionAst]::new(
250+
$ast.Extent, $body, 'Resource',
251+
[System.Management.Automation.Language.StringConstantExpressionAst]::new($ast.Extent, 'Test', 'BareWord'))
252+
$rule = [Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules.AvoidAlias]::new()
253+
254+
$violations = @($rule.AnalyzeScript($configuration, $null))
255+
256+
$violations.Count | Should -Be 1
257+
$violations[0].Extent.Text | Should -Be 'Verb'
258+
$violations[0].SuggestedCorrections[0].Text | Should -Be 'Get-Verb'
259+
}
260+
238261
It "does not pair following-line blocks with commands when the block has redirections or a statement separator intervenes" {
239262
$target = @'
240263
{

0 commit comments

Comments
 (0)