Skip to content

Commit 6f022f1

Browse files
author
Kapil Borle
committed
Set default value from ConfigurableScriptRule constructor
1 parent ade5bf5 commit 6f022f1

File tree

5 files changed

+64
-22
lines changed

5 files changed

+64
-22
lines changed

Engine/Generic/ConfigurableScriptRule.cs

Lines changed: 50 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,16 @@ namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic
88
// This is still an experimental class. Use at your own risk!
99
public abstract class ConfigurableScriptRule : IScriptRule
1010
{
11-
// Configurable rules should define a default value
11+
// Configurable rule properties should define a default value
1212
// because if reading the configuration fails
1313
// we use the property's default value
14-
[ConfigurableRuleProperty()]
15-
public bool Enable { get; protected set; } = false;
14+
[ConfigurableRuleProperty(defaultValue: false)]
15+
public bool Enable { get; protected set; }
16+
17+
protected ConfigurableScriptRule()
18+
{
19+
SetDefaultValues();
20+
}
1621

1722
public virtual void ConfigureRule(IDictionary<string, object> paramValueMap)
1823
{
@@ -23,29 +28,37 @@ public virtual void ConfigureRule(IDictionary<string, object> paramValueMap)
2328

2429
try
2530
{
26-
var properties = GetConfigurableProperties();
27-
foreach (var property in properties)
31+
foreach (var property in GetConfigurableProperties())
2832
{
2933
if (paramValueMap.ContainsKey(property.Name))
3034
{
31-
var type = property.PropertyType;
32-
var obj = paramValueMap[property.Name];
33-
34-
// TODO Check if type is convertible
35-
property.SetValue(
36-
this,
37-
System.Convert.ChangeType(obj, type));
35+
SetValue(property, paramValueMap[property.Name]);
3836
}
3937
}
4038
}
4139
catch
4240
{
4341
// we do not know how to handle an exception in this case yet!
44-
// but we know that this should not crash the program hence we
45-
// have this empty catch block
42+
// but we know that this should not crash the program
43+
// hence we revert the property values to their default
44+
SetDefaultValues();
45+
}
46+
}
47+
48+
private void SetDefaultValues()
49+
{
50+
foreach (var property in GetConfigurableProperties())
51+
{
52+
SetValue(property, GetDefaultValue(property));
4653
}
4754
}
4855

56+
private void SetValue(PropertyInfo property, object value)
57+
{
58+
// TODO Check if type is convertible
59+
property.SetValue(this, Convert.ChangeType(value, property.PropertyType));
60+
}
61+
4962
private IEnumerable<PropertyInfo> GetConfigurableProperties()
5063
{
5164
foreach (var property in this.GetType().GetProperties())
@@ -57,6 +70,19 @@ private IEnumerable<PropertyInfo> GetConfigurableProperties()
5770
}
5871
}
5972

73+
private Object GetDefaultValue(PropertyInfo property)
74+
{
75+
var attr = property.GetCustomAttribute(typeof(ConfigurableRulePropertyAttribute));
76+
if (attr == null)
77+
{
78+
throw new ArgumentException(
79+
String.Format(Strings.ConfigurableScriptRulePropertyHasNotAttribute, property.Name),
80+
nameof(property));
81+
}
82+
83+
return ((ConfigurableRulePropertyAttribute)attr).DefaultValue;
84+
}
85+
6086
public abstract IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName);
6187
public abstract string GetCommonName();
6288
public abstract string GetDescription();
@@ -69,6 +95,16 @@ private IEnumerable<PropertyInfo> GetConfigurableProperties()
6995
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
7096
public class ConfigurableRulePropertyAttribute : Attribute
7197
{
98+
public object DefaultValue { get; private set; }
7299

100+
public ConfigurableRulePropertyAttribute(object defaultValue)
101+
{
102+
if (defaultValue == null)
103+
{
104+
throw new ArgumentNullException(nameof(defaultValue), Strings.ConfigurableScriptRuleNRE);
105+
}
106+
107+
DefaultValue = defaultValue;
108+
}
73109
}
74110
}

Engine/Strings.resx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,4 +255,10 @@
255255
<data name="DigraphVertexDoesNotExists" xml:space="preserve">
256256
<value>Vertex {0} does not exist in the digraph.</value>
257257
</data>
258+
<data name="ConfigurableScriptRulePropertyHasNotAttribute" xml:space="preserve">
259+
<value>"Cannot find a ConfigurableRuleProperty attribute on property {0}".</value>
260+
</data>
261+
<data name="ConfigurableScriptRuleNRE" xml:space="preserve">
262+
<value>"Argument should not be null.".</value>
263+
</data>
258264
</root>

Rules/PlaceCloseBrace.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules
2828
#endif
2929
public class PlaceCloseBrace : ConfigurableScriptRule
3030
{
31-
[ConfigurableRuleProperty()]
32-
public bool NoEmptyLineBefore { get; protected set; } = false;
31+
[ConfigurableRuleProperty(defaultValue:false)]
32+
public bool NoEmptyLineBefore { get; protected set; }
3333

3434
/// <summary>
3535
/// Analyzes the given ast to find the [violation]

Rules/PlaceOpenBrace.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ public class PlaceOpenBrace : ConfigurableScriptRule
3232
private List<Func<Token[], Ast, string, IEnumerable<DiagnosticRecord>>> violationFinders
3333
= new List<Func<Token[], Ast, string, IEnumerable<DiagnosticRecord>>>();
3434

35-
[ConfigurableRuleProperty()]
36-
public bool OnSameLine { get; protected set; } = true;
35+
[ConfigurableRuleProperty(defaultValue:true)]
36+
public bool OnSameLine { get; protected set; }
3737

38-
[ConfigurableRuleProperty()]
39-
public bool NewLineAfter { get; protected set; } = true;
38+
[ConfigurableRuleProperty(defaultValue:true)]
39+
public bool NewLineAfter { get; protected set; }
4040

4141
/// <summary>
4242
/// Analyzes the given ast to find the [violation]

Rules/UseConsistentIndentation.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ private enum IndentationKind { Space, Tab };
3333
// TODO make this configurable
3434
private readonly IndentationKind indentationKind = IndentationKind.Space;
3535

36-
[ConfigurableRuleProperty()]
37-
public int IndentationSize { get; protected set; } = 4;
36+
[ConfigurableRuleProperty(defaultValue:4)]
37+
public int IndentationSize { get; protected set; }
3838

3939
/// <summary>
4040
/// Analyzes the given ast to find the [violation]

0 commit comments

Comments
 (0)