Skip to content

Commit c3f2a20

Browse files
authored
Merge pull request #1235 from bergmeister/CloseBraceFix
Prevent PSCloseBrace crash if hashtable definition start on first token and there is a PSCloseBrace violation
2 parents 84f433a + 098139f commit c3f2a20

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

Rules/PlaceCloseBrace.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ private string GetIndentation(Token[] tokens, int closeBracePos, int openBracePo
288288
{
289289
// if open brace on a new line by itself, use its indentation
290290
var openBraceToken = tokens[openBracePos];
291-
if (tokens[openBracePos - 1].Kind == TokenKind.NewLine)
291+
if (openBracePos > 0 && tokens[openBracePos - 1].Kind == TokenKind.NewLine)
292292
{
293293
return new String(' ', openBraceToken.Extent.StartColumnNumber - 1);
294294
}

Tests/Rules/PlaceCloseBrace.tests.ps1

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,19 +80,28 @@ $hashtable = @{a = 1; b = 2}
8080

8181
Context "When there is a multi-line hashtable" {
8282
BeforeAll {
83-
$def = @'
84-
$hashtable = @{
85-
a = 1
86-
b = 2}
87-
'@
83+
8884
$ruleConfiguration.'NoEmptyLineBefore' = $false
8985
$ruleConfiguration.'IgnoreOneLineBlock' = $true
9086
$ruleConfiguration.'NewLineAfter' = $false
91-
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
9287
}
9388

9489
It "Should find a violation" {
90+
$def = @'
91+
$hashtable = @{
92+
a = 1
93+
b = 2}
94+
'@
95+
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
96+
$violations.Count | Should -Be 1
97+
}
98+
99+
It "Should not crash when hashtable is defined on first token" {
100+
$def = "@{ `n Key = value }"
101+
102+
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings -ErrorAction Stop
95103
$violations.Count | Should -Be 1
104+
Invoke-Formatter -ScriptDefinition $def -ErrorAction Stop
96105
}
97106
}
98107

0 commit comments

Comments
 (0)