Skip to content

Commit

Permalink
Fix parser bug related to empty records
Browse files Browse the repository at this point in the history
Empty records would record wrong whitespace in the LST which would lead to problems when serialized to source again.

Fixes openrewrite#2956
  • Loading branch information
knutwannheden committed Mar 9, 2023
1 parent 9a2aa26 commit 105be93
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,9 @@ public J visitClass(ClassTree node, Space fmt) {
}
primaryConstructor = JContainer.build(
sourceBefore("("),
convertAll(stateVector, commaDelim, t -> sourceBefore(")")),
stateVector.isEmpty() ?
singletonList(padRight(new J.Empty(randomId(), sourceBefore(")"), Markers.EMPTY), EMPTY)) :
convertAll(stateVector, commaDelim, t -> sourceBefore(")")),
Markers.EMPTY
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@
@MinimumJava17
class RecordTest implements RewriteTest {

@Test
void emptyRecord() {
rewriteRun(
java(
"""
public record JavaRecord() {
}
"""
)
);
}

@Test
void javaRecord() {
rewriteRun(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.openrewrite.test.RewriteTest;

import static org.openrewrite.java.Assertions.java;
import static org.openrewrite.java.Assertions.version;

@SuppressWarnings("ALL")
class FinalizeLocalVariablesTest implements RewriteTest {
Expand Down Expand Up @@ -338,4 +339,32 @@ class Person {
)
);
}

@Test
@Issue("https://github.com/openrewrite/rewrite/issues/2956")
void recordShouldNotIntroduceExtraClosingParenthesis() {
rewriteRun(
version(
java(
"""
public class Main {
public static void test() {
var myVar = "";
}
public record EmptyRecord() {
}
}
""",
"""
public class Main {
public static void test() {
final var myVar = "";
}
public record EmptyRecord() {
}
}
"""
), 17)
);
}
}

0 comments on commit 105be93

Please sign in to comment.