diff --git a/RuleDocumentation/UseConsistentWhitespace.md b/RuleDocumentation/UseConsistentWhitespace.md index a100e9957..8d47809a6 100644 --- a/RuleDocumentation/UseConsistentWhitespace.md +++ b/RuleDocumentation/UseConsistentWhitespace.md @@ -39,7 +39,7 @@ Checks if there is a space after the opening brace and a space before the closin #### CheckOpenBrace: bool (Default value is `$true`) -Checks if there is a space between a keyword and its corresponding open brace. E.g. `foo { }` instead of `foo{ }`. +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. #### CheckOpenParen: bool (Default value is `$true`) diff --git a/Rules/UseConsistentWhitespace.cs b/Rules/UseConsistentWhitespace.cs index bad8272b5..6112c43f9 100644 --- a/Rules/UseConsistentWhitespace.cs +++ b/Rules/UseConsistentWhitespace.cs @@ -238,17 +238,19 @@ private IEnumerable FindOpenBraceViolations(TokenOperations to continue; } - if (!IsPreviousTokenApartByWhitespace(lcurly)) + if (IsPreviousTokenApartByWhitespace(lcurly) || IsPreviousTokenLParen(lcurly)) { - yield return new DiagnosticRecord( - GetError(ErrorKind.BeforeOpeningBrace), - lcurly.Value.Extent, - GetName(), - GetDiagnosticSeverity(), - tokenOperations.Ast.Extent.File, - null, - GetCorrections(lcurly.Previous.Value, lcurly.Value, lcurly.Next.Value, false, true).ToList()); + continue; } + + yield return new DiagnosticRecord( + GetError(ErrorKind.BeforeOpeningBrace), + lcurly.Value.Extent, + GetName(), + GetDiagnosticSeverity(), + tokenOperations.Ast.Extent.File, + null, + GetCorrections(lcurly.Previous.Value, lcurly.Value, lcurly.Next.Value, false, true).ToList()); } } @@ -496,6 +498,11 @@ private static bool IsPreviousTokenApartByWhitespace(LinkedListNode token hasRedundantWhitespace = actualWhitespaceSize - whiteSpaceSize > 0; return whiteSpaceSize == actualWhitespaceSize; } + + private static bool IsPreviousTokenLParen(LinkedListNode tokenNode) + { + return tokenNode.Previous.Value.Kind == TokenKind.LParen; + } private static bool IsNextTokenApartByWhitespace(LinkedListNode tokenNode) { diff --git a/Tests/Rules/UseConsistentWhitespace.tests.ps1 b/Tests/Rules/UseConsistentWhitespace.tests.ps1 index 681fc4027..27e17e74e 100644 --- a/Tests/Rules/UseConsistentWhitespace.tests.ps1 +++ b/Tests/Rules/UseConsistentWhitespace.tests.ps1 @@ -68,6 +68,10 @@ if ($true) Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings | Should -BeNullOrEmpty } + It 'Should not find a violation if an open paren is before an opening brace' { + Invoke-ScriptAnalyzer -ScriptDefinition '$ast.Find({ $oneAst -is [TypeExpressionAst] })' -Settings $settings | + Should -BeNullOrEmpty + } } Context "When a parenthesis follows a keyword" {