Skip to content

Commit 3bb1a08

Browse files
author
Kapil Borle
committed
Setup PlaceCloseBrace methods during configuration stage
1 parent 63d78e1 commit 3bb1a08

File tree

1 file changed

+28
-20
lines changed

1 file changed

+28
-20
lines changed

Rules/PlaceCloseBrace.cs

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

0 commit comments

Comments
 (0)