Skip to content
This repository was archived by the owner on Jul 12, 2022. It is now read-only.

Commit 820c031

Browse files
committed
Keep newline before comment close brace
Consider the following code: ``` csharp void M() { G(); // Comment } ``` The new line between G() and the comment is meaningful and should be kept by the tool. This change fixes that and a couple of subtle issues around white spaces and new lines. closes #36
1 parent 280a9c4 commit 820c031

File tree

2 files changed

+307
-68
lines changed

2 files changed

+307
-68
lines changed

src/Microsoft.DotNet.CodeFormatting.Tests/Rules/BracesRuleTests.cs

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ class S
122122
}
123123

124124
[Fact]
125-
public void TestNoNewLineBeforeEndBrace04()
125+
public void TestEmptyBraceWhitespaceAfterOpen()
126126
{
127127
var text = @"
128128
class S
@@ -137,6 +137,48 @@ class S
137137
Verify(text, expected);
138138
}
139139

140+
[Fact]
141+
public void TestEmptyBraceNoWhitespaceAfterOpen()
142+
{
143+
var text = @"
144+
class S
145+
{
146+
147+
148+
}";
149+
var expected = @"
150+
class S
151+
{
152+
}";
153+
Verify(text, expected);
154+
}
155+
156+
[Fact]
157+
public void TestBraceSingleMethodCall()
158+
{
159+
var text = @"
160+
class S
161+
{
162+
void M()
163+
{
164+
165+
166+
G();
167+
168+
169+
}
170+
}";
171+
var expected = @"
172+
class S
173+
{
174+
void M()
175+
{
176+
G();
177+
}
178+
}";
179+
Verify(text, expected);
180+
}
181+
140182
[Fact]
141183
public void TestRemoveNewLinesBetweenPragmaAndCloseBrace()
142184
{
@@ -272,5 +314,41 @@ class L
272314

273315
Verify(text, expected);
274316
}
317+
318+
/// <summary>
319+
/// This is a regression test for issue #36
320+
/// </summary>
321+
[Fact]
322+
public void CommentsBeforeCloseBraces()
323+
{
324+
var text = @"
325+
class C
326+
{
327+
void M()
328+
{
329+
if (b) {
330+
G();
331+
332+
// A comment
333+
}
334+
}
335+
}";
336+
337+
var expected = @"
338+
class C
339+
{
340+
void M()
341+
{
342+
if (b)
343+
{
344+
G();
345+
346+
// A comment
347+
}
348+
}
349+
}";
350+
351+
Verify(text, expected, runFormatter: true);
352+
}
275353
}
276354
}

0 commit comments

Comments
 (0)