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 --- .../output/lang/typescript/TypescriptWriter.java | 23 +++++++++++ .../lang/typescript/ast/TypescriptExpression.java | 2 +- .../typescript/ast/TypescriptObjectLiteral.java | 44 +++++++++++++++++++++ .../ast/TypescriptObjectLiteralProperty.java | 46 ++++++++++++++++++++++ 4 files changed, 114 insertions(+), 1 deletion(-) create mode 100644 src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptObjectLiteral.java create mode 100644 src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptObjectLiteralProperty.java (limited to 'src/main/java') 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 63e4e72..7719474 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 @@ -273,6 +273,10 @@ public class TypescriptWriter implements AutoCloseable { this.writeNumberLiteral(numberLiteral); } + case TypescriptObjectLiteral objectLiteral -> { + this.writeObjectLiteral(objectLiteral); + } + case TypescriptRef ref -> { this.writeRef(ref); } @@ -345,6 +349,25 @@ public class TypescriptWriter implements AutoCloseable { this.writer.write(Double.toString(numberLiteral.value())); } + private void writeObjectLiteral(TypescriptObjectLiteral objectLiteral) throws IOException { + this.writer.write("{"); + + if (!objectLiteral.properties().isEmpty()) { + this.writer.indent(); + for (var property : objectLiteral.properties()) { + this.writer.newLine(); + this.writeIdentifier(property.name()); + this.writer.write(": "); + this.writeExpression(property.value()); + this.writer.write(","); + } + this.writer.dedent(); + this.writer.newLine(); + } + + this.writer.write("}"); + } + private void writeRef(TypescriptRef ref) throws IOException { this.writeIdentifier(ref.name()); } 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 0e5fe02..e047bca 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.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, org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeof, org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeUnion { +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.TypescriptObjectLiteral, org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptRef, org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeof, org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeUnion { 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/TypescriptObjectLiteral.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptObjectLiteral.java new file mode 100644 index 0000000..a051986 --- /dev/null +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptObjectLiteral.java @@ -0,0 +1,44 @@ +// Generated by hobgoblin. + +package org.zwobble.hobgoblin.compiler.output.lang.typescript.ast; + +// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteral imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteral imports + +public record TypescriptObjectLiteral( + java.util.List properties +) implements org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression { + public static org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteral.Builder arbitrary() { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteral.Builder(java.util.List.of()); + } + + public record Builder( + java.util.List properties + ) implements org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression.Builder { + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteral build() { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteral(properties); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteral.Builder withProperties(java.util.List properties) { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteral.Builder(properties); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteral.Builder addPropertie(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteralProperty propertie) { + var properties = new java.util.ArrayList(this.properties); + properties.add(propertie); + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteral.Builder(properties); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteral.Builder addPropertie(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteralProperty.Builder propertie) { + var properties = new java.util.ArrayList(this.properties); + properties.add(propertie.build()); + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteral.Builder(properties); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteral.Builder body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteral.Builder body + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteral body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteral body +} diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptObjectLiteralProperty.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptObjectLiteralProperty.java new file mode 100644 index 0000000..984eb19 --- /dev/null +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptObjectLiteralProperty.java @@ -0,0 +1,46 @@ +// Generated by hobgoblin. + +package org.zwobble.hobgoblin.compiler.output.lang.typescript.ast; + +// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteralProperty imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteralProperty imports + +public record TypescriptObjectLiteralProperty( + org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptIdentifier name, + org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression value +) { + public static org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteralProperty.Builder arbitrary() { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteralProperty.Builder(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptIdentifier.arbitrary().build(), org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNumberLiteral.arbitrary().build()); + } + + public record Builder( + org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptIdentifier name, + org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression value + ) { + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteralProperty build() { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteralProperty(name, value); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteralProperty.Builder withName(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptIdentifier name) { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteralProperty.Builder(name, value); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteralProperty.Builder withName(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptIdentifier.Builder name) { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteralProperty.Builder(name.build(), value); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteralProperty.Builder withValue(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression value) { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteralProperty.Builder(name, value); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteralProperty.Builder withValue(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression.Builder value) { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteralProperty.Builder(name, value.build()); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteralProperty.Builder body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteralProperty.Builder body + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteralProperty body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteralProperty body +} -- cgit v1.2.3