Skip to content

Commit 58305c3

Browse files
author
Kapil Borle
committed
Check for new line after open brace in PlaceOpenBrace rule
1 parent 4dfb159 commit 58305c3

File tree

2 files changed

+91
-17
lines changed

2 files changed

+91
-17
lines changed

Rules/PlaceOpenBrace.cs

Lines changed: 62 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
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
2625
/// <summary>
2726
/// A class to walk an AST to check for [violation]
2827
/// </summary>
@@ -32,12 +31,16 @@ namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules
3231
public class PlaceOpenBrace : ConfigurableScriptRule
3332
{
3433
private Func<Token[], string, IEnumerable<DiagnosticRecord>> findViolations;
34+
private List<Func<Token[], string, IEnumerable<DiagnosticRecord>>> violationFinders = new List<Func<Token[], string, IEnumerable<DiagnosticRecord>>>();
3535

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

3939
[ConfigurableRuleProperty()]
40-
public bool Enable {get; protected set;} = false;
40+
public bool NewLineAfter { get; protected set; } = true;
41+
42+
[ConfigurableRuleProperty()]
43+
public bool Enable { get; protected set; } = false;
4144

4245
/// <summary>
4346
/// Analyzes the given ast to find the [violation]
@@ -57,24 +60,35 @@ public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string file
5760
ConfigureRule();
5861
if (OnSameLine)
5962
{
60-
findViolations = this.FindViolationsForBraceShouldBeOnSameLine;
63+
// findViolations = this.FindViolationsForBraceShouldBeOnSameLine;
64+
violationFinders.Add(FindViolationsForBraceShouldBeOnSameLine);
6165
}
6266
else
6367
{
64-
findViolations = this.FindViolationsForBraceShouldNotBeOnSameLine;
68+
// findViolations = this.FindViolationsForBraceShouldNotBeOnSameLine;
69+
violationFinders.Add(FindViolationsForBraceShouldNotBeOnSameLine);
70+
}
71+
72+
if (NewLineAfter)
73+
{
74+
violationFinders.Add(FindViolationsForNoNewLineAfterBrace);
6575
}
6676
}
6777

68-
if (!Enable)
78+
var diagnosticRecords = new List<DiagnosticRecord>();
79+
80+
if (Enable)
6981
{
70-
return Enumerable.Empty<DiagnosticRecord>();
82+
foreach (var violationFinder in violationFinders)
83+
{
84+
diagnosticRecords.AddRange(violationFinder(Helper.Instance.Tokens, fileName));
85+
}
7186
}
7287

7388
// TODO Should have the following options
7489
// * new-line-after
7590
// * no-empty-line-after
76-
77-
return findViolations(Helper.Instance.Tokens, fileName);
91+
return diagnosticRecords;
7892
}
7993

8094
private IEnumerable<DiagnosticRecord> FindViolationsForBraceShouldBeOnSameLine(
@@ -98,6 +112,46 @@ private IEnumerable<DiagnosticRecord> FindViolationsForBraceShouldBeOnSameLine(
98112
}
99113
}
100114

115+
private IEnumerable<DiagnosticRecord> FindViolationsForNoNewLineAfterBrace(
116+
Token[] tokens,
117+
string fileName)
118+
{
119+
for (int k = 0; k < tokens.Length - 1; k++)
120+
{
121+
if (tokens[k].Kind == TokenKind.LCurly
122+
&& tokens[k + 1].Kind != TokenKind.NewLine)
123+
{
124+
yield return new DiagnosticRecord(
125+
GetError(),
126+
tokens[k].Extent,
127+
GetName(),
128+
GetDiagnosticSeverity(),
129+
fileName,
130+
null,
131+
GetCorrectionsForNoNewLineAfterBrace(tokens, k, fileName));
132+
}
133+
}
134+
}
135+
136+
private List<CorrectionExtent> GetCorrectionsForNoNewLineAfterBrace(
137+
Token[] tokens,
138+
int openBracePos,
139+
string fileName)
140+
{
141+
var corrections = new List<CorrectionExtent>();
142+
var extent = tokens[openBracePos].Extent;
143+
144+
corrections.Add(
145+
new CorrectionExtent(
146+
extent.StartLineNumber,
147+
extent.EndLineNumber,
148+
extent.StartColumnNumber,
149+
extent.EndColumnNumber,
150+
new StringBuilder().Append(extent.Text).Append(Environment.NewLine).ToString(),
151+
fileName));
152+
return corrections;
153+
}
154+
101155
private IEnumerable<DiagnosticRecord> FindViolationsForBraceShouldNotBeOnSameLine(
102156
Token[] tokens,
103157
string fileName)

Tests/Rules/PlaceOpenBrace.tests.ps1

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
Import-Module PSScriptAnalyzer
2+
$ruleConfiguration = @{
3+
Enable = $true
4+
OnSameLine = $true
5+
NewLineAfter = $true
6+
}
7+
28
$settings = @{
39
IncludeRules = @("PSPlaceOpenBrace")
410
Rules = @{
5-
PSPlaceOpenBrace = @{
6-
Enable = $true
7-
OnSameLine = $true
8-
}
11+
PSPlaceOpenBrace = $ruleConfiguration
912
}
1013
}
1114

12-
13-
Describe "PlaceOpenBrace on same line" {
15+
Describe "PlaceOpenBrace" {
1416
Context "When an open brace must be on the same line" {
1517
BeforeAll{
1618
$def = @'
@@ -19,6 +21,7 @@ function foo ($param1)
1921
2022
}
2123
'@
24+
$ruleConfiguration.'OnSameLine' = $true
2225
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
2326
}
2427

@@ -38,10 +41,27 @@ function foo ($param1) {
3841
3942
}
4043
'@
44+
$ruleConfiguration.'OnSameLine' = $false
45+
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
46+
}
47+
48+
It "Should find a violation" {
49+
$violations.Count | Should Be 1
50+
}
4151

42-
$settingsNewLine = $settings.Clone()
43-
$settingsNewLine["Rules"]["PSPlaceOpenBrace"]["OnSameLine"] = $false
44-
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settingsNewLine
52+
It "Should mark only the open brace" {
53+
$violations[0].Extent.Text | Should Be '{'
54+
}
55+
}
56+
57+
Context "When a new line should follow an open brace" {
58+
BeforeAll{
59+
$def = @'
60+
function foo { }
61+
'@
62+
$ruleConfiguration.'OnSameLine' = $true
63+
$ruleConfiguration.'NewLineAfter' = $true
64+
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
4565
}
4666

4767
It "Should find a violation" {

0 commit comments

Comments
 (0)