Skip to content

Commit 294ab5c

Browse files
author
Kapil Borle
committed
Check for whitespace before open brace
1 parent 820538f commit 294ab5c

1 file changed

Lines changed: 36 additions & 11 deletions

File tree

Rules/UseWhitespace.cs

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,48 +23,73 @@ namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules
2323
/// <summary>
2424
/// A class to walk an AST to check for [violation]
2525
/// </summary>
26-
#if !CORECLR
26+
#if !CORECLR
2727
[Export(typeof(IScriptRule))]
2828
#endif
29-
class UseWhitespace : IScriptRule
29+
class UseWhitespace : ConfigurableRule
3030
{
31+
private readonly int whiteSpaceSize = 1;
32+
33+
[ConfigurableRuleProperty(defaultValue: true)]
34+
public bool CheckOpenBrace { get; protected set; }
35+
3136
/// <summary>
3237
/// Analyzes the given ast to find the [violation]
3338
/// </summary>
3439
/// <param name="ast">AST to be analyzed. This should be non-null</param>
3540
/// <param name="fileName">Name of file that corresponds to the input AST.</param>
3641
/// <returns>A an enumerable type containing the violations</returns>
37-
public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
42+
public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
3843
{
3944
if (ast == null)
4045
{
4146
throw new ArgumentNullException("ast");
4247
}
4348

44-
// your code goes here
45-
yield return new DiagnosticRecord();
49+
var tokenOperations = new TokenOperations(Helper.Instance.Tokens, ast);
50+
51+
var tokensAndWhitespaces = tokenOperations.GetOpenBracesWithWhiteSpacesBefore();
52+
foreach (var item in tokensAndWhitespaces)
53+
{
54+
if (item.Item2 != whiteSpaceSize)
55+
{
56+
yield return new DiagnosticRecord(
57+
GetError(),
58+
item.Item1.Extent,
59+
GetName(),
60+
GetDiagnosticSeverity(),
61+
fileName,
62+
null,
63+
null);
64+
}
65+
}
66+
}
67+
68+
private string GetError()
69+
{
70+
return string.Format(CultureInfo.CurrentCulture, Strings.UseWhitespaceError);
4671
}
4772

4873
/// <summary>
4974
/// Retrieves the common name of this rule.
5075
/// </summary>
51-
public string GetCommonName()
76+
public override string GetCommonName()
5277
{
5378
return string.Format(CultureInfo.CurrentCulture, Strings.UseWhitespaceCommonName);
5479
}
5580

5681
/// <summary>
5782
/// Retrieves the description of this rule.
5883
/// </summary>
59-
public string GetDescription()
84+
public override string GetDescription()
6085
{
6186
return string.Format(CultureInfo.CurrentCulture, Strings.UseWhitespaceDescription);
6287
}
6388

6489
/// <summary>
6590
/// Retrieves the name of this rule.
6691
/// </summary>
67-
public string GetName()
92+
public override string GetName()
6893
{
6994
return string.Format(
7095
CultureInfo.CurrentCulture,
@@ -76,7 +101,7 @@ public string GetName()
76101
/// <summary>
77102
/// Retrieves the severity of the rule: error, warning or information.
78103
/// </summary>
79-
public RuleSeverity GetSeverity()
104+
public override RuleSeverity GetSeverity()
80105
{
81106
return RuleSeverity.Warning;
82107
}
@@ -93,15 +118,15 @@ public DiagnosticSeverity GetDiagnosticSeverity()
93118
/// <summary>
94119
/// Retrieves the name of the module/assembly the rule is from.
95120
/// </summary>
96-
public string GetSourceName()
121+
public override string GetSourceName()
97122
{
98123
return string.Format(CultureInfo.CurrentCulture, Strings.SourceName);
99124
}
100125

101126
/// <summary>
102127
/// Retrieves the type of the rule, Builtin, Managed or Module.
103128
/// </summary>
104-
public SourceType GetSourceType()
129+
public override SourceType GetSourceType()
105130
{
106131
return SourceType.Builtin;
107132
}

0 commit comments

Comments
 (0)