Skip to content
Open
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
6 changes: 6 additions & 0 deletions Rules/UseConsistentWhitespace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,12 @@ private IEnumerable<DiagnosticRecord> FindPipeViolations(TokenOperations tokenOp
continue;
}

// The verbatim argument after --% owns any trailing whitespace before the pipe.
if (pipe.Previous.Previous?.Value.Text == "--%")
{
continue;
}

if (!IsPreviousTokenApartByWhitespace(pipe, out bool hasRedundantWhitespace))
{
if (CheckPipeForRedundantWhitespace && hasRedundantWhitespace || CheckPipe && !hasRedundantWhitespace)
Expand Down
16 changes: 16 additions & 0 deletions Tests/Engine/InvokeFormatter.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,22 @@ function foo {
}

Context "When no settings are given" {
It "Should preserve verbatim arguments across repeated formatting passes: <ScriptDefinition>" -TestCases @(
@{ ScriptDefinition = 'cmd --% raw | text & stuff' }
@{ ScriptDefinition = 'cmd --% raw| text' }
@{ ScriptDefinition = 'cmd --% raw | text' }
@{ ScriptDefinition = "cmd --% raw`t| text" }
@{ ScriptDefinition = 'cmd --% | text' }
) {
param($ScriptDefinition)

$formatted = $ScriptDefinition
1..3 | ForEach-Object {
$formatted = Invoke-Formatter $formatted
$formatted | Should -BeExactly $ScriptDefinition
}
}

It "Should format using default settings" {
$def = @'
function foo
Expand Down
18 changes: 18 additions & 0 deletions Tests/Rules/UseConsistentWhitespace.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,19 @@ $x = "abc";
Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings | Should -Be $null
}

It "Should still correct whitespace after a pipe following a verbatim argument" {
$def = 'cmd --% raw |text'
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
@($violations).Count | Should -Be 1
Test-CorrectionExtentFromContent $def $violations 1 '' ' '
Invoke-Formatter $def -Settings $settings | Should -BeExactly 'cmd --% raw | text'
}

It "Should still format other pipes around a command with verbatim arguments" {
$def = 'foo|cmd --% raw |text|more'
Invoke-Formatter $def -Settings $settings | Should -BeExactly 'foo | cmd --% raw | text | more'
}

It "Should not find a violation if a backtick is before the pipe" {
$def = @'
Get-Item `
Expand Down Expand Up @@ -404,6 +417,11 @@ foo
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings | Should -BeNullOrEmpty
}

It "Should preserve whitespace in verbatim arguments while removing redundant whitespace after the pipe" {
$def = 'cmd --% raw | text'
Invoke-Formatter $def -Settings $settings | Should -BeExactly 'cmd --% raw | text'
}

It "Should find a violation if there is one space too much before pipe" {
$def = 'foo | bar'
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
Expand Down
Loading