Skip to content

Check an expression body's expression before writing its class, and refuse a comment-only one [patch] - #186

Open
matt-edmondson wants to merge 1 commit into
mainfrom
fix/expression-body-validation-183-184
Open

matt-edmondson wants to merge 1 commit into
mainfrom
fix/expression-body-validation-183-184

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #183
Fixes #184

The triage on both issues suggested fixing them together, since #184's guard is the one #183 moves into validation.

#183: an empty expression body truncated its class

RenderExpression's empty-expression check only ran when the property itself was written. Inside a class, that happens after the class header and the members before it, and disposing the body scope then closes the class. The result was a well-formed class missing the property and every member after it.

  • TemplateBase.Validate now takes the CodeBlocker it is checking for (internal, so the public API is unchanged). That writer only supplies the indent and line terminator the callback is rendered with. Nothing is written to it.
  • PropertyTemplate.Validate(codeBlocker) renders the expression body, or each expression accessor's body, and throws the same InvalidOperationException that WriteTo would.
  • ClassTemplate, AddClasses and AddSourceFile pass the writer down, so they refuse the whole type or file before writing anything, as Check a class's members before writing any of it [patch] #169 does for the other member checks.
  • Standalone PropertyTemplate.WriteTo still renders each expression once. Inside a class the callback now runs twice, once to validate and once to write. The remarks on Validate say so.

#184: a comment- or directive-only expression rendered => ;

RenderExpression now throws when !HasCode(lines) rather than only when every line is blank. A body that writes only // ..., or only #if/#endif, gets the #175 exception. An expression with a trailing comment (#144), or real code wrapped in directives (#158), renders as before.

Tests

Note: the triage on #183 points out that open PR #182 also touches TemplateRendering. The two edit different methods, and this branch rebases cleanly on current main.


Generated by Claude Code

…efuse a comment-only one [patch]

A property whose expression body, or expression accessor, wrote no expression was only refused when
the property itself was written, inside its class's body. The class had already been written up to
that member, and the scope's dispose closed it, leaving a well-formed class that silently lacked the
property and every member after it (#183). Validate now takes the CodeBlocker it is checking for, and
a property's validation renders its expressions, so ClassTemplate, AddClasses and AddSourceFile refuse
it before writing anything.

The empty-expression check also only looked for blank lines, so a body that wrote only a line
comment or only preprocessor directives rendered as "=> ;" (CS1525) (#184). It now requires a line
with code, through the existing HasCode helper.

Fixes #183
Fixes #184

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

sonarqubecloud Bot commented Nov 5, 2026

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants