From f99297e9322e718ddc3f91cb7b3b681caadabd49 Mon Sep 17 00:00:00 2001 From: Christoph Bergmeister Date: Tue, 12 May 2020 16:43:07 +0100 Subject: [PATCH 1/2] UseConsistentWhitespace: Check previous token only if it starts on the same line --- Rules/UseConsistentWhitespace.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Rules/UseConsistentWhitespace.cs b/Rules/UseConsistentWhitespace.cs index f589d9e45..7e2562327 100644 --- a/Rules/UseConsistentWhitespace.cs +++ b/Rules/UseConsistentWhitespace.cs @@ -483,6 +483,11 @@ private static bool IsPreviousTokenApartByWhitespace(LinkedListNode token private static bool IsPreviousTokenApartByWhitespace(LinkedListNode tokenNode, out bool hasRedundantWhitespace) { + if (tokenNode.Value.Extent.StartLineNumber != tokenNode.Previous.Value.Extent.StartLineNumber) + { + hasRedundantWhitespace = false; + return true; + } var actualWhitespaceSize = tokenNode.Value.Extent.StartColumnNumber - tokenNode.Previous.Value.Extent.EndColumnNumber; hasRedundantWhitespace = actualWhitespaceSize - whiteSpaceSize > 0; return whiteSpaceSize == actualWhitespaceSize; From 6b9fcded80cda73882913a7b97e94c47f9038633 Mon Sep 17 00:00:00 2001 From: Christoph Bergmeister Date: Tue, 12 May 2020 16:50:23 +0100 Subject: [PATCH 2/2] add test --- Tests/Rules/UseConsistentWhitespace.tests.ps1 | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/Tests/Rules/UseConsistentWhitespace.tests.ps1 b/Tests/Rules/UseConsistentWhitespace.tests.ps1 index 23bf18350..36da74f45 100644 --- a/Tests/Rules/UseConsistentWhitespace.tests.ps1 +++ b/Tests/Rules/UseConsistentWhitespace.tests.ps1 @@ -39,17 +39,27 @@ Describe "UseWhitespace" { It "Should not find violation if an open brace follows a whitespace" { $def = 'if($true) {}' - Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings | Should -Be $null + Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings | Should -BeNullOrEmpty } It "Should not find violation if an open brace follows a foreach member invocation" { $def = '(1..5).foreach{$_}' - Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings | Should -Be $null + Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings | Should -BeNullOrEmpty } It "Should not find violation if an open brace follows a where member invocation" { $def = '(1..5).where{$_}' - Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings | Should -Be $null + Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings | Should -BeNullOrEmpty + } + + It "Should not find violation if an open brace is on the next line" { + $def = @' +if ($true) +{ + foo +} +'@ + Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings | Should -BeNullOrEmpty } }