Skip to content

Fix PlaceCloseBrace rule behavior for NewLineAfter option #741

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 31, 2017
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
6 changes: 2 additions & 4 deletions Rules/PlaceCloseBrace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -314,11 +314,9 @@ private DiagnosticRecord GetViolationForBraceShouldHaveNewLineAfter(
if (tokens.Length > 1 && tokens.Length > expectedNewLinePos)
{
var closeBraceToken = tokens[closeBracePos];
if (tokens[expectedNewLinePos].Kind != TokenKind.NewLine
&& tokens[expectedNewLinePos].Kind != TokenKind.Comment
&& !tokensToIgnore.Contains(tokens[closeBracePos]))
if ((tokens[expectedNewLinePos].Kind == TokenKind.Else
|| tokens[expectedNewLinePos].Kind == TokenKind.ElseIf))
{

return new DiagnosticRecord(
GetError(Strings.PlaceCloseBraceErrorShouldFollowNewLine),
closeBraceToken.Extent,
Expand Down
49 changes: 41 additions & 8 deletions Tests/Rules/PlaceCloseBrace.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -122,21 +122,21 @@ $x = if ($true) { "blah" } else { "blah blah" }

Context "When a close brace should be follow a new line" {
BeforeAll {
$ruleConfiguration.'NoEmptyLineBefore' = $false
$ruleConfiguration.'IgnoreOneLineBlock' = $false
$ruleConfiguration.'NewLineAfter' = $true
}

It "Should find a violation for a close brace followed by else" {
$def = @'
if (Test-Path "blah") {
"blah"
} else {
"blah blah"
}
'@
$ruleConfiguration.'NoEmptyLineBefore' = $false
$ruleConfiguration.'IgnoreOneLineBlock' = $false
$ruleConfiguration.'NewLineAfter' = $true
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
}

It "Should find two violations" {
$violations.Count | Should Be 2
$violations.Count | Should Be 1
$params = @{
RawContent = $def
DiagnosticRecord = $violations[0]
Expand All @@ -145,15 +145,48 @@ if (Test-Path "blah") {
CorrectionText = '}' + [System.Environment]::NewLine
}
Test-CorrectionExtentFromContent @params
}

It "Should find a violation for a close brace followed by elseif" {
$def = @'
if (Test-Path "blah") {
"blah"
} elseif (Test-Path "blah blah") {
"blah blah"
}
'@
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
$violations.Count | Should Be 1
$params = @{
RawContent = $def
DiagnosticRecord = $violations[1]
DiagnosticRecord = $violations[0]
CorrectionsCount = 1
ViolationText = '}'
CorrectionText = '}' + [System.Environment]::NewLine
}
Test-CorrectionExtentFromContent @params
}

It "Should not find a violation for a close brace followed by a comma in an array expression" {
$def = @'
Some-Command -Param1 @{
key1="value1"
},@{
key2="value2"
}
'@
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
$violations.Count | Should Be 0
}

It "Should not find a violation for a close brace followed by parameter in a command expression" {
$def = @'
Some-Command -Param1 @{
key="value"
} -Param2
'@
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
$violations.Count | Should Be 0
}
}
}