Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UseConsistentWhitespace: Check previous token only if it starts on the same line #1491

Merged
merged 2 commits into from
Jun 11, 2020
Merged
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
5 changes: 5 additions & 0 deletions Rules/UseConsistentWhitespace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,11 @@ private static bool IsPreviousTokenApartByWhitespace(LinkedListNode<Token> token

private static bool IsPreviousTokenApartByWhitespace(LinkedListNode<Token> 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;
Expand Down
16 changes: 13 additions & 3 deletions Tests/Rules/UseConsistentWhitespace.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

}
Expand Down