@@ -48,6 +48,8 @@ public class PlaceOpenBrace : ConfigurableScriptRule
4848 private List < Func < Token [ ] , Ast , string , IEnumerable < DiagnosticRecord > > > violationFinders
4949 = new List < Func < Token [ ] , Ast , string , IEnumerable < DiagnosticRecord > > > ( ) ;
5050
51+ private HashSet < Token > tokensToIgnore ;
52+
5153 /// <summary>
5254 /// Sets the configurable properties of this rule.
5355 /// </summary>
@@ -92,6 +94,14 @@ public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string file
9294 }
9395
9496 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 ( ) ) ;
95105 foreach ( var violationFinder in violationFinders )
96106 {
97107 diagnosticRecords . AddRange ( violationFinder ( tokens , ast , fileName ) ) ;
@@ -166,35 +176,6 @@ private static string GetError(string errorString)
166176 return string . Format ( CultureInfo . CurrentCulture , errorString ) ;
167177 }
168178
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-
198179 private IEnumerable < DiagnosticRecord > FindViolationsForBraceShouldBeOnSameLine (
199180 Token [ ] tokens ,
200181 Ast ast ,
@@ -232,7 +213,8 @@ private IEnumerable<DiagnosticRecord> FindViolationsForNoNewLineAfterBrace(
232213 }
233214
234215 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 ] ) )
236218 {
237219 yield return new DiagnosticRecord (
238220 GetError ( Strings . PlaceOpenBraceErrorNoNewLineAfterBrace ) ,
@@ -246,32 +228,11 @@ private IEnumerable<DiagnosticRecord> FindViolationsForNoNewLineAfterBrace(
246228 }
247229 }
248230
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-
268231 private IEnumerable < DiagnosticRecord > FindViolationsForBraceShouldNotBeOnSameLine (
269232 Token [ ] tokens ,
270233 Ast ast ,
271234 string fileName )
272235 {
273-
274- var tokensToIgnore = FindTokensToIgnore ( tokens , ast ) ;
275236 for ( int k = 1 ; k < tokens . Length ; k ++ )
276237 {
277238 if ( tokens [ k ] . Kind == TokenKind . EndOfInput )
@@ -295,6 +256,25 @@ private IEnumerable<DiagnosticRecord> FindViolationsForBraceShouldNotBeOnSameLin
295256 }
296257 }
297258
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+
298278 private List < CorrectionExtent > GetCorrectionsForBraceShouldNotBeOnSameLine (
299279 Token [ ] tokens ,
300280 int prevTokenPos ,
0 commit comments