Skip to content

Commit 649dec9

Browse files
author
Kapil Borle
committed
Inherit PlaceOpenBrace rules from configurablerules
1 parent b61d21b commit 649dec9

1 file changed

Lines changed: 19 additions & 60 deletions

File tree

Rules/PlaceOpenBrace.cs

Lines changed: 19 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -29,63 +29,36 @@ namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules
2929
#if !CORECLR
3030
[Export(typeof(IScriptRule))]
3131
#endif
32-
class PlaceOpenBrace : IScriptRule
32+
class PlaceOpenBrace : ConfigurableScriptRule
3333
{
34-
private class RuleArguments
35-
{
36-
public bool OnSameLine { get; set; } = true;
37-
38-
public static RuleArguments Create(Dictionary<string, object> arguments)
39-
{
40-
try
41-
{
42-
var ruleArguments = new RuleArguments();
43-
var properties = ruleArguments.GetType().GetProperties();
44-
foreach (var property in properties)
45-
{
46-
if (arguments.ContainsKey(property.Name))
47-
{
48-
var type = property.PropertyType;
49-
var obj = arguments[property.Name];
50-
property.SetValue(ruleArguments, System.Convert.ChangeType(obj, Type.GetTypeCode(type)));
51-
}
52-
}
53-
54-
return ruleArguments;
55-
}
56-
catch
57-
{
58-
return new RuleArguments(); // return arguments with defaults
59-
}
60-
}
61-
}
62-
63-
private bool isRuleConfigured;
64-
65-
private RuleArguments ruleArgs;
6634
private Func<Token[], string, IEnumerable<DiagnosticRecord>> findViolations;
6735

36+
public bool OnSameLine { get; protected set; } = true;
37+
6838
/// <summary>
6939
/// Analyzes the given ast to find the [violation]
7040
/// </summary>
7141
/// <param name="ast">AST to be analyzed. This should be non-null</param>
7242
/// <param name="fileName">Name of file that corresponds to the input AST.</param>
7343
/// <returns>A an enumerable type containing the violations</returns>
74-
public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
44+
public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
7545
{
7646
if (ast == null)
7747
{
7848
throw new ArgumentNullException("ast");
7949
}
8050

81-
// We cannot set the rule arguments in the rule constructor
82-
// because it needs Helper.Instance. Helper doesn't get
83-
// initialized in Get-ScriptAnalyzerRule cmdlet so configuring
84-
// the rule in the constructor causes Get-ScriptAnalyzerRule to
85-
// crash.
86-
if (!isRuleConfigured)
51+
if (!IsRuleConfigured)
8752
{
8853
ConfigureRule();
54+
if (OnSameLine)
55+
{
56+
findViolations = this.FindViolationsForBraceShouldBeOnSameLine;
57+
}
58+
else
59+
{
60+
findViolations = this.FindViolationsForBraceShouldNotBeOnSameLine;
61+
}
8962
}
9063

9164
// TODO Should have the following options
@@ -95,20 +68,6 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
9568
return findViolations(Helper.Instance.Tokens, fileName);
9669
}
9770

98-
private void ConfigureRule()
99-
{
100-
ruleArgs = RuleArguments.Create(Helper.Instance.GetRuleArguments(this.GetName()));
101-
if (ruleArgs.OnSameLine)
102-
{
103-
findViolations = this.FindViolationsForBraceShouldBeOnSameLine;
104-
}
105-
else
106-
{
107-
findViolations = this.FindViolationsForBraceShouldNotBeOnSameLine;
108-
}
109-
isRuleConfigured = true;
110-
}
111-
11271
private IEnumerable<DiagnosticRecord> FindViolationsForBraceShouldBeOnSameLine(
11372
Token[] tokens,
11473
string fileName)
@@ -215,7 +174,7 @@ private List<CorrectionExtent> GetCorrectionsForBraceShouldBeOnSameLine(
215174
/// <summary>
216175
/// Retrieves the common name of this rule.
217176
/// </summary>
218-
public string GetCommonName()
177+
public override string GetCommonName()
219178
{
220179
return string.Format(CultureInfo.CurrentCulture, Strings.PlaceOpenBraceCommonName);
221180
}
@@ -231,15 +190,15 @@ public string GetError()
231190
/// <summary>
232191
/// Retrieves the description of this rule.
233192
/// </summary>
234-
public string GetDescription()
193+
public override string GetDescription()
235194
{
236195
return string.Format(CultureInfo.CurrentCulture, Strings.PlaceOpenBraceDescription);
237196
}
238197

239198
/// <summary>
240199
/// Retrieves the name of this rule.
241200
/// </summary>
242-
public string GetName()
201+
public override string GetName()
243202
{
244203
return string.Format(
245204
CultureInfo.CurrentCulture,
@@ -251,7 +210,7 @@ public string GetName()
251210
/// <summary>
252211
/// Retrieves the severity of the rule: error, warning or information.
253212
/// </summary>
254-
public RuleSeverity GetSeverity()
213+
public override RuleSeverity GetSeverity()
255214
{
256215
return RuleSeverity.Information;
257216
}
@@ -268,15 +227,15 @@ public DiagnosticSeverity GetDiagnosticSeverity()
268227
/// <summary>
269228
/// Retrieves the name of the module/assembly the rule is from.
270229
/// </summary>
271-
public string GetSourceName()
230+
public override string GetSourceName()
272231
{
273232
return string.Format(CultureInfo.CurrentCulture, Strings.SourceName);
274233
}
275234

276235
/// <summary>
277236
/// Retrieves the type of the rule, Builtin, Managed or Module.
278237
/// </summary>
279-
public SourceType GetSourceType()
238+
public override SourceType GetSourceType()
280239
{
281240
return SourceType.Builtin;
282241
}

0 commit comments

Comments
 (0)