@@ -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