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
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There doesn't seem to be an assertion here. Since the formatter uses the same rule, this seems superfluous. (do you want an explicit test for the formatter?)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The assertion here is that the formatter does not throw an exception. The call to Invoke-ScriptAnalyzer is the core unit test but the call to Invoke-Formatter is the integration test.

}
}

Expand Down