Skip to content

Commit 9f10590

Browse files
author
Kapil Borle
committed
Use attributes for configurable properties
1 parent 649dec9 commit 9f10590

3 files changed

Lines changed: 19 additions & 8 deletions

File tree

Rules/ConfigurableScriptRule.cs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Management.Automation.Language;
5+
using System.Reflection;
56
using System.Text;
67
using System.Threading.Tasks;
78

@@ -17,10 +18,9 @@ internal abstract class ConfigurableScriptRule : IScriptRule
1718
public void ConfigureRule()
1819
{
1920
var arguments = Helper.Instance.GetRuleArguments(this.GetName());
20-
//var configurableProps = GetConfigurableProperties();
2121
try
2222
{
23-
var properties = this.GetType().GetProperties();
23+
var properties = GetConfigurableProperties();
2424
foreach (var property in properties)
2525
{
2626
if (arguments.ContainsKey(property.Name))
@@ -35,16 +35,24 @@ public void ConfigureRule()
3535
}
3636
catch
3737
{
38-
// return arguments with defaults
38+
// we do not know how to handle an exception yet in this case yet!
39+
// but we know that this should not crash the program hence we
40+
// have this empty catch block
3941
}
4042

4143
IsRuleConfigured = true;
4244
}
4345

44-
// private GetConfigurableProperties()
45-
// {
46-
47-
// }
46+
private IEnumerable<PropertyInfo> GetConfigurableProperties()
47+
{
48+
foreach (var property in this.GetType().GetProperties())
49+
{
50+
if (property.GetCustomAttribute(typeof(ConfigurableRulePropertyAttribute)) != null)
51+
{
52+
yield return property;
53+
}
54+
}
55+
}
4856

4957
public abstract IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName);
5058
public abstract string GetCommonName();
@@ -56,7 +64,7 @@ public void ConfigureRule()
5664
}
5765

5866
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
59-
public class ConfigurablePropertyAttribute : Attribute
67+
internal class ConfigurableRulePropertyAttribute : Attribute
6068
{
6169

6270
}

Rules/PlaceOpenBrace.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class PlaceOpenBrace : ConfigurableScriptRule
3333
{
3434
private Func<Token[], string, IEnumerable<DiagnosticRecord>> findViolations;
3535

36+
[ConfigurableRuleProperty()]
3637
public bool OnSameLine { get; protected set; } = true;
3738

3839
/// <summary>

Rules/UseConsistentIndentation.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ private enum IndentationKind { Space, Tab };
3232

3333
// TODO make this configurable
3434
private readonly IndentationKind indentationKind = IndentationKind.Space;
35+
36+
[ConfigurableRuleProperty()]
3537
public int IndentationSize { get; protected set; } = 4;
3638

3739
/// <summary>

0 commit comments

Comments
 (0)