Skip to content

Commit 9a420d1

Browse files
author
Kapil Borle
authored
Merge pull request #741 from PowerShell/kapilmb/fix-close-brace-rule
Fix PlaceCloseBrace rule behavior for NewLineAfter option
2 parents 8e3e68a + acafe85 commit 9a420d1

File tree

2 files changed

+43
-12
lines changed

2 files changed

+43
-12
lines changed

Rules/PlaceCloseBrace.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -314,11 +314,9 @@ private DiagnosticRecord GetViolationForBraceShouldHaveNewLineAfter(
314314
if (tokens.Length > 1 && tokens.Length > expectedNewLinePos)
315315
{
316316
var closeBraceToken = tokens[closeBracePos];
317-
if (tokens[expectedNewLinePos].Kind != TokenKind.NewLine
318-
&& tokens[expectedNewLinePos].Kind != TokenKind.Comment
319-
&& !tokensToIgnore.Contains(tokens[closeBracePos]))
317+
if ((tokens[expectedNewLinePos].Kind == TokenKind.Else
318+
|| tokens[expectedNewLinePos].Kind == TokenKind.ElseIf))
320319
{
321-
322320
return new DiagnosticRecord(
323321
GetError(Strings.PlaceCloseBraceErrorShouldFollowNewLine),
324322
closeBraceToken.Extent,

Tests/Rules/PlaceCloseBrace.tests.ps1

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -122,21 +122,21 @@ $x = if ($true) { "blah" } else { "blah blah" }
122122

123123
Context "When a close brace should be follow a new line" {
124124
BeforeAll {
125+
$ruleConfiguration.'NoEmptyLineBefore' = $false
126+
$ruleConfiguration.'IgnoreOneLineBlock' = $false
127+
$ruleConfiguration.'NewLineAfter' = $true
128+
}
129+
130+
It "Should find a violation for a close brace followed by else" {
125131
$def = @'
126132
if (Test-Path "blah") {
127133
"blah"
128134
} else {
129135
"blah blah"
130136
}
131137
'@
132-
$ruleConfiguration.'NoEmptyLineBefore' = $false
133-
$ruleConfiguration.'IgnoreOneLineBlock' = $false
134-
$ruleConfiguration.'NewLineAfter' = $true
135138
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
136-
}
137-
138-
It "Should find two violations" {
139-
$violations.Count | Should Be 2
139+
$violations.Count | Should Be 1
140140
$params = @{
141141
RawContent = $def
142142
DiagnosticRecord = $violations[0]
@@ -145,15 +145,48 @@ if (Test-Path "blah") {
145145
CorrectionText = '}' + [System.Environment]::NewLine
146146
}
147147
Test-CorrectionExtentFromContent @params
148+
}
148149

150+
It "Should find a violation for a close brace followed by elseif" {
151+
$def = @'
152+
if (Test-Path "blah") {
153+
"blah"
154+
} elseif (Test-Path "blah blah") {
155+
"blah blah"
156+
}
157+
'@
158+
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
159+
$violations.Count | Should Be 1
149160
$params = @{
150161
RawContent = $def
151-
DiagnosticRecord = $violations[1]
162+
DiagnosticRecord = $violations[0]
152163
CorrectionsCount = 1
153164
ViolationText = '}'
154165
CorrectionText = '}' + [System.Environment]::NewLine
155166
}
156167
Test-CorrectionExtentFromContent @params
157168
}
169+
170+
It "Should not find a violation for a close brace followed by a comma in an array expression" {
171+
$def = @'
172+
Some-Command -Param1 @{
173+
key1="value1"
174+
},@{
175+
key2="value2"
176+
}
177+
'@
178+
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
179+
$violations.Count | Should Be 0
180+
}
181+
182+
It "Should not find a violation for a close brace followed by parameter in a command expression" {
183+
$def = @'
184+
Some-Command -Param1 @{
185+
key="value"
186+
} -Param2
187+
'@
188+
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
189+
$violations.Count | Should Be 0
190+
}
158191
}
159192
}

0 commit comments

Comments
 (0)