Skip to content

Commit 933a39a

Browse files
author
Kapil Borle
committed
Create ConfigurableScriptRule class
1 parent bdf652f commit 933a39a

3 files changed

Lines changed: 72 additions & 52 deletions

File tree

Rules/ConfigurableScriptRule.cs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Management.Automation.Language;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic
9+
{
10+
public abstract class ConfigurableScriptRule : IScriptRule
11+
{
12+
public bool IsRuleConfigured { get; protected set; } = false;
13+
14+
public void ConfigureRule()
15+
{
16+
var arguments = Helper.Instance.GetRuleArguments(this.GetName());
17+
//var configurableProps = GetConfigurableProperties();
18+
try
19+
{
20+
var properties = this.GetType().GetProperties();
21+
foreach (var property in properties)
22+
{
23+
if (arguments.ContainsKey(property.Name))
24+
{
25+
var type = property.PropertyType;
26+
var obj = arguments[property.Name];
27+
property.SetValue(
28+
this,
29+
System.Convert.ChangeType(obj, Type.GetTypeCode(type)));
30+
}
31+
}
32+
}
33+
catch
34+
{
35+
// return arguments with defaults
36+
}
37+
38+
IsRuleConfigured = true;
39+
}
40+
41+
// private GetConfigurableProperties()
42+
// {
43+
44+
// }
45+
46+
public abstract IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName);
47+
public abstract string GetCommonName();
48+
public abstract string GetDescription();
49+
public abstract string GetName();
50+
public abstract RuleSeverity GetSeverity();
51+
public abstract string GetSourceName();
52+
public abstract SourceType GetSourceType();
53+
}
54+
55+
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
56+
public class ConfigurablePropertyAttribute : Attribute
57+
{
58+
59+
}
60+
}

Rules/ScriptAnalyzerBuiltinRules.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
<Compile Include="AvoidUsingPlainTextForPassword.cs" />
8989
<Compile Include="AvoidUsingWMICmdlet.cs" />
9090
<Compile Include="AvoidUsingWriteHost.cs" />
91+
<Compile Include="ConfigurableScriptRule.cs" />
9192
<Compile Include="DscTestsPresent.cs" />
9293
<Compile Include="DscExamplesPresent.cs" />
9394
<Compile Include="MisleadingBacktick.cs" />

Rules/UseConsistentIndentation.cs

Lines changed: 11 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -26,69 +26,28 @@ namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules
2626
#if !CORECLR
2727
[Export(typeof(IScriptRule))]
2828
#endif
29-
class UseConsistentIndentation : IScriptRule
29+
class UseConsistentIndentation : ConfigurableScriptRule
3030
{
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;
6631
private enum IndentationKind { Space, Tab };
6732

6833
// TODO make this configurable
6934
private readonly IndentationKind indentationKind = IndentationKind.Space;
70-
71-
private void ConfigureRule()
72-
{
73-
ruleArguments = RuleArguments.Create(Helper.Instance.GetRuleArguments(this.GetName()));
74-
indentationSize = ruleArguments.indentationSize;
75-
isRuleConfigured = true;
76-
}
35+
public int IndentationSize { get; protected set; } = 4;
7736

7837
/// <summary>
7938
/// Analyzes the given ast to find the [violation]
8039
/// </summary>
8140
/// <param name="ast">AST to be analyzed. This should be non-null</param>
8241
/// <param name="fileName">Name of file that corresponds to the input AST.</param>
8342
/// <returns>A an enumerable type containing the violations</returns>
84-
public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
43+
public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
8544
{
8645
if (ast == null)
8746
{
8847
throw new ArgumentNullException("ast");
8948
}
9049

91-
if (!isRuleConfigured)
50+
if (!IsRuleConfigured)
9251
{
9352
ConfigureRule();
9453
}
@@ -197,7 +156,7 @@ private int GetIndentationColumnNumber(int indentationLevel)
197156

198157
private int GetIndentation(int indentationLevel)
199158
{
200-
return indentationLevel * this.indentationSize;
159+
return indentationLevel * this.IndentationSize;
201160
}
202161

203162
private char GetIndentationChar()
@@ -213,23 +172,23 @@ private string GetIndentationString(int indentationLevel)
213172
/// <summary>
214173
/// Retrieves the common name of this rule.
215174
/// </summary>
216-
public string GetCommonName()
175+
public override string GetCommonName()
217176
{
218177
return string.Format(CultureInfo.CurrentCulture, Strings.UseConsistentIndentationCommonName);
219178
}
220179

221180
/// <summary>
222181
/// Retrieves the description of this rule.
223182
/// </summary>
224-
public string GetDescription()
183+
public override string GetDescription()
225184
{
226185
return string.Format(CultureInfo.CurrentCulture, Strings.UseConsistentIndentationDescription);
227186
}
228187

229188
/// <summary>
230189
/// Retrieves the name of this rule.
231190
/// </summary>
232-
public string GetName()
191+
public override string GetName()
233192
{
234193
return string.Format(
235194
CultureInfo.CurrentCulture,
@@ -241,7 +200,7 @@ public string GetName()
241200
/// <summary>
242201
/// Retrieves the severity of the rule: error, warning or information.
243202
/// </summary>
244-
public RuleSeverity GetSeverity()
203+
public override RuleSeverity GetSeverity()
245204
{
246205
return RuleSeverity.Information;
247206
}
@@ -258,15 +217,15 @@ public DiagnosticSeverity GetDiagnosticSeverity()
258217
/// <summary>
259218
/// Retrieves the name of the module/assembly the rule is from.
260219
/// </summary>
261-
public string GetSourceName()
220+
public override string GetSourceName()
262221
{
263222
return string.Format(CultureInfo.CurrentCulture, Strings.SourceName);
264223
}
265224

266225
/// <summary>
267226
/// Retrieves the type of the rule, Builtin, Managed or Module.
268227
/// </summary>
269-
public SourceType GetSourceType()
228+
public override SourceType GetSourceType()
270229
{
271230
return SourceType.Builtin;
272231
}

0 commit comments

Comments
 (0)