Skip to content

Commit 6aaa6f4

Browse files
author
Kapil Borle
authored
Merge pull request #744 from PowerShell/kapilmb/update-whitespace-rule
Add exceptions to UseConsistentWhitespace rule
2 parents 2114db7 + 365f337 commit 6aaa6f4

File tree

2 files changed

+49
-32
lines changed

2 files changed

+49
-32
lines changed

Rules/UseConsistentWhitespace.cs

+17-10
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,11 @@ private IEnumerable<DiagnosticRecord> FindOpenBraceViolations(TokenOperations to
195195
{
196196
foreach (var lcurly in tokenOperations.GetTokenNodes(TokenKind.LCurly))
197197
{
198+
198199
if (lcurly.Previous == null
199200
|| !IsPreviousTokenOnSameLine(lcurly)
200-
|| lcurly.Previous.Value.Kind == TokenKind.LCurly)
201+
|| lcurly.Previous.Value.Kind == TokenKind.LCurly
202+
|| ((lcurly.Previous.Value.TokenFlags & TokenFlags.MemberName) == TokenFlags.MemberName))
201203
{
202204
continue;
203205
}
@@ -296,19 +298,24 @@ private bool IsPreviousTokenApartByWhitespace(LinkedListNode<Token> tokenNode)
296298
(tokenNode.Value.Extent.StartColumnNumber - tokenNode.Previous.Value.Extent.EndColumnNumber);
297299
}
298300

299-
private IEnumerable<DiagnosticRecord> FindOperatorViolations(TokenOperations tokenOperations)
301+
private bool IsPreviousTokenOnSameLineAndApartByWhitespace(LinkedListNode<Token> tokenNode)
300302
{
301-
Func<LinkedListNode<Token>, bool> predicate = tokenNode =>
302-
{
303-
return tokenNode.Previous != null
304-
&& IsPreviousTokenOnSameLine(tokenNode)
305-
&& IsPreviousTokenApartByWhitespace(tokenNode);
306-
};
303+
return IsPreviousTokenOnSameLine(tokenNode) && IsPreviousTokenApartByWhitespace(tokenNode);
304+
}
307305

306+
private IEnumerable<DiagnosticRecord> FindOperatorViolations(TokenOperations tokenOperations)
307+
{
308308
foreach (var tokenNode in tokenOperations.GetTokenNodes(IsOperator))
309309
{
310-
var hasWhitespaceBefore = predicate(tokenNode);
311-
var hasWhitespaceAfter = predicate(tokenNode.Next);
310+
if (tokenNode.Previous == null
311+
|| tokenNode.Next == null
312+
|| tokenNode.Value.Kind == TokenKind.DotDot)
313+
{
314+
continue;
315+
}
316+
317+
var hasWhitespaceBefore = IsPreviousTokenOnSameLineAndApartByWhitespace(tokenNode);
318+
var hasWhitespaceAfter = IsPreviousTokenOnSameLineAndApartByWhitespace(tokenNode.Next);
312319

313320
if (!hasWhitespaceAfter || !hasWhitespaceBefore)
314321
{

Tests/Rules/UseConsistentWhitespace.tests.ps1

+32-22
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,21 @@ if ($true){}
4141
$def = @'
4242
if($true) {}
4343
'@
44-
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
45-
$violations.Count | Should Be 0
44+
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings | Should Be $null
45+
}
46+
47+
It "Should not find violation if an open brace follows a foreach member invocation" {
48+
$def = @'
49+
(1..5).foreach{$_}
50+
'@
51+
Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings | Should Be $null
52+
}
53+
54+
It "Should not find violation if an open brace follows a where member invocation" {
55+
$def = @'
56+
(1..5).where{$_}
57+
'@
58+
Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings | Should Be $null
4659
}
4760

4861
}
@@ -69,8 +82,7 @@ function foo($param1) {
6982
7083
}
7184
'@
72-
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
73-
$violations.Count | Should Be 0
85+
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings | Should Be $null
7486
}
7587

7688
It "Should not find a violation in a param block" {
@@ -79,8 +91,7 @@ function foo() {
7991
param( )
8092
}
8193
'@
82-
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
83-
$violations.Count | Should Be 0
94+
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings | Should Be $null
8495
}
8596

8697
It "Should not find a violation in a nested open paren" {
@@ -89,16 +100,14 @@ function foo($param) {
89100
((Get-Process))
90101
}
91102
'@
92-
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
93-
$violations.Count | Should Be 0
103+
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings | Should Be $null
94104
}
95105

96106
It "Should not find a violation on a method call" {
97107
$def = @'
98108
$x.foo("bar")
99109
'@
100-
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
101-
$violations.Count | Should Be 0
110+
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings | Should Be $null
102111
}
103112
}
104113

@@ -148,16 +157,21 @@ $x = @"
148157
"abc"
149158
"@
150159
'@
151-
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
152-
$violations.Count | Should Be 0
160+
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings | Should Be $null
153161
}
154162

155163
It "Should not find violation if there are whitespaces of size 1 around an assignment operator for here string" {
156164
$def = @'
157165
$x = 1
158166
'@
159-
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
160-
$violations.Count | Should Be 0
167+
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings | Should Be $null
168+
}
169+
170+
It "Should not find violation if there are no whitespaces around DotDot operator" {
171+
$def = @'
172+
1..5
173+
'@
174+
Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings | Should Be $null
161175
}
162176
}
163177

@@ -181,8 +195,7 @@ $x = @(1,2)
181195
$def = @'
182196
$x = @(1, 2)
183197
'@
184-
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
185-
$violations.Count | Should Be 0
198+
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings | Should Be $null
186199
}
187200
}
188201

@@ -206,8 +219,7 @@ $x = @{a=1;b=2}
206219
$def = @'
207220
$x = @{a=1; b=2}
208221
'@
209-
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
210-
$violations.Count | Should Be 0
222+
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings | Should Be $null
211223
}
212224

213225
It "Should not find a violation if a new-line follows a semi-colon" {
@@ -217,16 +229,14 @@ $x = @{
217229
b=2
218230
}
219231
'@
220-
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
221-
$violations.Count | Should Be 0
232+
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings | Should Be $null
222233
}
223234

224235
It "Should not find a violation if a end of input follows a semi-colon" {
225236
$def = @'
226237
$x = "abc";
227238
'@
228-
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
229-
$violations.Count | Should Be 0
239+
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings | Should Be $null
230240
}
231241

232242

0 commit comments

Comments
 (0)