Skip to content

Commit 70894cc

Browse files
author
Kapil Borle
committed
Ignore open brace if followed by magic methods
Adds exception to open brace after `foreach` and `where` magic methods. For example, the open brace in `(1..5).foreach{$_}` will not formatted but the one in `1..5 | foreach{$_}` will be formatted.
1 parent 2114db7 commit 70894cc

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

Rules/UseConsistentWhitespace.cs

Lines changed: 3 additions & 1 deletion
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
}

Tests/Rules/UseConsistentWhitespace.tests.ps1

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,20 @@ if($true) {}
4545
$violations.Count | Should Be 0
4646
}
4747

48+
It "Should not find violation if an open brace follows a foreach member invocation" {
49+
$def = @'
50+
(1..5).foreach{$_}
51+
'@
52+
Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings | Should Be $null
53+
}
54+
55+
It "Should not find violation if an open brace follows a where member invocation" {
56+
$def = @'
57+
(1..5).where{$_}
58+
'@
59+
Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings | Should Be $null
60+
}
61+
4862
}
4963

5064
Context "When a parenthesis follows a keyword" {

0 commit comments

Comments
 (0)