Skip to content

Commit 26f025c

Browse files
Copilotbergmeister
andauthored
Preserve alias corrections for executable commands in fallback DSC ASTs (#2284)
* Initial plan * Fix fallback DSC resource detection Co-authored-by: bergmeister <9250262+bergmeister@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: bergmeister <9250262+bergmeister@users.noreply.github.com>
1 parent ab700bf commit 26f025c

2 files changed

Lines changed: 41 additions & 4 deletions

File tree

‎Rules/AvoidAlias.cs‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,9 @@ private bool IsDscResourceDeclaration(CommandAst cmdAst)
207207
if (parent is FunctionDefinitionAst
208208
|| parent is HashtableAst
209209
|| parent is AssignmentStatementAst
210-
|| parent is ParenExpressionAst)
210+
|| parent is ParenExpressionAst
211+
|| parent is SubExpressionAst
212+
|| parent is ArrayExpressionAst)
211213
{
212214
return false;
213215
}
@@ -228,7 +230,9 @@ private bool IsDscResourceDeclaration(CommandAst cmdAst)
228230
if (parent is FunctionDefinitionAst
229231
|| parent is HashtableAst
230232
|| parent is AssignmentStatementAst
231-
|| parent is ParenExpressionAst)
233+
|| parent is ParenExpressionAst
234+
|| parent is SubExpressionAst
235+
|| parent is ArrayExpressionAst)
232236
{
233237
return false;
234238
}
@@ -305,14 +309,16 @@ private ScriptBlockExpressionAst GetDscResourceBody(CommandAst cmdAst)
305309
if (cmdAst.InvocationOperator != TokenKind.Unknown
306310
|| cmdAst.CommandElements.Count < 2
307311
|| !(cmdAst.CommandElements[1] is ExpressionAst)
308-
|| cmdAst.CommandElements[1].Extent.Text == "=")
312+
|| cmdAst.CommandElements[1].Extent.Text == "="
313+
|| cmdAst.Redirections.Count != 0)
309314
{
310315
return null;
311316
}
312317

313318
if (!(cmdAst.Parent is PipelineAst commandPipeline)
314319
|| commandPipeline.PipelineElements.Count != 1
315-
|| commandPipeline.PipelineElements[0] != cmdAst)
320+
|| commandPipeline.PipelineElements[0] != cmdAst
321+
|| !(commandPipeline.Parent is NamedBlockAst || commandPipeline.Parent is StatementBlockAst))
316322
{
317323
return null;
318324
}

‎Tests/Rules/AvoidUsingAlias.tests.ps1‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,37 @@ Configuration PackageTest {
201201
$violations[5].Extent.Text | Should -Be 'Verb'
202202
$violations[6].Extent.Text | Should -Be 'gci'
203203
}
204+
205+
It "does not mistake executable pipelines for DSC resources in fallback configuration ASTs" {
206+
$target = @'
207+
{
208+
Write-Output $(Verb Argument {})
209+
@(Verb Argument {})
210+
Verb Argument {} > $null
211+
if (Verb Argument {}) {}
212+
return Verb Argument {}
213+
if ($true) {
214+
Verb SameLineResource {}
215+
Verb NextLineResource
216+
{
217+
}
218+
}
219+
}
220+
'@
221+
$ast = [System.Management.Automation.Language.Parser]::ParseInput($target, [ref]$null, [ref]$null)
222+
$body = $ast.EndBlock.Statements[0].PipelineElements[0].Expression.Copy()
223+
$configuration = [System.Management.Automation.Language.ConfigurationDefinitionAst]::new(
224+
$ast.Extent, $body, 'Resource',
225+
[System.Management.Automation.Language.StringConstantExpressionAst]::new($ast.Extent, 'Test', 'BareWord'))
226+
$rule = [Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules.AvoidAlias]::new()
227+
228+
$violations = @($rule.AnalyzeScript($configuration, $null))
229+
230+
$violations.Count | Should -Be 5
231+
$violations | ForEach-Object {
232+
$_.SuggestedCorrections[0].Text | Should -Be 'Get-Verb'
233+
}
234+
}
204235
}
205236

206237
Context "Settings file provides allowlist" {

0 commit comments

Comments
 (0)