Skip to content

Commit fb936c5

Browse files
fix: fix parsing of nested sealed interfaces and classes
1 parent 8dd6737 commit fb936c5

File tree

4 files changed

+59
-5
lines changed

4 files changed

+59
-5
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -366,11 +366,13 @@ function defineRules($, t) {
366366
{ ALT: () => $.CONSUME(t.Public) },
367367
{ ALT: () => $.CONSUME(t.Protected) },
368368
{ ALT: () => $.CONSUME(t.Private) },
369+
{ ALT: () => $.CONSUME(t.Abstract) },
369370
{ ALT: () => $.CONSUME(t.Static) },
371+
{ ALT: () => $.CONSUME(t.Sealed) },
372+
{ ALT: () => $.CONSUME(t.NonSealed) },
373+
{ ALT: () => $.CONSUME(t.Strictfp) },
370374
{ ALT: () => $.CONSUME(t.Final) },
371-
{ ALT: () => $.CONSUME(t.Abstract) },
372-
{ ALT: () => $.CONSUME(t.Default) },
373-
{ ALT: () => $.CONSUME(t.Strictfp) }
375+
{ ALT: () => $.CONSUME(t.Default) }
374376
]);
375377
}
376378
});

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

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,11 @@ describe("Sealed Classes & Interfaces", () => {
5656
expect(() => javaParser.parse(input, "compilationUnit")).to.not.throw();
5757
});
5858

59-
it("should handle sealed classes inside class declarations", () => {
59+
it("should handle sealed classes and interfaces inside class declarations", () => {
6060
const input = `
6161
public class SealedClasses {
62+
sealed interface SealedParent {}
63+
6264
public static sealed abstract class SealedParent permits SealedChild {}
6365
6466
final static class SealedChild extends SealedParent {}
@@ -67,9 +69,35 @@ describe("Sealed Classes & Interfaces", () => {
6769
expect(() => javaParser.parse(input, "compilationUnit")).to.not.throw();
6870
});
6971

70-
it("should handle non-sealed classes inside class declarations", () => {
72+
it("should handle non-sealed classes and interfaces inside class declarations", () => {
7173
const input = `
7274
public class SealedClasses {
75+
non-sealed interface SealedParent {}
76+
77+
public static non-sealed abstract class SealedParent {}
78+
79+
final static class SealedChild extends SealedParent {}
80+
}
81+
`;
82+
expect(() => javaParser.parse(input, "compilationUnit")).to.not.throw();
83+
});
84+
85+
it("should handle sealed classes and interfaces inside interface declarations", () => {
86+
const input = `
87+
public interface Test {
88+
sealed interface Inner {}
89+
90+
public static sealed abstract class SealedParent {}
91+
}
92+
`;
93+
expect(() => javaParser.parse(input, "compilationUnit")).to.not.throw();
94+
});
95+
96+
it("should handle non-sealed classes and interfaces inside interface declarations", () => {
97+
const input = `
98+
public interface SealedClasses {
99+
non-sealed interface Inner {}
100+
73101
public static non-sealed abstract class SealedParent {}
74102
75103
final static class SealedChild extends SealedParent {}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,15 @@ public static non-sealed abstract class NonSealedParent {}
8282

8383
final static class SealedChild extends NonSealedParent {}
8484
}
85+
86+
public interface Test {
87+
sealed interface Inner {}
88+
89+
public static sealed abstract class SealedParent {}
90+
91+
non-sealed interface Inner {}
92+
93+
public static non-sealed abstract class SealedParent {}
94+
95+
final static class SealedChild extends SealedParent {}
96+
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,15 @@ public abstract static non-sealed class NonSealedParent {}
101101

102102
static final class SealedChild extends NonSealedParent {}
103103
}
104+
105+
public interface Test {
106+
sealed interface Inner {}
107+
108+
public abstract static sealed class SealedParent {}
109+
110+
non-sealed interface Inner {}
111+
112+
public abstract static non-sealed class SealedParent {}
113+
114+
static final class SealedChild extends SealedParent {}
115+
}

0 commit comments

Comments
 (0)