diff --git a/Rules/PlaceCloseBrace.cs b/Rules/PlaceCloseBrace.cs index d9df57c75..652197afd 100644 --- a/Rules/PlaceCloseBrace.cs +++ b/Rules/PlaceCloseBrace.cs @@ -288,7 +288,7 @@ private string GetIndentation(Token[] tokens, int closeBracePos, int openBracePo { // if open brace on a new line by itself, use its indentation var openBraceToken = tokens[openBracePos]; - if (tokens[openBracePos - 1].Kind == TokenKind.NewLine) + if (openBracePos > 0 && tokens[openBracePos - 1].Kind == TokenKind.NewLine) { return new String(' ', openBraceToken.Extent.StartColumnNumber - 1); } diff --git a/Tests/Rules/PlaceCloseBrace.tests.ps1 b/Tests/Rules/PlaceCloseBrace.tests.ps1 index d9e6dda0a..22d4785f6 100644 --- a/Tests/Rules/PlaceCloseBrace.tests.ps1 +++ b/Tests/Rules/PlaceCloseBrace.tests.ps1 @@ -80,19 +80,28 @@ $hashtable = @{a = 1; b = 2} Context "When there is a multi-line hashtable" { BeforeAll { - $def = @' -$hashtable = @{ - a = 1 - b = 2} -'@ + $ruleConfiguration.'NoEmptyLineBefore' = $false $ruleConfiguration.'IgnoreOneLineBlock' = $true $ruleConfiguration.'NewLineAfter' = $false - $violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings } It "Should find a violation" { + $def = @' + $hashtable = @{ + a = 1 + b = 2} +'@ + $violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings + $violations.Count | Should -Be 1 + } + + It "Should not crash when hashtable is defined on first token" { + $def = "@{ `n Key = value }" + + $violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings -ErrorAction Stop $violations.Count | Should -Be 1 + Invoke-Formatter -ScriptDefinition $def -ErrorAction Stop } }