22
22
23
23
namespace Microsoft . Windows . PowerShell . ScriptAnalyzer . BuiltinRules
24
24
{
25
- // TODO place public in front of all new rules to be discoverable in PS Core
26
25
/// <summary>
27
26
/// A class to walk an AST to check for [violation]
28
27
/// </summary>
@@ -32,12 +31,16 @@ namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules
32
31
public class PlaceOpenBrace : ConfigurableScriptRule
33
32
{
34
33
private Func < Token [ ] , string , IEnumerable < DiagnosticRecord > > findViolations ;
34
+ private List < Func < Token [ ] , string , IEnumerable < DiagnosticRecord > > > violationFinders = new List < Func < Token [ ] , string , IEnumerable < DiagnosticRecord > > > ( ) ;
35
35
36
36
[ ConfigurableRuleProperty ( ) ]
37
37
public bool OnSameLine { get ; protected set ; } = true ;
38
38
39
39
[ 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 ;
41
44
42
45
/// <summary>
43
46
/// Analyzes the given ast to find the [violation]
@@ -57,24 +60,35 @@ public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string file
57
60
ConfigureRule ( ) ;
58
61
if ( OnSameLine )
59
62
{
60
- findViolations = this . FindViolationsForBraceShouldBeOnSameLine ;
63
+ // findViolations = this.FindViolationsForBraceShouldBeOnSameLine;
64
+ violationFinders . Add ( FindViolationsForBraceShouldBeOnSameLine ) ;
61
65
}
62
66
else
63
67
{
64
- findViolations = this . FindViolationsForBraceShouldNotBeOnSameLine ;
68
+ // findViolations = this.FindViolationsForBraceShouldNotBeOnSameLine;
69
+ violationFinders . Add ( FindViolationsForBraceShouldNotBeOnSameLine ) ;
70
+ }
71
+
72
+ if ( NewLineAfter )
73
+ {
74
+ violationFinders . Add ( FindViolationsForNoNewLineAfterBrace ) ;
65
75
}
66
76
}
67
77
68
- if ( ! Enable )
78
+ var diagnosticRecords = new List < DiagnosticRecord > ( ) ;
79
+
80
+ if ( Enable )
69
81
{
70
- return Enumerable . Empty < DiagnosticRecord > ( ) ;
82
+ foreach ( var violationFinder in violationFinders )
83
+ {
84
+ diagnosticRecords . AddRange ( violationFinder ( Helper . Instance . Tokens , fileName ) ) ;
85
+ }
71
86
}
72
87
73
88
// TODO Should have the following options
74
89
// * new-line-after
75
90
// * no-empty-line-after
76
-
77
- return findViolations ( Helper . Instance . Tokens , fileName ) ;
91
+ return diagnosticRecords ;
78
92
}
79
93
80
94
private IEnumerable < DiagnosticRecord > FindViolationsForBraceShouldBeOnSameLine (
@@ -98,6 +112,46 @@ private IEnumerable<DiagnosticRecord> FindViolationsForBraceShouldBeOnSameLine(
98
112
}
99
113
}
100
114
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
+
101
155
private IEnumerable < DiagnosticRecord > FindViolationsForBraceShouldNotBeOnSameLine (
102
156
Token [ ] tokens ,
103
157
string fileName )
0 commit comments