Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions Rules/AvoidAlias.cs
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ private ScriptBlockExpressionAst GetDscResourceBody(CommandAst cmdAst)
&& bodyPipeline.PipelineElements.Count == 1
&& bodyPipeline.PipelineElements[0] is CommandExpressionAst bodyExpression)
{
if (HasSemicolonSeparator(pipeline, bodyPipeline)
if (HasStatementSeparator(cmdAst, bodyPipeline)
|| bodyExpression.Redirections.Count != 0)
{
return null;
Expand All @@ -364,9 +364,9 @@ private ScriptBlockExpressionAst GetDscResourceBody(CommandAst cmdAst)
return null;
}

private bool HasSemicolonSeparator(Ast statement, Ast nextStatement)
private bool HasStatementSeparator(CommandAst statement, Ast nextStatement)
{
int separatorStart = statement.Extent.EndOffset;
int separatorStart = statement.CommandElements[statement.CommandElements.Count - 1].Extent.EndOffset;
int separatorLength = nextStatement.Extent.StartOffset - separatorStart;
if (separatorLength <= 0)
{
Expand All @@ -375,13 +375,13 @@ private bool HasSemicolonSeparator(Ast statement, Ast nextStatement)

string script = statement.Extent.EndScriptPosition.GetFullScript();
string separator = script.Substring(separatorStart, separatorLength);
if (separator.IndexOf(';') < 0)
if (separator.IndexOf(';') < 0 && separator.IndexOf('&') < 0)
{
return false;
}

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

/// <summary>
Expand Down
6 changes: 4 additions & 2 deletions Tests/Rules/AvoidUsingAlias.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -233,12 +233,14 @@ Configuration PackageTest {
}
}

It "does not pair following-line blocks with commands when the block has redirections or a statement terminator intervenes" {
It "does not pair following-line blocks with commands when the block has redirections or a statement separator intervenes" {
$target = @'
{
gci .
{} > $null
gci .; {}
gci . &
{}
}
'@
$ast = [System.Management.Automation.Language.Parser]::ParseInput($target, [ref]$null, [ref]$null)
Expand All @@ -250,7 +252,7 @@ Configuration PackageTest {

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

$violations.Count | Should -Be 2
$violations.Count | Should -Be 3
$violations | ForEach-Object {
$_.Extent.Text | Should -Be 'gci'
$_.SuggestedCorrections[0].Text | Should -Be 'Get-ChildItem'
Expand Down
Loading