Skip to content

Add exceptions to UseConsistentWhitespace rule #744

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

Merged
merged 3 commits into from
Apr 3, 2017
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
27 changes: 17 additions & 10 deletions Rules/UseConsistentWhitespace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,11 @@ private IEnumerable<DiagnosticRecord> 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;
}
Expand Down Expand Up @@ -296,19 +298,24 @@ private bool IsPreviousTokenApartByWhitespace(LinkedListNode<Token> tokenNode)
(tokenNode.Value.Extent.StartColumnNumber - tokenNode.Previous.Value.Extent.EndColumnNumber);
}

private IEnumerable<DiagnosticRecord> FindOperatorViolations(TokenOperations tokenOperations)
private bool IsPreviousTokenOnSameLineAndApartByWhitespace(LinkedListNode<Token> tokenNode)
{
Func<LinkedListNode<Token>, bool> predicate = tokenNode =>
{
return tokenNode.Previous != null
&& IsPreviousTokenOnSameLine(tokenNode)
&& IsPreviousTokenApartByWhitespace(tokenNode);
};
return IsPreviousTokenOnSameLine(tokenNode) && IsPreviousTokenApartByWhitespace(tokenNode);
}

private IEnumerable<DiagnosticRecord> 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)
{
Expand Down
54 changes: 32 additions & 22 deletions Tests/Rules/UseConsistentWhitespace.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

}
Expand All @@ -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" {
Expand All @@ -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" {
Expand All @@ -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
}
}

Expand Down Expand Up @@ -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
}
}

Expand All @@ -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
}
}

Expand All @@ -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" {
Expand All @@ -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
}


Expand Down