Skip to content

Commit

Permalink
Merge pull request #1235 from bergmeister/CloseBraceFix
Browse files Browse the repository at this point in the history
Prevent PSCloseBrace crash if hashtable definition start on first token and there is a PSCloseBrace violation
  • Loading branch information
JamesWTruher authored Jun 6, 2019
2 parents 84f433a + 098139f commit c3f2a20
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Rules/PlaceCloseBrace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
21 changes: 15 additions & 6 deletions Tests/Rules/PlaceCloseBrace.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down

0 comments on commit c3f2a20

Please sign in to comment.