From a7f891925aea257958205b62141790e7ea63c7fa Mon Sep 17 00:00:00 2001 From: Richard North Date: Sun, 18 Jul 2021 14:28:06 +0100 Subject: [PATCH 1/3] Add a failing test and tests for debugging --- .../java-parser/test/records/records-spec.js | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/packages/java-parser/test/records/records-spec.js b/packages/java-parser/test/records/records-spec.js index ed2a0c7a..591fc99a 100644 --- a/packages/java-parser/test/records/records-spec.js +++ b/packages/java-parser/test/records/records-spec.js @@ -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(); + }); }); From e83c08eaa00120e2e64f886e7b2500c47cacecfb Mon Sep 17 00:00:00 2001 From: Richard North Date: Sun, 18 Jul 2021 15:11:03 +0100 Subject: [PATCH 2/3] Fix interface member detection --- packages/java-parser/src/productions/interfaces.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/java-parser/src/productions/interfaces.js b/packages/java-parser/src/productions/interfaces.js index 9dd3a42a..24058786 100644 --- a/packages/java-parser/src/productions/interfaces.js +++ b/packages/java-parser/src/productions/interfaces.js @@ -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; } From 6f8050b6a0aa7b655434a8761e9b7bace5604966 Mon Sep 17 00:00:00 2001 From: Richard North Date: Sun, 18 Jul 2021 15:51:16 +0100 Subject: [PATCH 3/3] Add formatting tests for completeness --- .../test/unit-test/records/_input.java | 7 +++++++ .../test/unit-test/records/_output.java | 4 ++++ 2 files changed, 11 insertions(+) diff --git a/packages/prettier-plugin-java/test/unit-test/records/_input.java b/packages/prettier-plugin-java/test/unit-test/records/_input.java index 413b5473..d8ef6929 100644 --- a/packages/prettier-plugin-java/test/unit-test/records/_input.java +++ b/packages/prettier-plugin-java/test/unit-test/records/_input.java @@ -90,3 +90,10 @@ public record MyRecord( } } } + +public interface MyInterface { + record MyRecord( + String param) implements MyInterface { + + } +} \ No newline at end of file diff --git a/packages/prettier-plugin-java/test/unit-test/records/_output.java b/packages/prettier-plugin-java/test/unit-test/records/_output.java index 3539e3c5..ee075b75 100644 --- a/packages/prettier-plugin-java/test/unit-test/records/_output.java +++ b/packages/prettier-plugin-java/test/unit-test/records/_output.java @@ -78,3 +78,7 @@ public record MyRecord(String name, int age) { } } } + +public interface MyInterface { + record MyRecord(String param) implements MyInterface {} +}