Skip to content

Commit 18c85ec

Browse files
Copilotbergmeister
andauthored
Reject ampersand-separated DSC body pairing
Co-authored-by: bergmeister <9250262+bergmeister@users.noreply.github.com>
1 parent a67f347 commit 18c85ec

2 files changed

Lines changed: 8 additions & 6 deletions

File tree

‎Rules/AvoidAlias.cs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ private ScriptBlockExpressionAst GetDscResourceBody(CommandAst cmdAst)
348348
&& bodyPipeline.PipelineElements.Count == 1
349349
&& bodyPipeline.PipelineElements[0] is CommandExpressionAst bodyExpression)
350350
{
351-
if (HasSemicolonSeparator(pipeline, bodyPipeline)
351+
if (HasStatementSeparator(pipeline, bodyPipeline)
352352
|| bodyExpression.Redirections.Count != 0)
353353
{
354354
return null;
@@ -364,7 +364,7 @@ private ScriptBlockExpressionAst GetDscResourceBody(CommandAst cmdAst)
364364
return null;
365365
}
366366

367-
private bool HasSemicolonSeparator(Ast statement, Ast nextStatement)
367+
private bool HasStatementSeparator(Ast statement, Ast nextStatement)
368368
{
369369
int separatorStart = statement.Extent.EndOffset;
370370
int separatorLength = nextStatement.Extent.StartOffset - separatorStart;
@@ -375,13 +375,13 @@ private bool HasSemicolonSeparator(Ast statement, Ast nextStatement)
375375

376376
string script = statement.Extent.EndScriptPosition.GetFullScript();
377377
string separator = script.Substring(separatorStart, separatorLength);
378-
if (separator.IndexOf(';') < 0)
378+
if (separator.IndexOf(';') < 0 && separator.IndexOf('&') < 0)
379379
{
380380
return false;
381381
}
382382

383383
Parser.ParseInput(separator, out Token[] tokens, out ParseError[] errors);
384-
return tokens.Any(token => token.Kind == TokenKind.Semi);
384+
return tokens.Any(token => token.Kind == TokenKind.Semi || token.Kind == TokenKind.Ampersand);
385385
}
386386

387387
/// <summary>

‎Tests/Rules/AvoidUsingAlias.tests.ps1‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,12 +233,14 @@ Configuration PackageTest {
233233
}
234234
}
235235

236-
It "does not pair following-line blocks with commands when the block has redirections or a statement terminator intervenes" {
236+
It "does not pair following-line blocks with commands when the block has redirections or a statement separator intervenes" {
237237
$target = @'
238238
{
239239
gci .
240240
{} > $null
241241
gci .; {}
242+
gci . &
243+
{}
242244
}
243245
'@
244246
$ast = [System.Management.Automation.Language.Parser]::ParseInput($target, [ref]$null, [ref]$null)
@@ -250,7 +252,7 @@ Configuration PackageTest {
250252

251253
$violations = @($rule.AnalyzeScript($configuration, $null))
252254

253-
$violations.Count | Should -Be 2
255+
$violations.Count | Should -Be 3
254256
$violations | ForEach-Object {
255257
$_.Extent.Text | Should -Be 'gci'
256258
$_.SuggestedCorrections[0].Text | Should -Be 'Get-ChildItem'

0 commit comments

Comments
 (0)