FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Fix PSCloseBrace rule to not wrongly flag closing brace of one-line hashtable, which lead to incorrect formatting by bergmeister · Pull Request #1309 · PowerShell/PSScriptAnalyzer · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .cs  (1) .ps1  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
12 changes: 11 additions & 1 deletion Engine/TokenOperations.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public IEnumerable<Token> GetCloseBracesInCommandElements()
public IEnumerable<Tuple<Token, Token>> GetBracePairs()
{
var openBraceStack = new Stack<Token>();
IEnumerable<Ast> hashtableAsts = ast.FindAll(oneAst => oneAst is HashtableAst, searchNestedScriptBlocks: true);
foreach (var token in tokens)
{
if (token.Kind == TokenKind.LCurly)
Expand All @@ -84,7 +85,16 @@ public IEnumerable<Tuple<Token, Token>> GetBracePairs()
if (token.Kind == TokenKind.RCurly
&& openBraceStack.Count > 0)
{
yield return new Tuple<Token, Token>(openBraceStack.Pop(), token);
bool closeBraceBelongsToHashTable = hashtableAsts.Any(hashtableAst =>
{
return hashtableAst.Extent.EndLineNumber == token.Extent.EndLineNumber
&& hashtableAst.Extent.EndColumnNumber == token.Extent.EndColumnNumber;
});
Comment thread
bergmeister marked this conversation as resolved.

if (!closeBraceBelongsToHashTable)
{
yield return new Tuple<Token, Token>(openBraceStack.Pop(), token);
}
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions Tests/Rules/PlaceCloseBrace.tests.ps1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,18 @@ $hashtable = @{a = 1; b = 2}
}
}

Context "When there is a one line hashtable inside a one line statement" {
BeforeAll {
$scriptDefinition = 'if ($true) { $test = @{ } }'
$ruleConfiguration.'IgnoreOneLineBlock' = $true
}

It "Should not find a violation" {
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $scriptDefinition -Settings $settings
$violations.Count | Should -Be 0
}
}

Context "When there is a multi-line hashtable" {
BeforeAll {

Expand Down

Back | FazBrowse Home | New Git URL