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
5 changes: 4 additions & 1 deletion Rules/PlaceCloseBrace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,9 @@ private List<CorrectionExtent> GetCorrectionsForBraceShouldHaveNewLineAfter(
var corrections = new List<CorrectionExtent>();
var nextToken = tokens[closeBracePos + 1];
var closeBraceToken = tokens[closeBracePos];
var indentation = tokens[closeBracePos - 1].Kind == TokenKind.NewLine
? new String(' ', closeBraceToken.Extent.StartColumnNumber - 1)
: GetIndentation(tokens, closeBracePos, openBracePos);

// Also remove the whitespace between the close brace and the next token
// and indent the next token to the same level as the close brace
Expand All @@ -285,7 +288,7 @@ private List<CorrectionExtent> GetCorrectionsForBraceShouldHaveNewLineAfter(
nextToken.Extent.StartLineNumber,
closeBraceToken.Extent.StartColumnNumber,
nextToken.Extent.StartColumnNumber,
closeBraceToken.Text + Environment.NewLine + GetIndentation(tokens, closeBracePos, openBracePos),
closeBraceToken.Text + Environment.NewLine + indentation,
fileName));
return corrections;
}
Expand Down
46 changes: 46 additions & 0 deletions Tests/Rules/PlaceCloseBrace.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,52 @@ if (Test-Path "blah") {
Test-CorrectionExtentFromContent @params
}

It "Should preserve close brace indentation after a multiline if condition" {
$def = @'
if (
$true) {
$true
} else {
$false
}
'@
$expected = @'
if (
$true) {
$true
}
else {
$false
}
'@
Invoke-Formatter -ScriptDefinition $def -Settings $settings | Should -Be $expected
}

It "Should preserve nested close brace indentation after a multiline if condition" {
$def = @'
function Test {
if (
$true) {
$true
} else {
$false
}
}
'@
$expected = @'
function Test {
if (
$true) {
$true
}
else {
$false
}
}
'@
Invoke-Formatter -ScriptDefinition $def -Settings $settings | Should -Be $expected
}

It "Should find a violation for a close brace followed by elseif" {
$def = @'
if (Test-Path "blah") {
Expand Down
Loading