@@ -28,6 +28,10 @@ namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules
28
28
#endif
29
29
public class PlaceCloseBrace : ConfigurableRule
30
30
{
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
+
31
35
/// <summary>
32
36
/// Indicates if there should or should not be an empty line before a close brace.
33
37
///
@@ -57,7 +61,28 @@ public class PlaceCloseBrace : ConfigurableRule
57
61
[ ConfigurableRuleProperty ( defaultValue : true ) ]
58
62
public bool NewLineAfter { get ; protected set ; }
59
63
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
+ }
61
86
62
87
/// <summary>
63
88
/// Analyzes the given ast to find violations.
@@ -121,27 +146,10 @@ public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string file
121
146
continue ;
122
147
}
123
148
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 )
142
150
{
143
151
AddToDiagnosticRecords (
144
- GetViolationsForUncuddledBranches ( tokens , k , openBracePos , fileName ) ,
152
+ violationFinder ( tokens , k , openBracePos , fileName ) ,
145
153
ref diagnosticRecords ) ;
146
154
}
147
155
}
@@ -320,9 +328,7 @@ private DiagnosticRecord GetViolationForBraceShouldHaveNewLineAfter(
320
328
if ( tokens . Length > 1 && tokens . Length > expectedNewLinePos )
321
329
{
322
330
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 ] ) )
326
332
{
327
333
return new DiagnosticRecord (
328
334
GetError ( Strings . PlaceCloseBraceErrorShouldFollowNewLine ) ,
0 commit comments