Skip to content

Commit 32f15e9

Browse files
author
Kapil Borle
committed
Create violation for close brace in branch statements
Whenever `NewLineAfter = $false`, the rule creates a violation if a branching statement is on a new line after the preceding close brace. We consider the following braching statements: - else - elseif - catch - finally
1 parent 2ed036f commit 32f15e9

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

Rules/PlaceCloseBrace.cs

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,12 @@ public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string file
138138
GetViolationForBraceShouldHaveNewLineAfter(tokens, k, openBracePos, fileName),
139139
ref diagnosticRecords);
140140
}
141+
else
142+
{
143+
AddToDiagnosticRecords(
144+
GetViolationsForUncuddledBranches(tokens, k, openBracePos, fileName),
145+
ref diagnosticRecords);
146+
}
141147
}
142148
else
143149
{
@@ -326,12 +332,41 @@ private DiagnosticRecord GetViolationForBraceShouldHaveNewLineAfter(
326332
fileName,
327333
null,
328334
GetCorrectionsForBraceShouldHaveNewLineAfter(tokens, closeBracePos, openBracePos, fileName));
329-
}
335+
}
330336
}
331337

332338
return null;
333339
}
334340

341+
private DiagnosticRecord GetViolationsForUncuddledBranches(
342+
Token[] tokens,
343+
int closeBracePos,
344+
int openBracePos,
345+
string fileName)
346+
{
347+
var expectedNewLinePos = closeBracePos + 1;
348+
349+
if (tokens.Length > 1 && tokens.Length > expectedNewLinePos)
350+
{
351+
var closeBraceToken = tokens[closeBracePos];
352+
if (tokens[closeBracePos + 1].Kind == TokenKind.NewLine &&
353+
IsBranchingStatementToken(tokens[closeBracePos + 2]))
354+
{
355+
return new DiagnosticRecord(
356+
GetError(Strings.PlaceCloseBraceErrorShouldCuddleBranchStatement),
357+
closeBraceToken.Extent,
358+
GetName(),
359+
GetDiagnosticSeverity(),
360+
fileName,
361+
null,
362+
GetCorrectionsForBraceShouldHaveNewLineAfter(tokens, closeBracePos, openBracePos, fileName));
363+
}
364+
}
365+
366+
return null;
367+
}
368+
369+
335370
private DiagnosticRecord GetViolationForBraceShouldBeOnNewLine(
336371
Token[] tokens,
337372
int closeBracePos,
@@ -376,6 +411,20 @@ private List<CorrectionExtent> GetCorrectionsForBraceShouldBeOnNewLine(
376411
return corrections;
377412
}
378413

414+
private bool IsBranchingStatementToken(Token token)
415+
{
416+
switch (token.Kind)
417+
{
418+
case TokenKind.Else:
419+
case TokenKind.ElseIf:
420+
case TokenKind.Catch:
421+
case TokenKind.Finally:
422+
return true;
423+
424+
default:
425+
return false;
426+
}
427+
}
379428
private void AddToDiagnosticRecords(
380429
DiagnosticRecord diagnosticRecord,
381430
ref List<DiagnosticRecord> diagnosticRecords)

Rules/Strings.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -906,6 +906,9 @@
906906
<data name="PlaceCloseBraceErrorShouldFollowNewLine" xml:space="preserve">
907907
<value>Close brace does not follow a new line.</value>
908908
</data>
909+
<data name="PlaceCloseBraceErrorShouldCuddleBranchStatement" xml:space="preserve">
910+
<value>Close brace follows a new line.</value>
911+
</data>
909912
<data name="UseConsistentIndentationName" xml:space="preserve">
910913
<value>UseConsistentIndentation</value>
911914
</data>

0 commit comments

Comments
 (0)