Skip to content

Render a body of only blank lines as an empty block - #133

Merged
matt-edmondson merged 1 commit into
mainfrom
fix-blank-body
Oct 7, 2026
Merged

matt-edmondson merged 1 commit into
mainfrom
fix-blank-body

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #119

What changed

TemplateRendering.WriteBody chooses between an empty block, an expression body and a braced block by counting lines, and it counted blank lines too. A factory that wrote only a NewLine() therefore produced public void M() with no body (CS0501). This hits methods, constructors and operators.

WriteBody now chooses the shape from the lines that have content:

  • No content lines gives { }.
  • One content line goes on the declaration line, even if blank lines surround it.
  • Two or more content lines keep the existing braced path. The fragment is spliced as written, so blank lines inside a braced body stay where they are.

Tests

  • New: ABodyThatWritesOnlyBlankLinesRendersAsAnEmptyBlock covers a method body with one and with two NewLine() calls.
  • New: AConstructorBodyThatWritesOnlyBlankLinesIsAnEmptyBlock covers the same for a constructor.
  • New: AnExpressionBodyBetweenBlankLinesStaysOnTheDeclarationLine.
  • New: ABlankLineInsideABracedBodyIsKept, which pins the unchanged interior-blank behaviour.
  • With the fix reverted, 5 of the new cases fail. The full suite passes with the fix (193/193).

🤖 Generated with Claude Code

https://claude.ai/code/session_01634kz4hrbkEJcGHKJWQjtA


Generated by Claude Code

WriteBody counted blank lines when choosing between an empty block, an
expression body and a braced block, so a factory that only wrote a
NewLine left the declaration with no body (CS0501). Blank lines are now
ignored when choosing the shape; interior blank lines in a braced body
are still spliced as written.

Fixes #119

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01634kz4hrbkEJcGHKJWQjtA
@sonarqubecloud

sonarqubecloud Bot commented Oct 7, 2026

Copy link
Copy Markdown

@matt-edmondson
matt-edmondson merged commit 08896e4 into main Oct 7, 2026
14 checks passed
@matt-edmondson
matt-edmondson deleted the fix-blank-body branch October 7, 2026 07:40
matt-edmondson added a commit that referenced this pull request Nov 11, 2026
WriteBody left blank lines out only when deciding a body's shape, then
spliced the factory's whole output. A factory that ended with NewLine()
(the pattern #133 had in mind) added a second blank line between
members, and one that started with NewLine() put a blank line between
the signature and its opening brace. Leading and trailing blank lines
are now trimmed before the body is spliced, for braced and comment-only
bodies alike; blank lines inside the body are kept.

Fixes #180


Claude-Session: https://claude.ai/code/session_017vfJW3rHMVCnBN5uuK5G3N

Co-authored-by: Claude <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A BodyFactory that writes only blank lines emits a declaration with no body: public void M() (CS0501)

2 participants