Skip to content

Commit 9e3c1d6

Browse files
author
Kapil Borle
committed
Add option to ignore close brace in a one line block
1 parent 613c9a0 commit 9e3c1d6

File tree

2 files changed

+16
-14
lines changed

2 files changed

+16
-14
lines changed

Rules/PlaceCloseBrace.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public class PlaceCloseBrace : ConfigurableRule
3737
public bool NoEmptyLineBefore { get; protected set; }
3838

3939
[ConfigurableRuleProperty(defaultValue: true)]
40-
public bool IgnoreOneLineIf { get; protected set; }
40+
public bool IgnoreOneLineBlock { get; protected set; }
4141

4242
private HashSet<Token> tokensToIgnore;
4343

@@ -61,22 +61,22 @@ public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string file
6161

6262
// TODO Should have the following options
6363
// * no-empty-lines-before
64-
// * align (if close brance and open brace on new lines align with open brace,
65-
// if close brace is on new line but open brace is not align with the first keyword on open brace line)
6664

6765
var tokens = Helper.Instance.Tokens;
6866
var diagnosticRecords = new List<DiagnosticRecord>();
6967
var curlyStack = new Stack<Tuple<Token, int>> ();
7068

7169
// TODO move part common with PlaceOpenBrace to one place
72-
// TODO use a general switch to ignore blocks on one line
7370
var tokenOps = new TokenOperations(tokens, ast);
7471
tokensToIgnore = new HashSet<Token> (tokenOps.GetCloseBracesInCommandElements());
75-
if (IgnoreOneLineIf)
72+
73+
// Ignore open braces that are part of a one line if-else statement
74+
// E.g. $x = if ($true) { "blah" } else { "blah blah" }
75+
if (IgnoreOneLineBlock)
7676
{
77-
foreach (var closeBraceToken in tokenOps.GetCloseBraceInOneLineIfStatement())
77+
foreach (var pair in tokenOps.GetBracePairsOnSameLine())
7878
{
79-
tokensToIgnore.Add(closeBraceToken);
79+
tokensToIgnore.Add(pair.Item2);
8080
}
8181
}
8282

Tests/Rules/PlaceCloseBrace.tests.ps1

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
Import-Module PSScriptAnalyzer
2-
$ruleName = "PSPlaceCloseBrace"
2+
$ruleConfiguration = @{
3+
Enable = $true
4+
NoEmptyLineBefore = $true
5+
IgnoreOneLineBlock = $true
6+
}
7+
38
$settings = @{
49
IncludeRules = @("PSPlaceCloseBrace")
510
Rules = @{
6-
PSPlaceCloseBrace = @{
7-
Enable = $true
8-
NoEmptyLineBefore = $true
9-
IgnoreOneLineIf = $true
10-
}
11+
PSPlaceCloseBrace = $ruleConfiguration
1112
}
1213
}
1314

14-
1515
Describe "PlaceCloseBrace" {
1616
Context "When a close brace is not on a new line" {
1717
BeforeAll {
@@ -84,6 +84,7 @@ $hashtable = @{
8484
$def = @'
8585
Get-Process * | % { "blah" }
8686
'@
87+
$ruleConfiguration.'IgnoreOneLineBlock' = $false
8788
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
8889
}
8990

@@ -95,6 +96,7 @@ Get-Process * | % { "blah" }
9596
$def = @'
9697
$x = if ($true) { "blah" } else { "blah blah" }
9798
'@
99+
$ruleConfiguration.'IgnoreOneLineBlock' = $true
98100
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
99101
$violations.Count | Should Be 0
100102
}

0 commit comments

Comments
 (0)