Skip to content

Check a template's doc comment before writing the class that holds it [patch] - #193

Open
matt-edmondson wants to merge 1 commit into
mainfrom
fix/validate-doc-comments-188
Open

matt-edmondson wants to merge 1 commit into
mainfrom
fix/validate-doc-comments-188

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #188

What was wrong

ClassTemplate.Validate exists to refuse a bad class before writing any of it (#169). It checked the kind, the members and the nested classes, but never the doc comments. The doc-comment attribute checks (a line break in a param/typeparam name, an exception/seealso cref, or an inheritdoc cref) ran only inside DocComment.WriteTo. So AddClass wrote public class C { void A() { } } and then threw on member B. AddSourceFile wrote the namespace, the usings and all of class First, then threw on the second class.

Change

  • DocComment.EnsureAttributesFitOnOneLine is now internal void ValidateAttributes(). WriteTo still calls it, so a standalone AddDocumentation behaves as before.
  • TemplateBase.Validate now runs Documentation?.ValidateAttributes(). The ClassTemplate, PropertyTemplate and OperatorTemplate overrides call base.Validate() first. That makes the existing up-front validation cover the class itself, every member and every nested class, and through SourceFileTemplate every class in the file.
  • No public API change.

Tests

  • AClassWithAMemberWhoseDocCommentCannotBeWrittenWritesNothing: the issue's first case. B's param name is "a\nb", AddClass throws ArgumentException, and the CodeBlocker keeps only what it held before.
  • ASourceFileWhoseLaterClassHasADocCommentThatCannotBeWrittenWritesNothing: the issue's second case (SeeAlso "A\nB" on the second class). Nothing is written.
  • ANestedClassWhoseDocCommentCannotBeWrittenLeavesTheOuterClassUnwritten: an inheritdoc cref with a line break on a nested class.
  • With the Templates/ changes reverted, all three new tests fail.
  • Full suite on Linux / .NET 10: 332/332 passed.

This PR and #192 (for #190) both branch from main and touch different lines of PropertyTemplate.Validate, so they merge in either order.


Generated by Claude Code

… [patch]

The doc-comment attribute checks ran only inside DocComment.WriteTo, so a
member, nested class or later class with a line break in a param name,
seealso cref or inheritdoc cref threw after the class had been partly
written. TemplateBase.Validate now runs those checks for the template's own
Documentation, and every Validate override calls it, so ClassTemplate and
SourceFileTemplate refuse it before writing anything.

Fixes #188

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017vfJW5kkYGVswnWUL9YgPE
@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