diff --git a/Rules/UseConsistentWhitespace.cs b/Rules/UseConsistentWhitespace.cs index 7bd3a15f1..9975036fb 100644 --- a/Rules/UseConsistentWhitespace.cs +++ b/Rules/UseConsistentWhitespace.cs @@ -195,9 +195,11 @@ private IEnumerable FindOpenBraceViolations(TokenOperations to { foreach (var lcurly in tokenOperations.GetTokenNodes(TokenKind.LCurly)) { + if (lcurly.Previous == null || !IsPreviousTokenOnSameLine(lcurly) - || lcurly.Previous.Value.Kind == TokenKind.LCurly) + || lcurly.Previous.Value.Kind == TokenKind.LCurly + || ((lcurly.Previous.Value.TokenFlags & TokenFlags.MemberName) == TokenFlags.MemberName)) { continue; } @@ -296,19 +298,24 @@ private bool IsPreviousTokenApartByWhitespace(LinkedListNode tokenNode) (tokenNode.Value.Extent.StartColumnNumber - tokenNode.Previous.Value.Extent.EndColumnNumber); } - private IEnumerable FindOperatorViolations(TokenOperations tokenOperations) + private bool IsPreviousTokenOnSameLineAndApartByWhitespace(LinkedListNode tokenNode) { - Func, bool> predicate = tokenNode => - { - return tokenNode.Previous != null - && IsPreviousTokenOnSameLine(tokenNode) - && IsPreviousTokenApartByWhitespace(tokenNode); - }; + return IsPreviousTokenOnSameLine(tokenNode) && IsPreviousTokenApartByWhitespace(tokenNode); + } + private IEnumerable FindOperatorViolations(TokenOperations tokenOperations) + { foreach (var tokenNode in tokenOperations.GetTokenNodes(IsOperator)) { - var hasWhitespaceBefore = predicate(tokenNode); - var hasWhitespaceAfter = predicate(tokenNode.Next); + if (tokenNode.Previous == null + || tokenNode.Next == null + || tokenNode.Value.Kind == TokenKind.DotDot) + { + continue; + } + + var hasWhitespaceBefore = IsPreviousTokenOnSameLineAndApartByWhitespace(tokenNode); + var hasWhitespaceAfter = IsPreviousTokenOnSameLineAndApartByWhitespace(tokenNode.Next); if (!hasWhitespaceAfter || !hasWhitespaceBefore) { diff --git a/Tests/Rules/UseConsistentWhitespace.tests.ps1 b/Tests/Rules/UseConsistentWhitespace.tests.ps1 index 67b0f20fc..7be2675d7 100644 --- a/Tests/Rules/UseConsistentWhitespace.tests.ps1 +++ b/Tests/Rules/UseConsistentWhitespace.tests.ps1 @@ -41,8 +41,21 @@ if ($true){} $def = @' if($true) {} '@ - $violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings - $violations.Count | Should Be 0 + $violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings | Should Be $null + } + + 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 + } + + 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 } } @@ -69,8 +82,7 @@ function foo($param1) { } '@ - $violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings - $violations.Count | Should Be 0 + $violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings | Should Be $null } It "Should not find a violation in a param block" { @@ -79,8 +91,7 @@ function foo() { param( ) } '@ - $violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings - $violations.Count | Should Be 0 + $violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings | Should Be $null } It "Should not find a violation in a nested open paren" { @@ -89,16 +100,14 @@ function foo($param) { ((Get-Process)) } '@ - $violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings - $violations.Count | Should Be 0 + $violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings | Should Be $null } It "Should not find a violation on a method call" { $def = @' $x.foo("bar") '@ - $violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings - $violations.Count | Should Be 0 + $violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings | Should Be $null } } @@ -148,16 +157,21 @@ $x = @" "abc" "@ '@ - $violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings - $violations.Count | Should Be 0 + $violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings | Should Be $null } It "Should not find violation if there are whitespaces of size 1 around an assignment operator for here string" { $def = @' $x = 1 '@ - $violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings - $violations.Count | Should Be 0 + $violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings | Should Be $null + } + + It "Should not find violation if there are no whitespaces around DotDot operator" { + $def = @' +1..5 +'@ + Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings | Should Be $null } } @@ -181,8 +195,7 @@ $x = @(1,2) $def = @' $x = @(1, 2) '@ - $violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings - $violations.Count | Should Be 0 + $violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings | Should Be $null } } @@ -206,8 +219,7 @@ $x = @{a=1;b=2} $def = @' $x = @{a=1; b=2} '@ - $violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings - $violations.Count | Should Be 0 + $violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings | Should Be $null } It "Should not find a violation if a new-line follows a semi-colon" { @@ -217,16 +229,14 @@ $x = @{ b=2 } '@ - $violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings - $violations.Count | Should Be 0 + $violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings | Should Be $null } It "Should not find a violation if a end of input follows a semi-colon" { $def = @' $x = "abc"; '@ - $violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings - $violations.Count | Should Be 0 + $violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings | Should Be $null }