diff options
8 files changed, 249 insertions, 1 deletions
diff --git a/hobgoblin/src/output/lang/typescript/ast.hob b/hobgoblin/src/output/lang/typescript/ast.hob index 5438b8f..4a9057e 100644 --- a/hobgoblin/src/output/lang/typescript/ast.hob +++ b/hobgoblin/src/output/lang/typescript/ast.hob @@ -89,6 +89,7 @@ sum TypescriptExpression { variant TypescriptFieldAccess; variant TypescriptNewCall; variant TypescriptNumberLiteral; + variant TypescriptObjectLiteral; variant TypescriptRef; variant TypescriptTypeof; variant TypescriptTypeUnion; @@ -128,6 +129,15 @@ struct TypescriptNumberLiteral { field value: Float64; } +struct TypescriptObjectLiteral { + field properties: List[TypescriptObjectLiteralProperty]; +} + +struct TypescriptObjectLiteralProperty { + field name: TypescriptIdentifier; + field value: TypescriptExpression; +} + struct TypescriptRef { field name: TypescriptIdentifier; } 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<org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteralProperty> 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<org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteralProperty> 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<org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteralProperty> 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<org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteralProperty>(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<org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteralProperty>(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 +} 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<java.lang.Object> { + 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<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteral>> submatchers; + + private TypescriptObjectLiteralMatcher( + java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteral>> 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.TypescriptObjectLiteral.class, this.submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteralMatcher withProperties(org.zwobble.precisely.Matcher<? super java.util.List<org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteralProperty>> properties) { + var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteral>>(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<java.lang.Object> { + 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<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteralProperty>> submatchers; + + private TypescriptObjectLiteralPropertyMatcher( + java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteralProperty>> 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.TypescriptObjectLiteralProperty.class, this.submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteralPropertyMatcher withName(org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptIdentifier> name) { + var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteralProperty>>(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<? super org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression> value) { + var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteralProperty>>(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 +} |
