Skip to content

Commit 63d78e1

Browse files
author
Kapil Borle
committed
Fix PlaceCloseBrace rule behavior for NewLineAfter switch
Whenever the switch is set to false, we check if the following keyword is one of the branching statements (else, elseif, catch, finally) and if so then raise a violation. This makes the switch behavior exactly opposite to that of when it set to true.
1 parent 57ad2b8 commit 63d78e1

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

Rules/PlaceCloseBrace.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -320,9 +320,7 @@ private DiagnosticRecord GetViolationForBraceShouldHaveNewLineAfter(
320320
if (tokens.Length > 1 && tokens.Length > expectedNewLinePos)
321321
{
322322
var closeBraceToken = tokens[closeBracePos];
323-
if ((tokens[expectedNewLinePos].Kind == TokenKind.Else
324-
|| tokens[expectedNewLinePos].Kind == TokenKind.ElseIf)
325-
&& !tokensToIgnore.Contains(closeBraceToken))
323+
if (!tokensToIgnore.Contains(closeBraceToken) && IsBranchingStatementToken(tokens[expectedNewLinePos]))
326324
{
327325
return new DiagnosticRecord(
328326
GetError(Strings.PlaceCloseBraceErrorShouldFollowNewLine),

Tests/Rules/PlaceCloseBrace.tests.ps1

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,27 @@ if (Test-Path "blah") {
177177
Test-CorrectionExtentFromContent @params
178178
}
179179

180+
It "Should find a violation for a close brace followed by a catch statement" {
181+
$def = @'
182+
try {
183+
"try"
184+
} catch {
185+
"catch"
186+
}
187+
188+
'@
189+
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
190+
$violations.Count | Should Be 1
191+
$params = @{
192+
RawContent = $def
193+
DiagnosticRecord = $violations[0]
194+
CorrectionsCount = 1
195+
ViolationText = '}'
196+
CorrectionText = '}' + [System.Environment]::NewLine
197+
}
198+
Test-CorrectionExtentFromContent @params
199+
200+
}
180201
It "Should not find a violation for a close brace followed by a comma in an array expression" {
181202
$def = @'
182203
Some-Command -Param1 @{

0 commit comments

Comments
 (0)