From 82ee02024cd2950bc3f1da2c691d575fd548f4e8 Mon Sep 17 00:00:00 2001 From: Jay Arthanareeswaran Date: Mon, 30 Sep 2024 21:32:14 +0530 Subject: [PATCH] Issue with code fence > 3 in markdown comment (#3006) https://github.com/eclipse-jdt/eclipse.jdt.core/pull/3006 --- .../parser/AbstractCommentParser.java | 11 ++- .../parser/IMarkdownCommentHelper.java | 3 +- .../regression/MarkdownCommentsTest.java | 97 +++++++++++++++++++ 3 files changed, 107 insertions(+), 4 deletions(-) diff --git a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/parser/AbstractCommentParser.java b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/parser/AbstractCommentParser.java index 1e102e5a908..f965e62a608 100644 --- a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/parser/AbstractCommentParser.java +++ b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/parser/AbstractCommentParser.java @@ -180,9 +180,15 @@ protected boolean commentParse() { int lastStarPosition = -1; // Init scanner position + this.markdown = this.source[this.javadocStart + 1] == '/'; this.linePtr = getLineNumber(this.firstTagPosition); - int realStart = this.linePtr==1 ? this.javadocStart : this.scanner.getLineEnd(this.linePtr-1)+1; - if (realStart < this.javadocStart) realStart = this.javadocStart; + int realStart = this.javadocStart; + if (!this.markdown) { + realStart = this.linePtr==1 ? this.javadocStart : this.scanner.getLineEnd(this.linePtr-1)+1; + if (realStart < this.javadocStart) realStart = this.javadocStart; + } else { + this.linePtr = getLineNumber(realStart); + } this.scanner.resetTo(realStart, this.javadocEnd); this.index = realStart; if (realStart == this.javadocStart) { @@ -191,7 +197,6 @@ protected boolean commentParse() { } int previousPosition = this.index; char nextCharacter = 0; - this.markdown = this.source[this.javadocStart + 1] == '/'; this.markdownHelper = IMarkdownCommentHelper.create(this); if (realStart == this.javadocStart) { nextCharacter = readChar(); // second '*' or '/' diff --git a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/parser/IMarkdownCommentHelper.java b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/parser/IMarkdownCommentHelper.java index 5ee6b613b04..279c7cd9c84 100644 --- a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/parser/IMarkdownCommentHelper.java +++ b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/parser/IMarkdownCommentHelper.java @@ -138,8 +138,9 @@ public void recordFenceChar(char previous, char next, boolean lineStarted) { int required = this.insideFencedCodeBlock ? this.fenceLength : 3; if (++this.fenceCharCount == required) { this.insideFencedCodeBlock^=true; - this.fenceLength = this.fenceCharCount; } + if (this.insideFencedCodeBlock && this.fenceCharCount >= this.fenceLength) + this.fenceLength = this.fenceCharCount; } @Override diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/MarkdownCommentsTest.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/MarkdownCommentsTest.java index b044d576da5..e2d4810aebf 100644 --- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/MarkdownCommentsTest.java +++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/MarkdownCommentsTest.java @@ -56,6 +56,7 @@ protected Map getCompilerOptions() { options.put(CompilerOptions.OPTION_EnablePreviews, CompilerOptions.ENABLED); options.put(CompilerOptions.OPTION_DocCommentSupport, this.docCommentSupport); options.put(CompilerOptions.OPTION_ReportInvalidJavadoc, this.reportInvalidJavadoc); + options.put(CompilerOptions.OPTION_ReportUnusedImport, CompilerOptions.ERROR); if (!CompilerOptions.IGNORE.equals(this.reportInvalidJavadoc)) { options.put(CompilerOptions.OPTION_ReportInvalidJavadocTagsVisibility, this.reportInvalidJavadocVisibility); } @@ -612,4 +613,100 @@ public static void main(String[] args) { this.reportMissingJavadocTags = bkup; } } + public void test022() { + String bkup = this.reportMissingJavadocTags; + try { + this.reportMissingJavadocTags = CompilerOptions.IGNORE; + this.runConformTest(new String[] { + "X.java", + """ + import java.lang.annotation.Documented; + /** + * Reference type {@link Documented}: + */ + public class X { + public static void main(String[] args) { + System.out.println("Hello"); + } + } + """}, + "Hello"); + } finally { + this.reportMissingJavadocTags = bkup; + } + } + public void test023() { + String bkup = this.reportMissingJavadocTags; + try { + this.reportMissingJavadocTags = CompilerOptions.IGNORE; + this.runNegativeTest(new String[] { + "X.java", + """ + import java.lang.annotation.Documented; + /** + * Reference type but no reference here - : + */ + public class X { + public static void main(String[] args) { + System.out.println("Hello"); + } + } + """}, + "----------\n" + + "1. ERROR in X.java (at line 1)\n" + + " import java.lang.annotation.Documented;\n" + + " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + + "The import java.lang.annotation.Documented is never used\n" + + "----------\n"); + } finally { + this.reportMissingJavadocTags = bkup; + } + } + public void test024() { + String bkup = this.reportMissingJavadocTags; + try { + this.reportMissingJavadocTags = CompilerOptions.IGNORE; + this.runConformTest(new String[] { "X.java", + """ + import java.lang.annotation.Documented; + /// + /// Reference type {@link Documented}: + /// + public class X { + public static void main(String[] args) { + System.out.println("Hello"); + } + } + """}, + "Hello"); + } finally { + this.reportMissingJavadocTags = bkup; + } + } + public void test025() { + String bkup = this.reportMissingJavadocTags; + try { + this.reportMissingJavadocTags = CompilerOptions.IGNORE; + this.runNegativeTest(new String[] { "X.java", + """ + import java.lang.annotation.Documented; + /// + /// Reference type but no reference here: + /// + public class X { + public static void main(String[] args) { + System.out.println("Hello"); + } + } + """}, + "----------\n" + + "1. ERROR in X.java (at line 1)\n" + + " import java.lang.annotation.Documented;\n" + + " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + + "The import java.lang.annotation.Documented is never used\n" + + "----------\n"); + } finally { + this.reportMissingJavadocTags = bkup; + } + } }