Skip to content

Commit df3ae0a

Browse files
author
Kapil Borle
committed
Configure PlaceOpenBrace rule in AnalyzeScript method
1 parent fd47614 commit df3ae0a

1 file changed

Lines changed: 40 additions & 26 deletions

File tree

Rules/PlaceOpenBrace.cs

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules
2424
{
25+
// TODO place public in front of all new rules to be discoverable in PS Core
2526
/// <summary>
2627
/// A class to walk an AST to check for [violation]
2728
/// </summary>
@@ -59,22 +60,11 @@ public static RuleArguments Create(Dictionary<string, object> arguments)
5960
}
6061
}
6162

63+
private bool isRuleConfigured;
64+
6265
private RuleArguments ruleArgs;
6366
private Func<Token[], string, IEnumerable<DiagnosticRecord>> findViolations;
6467

65-
public PlaceOpenBrace()
66-
{
67-
ruleArgs = RuleArguments.Create(Helper.Instance.GetRuleArguments(this.GetName()));
68-
if (ruleArgs.OnSameLine)
69-
{
70-
findViolations = this.FindViolationsForBraceShouldBeOnSameLine;
71-
}
72-
else
73-
{
74-
findViolations = this.FindViolationsForBraceShouldNotBeOnSameLine;
75-
}
76-
}
77-
7868
/// <summary>
7969
/// Analyzes the given ast to find the [violation]
8070
/// </summary>
@@ -88,32 +78,56 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
8878
throw new ArgumentNullException("ast");
8979
}
9080

81+
// We cannot set the rule arguments in the rule constructor
82+
// because it needs Helper.Instance. Helper doesn't get
83+
// initialized in Get-ScriptAnalyzerRule cmdlet so configuring
84+
// the rule in the constructor causes Get-ScriptAnalyzerRule to
85+
// crash.
86+
if (!isRuleConfigured)
87+
{
88+
ConfigureRule();
89+
}
90+
9191
// TODO Should have the following options
9292
// * new-line-after
9393
// * no-empty-line-after
9494

9595
return findViolations(Helper.Instance.Tokens, fileName);
9696
}
9797

98+
private void ConfigureRule()
99+
{
100+
ruleArgs = RuleArguments.Create(Helper.Instance.GetRuleArguments(this.GetName()));
101+
if (ruleArgs.OnSameLine)
102+
{
103+
findViolations = this.FindViolationsForBraceShouldBeOnSameLine;
104+
}
105+
else
106+
{
107+
findViolations = this.FindViolationsForBraceShouldNotBeOnSameLine;
108+
}
109+
isRuleConfigured = true;
110+
}
111+
98112
private IEnumerable<DiagnosticRecord> FindViolationsForBraceShouldBeOnSameLine(
99113
Token[] tokens,
100114
string fileName)
101115
{
102-
for (int k = 2; k < tokens.Length; k++)
116+
for (int k = 2; k < tokens.Length; k++)
117+
{
118+
if (tokens[k].Kind == TokenKind.LCurly
119+
&& tokens[k - 1].Kind == TokenKind.NewLine)
103120
{
104-
if (tokens[k].Kind == TokenKind.LCurly
105-
&& tokens[k - 1].Kind == TokenKind.NewLine)
106-
{
107-
yield return new DiagnosticRecord(
108-
GetError(),
109-
tokens[k].Extent,
110-
GetName(),
111-
GetDiagnosticSeverity(),
112-
fileName,
113-
null,
114-
GetCorrectionsForBraceShouldBeOnSameLine(tokens[k - 2], tokens[k], fileName));
115-
}
121+
yield return new DiagnosticRecord(
122+
GetError(),
123+
tokens[k].Extent,
124+
GetName(),
125+
GetDiagnosticSeverity(),
126+
fileName,
127+
null,
128+
GetCorrectionsForBraceShouldBeOnSameLine(tokens[k - 2], tokens[k], fileName));
116129
}
130+
}
117131
}
118132

119133
private IEnumerable<DiagnosticRecord> FindViolationsForBraceShouldNotBeOnSameLine(

0 commit comments

Comments
 (0)