Skip to content

Commit

Permalink
Fix interface member detection to support inner records (#492)
Browse files Browse the repository at this point in the history
* Add a failing test and tests for debugging

* Fix interface member detection

* Add formatting tests for completeness
  • Loading branch information
rnorth authored Jul 19, 2021
1 parent 172e7d6 commit 1f4b729
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/java-parser/src/productions/interfaces.js
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,8 @@ function defineRules($, t) {
nextTokenType = this.LA(1).tokenType;
if (
tokenMatcher(nextTokenType, t.Class) ||
tokenMatcher(nextTokenType, t.Enum)
tokenMatcher(nextTokenType, t.Enum) ||
tokenMatcher(nextTokenType, t.Record)
) {
return InterfaceBodyTypes.classDeclaration;
}
Expand Down
33 changes: 33 additions & 0 deletions packages/java-parser/test/records/records-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,37 @@ describe("Records", () => {
`;
expect(() => javaParser.parse(input, "compilationUnit")).to.not.throw();
});

it("should handle Java records inside an interface definition", () => {
const input = `
public interface SomeInterface {
record SomeRecord(
String param
) { }
}
`;
expect(() => javaParser.parse(input, "compilationUnit")).to.not.throw();
});

it("should handle Java records implementing an interface inside an interface definition", () => {
const input = `
public interface SomeInterface {
record SomeRecord(
String param
) implements AnyInterface { }
}
`;
expect(() => javaParser.parse(input, "compilationUnit")).to.not.throw();
});

it("should handle Java records implementing an interface inside a class definition", () => {
const input = `
public class SomeClass {
record SomeRecord(
String param
) implements AnyInterface { }
}
`;
expect(() => javaParser.parse(input, "compilationUnit")).to.not.throw();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,10 @@ public record MyRecord(
}
}
}

public interface MyInterface {
record MyRecord(
String param) implements MyInterface {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,7 @@ public record MyRecord(String name, int age) {
}
}
}

public interface MyInterface {
record MyRecord(String param) implements MyInterface {}
}

0 comments on commit 1f4b729

Please sign in to comment.