Skip to content

Commit 6a631d7

Browse files
author
Kapil Borle
committed
Pass rule configuration to configure method
1 parent 6df1d7c commit 6a631d7

File tree

3 files changed

+38
-19
lines changed

3 files changed

+38
-19
lines changed

Engine/Generic/ConfigurableScriptRule.cs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@
88

99
namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic
1010
{
11-
// This is still an experimental class and we do not want to expose it
12-
// as a public API as of yet. So we place it in builtinrules project
13-
// and keep it internal as it consumed only by a handful of rules.
11+
// This is still an experimental class. Use at your own risk!
1412
public abstract class ConfigurableScriptRule : IScriptRule
1513
{
1614
// Configurable rules should define a default value
@@ -19,19 +17,24 @@ public abstract class ConfigurableScriptRule : IScriptRule
1917
[ConfigurableRuleProperty()]
2018
public bool Enable { get; protected set; } = false;
2119

22-
public virtual void ConfigureRule()
20+
public virtual void ConfigureRule(IDictionary<string, object> paramValueMap)
2321
{
24-
// TODO Do not use Helper. Pass this method a dictionary instead
25-
var arguments = Helper.Instance.GetRuleArguments(this.GetName());
22+
if (paramValueMap == null)
23+
{
24+
throw new ArgumentNullException(nameof(paramValueMap));
25+
}
26+
2627
try
2728
{
2829
var properties = GetConfigurableProperties();
2930
foreach (var property in properties)
3031
{
31-
if (arguments.ContainsKey(property.Name))
32+
if (paramValueMap.ContainsKey(property.Name))
3233
{
3334
var type = property.PropertyType;
34-
var obj = arguments[property.Name];
35+
var obj = paramValueMap[property.Name];
36+
37+
// TODO Check if type is convertible
3538
property.SetValue(
3639
this,
3740
System.Convert.ChangeType(obj, type));
@@ -40,7 +43,7 @@ public virtual void ConfigureRule()
4043
}
4144
catch
4245
{
43-
// we do not know how to handle an exception yet in this case yet!
46+
// we do not know how to handle an exception in this case yet!
4447
// but we know that this should not crash the program hence we
4548
// have this empty catch block
4649
}

Engine/ScriptAnalyzer.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -968,12 +968,26 @@ private void LoadRules(Dictionary<string, List<string>> result, CommandInvocatio
968968
// Configure rules derived from ConfigurableScriptRule class
969969
private void ConfigureScriptRules()
970970
{
971-
if (ScriptRules != null)
971+
if (ScriptRules == null)
972972
{
973-
foreach (var scriptRule in ScriptRules)
973+
return;
974+
}
975+
976+
foreach (var scriptRule in ScriptRules)
977+
{
978+
var configurableScriptRule = scriptRule as ConfigurableScriptRule;
979+
if (configurableScriptRule == null)
974980
{
975-
(scriptRule as ConfigurableScriptRule)?.ConfigureRule();
981+
continue;
976982
}
983+
984+
var paramValueMap = Helper.Instance.GetRuleArguments(scriptRule.GetName());
985+
if (paramValueMap == null)
986+
{
987+
continue;
988+
}
989+
990+
configurableScriptRule.ConfigureRule(paramValueMap);
977991
}
978992
}
979993

Rules/PlaceOpenBrace.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,13 @@ public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string file
5151
throw new ArgumentNullException("ast");
5252
}
5353

54+
// TODO Should have the following option
55+
// * no-empty-lines-after
56+
// TODO handle open brace for a command parameter.
57+
// * E.g. get-process | % { "blah }
58+
// In the above case even if OnSameLine == false, we should not
59+
// flag the open brace as it would move the brace to the next line
60+
// and it will invalidate the command
5461
var diagnosticRecords = new List<DiagnosticRecord>();
5562
if (Enable)
5663
{
@@ -60,23 +67,18 @@ public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string file
6067
}
6168
}
6269

63-
// TODO Should have the following options
64-
// * new-line-after
65-
// * no-empty-line-after
6670
return diagnosticRecords;
6771
}
6872

69-
public override void ConfigureRule()
73+
public override void ConfigureRule(IDictionary<string, object> paramValueMap)
7074
{
71-
base.ConfigureRule();
75+
base.ConfigureRule(paramValueMap);
7276
if (OnSameLine)
7377
{
74-
// findViolations = this.FindViolationsForBraceShouldBeOnSameLine;
7578
violationFinders.Add(FindViolationsForBraceShouldBeOnSameLine);
7679
}
7780
else
7881
{
79-
// findViolations = this.FindViolationsForBraceShouldNotBeOnSameLine;
8082
violationFinders.Add(FindViolationsForBraceShouldNotBeOnSameLine);
8183
}
8284

0 commit comments

Comments
 (0)