From bc68be40f1486ed2f450b14cbc3991c36e754456 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Fri, 4 Sep 2026 10:30:01 +0100 Subject: Add cast to TypeScript AST --- .../output/lang/typescript/TypescriptWriter.java | 10 +++++ .../output/lang/typescript/ast/TypescriptCast.java | 46 +++++++++++++++++++++ .../lang/typescript/ast/TypescriptExpression.java | 2 +- .../lang/typescript/TypescriptWriterTests.java | 14 +++++++ .../lang/typescript/ast/TypescriptCastMatcher.java | 47 ++++++++++++++++++++++ 5 files changed, 118 insertions(+), 1 deletion(-) create mode 100644 src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptCast.java create mode 100644 src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptCastMatcher.java diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/TypescriptWriter.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/TypescriptWriter.java index 7523fe0..ebb7aba 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/TypescriptWriter.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/TypescriptWriter.java @@ -277,6 +277,10 @@ public class TypescriptWriter implements AutoCloseable { this.writeCall(call); } + case TypescriptCast cast -> { + this.writeCast(cast); + } + case TypescriptConstructedType constructedType -> { this.writeConstructedType(constructedType); } @@ -336,6 +340,12 @@ public class TypescriptWriter implements AutoCloseable { this.writer.write(")"); } + private void writeCast(TypescriptCast cast) throws IOException { + this.writeExpression(cast.value()); + this.writer.write(" as "); + this.writeExpression(cast.type()); + } + private void writeConstructedType(TypescriptConstructedType constructedType) throws IOException { this.writeExpression(constructedType.receiver()); this.writer.write("<"); diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptCast.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptCast.java new file mode 100644 index 0000000..6a2d63e --- /dev/null +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptCast.java @@ -0,0 +1,46 @@ +// Generated by hobgoblin. + +package org.zwobble.hobgoblin.compiler.output.lang.typescript.ast; + +// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptCast imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptCast imports + +public record TypescriptCast( + org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression value, + org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression type +) implements org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression { + public static org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptCast.Builder arbitrary() { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptCast.Builder(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNumberLiteral.arbitrary().build(), org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNumberLiteral.arbitrary().build()); + } + + public record Builder( + org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression value, + org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression type + ) implements org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression.Builder { + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptCast build() { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptCast(value, type); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptCast.Builder withValue(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression value) { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptCast.Builder(value, type); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptCast.Builder withValue(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression.Builder value) { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptCast.Builder(value.build(), type); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptCast.Builder withType(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression type) { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptCast.Builder(value, type); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptCast.Builder withType(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression.Builder type) { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptCast.Builder(value, type.build()); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptCast.Builder body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptCast.Builder body + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptCast body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptCast body +} diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptExpression.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptExpression.java index e047bca..aa23783 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptExpression.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptExpression.java @@ -5,7 +5,7 @@ package org.zwobble.hobgoblin.compiler.output.lang.typescript.ast; // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression imports // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression imports -public sealed interface TypescriptExpression permits org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptBinaryOperation, org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptCall, org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptConstructedType, org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptFieldAccess, org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNewCall, org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNumberLiteral, org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteral, org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptRef, org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeof, org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeUnion { +public sealed interface TypescriptExpression permits org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptBinaryOperation, org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptCall, org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptCast, org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptConstructedType, org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptFieldAccess, org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNewCall, org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNumberLiteral, org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteral, org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptRef, org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeof, org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeUnion { public interface Builder { public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression build(); diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/TypescriptWriterTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/TypescriptWriterTests.java index 5a7c488..a5081d0 100644 --- a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/TypescriptWriterTests.java +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/TypescriptWriterTests.java @@ -383,6 +383,20 @@ public class TypescriptWriterTests { assertThat(string, equalTo("f(b, c)")); } + // === Casts === + + @Test + public void cast() throws IOException { + var typescript = TypescriptCast.arbitrary() + .withValue(ref("a")) + .withType(ref("A")) + .build(); + + var string = write(writer -> writer.writeExpression(typescript)); + + assertThat(string, equalTo("a as A")); + } + // === Constructed types === @Test diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptCastMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptCastMatcher.java new file mode 100644 index 0000000..ff77a96 --- /dev/null +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptCastMatcher.java @@ -0,0 +1,47 @@ +// Generated by hobgoblin. + +package org.zwobble.hobgoblin.compiler.output.lang.typescript.ast; + +// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptCastMatcher imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptCastMatcher imports + +public final class TypescriptCastMatcher implements org.zwobble.precisely.Matcher { + public static org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptCastMatcher isTypescriptCast() { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptCastMatcher(java.util.List.of()); + } + + private final java.util.List> submatchers; + + private TypescriptCastMatcher( + java.util.List> submatchers + ) { + this.submatchers = submatchers; + } + + public org.zwobble.precisely.MatchResult match(java.lang.Object actual) { + return this.toMatcher().match(actual); + } + + public org.zwobble.precisely.TextTree describe() { + return this.toMatcher().describe(); + } + + private org.zwobble.precisely.Matcher toMatcher() { + return org.zwobble.precisely.Matchers.instanceOf(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptCast.class, this.submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptCastMatcher withValue(org.zwobble.precisely.Matcher value) { + var submatchers = new java.util.ArrayList>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("value", org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptCast::value, (org.zwobble.precisely.Matcher) value)); + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptCastMatcher(submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptCastMatcher withType(org.zwobble.precisely.Matcher type) { + var submatchers = new java.util.ArrayList>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("type", org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptCast::type, (org.zwobble.precisely.Matcher) type)); + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptCastMatcher(submatchers); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptCastMatcher body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptCastMatcher body +} -- cgit v1.2.3