Fix multi-line Javadoc dropped when file and host line endings differ (#97) - #176
Merged
Conversation
Javadoc content was split with Environment.NewLine, but JavaParser's getContent() preserves the line endings of the parsed file, which are independent of the host OS. When the two differ -- most commonly a Unix-line-ending file converted on Windows -- the content stayed a single unsplit line, the per-line regex never matched, and the comment was dropped from the output entirely. Single-line /** ... */ comments were unaffected because they contain no newline to split on, which is why the bug looked specific to multi-line Javadoc. Add SplitLines(), which normalizes before splitting, and use it for the three places that split text originating from the Java source. FormatAsBlockComment already did this by hand and now shares the helper. Also normalize the raw comment trivia paths so comment content adopts the generated file's line endings rather than leaking the Java file's, matching the existing Whitespace.NewLine convention. The remaining Environment.NewLine uses are all on the output side and stay as they are. Add tests for both \n and \r\n inputs. These also cover the Javadoc to <summary>/<param>/<returns>/<exception> conversion, which previously had no test coverage at all, since every other suite sets IncludeComments to false. Fixes #97 Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
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 #97.
The bug
CommentsHelper.ConvertDocCommentsplit Javadoc content onEnvironment.NewLine:JavaParser's
getContent()preserves the line endings of the parsed file, which are independent of the host OS. I confirmed this with a throwaway probe: CRLF source yields content with 3 CRs, LF-only source yields 0.So when the two disagree — most commonly a Unix-line-ending file converted on Windows, which is exactly what the reporter narrowed it down to — the content stays one unsplit line, the per-line regex never matches, and the Javadoc is dropped from the output entirely.
Single-line
/** ... */comments were unaffected because they contain no newline to split on. That's why the bug appeared specific to multi-line Javadoc.Verification
Rather than assume, I reproduced it: on unmodified
master, patching the split to"\r\n"(simulating a Windows host) makes the new tests fail with the Javadoc completely absent — no<summary>, no<param>. With the fix, all cases pass.The change
SplitLines(), which normalizes line endings before splitting, and applied it to the three places that split text originating from the Java source.FormatAsBlockCommentalready did this correctly by hand and now shares the helper.NormalizeLineEndings()on the raw comment-trivia paths so comment content adopts the generated file's line endings instead of leaking the Java file's, consistent with the existingWhitespace.NewLineconvention.Environment.NewLineuses are all on the output side — includingAdjustBlockCommentIndentation, which splits trivia we generate ourselves — and are correct as-is. Only the input-side splits were platform-sensitive.Tests
Two new theories in
CommentTests.cs, each run against both\nand\r\ninput.Worth flagging: there was previously no test anywhere asserting Javadoc →
<summary>/<param>/<returns>/<exception>output — every other suite setsIncludeComments = false, so that path was entirely uncovered. These tests close that gap as well.Full suite: 382/382 passing, 0 build warnings.
Out of scope
Two pre-existing issues I noticed and left alone, each confirmed to reproduce on
masterindependently of this change:NormalizeWhitespace()emits\r\nregardless of platform — this happens even for a file containing no comments.filemode ignored the package declaration and dropped the type for one of my test inputs.Happy to look into either separately.
🤖 Generated with Claude Code