Skip to content

Commit 962ceca

Browse files
content of code tag is parsed outside (#4626)
The content of the @code tag is being parsed outside the code tag Fix: #4598
1 parent e03b6a4 commit 962ceca

File tree

2 files changed

+137
-15
lines changed

2 files changed

+137
-15
lines changed

org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/parser/AbstractCommentParser.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,7 @@ protected boolean commentParse() {
365365
considerTagAsPlainText = false; // re-enable tag validation
366366
}
367367
}
368+
boolean isLiteralOrCode = this.tagValue == TAG_LITERAL_VALUE || this.tagValue == TAG_CODE_VALUE;
368369
if (this.inlineTagStarted) {
369370
textEndPosition = this.index - 1;
370371
boolean treatAsText= considerTagAsPlainText || (this.inlineReturn && this.inlineReturnOpenBraces > 0);
@@ -377,8 +378,12 @@ protected boolean commentParse() {
377378
}
378379
if (!isFormatterParser && !treatAsText && (!this.inlineReturn || this.inlineReturnOpenBraces <= 0))
379380
this.textStart = this.index;
380-
if ((!isTagElementClose && this.markdown) || !this.markdown) { //The comment parser should create a TagElement only if the previous one is closed - markdown.
381+
if (!isTagElementClose && this.markdown) { //The comment parser should create a TagElement only if the previous one is closed - markdown.
381382
setInlineTagStarted(false);
383+
} else if (!this.markdown) {
384+
if (!(isLiteralOrCode && openingBraces != 0)) {
385+
setInlineTagStarted(false);
386+
}
382387
}
383388
if (this.inlineReturn) {
384389
if (this.inlineReturnOpenBraces > 0) {

org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverterJavadocTest.java

Lines changed: 131 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3404,19 +3404,16 @@ public void testBug206345a() throws JavaModelException {
34043404
assumeEquals(this.prefix+"Wrong number of tags", 1, docComment.tags().size());
34053405
TagElement tagElement = (TagElement) docComment.tags().get(0);
34063406
assumeNull(this.prefix+"Wrong type of tag ["+tagElement+"]", tagElement.getTagName());
3407-
assumeEquals(this.prefix+"Wrong number of fragments in tag ["+tagElement+"]", 3, tagElement.fragments().size());
3407+
assumeEquals(this.prefix+"Wrong number of fragments in tag ["+tagElement+"]", 2, tagElement.fragments().size());
34083408
ASTNode fragment = (ASTNode) tagElement.fragments().get(0);
34093409
assumeEquals(this.prefix+"Invalid type for fragment ["+fragment+"]", ASTNode.TEXT_ELEMENT, fragment.getNodeType());
34103410
fragment = (ASTNode) tagElement.fragments().get(1);
34113411
assumeEquals(this.prefix+"Invalid type for fragment ["+fragment+"]", ASTNode.TAG_ELEMENT, fragment.getNodeType());
34123412
TagElement inlineTag = (TagElement) fragment;
3413-
assumeEquals(this.prefix+"Wrong number of fragments in tag ["+inlineTag+"]", 1, inlineTag.fragments().size());
3414-
fragment = (ASTNode) inlineTag.fragments().get(0);
3415-
assumeEquals(this.prefix+"Invalid type for fragment ["+fragment+"]", ASTNode.TEXT_ELEMENT, fragment.getNodeType());
3416-
fragment = (ASTNode) tagElement.fragments().get(2);
3417-
assumeEquals(this.prefix+"Invalid type for fragment ["+fragment+"]", ASTNode.TEXT_ELEMENT, fragment.getNodeType());
3418-
TextElement textElement = (TextElement) fragment;
3419-
assumeEquals(this.prefix+"Invalid content for text element ", "{@link BadLink} is just text}", textElement.getText());
3413+
assumeEquals(this.prefix+"Wrong number of fragments in tag ["+inlineTag+"]", 2, inlineTag.fragments().size());
3414+
ASTNode fragment1 = (ASTNode) inlineTag.fragments().get(0);
3415+
assumeEquals(this.prefix+"Invalid type for fragment ["+fragment1+"]", ASTNode.TEXT_ELEMENT, fragment1.getNodeType());
3416+
assumeEquals(this.prefix+"Invalid content for text element ", "{@literal raw text:{@link BadLink} is just text}", fragment.toString());
34203417
}
34213418
}
34223419
/**
@@ -3451,19 +3448,139 @@ public void testBug206345b() throws JavaModelException {
34513448
assumeEquals(this.prefix+"Wrong number of tags", 1, docComment.tags().size());
34523449
TagElement tagElement = (TagElement) docComment.tags().get(0);
34533450
assumeNull(this.prefix+"Wrong type of tag ["+tagElement+"]", tagElement.getTagName());
3454-
assumeEquals(this.prefix+"Wrong number of fragments in tag ["+tagElement+"]", 3, tagElement.fragments().size());
3451+
assumeEquals(this.prefix+"Wrong number of fragments in tag ["+tagElement+"]", 2, tagElement.fragments().size());
34553452
ASTNode fragment = (ASTNode) tagElement.fragments().get(0);
34563453
assumeEquals(this.prefix+"Invalid type for fragment ["+fragment+"]", ASTNode.TEXT_ELEMENT, fragment.getNodeType());
34573454
fragment = (ASTNode) tagElement.fragments().get(1);
34583455
assumeEquals(this.prefix+"Invalid type for fragment ["+fragment+"]", ASTNode.TAG_ELEMENT, fragment.getNodeType());
34593456
TagElement inlineTag = (TagElement) fragment;
3460-
assumeEquals(this.prefix+"Wrong number of fragments in tag ["+inlineTag+"]", 1, inlineTag.fragments().size());
3457+
assumeEquals(this.prefix+"Wrong number of fragments in tag ["+inlineTag+"]", 2, inlineTag.fragments().size());
34613458
fragment = (ASTNode) inlineTag.fragments().get(0);
34623459
assumeEquals(this.prefix+"Invalid type for fragment ["+fragment+"]", ASTNode.TEXT_ELEMENT, fragment.getNodeType());
3463-
fragment = (ASTNode) tagElement.fragments().get(2);
3464-
assumeEquals(this.prefix+"Invalid type for fragment ["+fragment+"]", ASTNode.TEXT_ELEMENT, fragment.getNodeType());
3465-
TextElement textElement = (TextElement) fragment;
3466-
assumeEquals(this.prefix+"Invalid content for text element ", "{@link BadLink} is just text}", textElement.getText());
3460+
assumeEquals(this.prefix+"Invalid content for text element ", "{@link BadLink} is just text", inlineTag.fragments().get(1).toString());
34673461
}
34683462
}
3463+
3464+
public void testContentOfCodeParsedOutside4615_01() throws JavaModelException {
3465+
this.workingCopies = new ICompilationUnit[1];
3466+
this.astLevel = AST.JLS25;
3467+
this.workingCopies[0] = getWorkingCopy("/Converter25/src/javadoc/X.java",
3468+
"""
3469+
/**
3470+
* {@code public class Example { final int a = 1; } }
3471+
*/
3472+
public class X {}
3473+
"""
3474+
);
3475+
CompilationUnit compilUnit = (CompilationUnit) runConversion(this.workingCopies[0], true);
3476+
List unitComments = compilUnit.getCommentList();
3477+
assertEquals("Wrong number of comments", 1, unitComments.size());
3478+
Comment comment = (Comment) unitComments.get(0);
3479+
assertEquals("Comment should be javadoc", comment.getNodeType(), ASTNode.JAVADOC);
3480+
Javadoc docComment = (Javadoc) compilUnit.getCommentList().get(0);
3481+
assumeEquals("Wrong number of tags", 1, docComment.tags().size());
3482+
TagElement tagElement = (TagElement) docComment.tags().get(0);
3483+
List<TagElement> listFrag = tagElement.fragments();
3484+
assumeEquals("wrong number of tags", 1, listFrag.size());
3485+
}
3486+
3487+
public void testContentOfCodeParsedOutside4615_02() throws JavaModelException {
3488+
this.workingCopies = new ICompilationUnit[1];
3489+
this.astLevel = AST.JLS25;
3490+
this.workingCopies[0] = getWorkingCopy("/Converter25/src/javadoc/X.java",
3491+
"""
3492+
/**
3493+
* {@code com/{filename:\\w+}.jsp}
3494+
*/
3495+
public class X {}
3496+
"""
3497+
);
3498+
CompilationUnit compilUnit = (CompilationUnit) runConversion(this.workingCopies[0], true);
3499+
List unitComments = compilUnit.getCommentList();
3500+
assertEquals("Wrong number of comments", 1, unitComments.size());
3501+
Comment comment = (Comment) unitComments.get(0);
3502+
assertEquals("Comment should be javadoc", comment.getNodeType(), ASTNode.JAVADOC);
3503+
Javadoc docComment = (Javadoc) compilUnit.getCommentList().get(0);
3504+
assumeEquals("wrong number of tags", 1, docComment.tags().size());
3505+
TagElement parentTag = (TagElement) docComment.tags().get(0);
3506+
List<?> frags = parentTag.fragments();
3507+
assumeEquals("wrong number of frags", 1, frags.size());
3508+
TagElement problematicTag = (TagElement) frags.get(0);
3509+
assumeEquals("invalid content", "{@code com/{filename:\\w+}.jsp}", problematicTag.toString());
3510+
}
3511+
3512+
public void testContentOfCodeParsedOutside4615_03() throws JavaModelException {
3513+
this.workingCopies = new ICompilationUnit[1];
3514+
this.astLevel = AST.JLS25;
3515+
this.workingCopies[0] = getWorkingCopy("/Converter25/src/javadoc/X.java",
3516+
"""
3517+
/**
3518+
* {@code public class X { void foo() { int x; } } }
3519+
*/
3520+
public class X {}
3521+
"""
3522+
);
3523+
CompilationUnit compilUnit = (CompilationUnit) runConversion(this.workingCopies[0], true);
3524+
List unitComments = compilUnit.getCommentList();
3525+
assertEquals("Wrong number of comments", 1, unitComments.size());
3526+
Comment comment = (Comment) unitComments.get(0);
3527+
assertEquals("Comment should be javadoc", comment.getNodeType(), ASTNode.JAVADOC);
3528+
Javadoc docComment = (Javadoc) compilUnit.getCommentList().get(0);
3529+
assumeEquals("Wrong number of tags", 1, docComment.tags().size());
3530+
TagElement tagElement = (TagElement) docComment.tags().get(0);
3531+
List<TagElement> listFrag = tagElement.fragments();
3532+
assumeEquals("wrong number of tags", 1, listFrag.size());
3533+
assumeEquals("Invalid content", "{@code public class X { void foo() { int x; } } }", listFrag.get(0).toString());
3534+
}
3535+
3536+
public void testContentOfCodeParsedOutside4615_04() throws JavaModelException {
3537+
this.workingCopies = new ICompilationUnit[1];
3538+
this.astLevel = AST.JLS25;
3539+
this.workingCopies[0] = getWorkingCopy("/Converter25/src/javadoc/X.java",
3540+
"""
3541+
/**
3542+
* {@code public class Example { final int sasi; } class B{}}
3543+
*/
3544+
public class X {}
3545+
"""
3546+
);
3547+
CompilationUnit compilUnit = (CompilationUnit) runConversion(this.workingCopies[0], true);
3548+
List unitComments = compilUnit.getCommentList();
3549+
assertEquals("Wrong number of comments", 1, unitComments.size());
3550+
Comment comment = (Comment) unitComments.get(0);
3551+
assertEquals("Comment should be javadoc", comment.getNodeType(), ASTNode.JAVADOC);
3552+
Javadoc docComment = (Javadoc) compilUnit.getCommentList().get(0);
3553+
assumeEquals("Wrong number of tags", 1, docComment.tags().size());
3554+
TagElement tagElement = (TagElement) docComment.tags().get(0);
3555+
List<TagElement> listFrag = tagElement.fragments();
3556+
assumeEquals("wrong number of tags", 1, listFrag.size());
3557+
assumeEquals("Invalid content", "{@code public class Example { final int sasi; } class B{}}", listFrag.get(0).toString());
3558+
}
3559+
3560+
//code tag in multiple lines
3561+
public void testContentOfCodeParsedOutside4615_05() throws JavaModelException {
3562+
this.workingCopies = new ICompilationUnit[1];
3563+
this.astLevel = AST.JLS25;
3564+
this.workingCopies[0] = getWorkingCopy("/Converter25/src/javadoc/X.java",
3565+
"""
3566+
/**
3567+
* {@code com/{filename:\\w+}
3568+
* .jsp}
3569+
*/
3570+
public class X {}
3571+
"""
3572+
);
3573+
CompilationUnit compilUnit = (CompilationUnit) runConversion(this.workingCopies[0], true);
3574+
List unitComments = compilUnit.getCommentList();
3575+
assertEquals("Wrong number of comments", 1, unitComments.size());
3576+
Comment comment = (Comment) unitComments.get(0);
3577+
assertEquals("Comment should be javadoc", comment.getNodeType(), ASTNode.JAVADOC);
3578+
Javadoc docComment = (Javadoc) compilUnit.getCommentList().get(0);
3579+
assumeEquals("wrong number of tags", 1, docComment.tags().size());
3580+
TagElement parentTag = (TagElement) docComment.tags().get(0);
3581+
List<?> frags = parentTag.fragments();
3582+
assumeEquals("wrong number of frags", 1, frags.size());
3583+
TagElement problematicTag = (TagElement) frags.get(0);
3584+
assumeEquals("invalid content", "{@code com/{filename:\\w+}.jsp}", problematicTag.toString());
3585+
}
34693586
}

0 commit comments

Comments
 (0)