Skip to content

Commit

Permalink
fix: Java-parser routing to the wrong rule on class defining records …
Browse files Browse the repository at this point in the history
…with simplified and normal constructor
  • Loading branch information
Shaolans committed May 21, 2021
1 parent 6bc93b9 commit 7828cdf
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 1 deletion.
4 changes: 3 additions & 1 deletion packages/java-parser/src/productions/classes.js
Original file line number Diff line number Diff line change
Expand Up @@ -850,7 +850,8 @@ function defineRules($, t) {

if (
tokenMatcher(nextTokenType, t.Class) ||
tokenMatcher(nextTokenType, t.Enum)
tokenMatcher(nextTokenType, t.Enum) ||
tokenMatcher(nextTokenType, t.Record)
) {
return classBodyTypes.classDeclaration;
}
Expand Down Expand Up @@ -923,6 +924,7 @@ function defineRules($, t) {
$.RULE("isCompactConstructorDeclaration", () => {
$.MANY($.constructorModifier);
$.SUBRULE($.simpleTypeName);
$.CONSUME(t.LCurly);
});
}

Expand Down
38 changes: 38 additions & 0 deletions packages/java-parser/test/records/records-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,42 @@ describe("The Java Parser fixed bugs", () => {
`;
expect(() => javaParser.parse(input, "compilationUnit")).to.not.throw();
});

it("should handle Java records with simplified constructors inside java class declaration", () => {
const input = `
public class RecordClass {
record MyRecord(String name, int age) {
public MyRecord {
if (age < 0) {
throw new IllegalArgumentException("Age cannot be negative");
}
if (name == null || name.isBlank()) {
throw new IllegalArgumentException("Name cannot be blank");
}
}
}
}
`;
expect(() => javaParser.parse(input, "compilationUnit")).to.not.throw();
});

it("should handle Java records with constructor inside java class declaration", () => {
const input = `
public class RecordClass {
record MyRecord(String name, int age) {
public MyRecord(String name, int age) {
if (age < 0) {
throw new IllegalArgumentException("Age cannot be negative");
}
if (name == null || name.isBlank()) {
throw new IllegalArgumentException("Name cannot be blank");
}
}
}
}
`;
expect(() => javaParser.parse(input, "compilationUnit")).to.not.throw();
});
});
30 changes: 30 additions & 0 deletions packages/prettier-plugin-java/test/unit-test/records/_input.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,34 @@ class T {
void t() {
record = "12";
}

class MyRecordSimplifiedConstructor {
record MyRecord(String name, int age
) {
public MyRecord {
if (age < 0) {
throw new IllegalArgumentException("Age cannot be negative");
}

if (name == null || name.isBlank()) {
throw new IllegalArgumentException("Name cannot be blank");
}
}
}
}

class MyRecordConstructor {
record MyRecord(String name,
int age) {
public MyRecord(String name,
int age) {
if (age < 0) {
throw new IllegalArgumentException("Age cannot be negative");
}
if (name == null || name.isBlank()) {
throw new IllegalArgumentException("Name cannot be blank");
}
}
}
}
}
29 changes: 29 additions & 0 deletions packages/prettier-plugin-java/test/unit-test/records/_output.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,33 @@ class T {
void t() {
record = "12";
}

class MyRecordSimplifiedConstructor {

record MyRecord(String name, int age) {
public MyRecord {
if (age < 0) {
throw new IllegalArgumentException("Age cannot be negative");
}

if (name == null || name.isBlank()) {
throw new IllegalArgumentException("Name cannot be blank");
}
}
}
}

class MyRecordConstructor {

record MyRecord(String name, int age) {
public MyRecord(String name, int age) {
if (age < 0) {
throw new IllegalArgumentException("Age cannot be negative");
}
if (name == null || name.isBlank()) {
throw new IllegalArgumentException("Name cannot be blank");
}
}
}
}
}

0 comments on commit 7828cdf

Please sign in to comment.