Repository navigation
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
Open
matt-edmondson wants to merge 1 commit into
matt-edmondson wants to merge 1 commit into
Conversation
…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
|
This was referenced Nov 5, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



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.Validatenow takes theCodeBlockerit 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 sameInvalidOperationExceptionthatWriteTowould.ClassTemplate,AddClassesandAddSourceFilepass 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.PropertyTemplate.WriteTostill renders each expression once. Inside a class the callback now runs twice, once to validate and once to write. The remarks onValidatesay so.#184: a comment- or directive-only expression rendered
=> ;RenderExpressionnow 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
TemplateTests. They cover an empty or comment-only expression body and expression accessor inside a class (nothing written), the same throughAddSourceFile, and comment-only and directive-only bodies on a standalone property and accessor. A regression check confirms the An expression body whose last line ends in a // comment gets its semicolon inside the comment:public int P => x // note;does not compile (CS1002) #144 and An expression body containing #if/#else/#endif rendersint P => #if DEBUGand#endif;, which doesn't compile #158 shapes still render..sonarlint/sonar-local.props) reports nothing beyond the existingS2699findings.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 currentmain.Generated by Claude Code