@@ -28,6 +28,10 @@ namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules
2828#endif
2929 public class PlaceCloseBrace : ConfigurableRule
3030 {
31+ private HashSet < Token > tokensToIgnore ;
32+ private List < Func < Token [ ] , int , int , string , DiagnosticRecord > > violationFinders
33+ = new List < Func < Token [ ] , int , int , string , DiagnosticRecord > > ( ) ;
34+
3135 /// <summary>
3236 /// Indicates if there should or should not be an empty line before a close brace.
3337 ///
@@ -57,7 +61,28 @@ public class PlaceCloseBrace : ConfigurableRule
5761 [ ConfigurableRuleProperty ( defaultValue : true ) ]
5862 public bool NewLineAfter { get ; protected set ; }
5963
60- private HashSet < Token > tokensToIgnore ;
64+ /// <summary>
65+ /// Sets the configurable properties of this rule.
66+ /// </summary>
67+ /// <param name="paramValueMap">A dictionary that maps parameter name to it value. Must be non-null</param>
68+ public override void ConfigureRule ( IDictionary < string , object > paramValueMap )
69+ {
70+ base . ConfigureRule ( paramValueMap ) ;
71+ violationFinders . Add ( GetViolationForBraceShouldBeOnNewLine ) ;
72+ if ( NoEmptyLineBefore )
73+ {
74+ violationFinders . Add ( GetViolationForBraceShouldNotFollowEmptyLine ) ;
75+ }
76+
77+ if ( NewLineAfter )
78+ {
79+ violationFinders . Add ( GetViolationForBraceShouldHaveNewLineAfter ) ;
80+ }
81+ else
82+ {
83+ violationFinders . Add ( GetViolationsForUncuddledBranches ) ;
84+ }
85+ }
6186
6287 /// <summary>
6388 /// Analyzes the given ast to find violations.
@@ -121,27 +146,10 @@ public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string file
121146 continue ;
122147 }
123148
124- AddToDiagnosticRecords (
125- GetViolationForBraceShouldBeOnNewLine ( tokens , k , openBracePos , fileName ) ,
126- ref diagnosticRecords ) ;
127-
128- if ( NoEmptyLineBefore )
129- {
130- AddToDiagnosticRecords (
131- GetViolationForBraceShouldNotFollowEmptyLine ( tokens , k , openBracePos , fileName ) ,
132- ref diagnosticRecords ) ;
133- }
134-
135- if ( NewLineAfter )
136- {
137- AddToDiagnosticRecords (
138- GetViolationForBraceShouldHaveNewLineAfter ( tokens , k , openBracePos , fileName ) ,
139- ref diagnosticRecords ) ;
140- }
141- else
149+ foreach ( var violationFinder in violationFinders )
142150 {
143151 AddToDiagnosticRecords (
144- GetViolationsForUncuddledBranches ( tokens , k , openBracePos , fileName ) ,
152+ violationFinder ( tokens , k , openBracePos , fileName ) ,
145153 ref diagnosticRecords ) ;
146154 }
147155 }
@@ -320,9 +328,7 @@ private DiagnosticRecord GetViolationForBraceShouldHaveNewLineAfter(
320328 if ( tokens . Length > 1 && tokens . Length > expectedNewLinePos )
321329 {
322330 var closeBraceToken = tokens [ closeBracePos ] ;
323- if ( ( tokens [ expectedNewLinePos ] . Kind == TokenKind . Else
324- || tokens [ expectedNewLinePos ] . Kind == TokenKind . ElseIf )
325- && ! tokensToIgnore . Contains ( closeBraceToken ) )
331+ if ( ! tokensToIgnore . Contains ( closeBraceToken ) && IsBranchingStatementToken ( tokens [ expectedNewLinePos ] ) )
326332 {
327333 return new DiagnosticRecord (
328334 GetError ( Strings . PlaceCloseBraceErrorShouldFollowNewLine ) ,
0 commit comments