Skip to content

Commit 1de7fb3

Browse files
committed
fix: Java-parser routing to the wrong rule on class defining records with simplified and normal constructor
1 parent 6bc93b9 commit 1de7fb3

File tree

4 files changed

+159
-1
lines changed

4 files changed

+159
-1
lines changed

packages/java-parser/src/productions/classes.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -850,7 +850,8 @@ function defineRules($, t) {
850850

851851
if (
852852
tokenMatcher(nextTokenType, t.Class) ||
853-
tokenMatcher(nextTokenType, t.Enum)
853+
tokenMatcher(nextTokenType, t.Enum) ||
854+
tokenMatcher(nextTokenType, t.Record)
854855
) {
855856
return classBodyTypes.classDeclaration;
856857
}
@@ -923,6 +924,7 @@ function defineRules($, t) {
923924
$.RULE("isCompactConstructorDeclaration", () => {
924925
$.MANY($.constructorModifier);
925926
$.SUBRULE($.simpleTypeName);
927+
$.CONSUME(t.LCurly);
926928
});
927929
}
928930

packages/java-parser/test/records/records-spec.js

+59
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,63 @@ describe("The Java Parser fixed bugs", () => {
2525
`;
2626
expect(() => javaParser.parse(input, "compilationUnit")).to.not.throw();
2727
});
28+
29+
it("should handle Java records with simplified constructors inside java class declaration", () => {
30+
const input = `
31+
public class RecordClass {
32+
record MyRecord(String name, int age) {
33+
public MyRecord {
34+
if (age < 0) {
35+
throw new IllegalArgumentException("Age cannot be negative");
36+
}
37+
38+
if (name == null || name.isBlank()) {
39+
throw new IllegalArgumentException("Name cannot be blank");
40+
}
41+
}
42+
}
43+
}
44+
`;
45+
expect(() => javaParser.parse(input, "compilationUnit")).to.not.throw();
46+
});
47+
48+
it("should handle Java records with constructor inside java class declaration", () => {
49+
const input = `
50+
public class RecordClass {
51+
record MyRecord(String name, int age) {
52+
public MyRecord(String name, int age) {
53+
if (age < 0) {
54+
throw new IllegalArgumentException("Age cannot be negative");
55+
}
56+
57+
if (name == null || name.isBlank()) {
58+
throw new IllegalArgumentException("Name cannot be blank");
59+
}
60+
}
61+
}
62+
}
63+
`;
64+
expect(() => javaParser.parse(input, "compilationUnit")).to.not.throw();
65+
});
66+
67+
it("should handle Java records with constructor inside java class declaration", () => {
68+
const input = `
69+
public class RecordClass {
70+
public record MyRecord(String name, int age) {
71+
@Annotation
72+
@Annotation2
73+
public MyRecord {
74+
if (age < 0) {
75+
throw new IllegalArgumentException("Age cannot be negative");
76+
}
77+
78+
if (name == null || name.isBlank()) {
79+
throw new IllegalArgumentException("Name cannot be blank");
80+
}
81+
}
82+
}
83+
}
84+
`;
85+
expect(() => javaParser.parse(input, "compilationUnit")).to.not.throw();
86+
});
2887
});

packages/prettier-plugin-java/test/unit-test/records/_input.java

+51
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,55 @@ class T {
3838
void t() {
3939
record = "12";
4040
}
41+
42+
class MyRecordSimplifiedConstructor {
43+
record MyRecord(String name, int age
44+
) {
45+
public MyRecord {
46+
if (age < 0) {
47+
throw new IllegalArgumentException("Age cannot be negative");
48+
}
49+
50+
if (name == null || name.isBlank()) {
51+
throw new IllegalArgumentException("Name cannot be blank");
52+
}
53+
}
54+
}
55+
}
56+
57+
class MyRecordConstructor {
58+
record MyRecord(String name,
59+
int age) {
60+
public MyRecord(String name,
61+
int age) {
62+
if (age < 0) {
63+
throw new IllegalArgumentException("Age cannot be negative");
64+
}
65+
if (name == null || name.isBlank()) {
66+
throw new IllegalArgumentException("Name cannot be blank");
67+
}
68+
}
69+
}
70+
}
71+
72+
public class MyRecordWithAnnotationAndModifiers {
73+
public record MyRecord(
74+
String name,
75+
int age) {
76+
@Annotation
77+
@Annotation2
78+
public MyRecord {
79+
if (
80+
age < 0)
81+
{
82+
throw new IllegalArgumentException("Age cannot be negative");
83+
}
84+
85+
if (name == null ||
86+
name.isBlank()) {
87+
throw new IllegalArgumentException("Name cannot be blank");
88+
}
89+
}
90+
}
91+
}
4192
}

packages/prettier-plugin-java/test/unit-test/records/_output.java

+46
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,50 @@ class T {
3131
void t() {
3232
record = "12";
3333
}
34+
35+
class MyRecordSimplifiedConstructor {
36+
37+
record MyRecord(String name, int age) {
38+
public MyRecord {
39+
if (age < 0) {
40+
throw new IllegalArgumentException("Age cannot be negative");
41+
}
42+
43+
if (name == null || name.isBlank()) {
44+
throw new IllegalArgumentException("Name cannot be blank");
45+
}
46+
}
47+
}
48+
}
49+
50+
class MyRecordConstructor {
51+
52+
record MyRecord(String name, int age) {
53+
public MyRecord(String name, int age) {
54+
if (age < 0) {
55+
throw new IllegalArgumentException("Age cannot be negative");
56+
}
57+
if (name == null || name.isBlank()) {
58+
throw new IllegalArgumentException("Name cannot be blank");
59+
}
60+
}
61+
}
62+
}
63+
64+
public class MyRecordWithAnnotationAndModifiers {
65+
66+
public record MyRecord(String name, int age) {
67+
@Annotation
68+
@Annotation2
69+
public MyRecord {
70+
if (age < 0) {
71+
throw new IllegalArgumentException("Age cannot be negative");
72+
}
73+
74+
if (name == null || name.isBlank()) {
75+
throw new IllegalArgumentException("Name cannot be blank");
76+
}
77+
}
78+
}
79+
}
3480
}

0 commit comments

Comments
 (0)