Skip to content

Commit 93bb96d

Browse files
cushongoogle-java-format Team
authored andcommitted
Add tests demonstrating unclosed Javadoc inline tags leaving comments unformatted.
In classic Javadoc comments, an unclosed inline tag outside `<pre>` (for example, an unclosed `{@link ...` lacking a closing `}`) causes lexing to fail with a `LexException` in `checkMatchingTags()`. Because `JavadocFormatter` catches `LexException` and returns the input string unmodified, the comment is left completely unformatted. Add tests reproducing this behavior with unclosed inline tags before subsequent list items, blank lines, footer tags, and at the end of the comment. PiperOrigin-RevId: 1009630980
1 parent 2980bad commit 93bb96d

1 file changed

Lines changed: 94 additions & 0 deletions

File tree

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

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2622,6 +2622,100 @@ class Test {}
26222622
doFormatTest(input, expected);
26232623
}
26242624

2625+
@Test
2626+
public void unclosedInlineTagInList() {
2627+
String input =
2628+
"""
2629+
/**
2630+
* <ul>
2631+
* <li>{@link #validating() sets (allowInvalid=false, mayDissolve=false, snapFunction=null): Do not modify the input geometry. Validate it instead.
2632+
* <li>3. Check if the input geometry is valid as is.
2633+
* </ul>
2634+
*/
2635+
class Test {}
2636+
""";
2637+
// TODO(b/407787200): Lexing fails due to unclosed inline tag, leaving comment unformatted.
2638+
String expected =
2639+
"""
2640+
/**
2641+
* <ul>
2642+
* <li>{@link #validating() sets (allowInvalid=false, mayDissolve=false, snapFunction=null): Do not modify the input geometry. Validate it instead.
2643+
* <li>3. Check if the input geometry is valid as is.
2644+
* </ul>
2645+
*/
2646+
class Test {}
2647+
""";
2648+
doFormatTest(input, expected);
2649+
}
2650+
2651+
@Test
2652+
public void unclosedInlineTagAtEndOfComment() {
2653+
String input =
2654+
"""
2655+
/**
2656+
* Return the child position of this cell within its parent. For more details, see {@link com.google.common.geometry.S2CellId
2657+
*/
2658+
class Test {}
2659+
""";
2660+
// TODO(b/407787200): Lexing fails due to unclosed inline tag, leaving comment unformatted.
2661+
String expected =
2662+
"""
2663+
/**
2664+
* Return the child position of this cell within its parent. For more details, see {@link com.google.common.geometry.S2CellId
2665+
*/
2666+
class Test {}
2667+
""";
2668+
doFormatTest(input, expected);
2669+
}
2670+
2671+
@Test
2672+
public void unclosedInlineTagBeforeBlankLine() {
2673+
String input =
2674+
"""
2675+
/**
2676+
* Some description with {@link #validating() sets (allowInvalid=false, mayDissolve=false, snapFunction=null): Do not modify the input geometry.
2677+
*
2678+
* <p>Next paragraph that is also long enough to exceed the line length limit and require line wrapping.
2679+
*/
2680+
class Test {}
2681+
""";
2682+
// TODO(b/407787200): Lexing fails due to unclosed inline tag, leaving comment unformatted.
2683+
String expected =
2684+
"""
2685+
/**
2686+
* Some description with {@link #validating() sets (allowInvalid=false, mayDissolve=false, snapFunction=null): Do not modify the input geometry.
2687+
*
2688+
* <p>Next paragraph that is also long enough to exceed the line length limit and require line wrapping.
2689+
*/
2690+
class Test {}
2691+
""";
2692+
doFormatTest(input, expected);
2693+
}
2694+
2695+
@Test
2696+
public void unclosedInlineTagBeforeFooterTag() {
2697+
String input =
2698+
"""
2699+
/**
2700+
* Some description with {@link #validating() sets (allowInvalid=false, mayDissolve=false, snapFunction=null): Do not modify the input geometry.
2701+
*
2702+
* @param x a parameter description that is also long enough to exceed the line length limit and require line wrapping
2703+
*/
2704+
class Test {}
2705+
""";
2706+
// TODO(b/407787200): Lexing fails due to unclosed inline tag, leaving comment unformatted.
2707+
String expected =
2708+
"""
2709+
/**
2710+
* Some description with {@link #validating() sets (allowInvalid=false, mayDissolve=false, snapFunction=null): Do not modify the input geometry.
2711+
*
2712+
* @param x a parameter description that is also long enough to exceed the line length limit and require line wrapping
2713+
*/
2714+
class Test {}
2715+
""";
2716+
doFormatTest(input, expected);
2717+
}
2718+
26252719
// TODO: b/346668798 - Test the following Markdown constructs, and make the tests work as needed.
26262720
// We can assume that the CommonMark parser correctly handles Markdown, so the question is whether
26272721
// they are subsequently mishandled by our formatting logic. So for example the CommonMark parser

0 commit comments

Comments
 (0)