diff --git a/Rules/PlaceCloseBrace.cs b/Rules/PlaceCloseBrace.cs index e9a7183f3..1bb15f6e1 100644 --- a/Rules/PlaceCloseBrace.cs +++ b/Rules/PlaceCloseBrace.cs @@ -277,6 +277,9 @@ private List GetCorrectionsForBraceShouldHaveNewLineAfter( var corrections = new List(); 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 @@ -285,7 +288,7 @@ private List 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; } diff --git a/Tests/Rules/PlaceCloseBrace.tests.ps1 b/Tests/Rules/PlaceCloseBrace.tests.ps1 index 5f270f75b..151ed64e8 100644 --- a/Tests/Rules/PlaceCloseBrace.tests.ps1 +++ b/Tests/Rules/PlaceCloseBrace.tests.ps1 @@ -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") {