Skip to content

Commit 141e109

Browse files
author
Kapil Borle
authored
Merge pull request #748 from PowerShell/kapilmb/fix-close-brace-rule
Fix PlaceCloseBrace rule behavior for IgnoreOneLineBlock switch
2 parents f7ed15c + 2f2f158 commit 141e109

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

Rules/PlaceCloseBrace.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,8 @@ private DiagnosticRecord GetViolationForBraceShouldHaveNewLineAfter(
315315
{
316316
var closeBraceToken = tokens[closeBracePos];
317317
if ((tokens[expectedNewLinePos].Kind == TokenKind.Else
318-
|| tokens[expectedNewLinePos].Kind == TokenKind.ElseIf))
318+
|| tokens[expectedNewLinePos].Kind == TokenKind.ElseIf)
319+
&& !tokensToIgnore.Contains(closeBraceToken))
319320
{
320321
return new DiagnosticRecord(
321322
GetError(Strings.PlaceCloseBraceErrorShouldFollowNewLine),

Tests/Rules/PlaceCloseBrace.tests.ps1

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,16 @@ $x = if ($true) { "blah" } else { "blah blah" }
118118
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
119119
$violations.Count | Should Be 0
120120
}
121+
122+
It "Should ignore violations for one line if statement even if NewLineAfter is true" {
123+
$def = @'
124+
$x = if ($true) { "blah" } else { "blah blah" }
125+
'@
126+
$ruleConfiguration.'IgnoreOneLineBlock' = $true
127+
$ruleConfiguration.'NewLineAfter' = $true
128+
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
129+
$violations.Count | Should Be 0
130+
}
121131
}
122132

123133
Context "When a close brace should be follow a new line" {

0 commit comments

Comments
 (0)