From 5257c230be9bbbb1dad4d053d80405d29369a1c7 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 30 Aug 2026 09:33:49 +0100 Subject: Add assignment operations to TypeScript AST --- .../lang/typescript/TypescriptWriterTests.java | 47 +++++++++++++------ .../ast/TypescriptBinaryOperationMatcher.java | 53 ++++++++++++++++++++++ 2 files changed, 87 insertions(+), 13 deletions(-) create mode 100644 src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptBinaryOperationMatcher.java (limited to 'src/test/java') 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 82b76f0..690a676 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 @@ -118,7 +118,7 @@ public class TypescriptWriterTests { @Test public void expressionStatement() throws IOException { var typescript = TypescriptExpressionStatement.arbitrary() - .withExpression(TypescriptNumberLiteral.arbitrary().withValue(42)) + .withExpression(num(42)) .build(); var string = write(writer -> writer.writeStatement(typescript)); @@ -146,11 +146,11 @@ public class TypescriptWriterTests { var typescript = TypescriptClassConstructor.arbitrary() .addParam(TypescriptParam.arbitrary() .withName(id("a")) - .withType(TypescriptRef.arbitrary().withName(id("number"))) + .withType(ref("number")) ) .addParam(TypescriptParam.arbitrary() .withName(id("b")) - .withType(TypescriptRef.arbitrary().withName(id("string"))) + .withType(ref("string")) ) .build(); @@ -178,7 +178,7 @@ public class TypescriptWriterTests { public void classFieldDeclarationWithExplicitType() throws IOException { var typescript = TypescriptClassFieldDeclaration.arbitrary() .withName(id("a")) - .withType(TypescriptRef.arbitrary().withName(id("number"))) + .withType(ref("number")) .build(); var string = write(writer -> writer.writeClassElement(typescript)); @@ -190,7 +190,7 @@ public class TypescriptWriterTests { public void classFieldDeclarationWithInitialValue() throws IOException { var typescript = TypescriptClassFieldDeclaration.arbitrary() .withName(id("a")) - .withInitialValue(TypescriptNumberLiteral.arbitrary().withValue(42)) + .withInitialValue(num(42)) .build(); var string = write(writer -> writer.writeClassElement(typescript)); @@ -202,8 +202,8 @@ public class TypescriptWriterTests { public void classFieldDeclarationWithTypeAndInitialValue() throws IOException { var typescript = TypescriptClassFieldDeclaration.arbitrary() .withName(id("a")) - .withType(TypescriptRef.arbitrary().withName(id("number"))) - .withInitialValue(TypescriptNumberLiteral.arbitrary().withValue(42)) + .withType(ref("number")) + .withInitialValue(num(42)) .build(); var string = write(writer -> writer.writeClassElement(typescript)); @@ -211,15 +211,32 @@ public class TypescriptWriterTests { assertThat(string, equalTo("a: number = 42.0;")); } + private static TypescriptRef ref(String value) { + return TypescriptRef.arbitrary().withName(id(value)).build(); + } + // == Expressions == + // === Binary operations === + + @Test + public void assign() throws IOException { + var typescript = TypescriptBinaryOperation.arbitrary() + .withOperator(TypescriptBinaryOperator.ASSIGN) + .withLeft(ref("x")) + .withRight(num(42)) + .build(); + + var string = write(writer -> writer.writeExpression(typescript)); + + assertThat(string, equalTo("x = 42.0")); + } + // === Number literals === @Test public void numberLiteral() throws IOException { - var typescript = TypescriptNumberLiteral.arbitrary() - .withValue(42) - .build(); + var typescript = num(42); var string = write(writer -> writer.writeExpression(typescript)); @@ -230,9 +247,7 @@ public class TypescriptWriterTests { @Test public void reference() throws IOException { - var typescript = TypescriptRef.arbitrary() - .withName(id("x")) - .build(); + var typescript = ref("x"); var string = write(writer -> writer.writeExpression(typescript)); @@ -245,6 +260,12 @@ public class TypescriptWriterTests { return TypescriptIdentifier.of(name); } + private static TypescriptNumberLiteral num(int value) { + return TypescriptNumberLiteral.arbitrary() + .withValue(value) + .build(); + } + private String write(TypescriptWriter.Write write) throws IOException { return write(write, Optional.empty()); } diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptBinaryOperationMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptBinaryOperationMatcher.java new file mode 100644 index 0000000..a46c68d --- /dev/null +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptBinaryOperationMatcher.java @@ -0,0 +1,53 @@ +// Generated by hobgoblin. + +package org.zwobble.hobgoblin.compiler.output.lang.typescript.ast; + +// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptBinaryOperationMatcher imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptBinaryOperationMatcher imports + +public final class TypescriptBinaryOperationMatcher implements org.zwobble.precisely.Matcher { + public static org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptBinaryOperationMatcher isTypescriptBinaryOperation() { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptBinaryOperationMatcher(java.util.List.of()); + } + + private final java.util.List> submatchers; + + private TypescriptBinaryOperationMatcher( + 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.TypescriptBinaryOperation.class, this.submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptBinaryOperationMatcher withOperator(org.zwobble.precisely.Matcher operator) { + var submatchers = new java.util.ArrayList>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("operator", org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptBinaryOperation::operator, (org.zwobble.precisely.Matcher) operator)); + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptBinaryOperationMatcher(submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptBinaryOperationMatcher withLeft(org.zwobble.precisely.Matcher left) { + var submatchers = new java.util.ArrayList>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("left", org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptBinaryOperation::left, (org.zwobble.precisely.Matcher) left)); + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptBinaryOperationMatcher(submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptBinaryOperationMatcher withRight(org.zwobble.precisely.Matcher right) { + var submatchers = new java.util.ArrayList>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("right", org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptBinaryOperation::right, (org.zwobble.precisely.Matcher) right)); + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptBinaryOperationMatcher(submatchers); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptBinaryOperationMatcher body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptBinaryOperationMatcher body +} -- cgit v1.2.3