Skip to content

Commit 1329e30

Browse files
author
Kapil Borle
committed
Ignore open brace in a one line if else statement
1 parent 7755e78 commit 1329e30

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

Rules/PlaceOpenBrace.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ public class PlaceOpenBrace : ConfigurableRule
4545
[ConfigurableRuleProperty(defaultValue: true)]
4646
public bool NewLineAfter { get; protected set; }
4747

48+
[ConfigurableRuleProperty(defaultValue: true)]
49+
public bool IgnoreOneLineIf { get; protected set; }
50+
4851
private List<Func<Token[], Ast, string, IEnumerable<DiagnosticRecord>>> violationFinders
4952
= new List<Func<Token[], Ast, string, IEnumerable<DiagnosticRecord>>>();
5053

@@ -96,12 +99,21 @@ public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string file
9699
var tokens = Helper.Instance.Tokens;
97100

98101
// Ignore open braces that are part of arguments to a command
99-
// * E.g. get-process | % { "blah }
102+
// * E.g. get-process | % { "blah }f
100103
// In the above case even if OnSameLine == false, we should not
101104
// flag the open brace as it would move the brace to the next line
102105
// and will invalidate the command
103-
tokensToIgnore = new HashSet<Token>(
104-
new TokenOperations(tokens, ast).GetOpenBracesInCommandElements());
106+
var tokenOps = new TokenOperations(tokens, ast);
107+
tokensToIgnore = new HashSet<Token>(tokenOps.GetOpenBracesInCommandElements());
108+
109+
if (IgnoreOneLineIf)
110+
{
111+
foreach (var openBraceToken in tokenOps.GetOpenBraceInOneLineIfStatement())
112+
{
113+
tokensToIgnore.Add(openBraceToken);
114+
}
115+
}
116+
105117
foreach (var violationFinder in violationFinders)
106118
{
107119
diagnosticRecords.AddRange(violationFinder(tokens, ast, fileName));

0 commit comments

Comments
 (0)