Skip to content

Commit 39fbbd9

Browse files
authored
UseConsistenWhitespace - CheckOpenBrace setting: Do not warn when being preceded by open parenthesis (#1633)
1 parent 8a65e41 commit 39fbbd9

File tree

3 files changed

+21
-10
lines changed

3 files changed

+21
-10
lines changed

Diff for: RuleDocumentation/UseConsistentWhitespace.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Checks if there is a space after the opening brace and a space before the closin
3939

4040
#### CheckOpenBrace: bool (Default value is `$true`)
4141

42-
Checks if there is a space between a keyword and its corresponding open brace. E.g. `foo { }` instead of `foo{ }`.
42+
Checks if there is a space between a keyword and its corresponding open brace. E.g. `foo { }` instead of `foo{ }`. If an open brace is preceded by an open parenthesis, then no space is required.
4343

4444
#### CheckOpenParen: bool (Default value is `$true`)
4545

Diff for: Rules/UseConsistentWhitespace.cs

+16-9
Original file line numberDiff line numberDiff line change
@@ -238,17 +238,19 @@ private IEnumerable<DiagnosticRecord> FindOpenBraceViolations(TokenOperations to
238238
continue;
239239
}
240240

241-
if (!IsPreviousTokenApartByWhitespace(lcurly))
241+
if (IsPreviousTokenApartByWhitespace(lcurly) || IsPreviousTokenLParen(lcurly))
242242
{
243-
yield return new DiagnosticRecord(
244-
GetError(ErrorKind.BeforeOpeningBrace),
245-
lcurly.Value.Extent,
246-
GetName(),
247-
GetDiagnosticSeverity(),
248-
tokenOperations.Ast.Extent.File,
249-
null,
250-
GetCorrections(lcurly.Previous.Value, lcurly.Value, lcurly.Next.Value, false, true).ToList());
243+
continue;
251244
}
245+
246+
yield return new DiagnosticRecord(
247+
GetError(ErrorKind.BeforeOpeningBrace),
248+
lcurly.Value.Extent,
249+
GetName(),
250+
GetDiagnosticSeverity(),
251+
tokenOperations.Ast.Extent.File,
252+
null,
253+
GetCorrections(lcurly.Previous.Value, lcurly.Value, lcurly.Next.Value, false, true).ToList());
252254
}
253255
}
254256

@@ -496,6 +498,11 @@ private static bool IsPreviousTokenApartByWhitespace(LinkedListNode<Token> token
496498
hasRedundantWhitespace = actualWhitespaceSize - whiteSpaceSize > 0;
497499
return whiteSpaceSize == actualWhitespaceSize;
498500
}
501+
502+
private static bool IsPreviousTokenLParen(LinkedListNode<Token> tokenNode)
503+
{
504+
return tokenNode.Previous.Value.Kind == TokenKind.LParen;
505+
}
499506

500507
private static bool IsNextTokenApartByWhitespace(LinkedListNode<Token> tokenNode)
501508
{

Diff for: Tests/Rules/UseConsistentWhitespace.tests.ps1

+4
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ if ($true)
6868
Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings | Should -BeNullOrEmpty
6969
}
7070

71+
It 'Should not find a violation if an open paren is before an opening brace' {
72+
Invoke-ScriptAnalyzer -ScriptDefinition '$ast.Find({ $oneAst -is [TypeExpressionAst] })' -Settings $settings |
73+
Should -BeNullOrEmpty
74+
}
7175
}
7276

7377
Context "When a parenthesis follows a keyword" {

0 commit comments

Comments
 (0)