Skip to content
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
2 changes: 1 addition & 1 deletion RuleDocumentation/UseConsistentWhitespace.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`)

Expand Down
25 changes: 16 additions & 9 deletions Rules/UseConsistentWhitespace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -238,17 +238,19 @@ private IEnumerable<DiagnosticRecord> 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());
}
}

Expand Down Expand Up @@ -496,6 +498,11 @@ private static bool IsPreviousTokenApartByWhitespace(LinkedListNode<Token> token
hasRedundantWhitespace = actualWhitespaceSize - whiteSpaceSize > 0;
return whiteSpaceSize == actualWhitespaceSize;
}

private static bool IsPreviousTokenLParen(LinkedListNode<Token> tokenNode)
{
return tokenNode.Previous.Value.Kind == TokenKind.LParen;
}

private static bool IsNextTokenApartByWhitespace(LinkedListNode<Token> tokenNode)
{
Expand Down
4 changes: 4 additions & 0 deletions Tests/Rules/UseConsistentWhitespace.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down