Skip to content

Commit 08896e4

Browse files
Merge pull request #133 from ktsu-dev/fix-blank-body
Render a body of only blank lines as an empty block
2 parents d44ff56 + 3f7e579 commit 08896e4

2 files changed

Lines changed: 88 additions & 3 deletions

File tree

‎CodeBlocker.Test/TemplateTests.cs‎

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,67 @@ public void ABodyThatWritesNothingRendersAsAnEmptyBlock()
478478
Assert.AreEqual("public virtual void Run() { }\n", Render(method));
479479
}
480480

481+
[TestMethod]
482+
[DataRow(1)]
483+
[DataRow(2)]
484+
public void ABodyThatWritesOnlyBlankLinesRendersAsAnEmptyBlock(int newLines)
485+
{
486+
// A NewLine after a loop over an empty collection used to leave "public void M() " with no
487+
// body, which does not compile (CS0501).
488+
MethodTemplate method = new()
489+
{
490+
Type = "void",
491+
Name = "M",
492+
Keywords = { "public" },
493+
BodyFactory = codeBlocker =>
494+
{
495+
for (int i = 0; i < newLines; i++)
496+
{
497+
codeBlocker.NewLine();
498+
}
499+
},
500+
};
501+
502+
Assert.AreEqual("public void M() { }\n", Render(method));
503+
}
504+
505+
[TestMethod]
506+
public void AnExpressionBodyBetweenBlankLinesStaysOnTheDeclarationLine() =>
507+
Assert.AreEqual(
508+
"public void Reset() => count = 0;\n",
509+
Render(new MethodTemplate
510+
{
511+
Type = "void",
512+
Name = "Reset",
513+
Keywords = { "public" },
514+
BodyFactory = codeBlocker =>
515+
{
516+
codeBlocker.NewLine();
517+
codeBlocker.WriteLine("=> count = 0;");
518+
codeBlocker.NewLine();
519+
},
520+
}));
521+
522+
[TestMethod]
523+
public void ABlankLineInsideABracedBodyIsKept()
524+
{
525+
MethodTemplate method = new()
526+
{
527+
Type = "void",
528+
Name = "Run",
529+
Keywords = { "public" },
530+
BodyFactory = codeBlocker =>
531+
{
532+
using Scope scope = new(codeBlocker);
533+
codeBlocker.WriteLine("First();");
534+
codeBlocker.NewLine();
535+
codeBlocker.WriteLine("Second();");
536+
},
537+
};
538+
539+
Assert.AreEqual("public void Run()\n{\n\tFirst();\n\n\tSecond();\n}\n", Render(method));
540+
}
541+
481542
[TestMethod]
482543
public void AMultiLineBodyIsIndentedToWhereItIsSpliced()
483544
{
@@ -568,6 +629,25 @@ public void AConstructorWithNoBaseCallIsAnEmptyBlock() =>
568629
"public Widget() { }\n",
569630
Render(new ConstructorTemplate { Name = "Widget", Keywords = { "public" } }));
570631

632+
[TestMethod]
633+
[DataRow(1)]
634+
[DataRow(2)]
635+
public void AConstructorBodyThatWritesOnlyBlankLinesIsAnEmptyBlock(int newLines) =>
636+
Assert.AreEqual(
637+
"public Widget() { }\n",
638+
Render(new ConstructorTemplate
639+
{
640+
Name = "Widget",
641+
Keywords = { "public" },
642+
BodyFactory = codeBlocker =>
643+
{
644+
for (int i = 0; i < newLines; i++)
645+
{
646+
codeBlocker.NewLine();
647+
}
648+
},
649+
}));
650+
571651
[TestMethod]
572652
public void AConstructorInitialiserIsIndentedOnItsOwnLine()
573653
{

‎CodeBlocker/Templates/TemplateRendering.cs‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -223,19 +223,24 @@ internal static void WriteBody(CodeBlocker codeBlocker, Action<CodeBlocker>? bod
223223
string body = RenderFragment(codeBlocker, bodyFactory);
224224
string[] lines = SplitLines(codeBlocker, body);
225225

226+
// Blank lines carry no body of their own, so they are left out when deciding its shape. A
227+
// factory that only wrote spacing, such as a NewLine after a loop over an empty collection,
228+
// would otherwise leave the declaration with no body at all.
229+
string[] contentLines = [.. lines.Where(line => line.Length != 0)];
230+
226231
// A factory that wrote nothing means "declared, but empty" — a virtual base method, or a
227232
// constructor that only forwards to its base.
228-
if (lines.Length == 0)
233+
if (contentLines.Length == 0)
229234
{
230235
codeBlocker.WriteLine(" { }");
231236
return;
232237
}
233238

234-
if (lines.Length == 1)
239+
if (contentLines.Length == 1)
235240
{
236241
// An expression body stays on the declaration line.
237242
codeBlocker.Write(" ");
238-
codeBlocker.WriteLine(lines[0]);
243+
codeBlocker.WriteLine(contentLines[0]);
239244
return;
240245
}
241246

0 commit comments

Comments
 (0)