|
14 | 14 | namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules |
15 | 15 | { |
16 | 16 | /// <summary> |
17 | | - /// A class to walk an AST to check for violation. |
| 17 | + /// A formatting rule about whether braces should start on the same line or not. |
18 | 18 | /// </summary> |
19 | 19 | #if !CORECLR |
20 | 20 | [Export(typeof(IScriptRule))] |
@@ -201,14 +201,23 @@ private IEnumerable<DiagnosticRecord> FindViolationsForBraceShouldBeOnSameLine( |
201 | 201 | && tokens[k - 1].Kind == TokenKind.NewLine |
202 | 202 | && !tokensToIgnore.Contains(tokens[k])) |
203 | 203 | { |
| 204 | + var precedingExpression = tokens[k - 2]; |
| 205 | + Token optionalComment = null; |
| 206 | + // If a comment is before the open brace, then take the token before the comment |
| 207 | + if (precedingExpression.Kind == TokenKind.Comment && k > 2) |
| 208 | + { |
| 209 | + precedingExpression = tokens[k - 3]; |
| 210 | + optionalComment = tokens[k - 2]; |
| 211 | + } |
| 212 | + |
204 | 213 | yield return new DiagnosticRecord( |
205 | 214 | GetError(Strings.PlaceOpenBraceErrorShouldBeOnSameLine), |
206 | 215 | tokens[k].Extent, |
207 | 216 | GetName(), |
208 | 217 | GetDiagnosticSeverity(), |
209 | 218 | fileName, |
210 | 219 | null, |
211 | | - GetCorrectionsForBraceShouldBeOnSameLine(tokens[k - 2], tokens[k], fileName)); |
| 220 | + GetCorrectionsForBraceShouldBeOnSameLine(precedingExpression, optionalComment, tokens[k], fileName)); |
212 | 221 | } |
213 | 222 | } |
214 | 223 | } |
@@ -336,17 +345,19 @@ private int GetStartColumnNumberOfTokenLine(Token[] tokens, int refTokenPos) |
336 | 345 |
|
337 | 346 | private List<CorrectionExtent> GetCorrectionsForBraceShouldBeOnSameLine( |
338 | 347 | Token precedingExpression, |
| 348 | + Token optionalCommentOfPrecedingExpression, |
339 | 349 | Token lCurly, |
340 | 350 | string fileName) |
341 | 351 | { |
342 | 352 | var corrections = new List<CorrectionExtent>(); |
| 353 | + var optionalComment = optionalCommentOfPrecedingExpression != null ? $" {optionalCommentOfPrecedingExpression}" : string.Empty; |
343 | 354 | corrections.Add( |
344 | 355 | new CorrectionExtent( |
345 | 356 | precedingExpression.Extent.StartLineNumber, |
346 | 357 | lCurly.Extent.EndLineNumber, |
347 | 358 | precedingExpression.Extent.StartColumnNumber, |
348 | 359 | lCurly.Extent.EndColumnNumber, |
349 | | - precedingExpression.Text + " " + lCurly.Text, |
| 360 | + $"{precedingExpression.Text} {lCurly.Text}{optionalComment}", |
350 | 361 | fileName)); |
351 | 362 | return corrections; |
352 | 363 | } |
|
0 commit comments