From 8dd50423a53f87f15464c467874684300b129ba8 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Fri, 4 Sep 2026 09:40:50 +0100 Subject: Add object literal to TypeScript AST --- .../lang/typescript/TypescriptWriterTests.java | 37 +++++++++++++++++ .../ast/TypescriptObjectLiteralMatcher.java | 41 +++++++++++++++++++ .../TypescriptObjectLiteralPropertyMatcher.java | 47 ++++++++++++++++++++++ 3 files changed, 125 insertions(+) create mode 100644 src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptObjectLiteralMatcher.java create mode 100644 src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptObjectLiteralPropertyMatcher.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 8348743..b45e5c4 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 @@ -7,6 +7,7 @@ import org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.*; import java.io.IOException; import java.io.StringWriter; +import java.util.List; import java.util.Optional; import static org.zwobble.precisely.AssertThat.assertThat; @@ -421,6 +422,42 @@ public class TypescriptWriterTests { assertThat(string, equalTo("42.0")); } + // === Object literals === + + @Test + public void objectLiteralEmpty() throws IOException { + var typescript = TypescriptObjectLiteral.arbitrary() + .build(); + + var string = write(writer -> writer.writeExpression(typescript)); + + assertThat(string, equalTo("{}")); + } + + @Test + public void objectLiteralWithProperties() throws IOException { + var typescript = TypescriptObjectLiteral.arbitrary() + .withProperties(List.of( + TypescriptObjectLiteralProperty.arbitrary() + .withName(TypescriptIdentifier.of("a")) + .withValue(ref("valA")) + .build(), + TypescriptObjectLiteralProperty.arbitrary() + .withName(TypescriptIdentifier.of("b")) + .withValue(ref("valB")) + .build() + )) + .build(); + + var string = write(writer -> writer.writeExpression(typescript)); + + assertThat(string, equalTo(""" + { + a: valA, + b: valB, + }""")); + } + // === References === @Test diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptObjectLiteralMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptObjectLiteralMatcher.java new file mode 100644 index 0000000..bd8edc3 --- /dev/null +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptObjectLiteralMatcher.java @@ -0,0 +1,41 @@ +// Generated by hobgoblin. + +package org.zwobble.hobgoblin.compiler.output.lang.typescript.ast; + +// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteralMatcher imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteralMatcher imports + +public final class TypescriptObjectLiteralMatcher implements org.zwobble.precisely.Matcher { + public static org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteralMatcher isTypescriptObjectLiteral() { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteralMatcher(java.util.List.of()); + } + + private final java.util.List> submatchers; + + private TypescriptObjectLiteralMatcher( + 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.TypescriptObjectLiteral.class, this.submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteralMatcher withProperties(org.zwobble.precisely.Matcher> properties) { + var submatchers = new java.util.ArrayList>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("properties", org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteral::properties, (org.zwobble.precisely.Matcher) properties)); + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteralMatcher(submatchers); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteralMatcher body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteralMatcher body +} diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptObjectLiteralPropertyMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptObjectLiteralPropertyMatcher.java new file mode 100644 index 0000000..86ea550 --- /dev/null +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptObjectLiteralPropertyMatcher.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.TypescriptObjectLiteralPropertyMatcher imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteralPropertyMatcher imports + +public final class TypescriptObjectLiteralPropertyMatcher implements org.zwobble.precisely.Matcher { + public static org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteralPropertyMatcher isTypescriptObjectLiteralProperty() { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteralPropertyMatcher(java.util.List.of()); + } + + private final java.util.List> submatchers; + + private TypescriptObjectLiteralPropertyMatcher( + 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.TypescriptObjectLiteralProperty.class, this.submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteralPropertyMatcher withName(org.zwobble.precisely.Matcher name) { + var submatchers = new java.util.ArrayList>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("name", org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteralProperty::name, (org.zwobble.precisely.Matcher) name)); + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteralPropertyMatcher(submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteralPropertyMatcher 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.TypescriptObjectLiteralProperty::value, (org.zwobble.precisely.Matcher) value)); + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteralPropertyMatcher(submatchers); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteralPropertyMatcher body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteralPropertyMatcher body +} -- cgit v1.2.3