@@ -48,6 +48,8 @@ public class PlaceOpenBrace : ConfigurableScriptRule
48
48
private List < Func < Token [ ] , Ast , string , IEnumerable < DiagnosticRecord > > > violationFinders
49
49
= new List < Func < Token [ ] , Ast , string , IEnumerable < DiagnosticRecord > > > ( ) ;
50
50
51
+ private HashSet < Token > tokensToIgnore ;
52
+
51
53
/// <summary>
52
54
/// Sets the configurable properties of this rule.
53
55
/// </summary>
@@ -92,6 +94,14 @@ public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string file
92
94
}
93
95
94
96
var tokens = Helper . Instance . Tokens ;
97
+
98
+ // Ignore open braces that are part of arguments to a command
99
+ // * E.g. get-process | % { "blah }
100
+ // In the above case even if OnSameLine == false, we should not
101
+ // flag the open brace as it would move the brace to the next line
102
+ // and will invalidate the command
103
+ tokensToIgnore = new HashSet < Token > (
104
+ new TokenOperations ( tokens , ast ) . GetOpenBracesInCommandElements ( ) ) ;
95
105
foreach ( var violationFinder in violationFinders )
96
106
{
97
107
diagnosticRecords . AddRange ( violationFinder ( tokens , ast , fileName ) ) ;
@@ -166,35 +176,6 @@ private static string GetError(string errorString)
166
176
return string . Format ( CultureInfo . CurrentCulture , errorString ) ;
167
177
}
168
178
169
- private static HashSet < Token > FindTokensToIgnore ( Token [ ] tokens , Ast ast )
170
- {
171
- // Ignore open braces that are part of arguments to a command
172
- // * E.g. get-process | % { "blah }
173
- // In the above case even if OnSameLine == false, we should not
174
- // flag the open brace as it would move the brace to the next line
175
- // and will invalidate the command
176
-
177
- var cmdElemAsts = ast . FindAll ( x => x is CommandElementAst && x is ScriptBlockExpressionAst , true ) ;
178
- var tokensToIgnore = new HashSet < Token > ( ) ;
179
- if ( cmdElemAsts == null )
180
- {
181
- return tokensToIgnore ;
182
- }
183
-
184
- foreach ( var cmdElemAst in cmdElemAsts )
185
- {
186
- var tokenToIgnore = tokens . FirstOrDefault (
187
- token => token . Kind == TokenKind . LCurly
188
- && cmdElemAst . Extent . StartOffset == token . Extent . StartOffset ) ;
189
- if ( tokenToIgnore != null )
190
- {
191
- tokensToIgnore . Add ( tokenToIgnore ) ;
192
- }
193
- }
194
-
195
- return tokensToIgnore ;
196
- }
197
-
198
179
private IEnumerable < DiagnosticRecord > FindViolationsForBraceShouldBeOnSameLine (
199
180
Token [ ] tokens ,
200
181
Ast ast ,
@@ -232,7 +213,8 @@ private IEnumerable<DiagnosticRecord> FindViolationsForNoNewLineAfterBrace(
232
213
}
233
214
234
215
if ( tokens [ k ] . Kind == TokenKind . LCurly
235
- && tokens [ k + 1 ] . Kind != TokenKind . NewLine )
216
+ && tokens [ k + 1 ] . Kind != TokenKind . NewLine
217
+ && ! tokensToIgnore . Contains ( tokens [ k ] ) )
236
218
{
237
219
yield return new DiagnosticRecord (
238
220
GetError ( Strings . PlaceOpenBraceErrorNoNewLineAfterBrace ) ,
@@ -246,32 +228,11 @@ private IEnumerable<DiagnosticRecord> FindViolationsForNoNewLineAfterBrace(
246
228
}
247
229
}
248
230
249
- private List < CorrectionExtent > GetCorrectionsForNoNewLineAfterBrace (
250
- Token [ ] tokens ,
251
- int openBracePos ,
252
- string fileName )
253
- {
254
- var corrections = new List < CorrectionExtent > ( ) ;
255
- var extent = tokens [ openBracePos ] . Extent ;
256
-
257
- corrections . Add (
258
- new CorrectionExtent (
259
- extent . StartLineNumber ,
260
- extent . EndLineNumber ,
261
- extent . StartColumnNumber ,
262
- extent . EndColumnNumber ,
263
- new StringBuilder ( ) . Append ( extent . Text ) . Append ( Environment . NewLine ) . ToString ( ) ,
264
- fileName ) ) ;
265
- return corrections ;
266
- }
267
-
268
231
private IEnumerable < DiagnosticRecord > FindViolationsForBraceShouldNotBeOnSameLine (
269
232
Token [ ] tokens ,
270
233
Ast ast ,
271
234
string fileName )
272
235
{
273
-
274
- var tokensToIgnore = FindTokensToIgnore ( tokens , ast ) ;
275
236
for ( int k = 1 ; k < tokens . Length ; k ++ )
276
237
{
277
238
if ( tokens [ k ] . Kind == TokenKind . EndOfInput )
@@ -295,6 +256,25 @@ private IEnumerable<DiagnosticRecord> FindViolationsForBraceShouldNotBeOnSameLin
295
256
}
296
257
}
297
258
259
+ private List < CorrectionExtent > GetCorrectionsForNoNewLineAfterBrace (
260
+ Token [ ] tokens ,
261
+ int openBracePos ,
262
+ string fileName )
263
+ {
264
+ var corrections = new List < CorrectionExtent > ( ) ;
265
+ var extent = tokens [ openBracePos ] . Extent ;
266
+
267
+ corrections . Add (
268
+ new CorrectionExtent (
269
+ extent . StartLineNumber ,
270
+ extent . EndLineNumber ,
271
+ extent . StartColumnNumber ,
272
+ extent . EndColumnNumber ,
273
+ new StringBuilder ( ) . Append ( extent . Text ) . Append ( Environment . NewLine ) . ToString ( ) ,
274
+ fileName ) ) ;
275
+ return corrections ;
276
+ }
277
+
298
278
private List < CorrectionExtent > GetCorrectionsForBraceShouldNotBeOnSameLine (
299
279
Token [ ] tokens ,
300
280
int prevTokenPos ,
0 commit comments