diff options
| author | Michael Williamson <mike@zwobble.org> | 2026-08-30 13:43:59 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2026-08-30 13:43:59 +0100 |
| commit | 93c7437d22224ac72818a4cd4ed4d9beeedaac79 (patch) | |
| tree | 15b2e1a4944c4872701f610b426e82ec44c09e01 /src | |
| parent | d9945c3fd0dfa85bf0ca2bbb3af35af4b25b0fe7 (diff) | |
Add new calls to TypeScript AST
Diffstat (limited to 'src')
5 files changed, 144 insertions, 1 deletions
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 de38ea6..1110e96 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.writeFieldAccess(fieldAccess); } + case TypescriptNewCall newCall -> { + this.writeNewCall(newCall); + } + case TypescriptNumberLiteral numberLiteral -> { this.writeNumberLiteral(numberLiteral); } @@ -258,6 +262,18 @@ public class TypescriptWriter implements AutoCloseable { this.writeIdentifier(fieldAccess.fieldName()); } + private void writeNewCall(TypescriptNewCall newCall) throws IOException { + this.writer.write("new "); + this.writeExpression(newCall.receiver()); + this.writer.write("("); + writeWithSeparator( + newCall.args(), + this::writeExpression, + () -> this.writer.write(", ") + ); + this.writer.write(")"); + } + private void writeNumberLiteral(TypescriptNumberLiteral numberLiteral) throws IOException { this.writer.write(Double.toString(numberLiteral.value())); } 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 58dab10..1cf2e81 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.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.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/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptNewCall.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptNewCall.java new file mode 100644 index 0000000..f9305bb --- /dev/null +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptNewCall.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.TypescriptNewCall imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNewCall imports + +public record TypescriptNewCall( + org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression receiver, + java.util.List<org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression> args +) implements org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression { + public static org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNewCall.Builder arbitrary() { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNewCall.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<org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression> args + ) implements org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression.Builder { + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNewCall build() { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNewCall(receiver, args); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNewCall.Builder withReceiver(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression receiver) { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNewCall.Builder(receiver, args); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNewCall.Builder withReceiver(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression.Builder receiver) { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNewCall.Builder(receiver.build(), args); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNewCall.Builder withArgs(java.util.List<org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression> args) { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNewCall.Builder(receiver, args); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNewCall.Builder addArg(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression arg) { + var args = new java.util.ArrayList<org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression>(this.args); + args.add(arg); + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNewCall.Builder(receiver, args); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNewCall.Builder addArg(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression.Builder arg) { + var args = new java.util.ArrayList<org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression>(this.args); + args.add(arg.build()); + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNewCall.Builder(receiver, args); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNewCall.Builder body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNewCall.Builder body + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNewCall body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNewCall body +} 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 b0c583f..718f6e4 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 @@ -299,6 +299,32 @@ public class TypescriptWriterTests { assertThat(string, equalTo("x.y")); } + // === New calls === + + @Test + public void newCallWithoutArgs() throws IOException { + var typescript = TypescriptNewCall.arbitrary() + .withReceiver(ref("A")) + .build(); + + var string = write(writer -> writer.writeExpression(typescript)); + + assertThat(string, equalTo("new A()")); + } + + @Test + public void newCallWithArgs() throws IOException { + var typescript = TypescriptNewCall.arbitrary() + .withReceiver(ref("A")) + .addArg(ref("b")) + .addArg(ref("c")) + .build(); + + var string = write(writer -> writer.writeExpression(typescript)); + + assertThat(string, equalTo("new A(b, c)")); + } + // === Number literals === @Test diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptNewCallMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptNewCallMatcher.java new file mode 100644 index 0000000..491b0de --- /dev/null +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptNewCallMatcher.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.TypescriptNewCallMatcher imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNewCallMatcher imports + +public final class TypescriptNewCallMatcher implements org.zwobble.precisely.Matcher<java.lang.Object> { + public static org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNewCallMatcher isTypescriptNewCall() { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNewCallMatcher(java.util.List.of()); + } + + private final java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNewCall>> submatchers; + + private TypescriptNewCallMatcher( + java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNewCall>> 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<java.lang.Object> toMatcher() { + return org.zwobble.precisely.Matchers.instanceOf(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNewCall.class, this.submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNewCallMatcher withReceiver(org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression> receiver) { + var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNewCall>>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("receiver", org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNewCall::receiver, (org.zwobble.precisely.Matcher) receiver)); + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNewCallMatcher(submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNewCallMatcher withArgs(org.zwobble.precisely.Matcher<? super java.util.List<org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression>> args) { + var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNewCall>>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("args", org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNewCall::args, (org.zwobble.precisely.Matcher) args)); + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNewCallMatcher(submatchers); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNewCallMatcher body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNewCallMatcher body +} |
