From 3a6771834e55a64c04337bb0ee83fbfc57524edf Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 30 Aug 2026 15:07:22 +0100 Subject: Add constructed types to TypeScript AST --- .../output/lang/typescript/TypescriptWriter.java | 15 ++++++ .../typescript/ast/TypescriptConstructedType.java | 54 ++++++++++++++++++++++ .../lang/typescript/ast/TypescriptExpression.java | 2 +- .../lang/typescript/TypescriptWriterTests.java | 15 ++++++ .../ast/TypescriptConstructedTypeMatcher.java | 47 +++++++++++++++++++ 5 files changed, 132 insertions(+), 1 deletion(-) create mode 100644 src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptConstructedType.java create mode 100644 src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptConstructedTypeMatcher.java (limited to 'src') 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 fb98a30..79ff43f 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 @@ -228,6 +228,10 @@ public class TypescriptWriter implements AutoCloseable { this.writeBinaryOperation(binaryOperation); } + case TypescriptConstructedType constructedType -> { + this.writeConstructedType(constructedType); + } + case TypescriptFieldAccess fieldAccess -> { this.writeFieldAccess(fieldAccess); } @@ -260,6 +264,17 @@ public class TypescriptWriter implements AutoCloseable { }); } + private void writeConstructedType(TypescriptConstructedType constructedType) throws IOException { + this.writeExpression(constructedType.receiver()); + this.writer.write("<"); + writeWithSeparator( + constructedType.args(), + this::writeExpression, + () -> this.writer.write(", ") + ); + this.writer.write(">"); + } + private void writeFieldAccess(TypescriptFieldAccess fieldAccess) throws IOException { this.writeExpression(fieldAccess.receiver()); this.writer.write("."); diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptConstructedType.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptConstructedType.java new file mode 100644 index 0000000..8b2264d --- /dev/null +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptConstructedType.java @@ -0,0 +1,54 @@ +// Generated by hobgoblin. + +package org.zwobble.hobgoblin.compiler.output.lang.typescript.ast; + +// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptConstructedType imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptConstructedType imports + +public record TypescriptConstructedType( + org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression receiver, + java.util.List args +) implements org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression { + public static org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptConstructedType.Builder arbitrary() { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptConstructedType.Builder(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNumberLiteral.arbitrary().build(), java.util.List.of()); + } + + public record Builder( + org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression receiver, + java.util.List args + ) implements org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression.Builder { + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptConstructedType build() { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptConstructedType(receiver, args); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptConstructedType.Builder withReceiver(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression receiver) { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptConstructedType.Builder(receiver, args); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptConstructedType.Builder withReceiver(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression.Builder receiver) { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptConstructedType.Builder(receiver.build(), args); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptConstructedType.Builder withArgs(java.util.List args) { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptConstructedType.Builder(receiver, args); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptConstructedType.Builder addArg(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression arg) { + var args = new java.util.ArrayList(this.args); + args.add(arg); + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptConstructedType.Builder(receiver, args); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptConstructedType.Builder addArg(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression.Builder arg) { + var args = new java.util.ArrayList(this.args); + args.add(arg.build()); + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptConstructedType.Builder(receiver, args); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptConstructedType.Builder body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptConstructedType.Builder body + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptConstructedType body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptConstructedType 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 1cf2e81..79057fe 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.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.TypescriptRef { +public sealed interface TypescriptExpression permits org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptBinaryOperation, 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.TypescriptRef { 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 7ca267a..2606dd4 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 @@ -298,6 +298,21 @@ public class TypescriptWriterTests { assertThat(string, equalTo("x = 42.0")); } + // === Constructed types === + + @Test + public void constructedType() throws IOException { + var typescript = TypescriptConstructedType.arbitrary() + .withReceiver(ref("A")) + .addArg(ref("B")) + .addArg(ref("C")) + .build(); + + var string = write(writer -> writer.writeExpression(typescript)); + + assertThat(string, equalTo("A")); + } + // === Field accesses === @Test diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptConstructedTypeMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptConstructedTypeMatcher.java new file mode 100644 index 0000000..98a206e --- /dev/null +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptConstructedTypeMatcher.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.TypescriptConstructedTypeMatcher imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptConstructedTypeMatcher imports + +public final class TypescriptConstructedTypeMatcher implements org.zwobble.precisely.Matcher { + public static org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptConstructedTypeMatcher isTypescriptConstructedType() { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptConstructedTypeMatcher(java.util.List.of()); + } + + private final java.util.List> submatchers; + + private TypescriptConstructedTypeMatcher( + 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.TypescriptConstructedType.class, this.submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptConstructedTypeMatcher withReceiver(org.zwobble.precisely.Matcher receiver) { + var submatchers = new java.util.ArrayList>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("receiver", org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptConstructedType::receiver, (org.zwobble.precisely.Matcher) receiver)); + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptConstructedTypeMatcher(submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptConstructedTypeMatcher withArgs(org.zwobble.precisely.Matcher> args) { + var submatchers = new java.util.ArrayList>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("args", org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptConstructedType::args, (org.zwobble.precisely.Matcher) args)); + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptConstructedTypeMatcher(submatchers); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptConstructedTypeMatcher body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptConstructedTypeMatcher body +} -- cgit v1.2.3