Skip to content

Commit

Permalink
Comments cannot be parsed properly (#4624)
Browse files Browse the repository at this point in the history
* Invalid comments cannot be properly parsed

* Add more example

* Format

* Reduce testcase

* Fixed bug with parsing multiline comments

* Add continue

* Assert the exact output comments expected

* Restore break in ReloadableJava21ParserVisitor

Likely accidentally removed

---------

Co-authored-by: Niels de Bruin <[email protected]>
Co-authored-by: Tim te Beek <[email protected]>
  • Loading branch information
3 people authored Oct 30, 2024
1 parent 6ba5a6d commit ed1a481
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1815,7 +1815,8 @@ private int positionOfNext(String untilDelim, @Nullable Character stop) {
case '*':
if(c2 == '/') {
inMultiLineComment = false;
delimIndex += 2;
delimIndex++;
continue;
}
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1903,7 +1903,8 @@ private int positionOfNext(String untilDelim, @Nullable Character stop) {
case '*':
if (c2 == '/') {
inMultiLineComment = false;
delimIndex += 2;
delimIndex++;
continue;
}
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1900,7 +1900,8 @@ private int positionOfNext(String untilDelim, @Nullable Character stop) {
case '*':
if (c2 == '/') {
inMultiLineComment = false;
delimIndex += 2;
delimIndex++;
continue;
}
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1808,7 +1808,8 @@ private int positionOfNext(String untilDelim, @Nullable Character stop) {
case '*':
if(c2 == '/') {
inMultiLineComment = false;
delimIndex += 2;
delimIndex++;
continue;
}
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,15 +154,64 @@ void shouldResolvePathUsingPublicClasses(@Language("java") String source) {
rewriteRun(
java(
source,
spec -> spec.afterRecipe(cu -> assertThat(cu.getSourcePath()).isEqualTo(Path.of("my","example","PublicClass.java")))
spec -> spec.afterRecipe(cu -> assertThat(cu.getSourcePath()).isEqualTo(Path.of("my", "example", "PublicClass.java")))
)
);
}

@Test
@Issue("https://github.com/openrewrite/rewrite/issues/1895")
void moduleInfo(){
void moduleInfo() {
// Ignored until properly handled: https://github.com/openrewrite/rewrite/issues/4054#issuecomment-2267605739
assertFalse(JavaParser.fromJavaVersion().build().accept(Path.of("src/main/java/foo/module-info.java")));
}

@Test
@Issue("https://github.com/openrewrite/rewrite/pull/4624")
void shouldParseComments() {
rewriteRun(
java(
"""
class A {
/*
* public Some getOther() { return other; }
*
*//**
* Sets the value of the other property.
*
* @param value allowed object is {@link Some }
*
*//*
* public void setOther(Some value) { this.other =
* value; }
*/
}
""",
spec -> spec.afterRecipe(cu -> assertThat(cu.getClasses().get(0).getBody().getEnd().getComments())
.extracting("text")
.containsExactly(
"""
* public Some getOther() { return other; }
*
\
""",
"""
*
* Sets the value of the other property.
*
* @param value allowed object is {@link Some }
*
\
""",
"""
* public void setOther(Some value) { this.other =
* value; }
\
"""
))
)
);
}
}

0 comments on commit ed1a481

Please sign in to comment.