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
26 changes: 26 additions & 0 deletions Rules/AvoidAlias.cs
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,12 @@ private ScriptBlockExpressionAst GetDscResourceBody(CommandAst cmdAst)
&& bodyPipeline.PipelineElements.Count == 1
&& bodyPipeline.PipelineElements[0] is CommandExpressionAst bodyExpression)
{
if (HasSemicolonSeparator(pipeline, bodyPipeline)
|| bodyExpression.Redirections.Count != 0)
{
return null;
}

return bodyExpression.Expression as ScriptBlockExpressionAst;
}

Expand All @@ -358,6 +364,26 @@ private ScriptBlockExpressionAst GetDscResourceBody(CommandAst cmdAst)
return null;
}

private bool HasSemicolonSeparator(Ast statement, Ast nextStatement)
{
int separatorStart = statement.Extent.EndOffset;
int separatorLength = nextStatement.Extent.StartOffset - separatorStart;
if (separatorLength <= 0)
{
return false;
}

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

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

/// <summary>
/// For a command like "gci -path c:", returns the extent of "gci" in the command
/// </summary>
Expand Down
24 changes: 24 additions & 0 deletions Tests/Rules/AvoidUsingAlias.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,30 @@ Configuration PackageTest {
$_.SuggestedCorrections[0].Text | Should -Be 'Get-Verb'
}
}

It "does not pair following-line blocks with commands when the block has redirections or a statement terminator intervenes" {
$target = @'
{
gci .
{} > $null
gci .; {}
}
'@
$ast = [System.Management.Automation.Language.Parser]::ParseInput($target, [ref]$null, [ref]$null)
$body = $ast.EndBlock.Statements[0].PipelineElements[0].Expression.Copy()
$configuration = [System.Management.Automation.Language.ConfigurationDefinitionAst]::new(
$ast.Extent, $body, 'Resource',
[System.Management.Automation.Language.StringConstantExpressionAst]::new($ast.Extent, 'Test', 'BareWord'))
$rule = [Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules.AvoidAlias]::new()

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

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

Context "Settings file provides allowlist" {
Expand Down
Loading