Skip to content

Commit f52c770

Browse files
author
Kapil Borle
committed
Add enable parameter to formatting rules
We do not want formatting rules to be run by default whenever scriptanalyzer is invoked. However, the engine doesn't provide a way to disable a given built-in rule without messing with the input parameters or input settings file. Hence we add this parameter to the formatting rules so that they are not run by default whenever scriptanalyer is invoked.
1 parent 9f10590 commit f52c770

4 files changed

Lines changed: 44 additions & 11 deletions

File tree

Rules/ConfigurableScriptRule.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic
1111
// This is still an experimental class and we do not want to expose it
1212
// as a public API as of yet. So we place it in builtinrules project
1313
// and keep it internal as it consumed only by a handful of rules.
14-
internal abstract class ConfigurableScriptRule : IScriptRule
14+
public abstract class ConfigurableScriptRule : IScriptRule
1515
{
1616
public bool IsRuleConfigured { get; protected set; } = false;
1717

Rules/PlaceCloseBrace.cs

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,34 @@ namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules
2626
#if !CORECLR
2727
[Export(typeof(IScriptRule))]
2828
#endif
29-
public class PlaceCloseBrace : IScriptRule
29+
public class PlaceCloseBrace : ConfigurableScriptRule
3030
{
31+
[ConfigurableRuleProperty()]
32+
public bool Enable {get; protected set;} = false;
33+
3134
/// <summary>
3235
/// Analyzes the given ast to find the [violation]
3336
/// </summary>
3437
/// <param name="ast">AST to be analyzed. This should be non-null</param>
3538
/// <param name="fileName">Name of file that corresponds to the input AST.</param>
3639
/// <returns>A an enumerable type containing the violations</returns>
37-
public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
40+
public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
3841
{
3942
if (ast == null)
4043
{
4144
throw new ArgumentNullException("ast");
4245
}
4346

47+
if (!IsRuleConfigured)
48+
{
49+
ConfigureRule();
50+
}
51+
52+
if (!Enable)
53+
{
54+
return Enumerable.Empty<DiagnosticRecord>();
55+
}
56+
4457
// TODO Should have the following options
4558
// * no-empty-line-before
4659
// * align (if close brance and open brace on new lines align with open brace,
@@ -236,23 +249,23 @@ private string GetError()
236249
/// <summary>
237250
/// Retrieves the common name of this rule.
238251
/// </summary>
239-
public string GetCommonName()
252+
public override string GetCommonName()
240253
{
241254
return string.Format(CultureInfo.CurrentCulture, Strings.PlaceCloseBraceCommonName);
242255
}
243256

244257
/// <summary>
245258
/// Retrieves the description of this rule.
246259
/// </summary>
247-
public string GetDescription()
260+
public override string GetDescription()
248261
{
249262
return string.Format(CultureInfo.CurrentCulture, Strings.PlaceCloseBraceDescription);
250263
}
251264

252265
/// <summary>
253266
/// Retrieves the name of this rule.
254267
/// </summary>
255-
public string GetName()
268+
public override string GetName()
256269
{
257270
return string.Format(
258271
CultureInfo.CurrentCulture,
@@ -264,7 +277,7 @@ public string GetName()
264277
/// <summary>
265278
/// Retrieves the severity of the rule: error, warning or information.
266279
/// </summary>
267-
public RuleSeverity GetSeverity()
280+
public override RuleSeverity GetSeverity()
268281
{
269282
return RuleSeverity.Information;
270283
}
@@ -281,17 +294,19 @@ public DiagnosticSeverity GetDiagnosticSeverity()
281294
/// <summary>
282295
/// Retrieves the name of the module/assembly the rule is from.
283296
/// </summary>
284-
public string GetSourceName()
297+
public override string GetSourceName()
285298
{
286299
return string.Format(CultureInfo.CurrentCulture, Strings.SourceName);
287300
}
288301

289302
/// <summary>
290303
/// Retrieves the type of the rule, Builtin, Managed or Module.
291304
/// </summary>
292-
public SourceType GetSourceType()
305+
public override SourceType GetSourceType()
293306
{
294307
return SourceType.Builtin;
295308
}
309+
310+
296311
}
297312
}

Rules/PlaceOpenBrace.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,16 @@ namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules
2929
#if !CORECLR
3030
[Export(typeof(IScriptRule))]
3131
#endif
32-
class PlaceOpenBrace : ConfigurableScriptRule
32+
public class PlaceOpenBrace : ConfigurableScriptRule
3333
{
3434
private Func<Token[], string, IEnumerable<DiagnosticRecord>> findViolations;
3535

3636
[ConfigurableRuleProperty()]
3737
public bool OnSameLine { get; protected set; } = true;
3838

39+
[ConfigurableRuleProperty()]
40+
public bool Enable {get; protected set;} = false;
41+
3942
/// <summary>
4043
/// Analyzes the given ast to find the [violation]
4144
/// </summary>
@@ -62,6 +65,11 @@ public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string file
6265
}
6366
}
6467

68+
if (!Enable)
69+
{
70+
return Enumerable.Empty<DiagnosticRecord>();
71+
}
72+
6573
// TODO Should have the following options
6674
// * new-line-after
6775
// * no-empty-line-after

Rules/UseConsistentIndentation.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules
2626
#if !CORECLR
2727
[Export(typeof(IScriptRule))]
2828
#endif
29-
class UseConsistentIndentation : ConfigurableScriptRule
29+
public class UseConsistentIndentation : ConfigurableScriptRule
3030
{
3131
private enum IndentationKind { Space, Tab };
3232

@@ -36,6 +36,9 @@ private enum IndentationKind { Space, Tab };
3636
[ConfigurableRuleProperty()]
3737
public int IndentationSize { get; protected set; } = 4;
3838

39+
[ConfigurableRuleProperty()]
40+
public bool Enable {get; protected set;} = false;
41+
3942
/// <summary>
4043
/// Analyzes the given ast to find the [violation]
4144
/// </summary>
@@ -54,6 +57,13 @@ public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string file
5457
ConfigureRule();
5558
}
5659

60+
// we add this switch because there is no clean way
61+
// to disable the rule by default
62+
if (!Enable)
63+
{
64+
return Enumerable.Empty<DiagnosticRecord>();
65+
}
66+
5767
var tokens = Helper.Instance.Tokens;
5868
var diagnosticRecords = new List<DiagnosticRecord>();
5969
var indentationLevel = 0;

0 commit comments

Comments
 (0)