Repository navigation
Check a class's members before writing any of it [patch] - #169
Merged
matt-edmondson merged 2 commits intoNov 4, 2026
Merged
Conversation
ClassTemplate wrote its members one at a time inside a Scope, so a member that failed its check threw mid-body, and the Scope still wrote the closing brace on the way out. The buffer was left with a class that parses cleanly but silently lacks the failing member, everything sorted after it, and every nested class. AddSourceFile and AddClasses did the same one level up. Each template's up-front checks now live in an internal Validate(). ClassTemplate validates its kind, its members and its nested classes, recursively, before writing anything, and AddSourceFile and AddClasses validate every class before writing the header or the first type. The exception still names the offending member. Fixes #164 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017vfJW3PT52Zo3M7fFP8Z6J
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017vfJW3PT52Zo3M7fFP8Z6J
|
matt-edmondson
deleted the
claude/issue-164-validate-class-before-writing
branch
November 4, 2026 00:55
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 #164
Summary
ClassTemplate.WriteToopened the body withusing Scope scope = new(codeBlocker);and then wrote its members one at a time. When a member failed its own check, for example a property with no accessors, theScopestill wrote}as the exception unwound. The result was a class that parses cleanly but is silently missing three things:AddSourceFileandAddClassesdid the same one level up.Changes
TemplateBasegets aninternal virtual void Validate(), a no-op by default, matching the existingAccessorTemplate.Validate.PropertyTemplateandOperatorTemplatemove their existing up-front checks intoValidate(), andWriteTostill calls it first. The messages are unchanged and still name the member.ClassTemplate.Validate()checks its own kind, then callsValidate()on every member and every nested class, recursively.WriteTocalls it before writing anything.AddSourceFilevalidates every class before writing the file header.AddClassesvalidates every class before writing the first one.Exception types are unchanged:
InvalidOperationExceptionfor an invalid member or operator kind, andArgumentOutOfRangeExceptionfor an unknown type kind.The checks run before writing starts, so an exception thrown by a user
BodyFactorywhile writing can still leave partial output. That is outside what this issue covers.Tests
mainand pass with the fix (checked by reverting the library and running them again):'P'AddSourceFilewith an invalid class writes nothingAddClasseswith an invalid class writes nothingGenerated by Claude Code