Permalink
Browse files

Issue #3796: No unnecessary text shall be present in Javadoc ASTs

  • Loading branch information...
ps-sp authored and romani committed Jun 16, 2017
1 parent d1ecad1 commit 3744f4c56845e82bd85f4b92f07b06d0de2ab1f2
@@ -19,9 +19,8 @@
<!-- Javadoc files should not have new line by design (on purpose). -->
<suppress checks="NewlineAtEndOfFile" files="[\\/]test[\\/].*[\\/]grammars[\\/]javadoc[\\/]htmlTags[\\/].*"/>
<suppress checks="NewlineAtEndOfFile" files="[\\/]test[\\/].*[\\/]grammars[\\/]javadoc[\\/]javadocTags[\\/].*"/>
<suppress checks="NewlineAtEndOfFile" files="[\\/]test[\\/].*[\\/]astprinter[\\/]InputJavadocComment\.javadoc"/>
<suppress checks="NewlineAtEndOfFile" files="[\\/]test[\\/].*[\\/]astprinter[\\/]InputJavadocWithError\.javadoc"/>
<suppress checks="NewlineAtEndOfFile" files="[\\/]test[\\/].*[\\/]astprinter[\\/].*\.javadoc"/>
<!--Empty file cannot have any new line at the end. -->
<suppress checks="NewlineAtEndOfFile" files="[\\/]test[\\/].*[\\/]checks[\\/]InputEmptyFile\.txt"/>
<suppress checks="NewlineAtEndOfFile" files="[\\/]test[\\/].*[\\/]grammars[\\/]InputRegressionEmptyAst\.txt"/>
@@ -301,7 +301,13 @@ private JavadocNodeImpl createRootJavadocNode(ParseTree parseTreeNode) {
*/
private JavadocNodeImpl createJavadocNode(ParseTree parseTree, DetailNode parent, int index) {
final JavadocNodeImpl node = new JavadocNodeImpl();
node.setText(parseTree.getText());
if (parseTree.getChildCount() == 0
|| "Text".equals(getNodeClassNameWithoutContext(parseTree))) {
node.setText(parseTree.getText());
}
else {
node.setText(getFormattedNodeClassNameWithoutContext(parseTree));
}
node.setColumnNumber(getColumn(parseTree));
node.setLineNumber(getLine(parseTree) + blockCommentLineNumber);
node.setIndex(index);
@@ -410,6 +416,18 @@ private static int getTokenType(ParseTree node) {
return tokenType;
}
/**
* Gets class name of ParseTree node and removes 'Context' postfix at the
* end and formats it.
* @param node {@code ParseTree} node whose class name is to be formatted and returned
* @return uppercased class name without the word 'Context' and with appropriately
* inserted underscores
*/
private static String getFormattedNodeClassNameWithoutContext(ParseTree node) {
final String classNameWithoutContext = getNodeClassNameWithoutContext(node);
return CaseFormat.UPPER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE, classNameWithoutContext);
}
/**
* Gets class name of ParseTree node and removes 'Context' postfix at the
* end.
@@ -199,7 +199,13 @@ private static String getFirstSentence(DetailNode ast) {
final StringBuilder result = new StringBuilder();
final String periodSuffix = PERIOD + ' ';
for (DetailNode child : ast.getChildren()) {
final String text = child.getText();
final String text;
if (child.getChildren().length == 0) {
text = child.getText();
}
else {
text = getFirstSentence(child);
}
if (child.getType() != JavadocTokenTypes.JAVADOC_INLINE_TAG
&& text.contains(periodSuffix)) {
@@ -70,4 +70,14 @@ public void testParseFileWithError() throws Exception {
}
}
@Test
public void testNoUnnecessaryTextinJavadocAst() throws Exception {
final String actual = DetailNodeTreeStringPrinter.printFileAst(
new File(getPath("InputNoUnnecessaryTextInJavadocAst.javadoc")))
.replaceAll("\\\\r\\\\n", "\\\\n");
final String expected = new String(Files.readAllBytes(Paths.get(
getPath("expectedNoUnnecessaryTextInJavadocAst.txt"))), StandardCharsets.UTF_8)
.replaceAll("\\\\r\\\\n", "\\\\n");
Assert.assertEquals(expected, actual);
}
}
@@ -276,7 +276,7 @@ static void clearCounter() {
@Override
public void visitJavadocToken(DetailNode ast) {
Assert.assertEquals(ast.toString(), "Javadoc<EOF>", ast.getText());
Assert.assertEquals(ast.toString(), "JAVADOC", ast.getText());
javadocsNumber++;
}
}
@@ -253,8 +253,7 @@ public void testGetValueAtDetailNode() {
final int line = (int) parseTree.getValueAt(child, 2);
final int column = (int) parseTree.getValueAt(child, 3);
final String text = (String) parseTree.getValueAt(child, 4);
final String expectedText = String.join("", System.lineSeparator(),
"* class javadoc", System.lineSeparator(), "<EOF>");
final String expectedText = "JAVADOC";
Assert.assertNull(treeModel);
Assert.assertEquals("JAVADOC", type);
@@ -0,0 +1,4 @@
* <p>
* My {@code Class}
* </p>
* @see something
@@ -2,7 +2,7 @@ CLASS_DEF -> CLASS_DEF [4:0]
|--MODIFIERS -> MODIFIERS [4:0]
|--BLOCK_COMMENT_BEGIN -> /* [1:0]
| |--COMMENT_CONTENT -> *\n* class javadoc\n [1:2]
| | `--JAVADOC -> \n* class javadoc\n<EOF> [1:3]
| | `--JAVADOC -> JAVADOC [1:3]
| | |--NEWLINE -> \n [1:3]
| | |--LEADING_ASTERISK -> * [2:0]
| | |--TEXT -> class javadoc [2:1]
@@ -18,7 +18,7 @@ CLASS_DEF -> CLASS_DEF [4:0]
| |--TYPE -> TYPE [7:4]
| | |--BLOCK_COMMENT_BEGIN -> /* [6:4]
| | | |--COMMENT_CONTENT -> * attribute javadoc [6:6]
| | | | `--JAVADOC -> attribute javadoc<EOF> [6:7]
| | | | `--JAVADOC -> JAVADOC [6:7]
| | | | |--TEXT -> attribute javadoc [6:7]
| | | | `--EOF -> <EOF> [6:25]
| | | `--BLOCK_COMMENT_END -> */ [6:24]
@@ -29,7 +29,7 @@ CLASS_DEF -> CLASS_DEF [4:0]
| |--MODIFIERS -> MODIFIERS [12:4]
| | |--BLOCK_COMMENT_BEGIN -> /* [9:4]
| | | |--COMMENT_CONTENT -> *\n * method javadoc\n [9:6]
| | | | `--JAVADOC -> \n * method javadoc\n <EOF> [9:7]
| | | | `--JAVADOC -> JAVADOC [9:7]
| | | | |--NEWLINE -> \n [9:7]
| | | | |--LEADING_ASTERISK -> * [10:0]
| | | | |--TEXT -> method javadoc [10:5]
@@ -2,7 +2,7 @@ CLASS_DEF -> CLASS_DEF [4:0]
|--MODIFIERS -> MODIFIERS [4:0]
| |--BLOCK_COMMENT_BEGIN -> /* [1:0]
| | |--COMMENT_CONTENT -> *\n* some text\n [1:2]
| | | `--JAVADOC -> \n* some text\n<EOF> [1:3]
| | | `--JAVADOC -> JAVADOC [1:3]
| | | |--NEWLINE -> \n [1:3]
| | | |--LEADING_ASTERISK -> * [2:0]
| | | |--TEXT -> some text [2:1]
@@ -12,7 +12,7 @@ CLASS_DEF -> CLASS_DEF [4:0]
|--MODIFIERS -> MODIFIERS [4:0]
|--BLOCK_COMMENT_BEGIN -> /* [3:0]
| |--COMMENT_CONTENT -> *javadoc [3:2]
| | `--JAVADOC -> javadoc<EOF> [3:3]
| | `--JAVADOC -> JAVADOC [3:3]
| | |--TEXT -> javadoc [3:3]
| | `--EOF -> <EOF> [3:10]
| `--BLOCK_COMMENT_END -> */ [3:9]
@@ -1,28 +1,28 @@
JAVADOC -> <p>\r\nMy {@code Class}\r\n</p>\r\n@see never<EOF> [0:0]
|--HTML_ELEMENT -> <p>\r\nMy {@code Class}\r\n</p> [0:0]
| `--PARAGRAPH -> <p>\r\nMy {@code Class}\r\n</p> [0:0]
| |--P_TAG_OPEN -> <p> [0:0]
JAVADOC -> JAVADOC [0:0]
|--HTML_ELEMENT -> HTML_ELEMENT [0:0]
| `--PARAGRAPH -> PARAGRAPH [0:0]
| |--P_TAG_OPEN -> P_TAG_OPEN [0:0]
| | |--OPEN -> < [0:0]
| | |--P_HTML_TAG_NAME -> p [0:1]
| | `--CLOSE -> > [0:2]
| |--NEWLINE -> \r\n [0:3]
| |--TEXT -> My [1:0]
| |--JAVADOC_INLINE_TAG -> {@code Class} [1:3]
| |--JAVADOC_INLINE_TAG -> JAVADOC_INLINE_TAG [1:3]
| | |--JAVADOC_INLINE_TAG_START -> { [1:3]
| | |--CODE_LITERAL -> @code [1:4]
| | |--WS -> [1:9]
| | |--TEXT -> Class [1:10]
| | `--JAVADOC_INLINE_TAG_END -> } [1:16]
| |--NEWLINE -> \r\n [1:17]
| `--P_TAG_CLOSE -> </p> [2:0]
| `--P_TAG_CLOSE -> P_TAG_CLOSE [2:0]
| |--OPEN -> < [2:0]
| |--SLASH -> / [2:1]
| |--P_HTML_TAG_NAME -> p [2:2]
| `--CLOSE -> > [2:3]
|--NEWLINE -> \r\n [2:4]
|--JAVADOC_TAG -> @see never [3:0]
|--JAVADOC_TAG -> JAVADOC_TAG [3:0]
| |--SEE_LITERAL -> @see [3:0]
| |--WS -> [3:4]
| `--REFERENCE -> never [3:5]
| `--REFERENCE -> REFERENCE [3:5]
| `--PACKAGE -> never [3:5]
`--EOF -> <EOF> [3:10]
@@ -0,0 +1,35 @@
JAVADOC -> JAVADOC [0:0]
|--LEADING_ASTERISK -> * [0:0]
|--TEXT -> [0:1]
|--HTML_ELEMENT -> HTML_ELEMENT [0:2]
| `--PARAGRAPH -> PARAGRAPH [0:2]
| |--P_TAG_OPEN -> P_TAG_OPEN [0:2]
| | |--OPEN -> < [0:2]
| | |--P_HTML_TAG_NAME -> p [0:3]
| | `--CLOSE -> > [0:4]
| |--NEWLINE -> \r\n [0:5]
| |--LEADING_ASTERISK -> * [1:0]
| |--TEXT -> My [1:1]
| |--JAVADOC_INLINE_TAG -> JAVADOC_INLINE_TAG [1:5]
| | |--JAVADOC_INLINE_TAG_START -> { [1:5]
| | |--CODE_LITERAL -> @code [1:6]
| | |--WS -> [1:11]
| | |--TEXT -> Class [1:12]
| | `--JAVADOC_INLINE_TAG_END -> } [1:18]
| |--NEWLINE -> \r\n [1:19]
| |--LEADING_ASTERISK -> * [2:0]
| |--TEXT -> [2:1]
| `--P_TAG_CLOSE -> P_TAG_CLOSE [2:2]
| |--OPEN -> < [2:2]
| |--SLASH -> / [2:3]
| |--P_HTML_TAG_NAME -> p [2:4]
| `--CLOSE -> > [2:5]
|--NEWLINE -> \r\n [2:6]
|--LEADING_ASTERISK -> * [3:0]
|--WS -> [3:1]
|--JAVADOC_TAG -> JAVADOC_TAG [3:2]
| |--SEE_LITERAL -> @see [3:2]
| |--WS -> [3:6]
| `--REFERENCE -> REFERENCE [3:7]
| `--PACKAGE -> something [3:7]
`--EOF -> <EOF> [3:16]
Oops, something went wrong.

0 comments on commit 3744f4c

Please sign in to comment.