Skip to content

Commit bdf652f

Browse files
author
Kapil Borle
committed
Allow configuring UseConsistentIdentation rule
1 parent 9382e38 commit bdf652f

1 file changed

Lines changed: 47 additions & 6 deletions

File tree

Rules/UseConsistentIndentation.cs

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,51 @@ namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules
2828
#endif
2929
class UseConsistentIndentation : IScriptRule
3030
{
31-
private readonly int indentationSize;
31+
32+
private class RuleArguments
33+
{
34+
public int indentationSize { get; set; } = 4;
35+
36+
public static RuleArguments Create(Dictionary<string, object> arguments)
37+
{
38+
try
39+
{
40+
var ruleArguments = new RuleArguments();
41+
var properties = ruleArguments.GetType().GetProperties();
42+
foreach (var property in properties)
43+
{
44+
if (arguments.ContainsKey(property.Name))
45+
{
46+
var type = property.PropertyType;
47+
var obj = arguments[property.Name];
48+
property.SetValue(ruleArguments, System.Convert.ChangeType(obj, Type.GetTypeCode(type)));
49+
}
50+
}
51+
52+
return ruleArguments;
53+
}
54+
catch
55+
{
56+
return new RuleArguments(); // return arguments with defaults
57+
}
58+
}
59+
}
60+
61+
private bool isRuleConfigured;
62+
63+
private RuleArguments ruleArguments;
64+
65+
private int indentationSize;
3266
private enum IndentationKind { Space, Tab };
33-
private IndentationKind indentationKind;
3467

35-
UseConsistentIndentation()
68+
// TODO make this configurable
69+
private readonly IndentationKind indentationKind = IndentationKind.Space;
70+
71+
private void ConfigureRule()
3672
{
37-
// TODO make this configurable
38-
indentationKind = IndentationKind.Space;
39-
indentationSize = indentationKind == IndentationKind.Space ? 4 : 1;
73+
ruleArguments = RuleArguments.Create(Helper.Instance.GetRuleArguments(this.GetName()));
74+
indentationSize = ruleArguments.indentationSize;
75+
isRuleConfigured = true;
4076
}
4177

4278
/// <summary>
@@ -52,6 +88,11 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
5288
throw new ArgumentNullException("ast");
5389
}
5490

91+
if (!isRuleConfigured)
92+
{
93+
ConfigureRule();
94+
}
95+
5596
var tokens = Helper.Instance.Tokens;
5697
var diagnosticRecords = new List<DiagnosticRecord>();
5798
var indentationLevel = 0;

0 commit comments

Comments
 (0)