Skip to content

Commit 2957b19

Browse files
cushongoogle-java-format Team
authored andcommitted
Strip trailing asterisks before */ in Javadoc comments.
Decorative closing delimiters such as `/** ... **/` previously had the trailing asterisk retained as comment body content, resulting in `/** ... * */`. PiperOrigin-RevId: 994303609
1 parent 1fe23b7 commit 2957b19

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

‎core/src/main/java/com/google/googlejavaformat/java/javadoc/JavadocFormatter.java‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,11 @@ private static boolean oneLineJavadoc(String line, int blockIndent, int maxLineL
214214
private static String stripJavadocBeginAndEnd(String input) {
215215
checkArgument(input.startsWith("/**"), "Missing /**: %s", input);
216216
checkArgument(input.endsWith("*/") && input.length() > 4, "Missing */: %s", input);
217-
return input.substring("/**".length(), input.length() - "*/".length());
217+
int end = input.length() - "*/".length();
218+
while (end > "/**".length() && input.charAt(end - 1) == '*') {
219+
end--;
220+
}
221+
return input.substring("/**".length(), end);
218222
}
219223

220224
/**

‎core/src/test/java/com/google/googlejavaformat/java/JavadocFormattingTest.java‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2519,6 +2519,21 @@ void method() {}
25192519
assertThat(noJavadocFormatter.formatSource(input)).isEqualTo(input);
25202520
}
25212521

2522+
@Test
2523+
public void trailingAsterisksBeforeClose() {
2524+
String input =
2525+
"""
2526+
/** Version information for all the QuotaKeys that we have data for **/
2527+
class Test {}
2528+
""";
2529+
String expected =
2530+
"""
2531+
/** Version information for all the QuotaKeys that we have data for */
2532+
class Test {}
2533+
""";
2534+
doFormatTest(input, expected);
2535+
}
2536+
25222537
// TODO: b/346668798 - Test the following Markdown constructs, and make the tests work as needed.
25232538
// We can assume that the CommonMark parser correctly handles Markdown, so the question is whether
25242539
// they are subsequently mishandled by our formatting logic. So for example the CommonMark parser

0 commit comments

Comments
 (0)