Skip to content

Commit

Permalink
Fix parsing of multi-line comments "containing" single-line comments (#…
Browse files Browse the repository at this point in the history
…5090)

* Add test

* Fix parsing of multi-line comments containing URLs

This isn't really specific to URLs but rather to multi-line comments "containing" singe-line comments. Many of the parsers currently have this issue when trying to skip over multi-line comments using the `positionOfNext()` utility method.

---------

Co-authored-by: Knut Wannheden <[email protected]>
  • Loading branch information
rlsanders4 and knutwannheden authored Feb 25, 2025
1 parent 26726bf commit d984f97
Show file tree
Hide file tree
Showing 13 changed files with 75 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2255,7 +2255,7 @@ private int positionOfNext(String untilDelim) {
if (source.length() - untilDelim.length() > delimIndex + 1) {
switch (source.substring(delimIndex, delimIndex + 2)) {
case "//":
inSingleLineComment = true;
inSingleLineComment = !inMultiLineComment;
delimIndex++;
break;
case "/*":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,19 @@ void topLevelExpression() {
);
}

@Test
void multilineCommentWithUrl() {
rewriteRun(
groovy(
"""
class Foo {
/* https://foo.bar */
}
"""
)
);
}

@Test
void scriptImportsCanBeAnywhere() {
rewriteRun(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -771,7 +771,7 @@ private int positionOfNext(String untilDelim, @Nullable Character stop) {
inSingleLineComment = true;
} else switch (source.substring(delimIndex, delimIndex + 2)) {
case "//":
inSingleLineComment = true;
inSingleLineComment = !inMultiLineComment;
delimIndex += 1;
break;
case "/*":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,4 +237,17 @@ void emptySingleLineCommentAtTheEndOfTheFile() {
)
);
}

@Test
void multiLineCommentWithUrl() {
rewriteRun(
hcl(
"""
module "something" {
/* https://www.example.com */
}
"""
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1994,7 +1994,7 @@ private int positionOfNext(String untilDelim, @Nullable Character stop) {
case '/':
switch (c2) {
case '/':
inSingleLineComment = true;
inSingleLineComment = !inMultiLineComment;
delimIndex++;
break;
case '*':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@
import static java.util.stream.Collectors.toList;
import static java.util.stream.StreamSupport.stream;
import static org.openrewrite.Tree.randomId;
import static org.openrewrite.internal.StringUtils.indexOf;
import static org.openrewrite.internal.StringUtils.indexOfNextNonWhitespace;
import static org.openrewrite.java.tree.Space.EMPTY;
import static org.openrewrite.java.tree.Space.format;
Expand Down Expand Up @@ -2081,7 +2080,7 @@ private int positionOfNext(String untilDelim, @Nullable Character stop) {
case '/':
switch (c2) {
case '/':
inSingleLineComment = true;
inSingleLineComment = !inMultiLineComment;
delimIndex++;
break;
case '*':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2106,7 +2106,7 @@ private int positionOfNext(String untilDelim, @Nullable Character stop) {
case '/':
switch (c2) {
case '/':
inSingleLineComment = true;
inSingleLineComment = !inMultiLineComment;
delimIndex++;
break;
case '*':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1990,7 +1990,7 @@ private int positionOfNext(String untilDelim, @Nullable Character stop) {
case '/':
switch (c2) {
case '/':
inSingleLineComment = true;
inSingleLineComment = !inMultiLineComment;
delimIndex++;
break;
case '*':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,21 @@ class A {
)
);
}

@Test
@Issue("https://github.com/openrewrite/rewrite/pull/5090")
void multiLineCommentWithUrl() {
rewriteRun(
java(
"""
package hello;
public class Test {
public static void test() {
/*addItem("Site A", "https://hello.com/A");*/
}
}
"""
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -373,5 +373,4 @@ void filterArtifacts() {
assertThat(JavaParser.filterArtifacts("rewrite-java", classpath))
.containsOnly(Paths.get("/.m2/repository/org/openrewrite/rewrite-java/8.41.1/rewrite-java-8.41.1.jar"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,19 @@ void comments() {
);
}

@Test
void multilineCommentWithUrl() {
rewriteRun(
json(
"""
{
/* https://foo.bar */
}
"""
)
);
}

@Issue("https://github.com/openrewrite/rewrite/issues/1145")
@Test
void longValue() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ private int positionOfNext(String untilDelim, @Nullable Character stop) {
if (source.length() - untilDelim.length() > delimIndex + 1) {
switch (source.substring(delimIndex, delimIndex + 2)) {
case "//":
inSingleLineComment = true;
inSingleLineComment = !inMultiLineComment;
delimIndex++;
break;
case "/*":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,16 @@ void empty() {
)
);
}

@Test
void multilineCommentWithUrl() {
rewriteRun(
proto(
"""
/* https://foo.bar */
syntax = 'proto2';
"""
)
);
}
}

0 comments on commit d984f97

Please sign in to comment.