Skip to content

Commit 613c9a0

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

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

Rules/PlaceOpenBrace.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public class PlaceOpenBrace : ConfigurableRule
4646
public bool NewLineAfter { get; protected set; }
4747

4848
[ConfigurableRuleProperty(defaultValue: true)]
49-
public bool IgnoreOneLineIf { get; protected set; }
49+
public bool IgnoreOneLineBlock { get; protected set; }
5050

5151
private List<Func<Token[], Ast, string, IEnumerable<DiagnosticRecord>>> violationFinders
5252
= new List<Func<Token[], Ast, string, IEnumerable<DiagnosticRecord>>>();
@@ -99,18 +99,20 @@ public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string file
9999
var tokens = Helper.Instance.Tokens;
100100

101101
// Ignore open braces that are part of arguments to a command
102-
// * E.g. get-process | % { "blah }f
102+
// * E.g. get-process | % { "blah }
103103
// In the above case even if OnSameLine == false, we should not
104104
// flag the open brace as it would move the brace to the next line
105105
// and will invalidate the command
106106
var tokenOps = new TokenOperations(tokens, ast);
107107
tokensToIgnore = new HashSet<Token>(tokenOps.GetOpenBracesInCommandElements());
108108

109-
if (IgnoreOneLineIf)
109+
// Ignore open braces that are part of a one line if-else statement
110+
// E.g. $x = if ($true) { "blah" } else { "blah blah" }
111+
if (IgnoreOneLineBlock)
110112
{
111-
foreach (var openBraceToken in tokenOps.GetOpenBraceInOneLineIfStatement())
113+
foreach (var pair in tokenOps.GetBracePairsOnSameLine())
112114
{
113-
tokensToIgnore.Add(openBraceToken);
115+
tokensToIgnore.Add(pair.Item1);
114116
}
115117
}
116118

Tests/Rules/PlaceOpenBrace.tests.ps1

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ $settings = @{
1515

1616
Describe "PlaceOpenBrace" {
1717
Context "When an open brace must be on the same line" {
18-
BeforeAll{
18+
BeforeAll {
1919
$def = @'
2020
function foo ($param1)
2121
{
@@ -36,14 +36,15 @@ function foo ($param1)
3636
}
3737

3838
Context "When an open brace must be on a new line" {
39-
BeforeAll{
39+
BeforeAll {
4040
$def = @'
4141
function foo ($param1) {
4242
4343
}
4444
'@
4545
$ruleConfiguration.'OnSameLine' = $false
46-
$ruleConfiguration.'NewLineAfter' = $true
46+
$ruleConfiguration.'NewLineAfter' = $true
47+
$ruleConfiguration.'IgnoreOneLineBlock' = $false
4748
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
4849
$defShouldIgnore = @'
4950
Get-Process | % { "blah" }
@@ -67,19 +68,20 @@ Get-Process | % { "blah" }
6768
$def = @'
6869
$x = if ($true) { "blah" } else { "blah blah" }
6970
'@
70-
$ruleConfiguration.'IgnoreOneLineIf' = $true
71+
$ruleConfiguration.'IgnoreOneLineBlock' = $true
7172
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
7273
$violations.Count | Should Be 0
7374
}
7475
}
7576

7677
Context "When a new line should follow an open brace" {
77-
BeforeAll{
78+
BeforeAll {
7879
$def = @'
7980
function foo { }
8081
'@
8182
$ruleConfiguration.'OnSameLine' = $true
8283
$ruleConfiguration.'NewLineAfter' = $true
84+
$ruleConfiguration.'IgnoreOneLineBlock' = $false
8385
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
8486
}
8587

0 commit comments

Comments
 (0)