Skip to content

Commit a23af17

Browse files
bergmeisterChristoph Bergmeister
and
Christoph Bergmeister
authored
UseConsistentWhitespace: Check previous token only if it starts on the same line (#1491)
* UseConsistentWhitespace: Check previous token only if it starts on the same line * add test Co-authored-by: Christoph Bergmeister <[email protected]>
1 parent 7354892 commit a23af17

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

Rules/UseConsistentWhitespace.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,11 @@ private static bool IsPreviousTokenApartByWhitespace(LinkedListNode<Token> token
484484

485485
private static bool IsPreviousTokenApartByWhitespace(LinkedListNode<Token> tokenNode, out bool hasRedundantWhitespace)
486486
{
487+
if (tokenNode.Value.Extent.StartLineNumber != tokenNode.Previous.Value.Extent.StartLineNumber)
488+
{
489+
hasRedundantWhitespace = false;
490+
return true;
491+
}
487492
var actualWhitespaceSize = tokenNode.Value.Extent.StartColumnNumber - tokenNode.Previous.Value.Extent.EndColumnNumber;
488493
hasRedundantWhitespace = actualWhitespaceSize - whiteSpaceSize > 0;
489494
return whiteSpaceSize == actualWhitespaceSize;

Tests/Rules/UseConsistentWhitespace.tests.ps1

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,27 @@ Describe "UseWhitespace" {
4545

4646
It "Should not find violation if an open brace follows a whitespace" {
4747
$def = 'if($true) {}'
48-
Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings | Should -Be $null
48+
Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings | Should -BeNullOrEmpty
4949
}
5050

5151
It "Should not find violation if an open brace follows a foreach member invocation" {
5252
$def = '(1..5).foreach{$_}'
53-
Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings | Should -Be $null
53+
Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings | Should -BeNullOrEmpty
5454
}
5555

5656
It "Should not find violation if an open brace follows a where member invocation" {
5757
$def = '(1..5).where{$_}'
58-
Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings | Should -Be $null
58+
Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings | Should -BeNullOrEmpty
59+
}
60+
61+
It "Should not find violation if an open brace is on the next line" {
62+
$def = @'
63+
if ($true)
64+
{
65+
foo
66+
}
67+
'@
68+
Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings | Should -BeNullOrEmpty
5969
}
6070

6171
}

0 commit comments

Comments
 (0)