From 46005281330e2a001aba50a5c1358d9ae22334ec Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Mon, 31 Aug 2026 22:47:37 +0100 Subject: Add calls to TypeScript AST --- .../output/lang/typescript/TypescriptWriter.java | 15 ++++++ .../output/lang/typescript/ast/TypescriptCall.java | 54 ++++++++++++++++++++++ .../lang/typescript/ast/TypescriptExpression.java | 2 +- .../lang/typescript/TypescriptWriterTests.java | 26 +++++++++++ .../lang/typescript/ast/TypescriptCallMatcher.java | 47 +++++++++++++++++++ 5 files changed, 143 insertions(+), 1 deletion(-) create mode 100644 src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptCall.java create mode 100644 src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptCallMatcher.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 9e52843..93216bf 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 @@ -241,6 +241,10 @@ public class TypescriptWriter implements AutoCloseable { this.writeBinaryOperation(binaryOperation); } + case TypescriptCall call -> { + this.writeCall(call); + } + case TypescriptConstructedType constructedType -> { this.writeConstructedType(constructedType); } @@ -277,6 +281,17 @@ public class TypescriptWriter implements AutoCloseable { }); } + private void writeCall(TypescriptCall call) throws IOException { + this.writeExpression(call.receiver()); + this.writer.write("("); + writeWithSeparator( + call.args(), + this::writeExpression, + () -> this.writer.write(", ") + ); + this.writer.write(")"); + } + 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/TypescriptCall.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptCall.java new file mode 100644 index 0000000..1c47666 --- /dev/null +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptCall.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.TypescriptCall imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptCall imports + +public record TypescriptCall( + 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.TypescriptCall.Builder arbitrary() { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptCall.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.TypescriptCall build() { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptCall(receiver, args); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptCall.Builder withReceiver(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression receiver) { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptCall.Builder(receiver, args); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptCall.Builder withReceiver(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression.Builder receiver) { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptCall.Builder(receiver.build(), args); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptCall.Builder withArgs(java.util.List args) { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptCall.Builder(receiver, args); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptCall.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.TypescriptCall.Builder(receiver, args); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptCall.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.TypescriptCall.Builder(receiver, args); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptCall.Builder body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptCall.Builder body + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptCall body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptCall 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 79057fe..7d443a3 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.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 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.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 81dd965..c7d2e2d 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 @@ -315,6 +315,32 @@ public class TypescriptWriterTests { assertThat(string, equalTo("x = 42.0")); } + // === Calls === + + @Test + public void callWithoutArgs() throws IOException { + var typescript = TypescriptCall.arbitrary() + .withReceiver(ref("f")) + .build(); + + var string = write(writer -> writer.writeExpression(typescript)); + + assertThat(string, equalTo("f()")); + } + + @Test + public void callWithArgs() throws IOException { + var typescript = TypescriptCall.arbitrary() + .withReceiver(ref("f")) + .addArg(ref("b")) + .addArg(ref("c")) + .build(); + + var string = write(writer -> writer.writeExpression(typescript)); + + assertThat(string, equalTo("f(b, c)")); + } + // === Constructed types === @Test diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptCallMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptCallMatcher.java new file mode 100644 index 0000000..a8052ce --- /dev/null +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptCallMatcher.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.TypescriptCallMatcher imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptCallMatcher imports + +public final class TypescriptCallMatcher implements org.zwobble.precisely.Matcher { + public static org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptCallMatcher isTypescriptCall() { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptCallMatcher(java.util.List.of()); + } + + private final java.util.List> submatchers; + + private TypescriptCallMatcher( + 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.TypescriptCall.class, this.submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptCallMatcher 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.TypescriptCall::receiver, (org.zwobble.precisely.Matcher) receiver)); + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptCallMatcher(submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptCallMatcher 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.TypescriptCall::args, (org.zwobble.precisely.Matcher) args)); + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptCallMatcher(submatchers); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptCallMatcher body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptCallMatcher body +} -- cgit v1.2.3