2222
2323namespace Microsoft . Windows . PowerShell . ScriptAnalyzer . BuiltinRules
2424{
25- // TODO place public in front of all new rules to be discoverable in PS Core
2625 /// <summary>
2726 /// A class to walk an AST to check for [violation]
2827 /// </summary>
@@ -32,12 +31,16 @@ namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules
3231 public class PlaceOpenBrace : ConfigurableScriptRule
3332 {
3433 private Func < Token [ ] , string , IEnumerable < DiagnosticRecord > > findViolations ;
34+ private List < Func < Token [ ] , string , IEnumerable < DiagnosticRecord > > > violationFinders = new List < Func < Token [ ] , string , IEnumerable < DiagnosticRecord > > > ( ) ;
3535
3636 [ ConfigurableRuleProperty ( ) ]
3737 public bool OnSameLine { get ; protected set ; } = true ;
3838
3939 [ ConfigurableRuleProperty ( ) ]
40- public bool Enable { get ; protected set ; } = false ;
40+ public bool NewLineAfter { get ; protected set ; } = true ;
41+
42+ [ ConfigurableRuleProperty ( ) ]
43+ public bool Enable { get ; protected set ; } = false ;
4144
4245 /// <summary>
4346 /// Analyzes the given ast to find the [violation]
@@ -57,24 +60,35 @@ public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string file
5760 ConfigureRule ( ) ;
5861 if ( OnSameLine )
5962 {
60- findViolations = this . FindViolationsForBraceShouldBeOnSameLine ;
63+ // findViolations = this.FindViolationsForBraceShouldBeOnSameLine;
64+ violationFinders . Add ( FindViolationsForBraceShouldBeOnSameLine ) ;
6165 }
6266 else
6367 {
64- findViolations = this . FindViolationsForBraceShouldNotBeOnSameLine ;
68+ // findViolations = this.FindViolationsForBraceShouldNotBeOnSameLine;
69+ violationFinders . Add ( FindViolationsForBraceShouldNotBeOnSameLine ) ;
70+ }
71+
72+ if ( NewLineAfter )
73+ {
74+ violationFinders . Add ( FindViolationsForNoNewLineAfterBrace ) ;
6575 }
6676 }
6777
68- if ( ! Enable )
78+ var diagnosticRecords = new List < DiagnosticRecord > ( ) ;
79+
80+ if ( Enable )
6981 {
70- return Enumerable . Empty < DiagnosticRecord > ( ) ;
82+ foreach ( var violationFinder in violationFinders )
83+ {
84+ diagnosticRecords . AddRange ( violationFinder ( Helper . Instance . Tokens , fileName ) ) ;
85+ }
7186 }
7287
7388 // TODO Should have the following options
7489 // * new-line-after
7590 // * no-empty-line-after
76-
77- return findViolations ( Helper . Instance . Tokens , fileName ) ;
91+ return diagnosticRecords ;
7892 }
7993
8094 private IEnumerable < DiagnosticRecord > FindViolationsForBraceShouldBeOnSameLine (
@@ -98,6 +112,46 @@ private IEnumerable<DiagnosticRecord> FindViolationsForBraceShouldBeOnSameLine(
98112 }
99113 }
100114
115+ private IEnumerable < DiagnosticRecord > FindViolationsForNoNewLineAfterBrace (
116+ Token [ ] tokens ,
117+ string fileName )
118+ {
119+ for ( int k = 0 ; k < tokens . Length - 1 ; k ++ )
120+ {
121+ if ( tokens [ k ] . Kind == TokenKind . LCurly
122+ && tokens [ k + 1 ] . Kind != TokenKind . NewLine )
123+ {
124+ yield return new DiagnosticRecord (
125+ GetError ( ) ,
126+ tokens [ k ] . Extent ,
127+ GetName ( ) ,
128+ GetDiagnosticSeverity ( ) ,
129+ fileName ,
130+ null ,
131+ GetCorrectionsForNoNewLineAfterBrace ( tokens , k , fileName ) ) ;
132+ }
133+ }
134+ }
135+
136+ private List < CorrectionExtent > GetCorrectionsForNoNewLineAfterBrace (
137+ Token [ ] tokens ,
138+ int openBracePos ,
139+ string fileName )
140+ {
141+ var corrections = new List < CorrectionExtent > ( ) ;
142+ var extent = tokens [ openBracePos ] . Extent ;
143+
144+ corrections . Add (
145+ new CorrectionExtent (
146+ extent . StartLineNumber ,
147+ extent . EndLineNumber ,
148+ extent . StartColumnNumber ,
149+ extent . EndColumnNumber ,
150+ new StringBuilder ( ) . Append ( extent . Text ) . Append ( Environment . NewLine ) . ToString ( ) ,
151+ fileName ) ) ;
152+ return corrections ;
153+ }
154+
101155 private IEnumerable < DiagnosticRecord > FindViolationsForBraceShouldNotBeOnSameLine (
102156 Token [ ] tokens ,
103157 string fileName )
0 commit comments