Skip to content

Commit 97f6e1e

Browse files
author
Kapil Borle
committed
Ignore close brace in a CommandElementAst
1 parent f099781 commit 97f6e1e

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

Engine/TokenOperations.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public IEnumerable<Token> GetOpenBracesInCommandElements()
5555
/// In the above example it will return the close brace following "powershell".
5656
/// </summary>
5757
/// <returns>An enumerable of type Token</returns>
58-
public IEnumerable<Token> GetCloseBraceInCommandElement()
58+
public IEnumerable<Token> GetCloseBracesInCommandElements()
5959
{
6060
return GetBraceInCommandElement(TokenKind.RCurly);
6161
}

Rules/PlaceCloseBrace.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ public class PlaceCloseBrace : ConfigurableScriptRule
3636
[ConfigurableRuleProperty(defaultValue:false)]
3737
public bool NoEmptyLineBefore { get; protected set; }
3838

39+
private HashSet<Token> tokensToIgnore;
40+
3941
/// <summary>
4042
/// Analyzes the given ast to find violations.
4143
/// </summary>
@@ -62,6 +64,8 @@ public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string file
6264
var tokens = Helper.Instance.Tokens;
6365
var diagnosticRecords = new List<DiagnosticRecord>();
6466
var curlyStack = new Stack<Tuple<Token, int>> ();
67+
tokensToIgnore = new HashSet<Token> (
68+
new TokenOperations(tokens, ast).GetCloseBracesInCommandElements());
6569

6670
for (int k = 0; k < tokens.Length; k++)
6771
{
@@ -251,7 +255,8 @@ private DiagnosticRecord GetViolationForBraceShouldBeOnNewLine(Token[] tokens, i
251255
if (tokens.Length > 1 && tokens.Length > closeBracePos)
252256
{
253257
var closeBraceToken = tokens[closeBracePos];
254-
if (tokens[closeBracePos - 1].Kind != TokenKind.NewLine)
258+
if (tokens[closeBracePos - 1].Kind != TokenKind.NewLine
259+
&& !tokensToIgnore.Contains(closeBraceToken))
255260
{
256261
return new DiagnosticRecord(
257262
GetError(Strings.PlaceCloseBraceErrorShouldBeOnNewLine),

Tests/Rules/PlaceCloseBrace.tests.ps1

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,17 @@ $hashtable = @{
7777
$violations.Count | Should Be 1
7878
}
7979
}
80+
81+
Context "When a close brace is on the same line as its open brace" {
82+
BeforeAll {
83+
$def = @'
84+
Get-Process * | % { "blah" }
85+
'@
86+
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
87+
}
88+
89+
It "Should not find a violation" {
90+
$violations.Count | Should Be 0
91+
}
92+
}
8093
}

0 commit comments

Comments
 (0)