diff --git a/Rules/UseConsistentWhitespace.cs b/Rules/UseConsistentWhitespace.cs index 7f7550ffe..f96f68b13 100644 --- a/Rules/UseConsistentWhitespace.cs +++ b/Rules/UseConsistentWhitespace.cs @@ -362,6 +362,12 @@ private IEnumerable 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) diff --git a/Tests/Engine/InvokeFormatter.tests.ps1 b/Tests/Engine/InvokeFormatter.tests.ps1 index dcc0a6174..1705a7705 100644 --- a/Tests/Engine/InvokeFormatter.tests.ps1 +++ b/Tests/Engine/InvokeFormatter.tests.ps1 @@ -84,6 +84,22 @@ function foo { } Context "When no settings are given" { + It "Should preserve verbatim arguments across repeated formatting passes: " -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 diff --git a/Tests/Rules/UseConsistentWhitespace.tests.ps1 b/Tests/Rules/UseConsistentWhitespace.tests.ps1 index c2013fa31..c7c9c5be9 100644 --- a/Tests/Rules/UseConsistentWhitespace.tests.ps1 +++ b/Tests/Rules/UseConsistentWhitespace.tests.ps1 @@ -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 ` @@ -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