Skip to content

Commit 63c304d

Browse files
author
Kapil Borle
committed
Handle hashtable in PlaceCloseBrace rule
1 parent c6a311e commit 63c304d

File tree

2 files changed

+44
-7
lines changed

2 files changed

+44
-7
lines changed

Rules/PlaceCloseBrace.cs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,28 +50,37 @@ public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string file
5050
}
5151

5252
// TODO Should have the following options
53-
// * no-empty-line-before
53+
// * no-empty-lines-before
5454
// * align (if close brance and open brace on new lines align with open brace,
5555
// if close brace is on new line but open brace is not align with the first keyword on open brace line)
5656

5757
var tokens = Helper.Instance.Tokens;
5858
var diagnosticRecords = new List<DiagnosticRecord>();
59-
var openBracePosStack = new Stack<int>();
59+
var curlyStack = new Stack<Tuple<Token, int>> ();
6060

6161
for (int k = 0; k < tokens.Length; k++)
6262
{
6363
var token = tokens[k];
64-
if (token.Kind == TokenKind.LCurly)
64+
if (token.Kind == TokenKind.LCurly || token.Kind == TokenKind.AtCurly)
6565
{
66-
openBracePosStack.Push(k);
66+
curlyStack.Push(new Tuple<Token, int>(token, k));
6767
continue;
6868
}
6969

7070
if (token.Kind == TokenKind.RCurly)
7171
{
72-
if (openBracePosStack.Count > 0)
72+
if (curlyStack.Count > 0)
7373
{
74-
var openBracePos = openBracePosStack.Pop();
74+
var openBraceToken = curlyStack.Peek().Item1;
75+
var openBracePos = curlyStack.Pop().Item2;
76+
77+
// Ignore if a one line hashtable
78+
if (openBraceToken.Kind == TokenKind.AtCurly
79+
&& openBraceToken.Extent.StartLineNumber == token.Extent.StartLineNumber)
80+
{
81+
continue;
82+
}
83+
7584
AddToDiagnosticRecords(
7685
GetViolationForBraceOnSameLine(tokens, k, openBracePos, fileName),
7786
ref diagnosticRecords);

Tests/Rules/PlaceCloseBrace.tests.ps1

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,32 @@ function foo {
4949
$violations[0].Extent.Text | Should Be "}"
5050
}
5151
}
52-
}
52+
53+
Context "When there is a one line hashtable" {
54+
BeforeAll {
55+
$def = @'
56+
$hashtable = @{a = 1; b = 2}
57+
'@
58+
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
59+
}
60+
61+
It "Should not find a violation" {
62+
$violations.Count | Should Be 0
63+
}
64+
}
65+
66+
Context "When there is a multi-line hashtable" {
67+
BeforeAll {
68+
$def = @'
69+
$hashtable = @{
70+
a = 1
71+
b = 2}
72+
'@
73+
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
74+
}
75+
76+
It "Should find a violation" {
77+
$violations.Count | Should Be 1
78+
}
79+
}
80+
}

0 commit comments

Comments
 (0)