Skip to content

Fix crash on Javadoc comments before package/import (#143) - #155

Merged
paulirwin merged 1 commit into
masterfrom
fix/143-javadoc-package-using-comments
Aug 14, 2026
Merged

paulirwin merged 1 commit into
masterfrom
fix/143-javadoc-package-using-comments

Conversation

@paulirwin

Copy link
Copy Markdown
Owner

Fixes #143.

Problem

GetCommentInfo maps a JavadocComment to SyntaxKind.XmlComment, but that kind is not valid for SyntaxFactory.SyntaxTrivia, so any Javadoc attached to the compilation unit, the package declaration, or an import declaration crashed with System.ArgumentException: kind.

AddCommentsTrivias already special-cases XmlComment and converts it to a /// doc comment, but that isn't right here either — XML doc comments can't attach to a file header or a using directive, and doing so produces mangled output like <summary>foo</summary> using java.List;.

Fix

Added CreateNonMemberCommentTrivia, used by AddPackageComments and AddUsingComments, which renders Javadoc as a normalized block comment in those positions. FormatAsBlockComment strips the leading * from each line and trims blank leading/trailing lines, so the output is the clean form requested in the issue rather than the ragged raw content:

/*
 * Some comments
 */
using Java.Util;

Non-Javadoc comments keep their existing behavior.

Testing

Written TDD — the two new tests in CommentTests.cs (Javadoc before package, Javadoc before import) each reproduced the ArgumentException before the fix and assert the expected output after. Full suite: 258 passed.

SyntaxKind.XmlComment is not valid for SyntaxFactory.SyntaxTrivia, and
XML doc comments cannot attach to a file header or a using directive.
Render Javadoc as a normalized block comment in those positions instead.
@paulirwin
paulirwin enabled auto-merge (squash) August 14, 2026 19:58
@paulirwin
paulirwin merged commit f8cbc92 into master Aug 14, 2026
5 checks passed
@paulirwin
paulirwin deleted the fix/143-javadoc-package-using-comments branch August 14, 2026 20:00
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.

AddPackageContents/AddUsingComments Does Not Correctly Handle Handle XmlComment

1 participant