Skip to content

Commit

Permalink
fix: correct parsing of nested sealed classes
Browse files Browse the repository at this point in the history
Fix #522
  • Loading branch information
clementdessoude committed Jan 6, 2022
1 parent 650080c commit 2af631e
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/java-parser/src/productions/classes.js
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,8 @@ function defineRules($, t) {
{ ALT: () => $.CONSUME(t.Volatile) },
{ ALT: () => $.CONSUME(t.Synchronized) },
{ ALT: () => $.CONSUME(t.Native) },
{ ALT: () => $.CONSUME(t.Sealed) },
{ ALT: () => $.CONSUME(t.NonSealed) },
{ ALT: () => $.CONSUME(t.Strictfp) }
]);
}
Expand Down
22 changes: 22 additions & 0 deletions packages/java-parser/test/sealed/sealed-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,26 @@ describe("Sealed Classes & Interfaces", () => {
`;
expect(() => javaParser.parse(input, "compilationUnit")).to.not.throw();
});

it("should handle sealed classes inside class declarations", () => {
const input = `
public class SealedClasses {
public static sealed abstract class SealedParent permits SealedChild {}
final static class SealedChild extends SealedParent {}
}
`;
expect(() => javaParser.parse(input, "compilationUnit")).to.not.throw();
});

it("should handle non-sealed classes inside class declarations", () => {
const input = `
public class SealedClasses {
public static non-sealed abstract class SealedParent {}
final static class SealedChild extends SealedParent {}
}
`;
expect(() => javaParser.parse(input, "compilationUnit")).to.not.throw();
});
});
12 changes: 12 additions & 0 deletions packages/prettier-plugin-java/test/unit-test/sealed/_input.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,15 @@ public sealed interface Shape permits ALongVeryLongCircle, ALongVeryLongRectangl
public sealed interface Shape extends AbstractShape permits ALongVeryLongCircle, ALongVeryLongRectangle, ALongVeryLongTriangle, ALongVeryLongUnicorn {}
public sealed class Shape permits ALongVeryLongCircle, ALongVeryLongRectangle, ALongVeryLongTriangle, ALongVeryLongUnicorn {}
public sealed class Shape extends AbstractShape permits ALongVeryLongCircle, ALongVeryLongRectangle, ALongVeryLongTriangle, ALongVeryLongUnicorn {}

public class NestedSealedClasses {
public static sealed abstract class SealedParent permits SealedChild {}

final static class SealedChild extends SealedParent {}
}

public class NestedNonSealedClasses {
public static non-sealed abstract class NonSealedParent {}

final static class SealedChild extends NonSealedParent {}
}
14 changes: 14 additions & 0 deletions packages/prettier-plugin-java/test/unit-test/sealed/_output.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,17 @@ public sealed class Shape
ALongVeryLongRectangle,
ALongVeryLongTriangle,
ALongVeryLongUnicorn {}

public class NestedSealedClasses {

public abstract static sealed class SealedParent permits SealedChild {}

static final class SealedChild extends SealedParent {}
}

public class NestedNonSealedClasses {

public abstract static non-sealed class NonSealedParent {}

static final class SealedChild extends NonSealedParent {}
}

0 comments on commit 2af631e

Please sign in to comment.