Skip to content

Commit

Permalink
Don't format javadoc with unmatched braces or tags
Browse files Browse the repository at this point in the history
MOE_MIGRATED_REVID=126211153
  • Loading branch information
cushon committed Jul 8, 2016
1 parent c9ec4f7 commit 6720f6d
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 5 deletions.
Expand Up @@ -20,8 +20,10 @@
import static java.util.regex.Pattern.CASE_INSENSITIVE;
import static java.util.regex.Pattern.compile;

import com.google.common.collect.ImmutableList;
import com.google.googlejavaformat.java.JavaCommentsHelper;
import com.google.googlejavaformat.java.JavaFormatterOptions;
import com.google.googlejavaformat.java.javadoc.JavadocLexer.LexException;

import java.util.List;
import java.util.regex.Matcher;
Expand All @@ -41,7 +43,13 @@ public final class JavadocFormatter {
* start and end with the same characters.
*/
public static String formatJavadoc(String input, int blockIndent, JavaFormatterOptions options) {
String result = render(lex(input), blockIndent, options);
ImmutableList<Token> tokens;
try {
tokens = lex(input);
} catch (LexException e) {
return input;
}
String result = render(tokens, blockIndent, options);
return makeSingleLineIfPossible(blockIndent, result, options);
}

Expand Down
Expand Up @@ -59,7 +59,7 @@
/** Lexer for the Javadoc formatter. */
final class JavadocLexer {
/** Takes a Javadoc comment, including ∕✱✱ and ✱∕, and returns tokens, including ∕✱✱ and ✱∕. */
static ImmutableList<Token> lex(String input) {
static ImmutableList<Token> lex(String input) throws LexException {
/*
* TODO(cpovirk): In theory, we should interpret Unicode escapes (yet output them in their
* original form). This would mean mean everything from an encoded ∕✱✱ to an encoded <pre> tag,
Expand Down Expand Up @@ -88,7 +88,7 @@ private JavadocLexer(CharStream input) {
this.input = checkNotNull(input);
}

private ImmutableList<Token> generateTokens() {
private ImmutableList<Token> generateTokens() throws LexException {
ImmutableList.Builder<Token> tokens = ImmutableList.builder();

Token token = new Token(BEGIN_JAVADOC, "/**");
Expand All @@ -99,6 +99,10 @@ private ImmutableList<Token> generateTokens() {
tokens.add(token);
}

if (braceDepth.isPositive() || preDepth.isPositive() || tableDepth.isPositive()) {
throw new LexException();
}

token = new Token(END_JAVADOC, "*/");
tokens.add(token);

Expand All @@ -109,13 +113,13 @@ private ImmutableList<Token> generateTokens() {
return result;
}

private Token readToken() {
private Token readToken() throws LexException {
Type type = consumeToken();
String value = input.readAndResetRecorded();
return new Token(type, value);
}

private Type consumeToken() {
private Type consumeToken() throws LexException {
boolean preserveExistingFormatting = preDepth.isPositive() || tableDepth.isPositive();

if (input.tryConsumeRegex(NEWLINE_PATTERN)) {
Expand All @@ -134,6 +138,9 @@ private Type consumeToken() {
* https://github.com/google/google-java-format/issues/7#issuecomment-197383926
*/
if (!somethingSinceNewline && input.tryConsumeRegex(FOOTER_TAG_PATTERN)) {
if (braceDepth.isPositive() || preDepth.isPositive() || tableDepth.isPositive()) {
throw new LexException();
}
somethingSinceNewline = true;
return FOOTER_JAVADOC_TAG_START;
}
Expand Down Expand Up @@ -436,4 +443,6 @@ boolean isPositive() {
return value > 0;
}
}

static class LexException extends Exception {}
}
@@ -0,0 +1,13 @@
class B29618429 {
/**
* Hello
*
* <p>World
* <pre>
* @LooksLikeATag(
* foo = "bar"
* )
* </pre>
*/
int x;
}
@@ -0,0 +1,13 @@
class B29618429 {
/**
* Hello
*
* <p>World
* <pre>
* @LooksLikeATag(
* foo = "bar"
* )
* </pre>
*/
int x;
}
@@ -0,0 +1,8 @@
class B29705613 {
/**
* This tag isn't closed: {@link Foo
*
* <p>A paragraph
*/
int x;
}
@@ -0,0 +1,8 @@
class B29705613 {
/**
* This tag isn't closed: {@link Foo
*
* <p>A paragraph
*/
int x;
}

0 comments on commit 6720f6d

Please sign in to comment.