Skip to content

Commit 761076b

Browse files
author
Kapil Borle
committed
Implement logic to enforce line after close brace
1 parent a91cf7c commit 761076b

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

Rules/PlaceCloseBrace.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,13 @@ public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string file
131131
GetViolationForBraceShouldNotFollowEmptyLine(tokens, k, openBracePos, fileName),
132132
ref diagnosticRecords);
133133
}
134+
135+
if (NewLineAfter)
136+
{
137+
AddToDiagnosticRecords(
138+
GetViolationForNewLineShouldFollowBrace(tokens, k, openBracePos, fileName),
139+
ref diagnosticRecords);
140+
}
134141
}
135142
else
136143
{
@@ -254,6 +261,18 @@ private List<CorrectionExtent> GetCorrectionsForBraceShouldNotFollowEmptyLine(
254261
return corrections;
255262
}
256263

264+
private List<CorrectionExtent> GetCorrectionsForNewLineShouldFollowBrace(Token[] tokens, int closeBracePos, int openBracePos, string fileName)
265+
{
266+
var corrections = new List<CorrectionExtent>();
267+
var nextToken = tokens[closeBracePos + 1];
268+
var closeBraceToken = tokens[closeBracePos];
269+
corrections.Add(new CorrectionExtent(
270+
closeBraceToken.Extent,
271+
closeBraceToken.Text + TokenKind.NewLine.Text(),
272+
fileName));
273+
return corrections;
274+
}
275+
257276
private string GetIndentation(Token[] tokens, int closeBracePos, int openBracePos)
258277
{
259278
// if open brace on a new line by itself, use its indentation
@@ -281,6 +300,31 @@ private string GetIndentation(Token[] tokens, int closeBracePos, int openBracePo
281300
return new String(' ', firstTokenOnOpenBraceLine.Extent.StartColumnNumber - 1);
282301
}
283302

303+
private DiagnosticRecord GetViolationForNewLineShouldFollowBrace(Token[] tokens, int closeBracePos, int openBracePos, string fileName)
304+
{
305+
var expectedNewLinePos = closeBracePos + 1;
306+
if (tokens.Length > 1 && tokens.Length > expectedNewLinePos)
307+
{
308+
var closeBraceToken = tokens[closeBracePos];
309+
if (tokens[expectedNewLinePos].Kind != TokenKind.NewLine
310+
&& tokens[expectedNewLinePos].Kind != TokenKind.Comment
311+
&& !tokensToIgnore.Contains(tokens[closeBracePos]))
312+
{
313+
314+
return new DiagnosticRecord(
315+
GetError(Strings.PlaceCloseBraceErrorShouldFollowNewLine),
316+
closeBraceToken.Extent,
317+
GetName(),
318+
GetDiagnosticSeverity(),
319+
fileName,
320+
null,
321+
GetCorrectionsForNewLineShouldFollowBrace(tokens, closeBracePos, openBracePos, fileName));
322+
}
323+
}
324+
325+
return null;
326+
}
327+
284328
private DiagnosticRecord GetViolationForBraceShouldBeOnNewLine(Token[] tokens, int closeBracePos, int openBracePos, string fileName)
285329
{
286330
if (tokens.Length > 1 && tokens.Length > closeBracePos)

0 commit comments

Comments
 (0)