diff options
| author | Michael Williamson <mike@zwobble.org> | 2026-08-30 09:47:50 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2026-08-30 09:47:50 +0100 |
| commit | b27ce73f66a025ce16d7158c0283fff7b3bf7f1a (patch) | |
| tree | 9a217814438d612782844da9583867d2f37d8d60 | |
| parent | 83f64c3f47c95b746b2bb14ef26ee5533e5ed579 (diff) | |
Add field accesses to TypeScript AST
6 files changed, 124 insertions, 1 deletions
diff --git a/hobgoblin/src/output/lang/typescript/ast.hob b/hobgoblin/src/output/lang/typescript/ast.hob index a889fb3..9caf39b 100644 --- a/hobgoblin/src/output/lang/typescript/ast.hob +++ b/hobgoblin/src/output/lang/typescript/ast.hob @@ -56,6 +56,7 @@ struct TypescriptParam { sum TypescriptExpression { variant TypescriptBinaryOperation; + variant TypescriptFieldAccess; variant TypescriptNumberLiteral; variant TypescriptRef; } @@ -70,6 +71,11 @@ enum TypescriptBinaryOperator { variant assign; } +struct TypescriptFieldAccess { + field receiver: TypescriptExpression; + field fieldName: TypescriptIdentifier; +} + struct TypescriptNumberLiteral { field value: Float64; } 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 0c841de..b2674c6 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 @@ -188,6 +188,10 @@ public class TypescriptWriter implements AutoCloseable { this.writeBinaryOperation(binaryOperation); } + case TypescriptFieldAccess fieldAccess -> { + this.writeFieldAccess(fieldAccess); + } + case TypescriptNumberLiteral numberLiteral -> { this.writeNumberLiteral(numberLiteral); } @@ -212,6 +216,12 @@ public class TypescriptWriter implements AutoCloseable { }); } + private void writeFieldAccess(TypescriptFieldAccess fieldAccess) throws IOException { + this.writeExpression(fieldAccess.receiver()); + this.writer.write("."); + this.writeIdentifier(fieldAccess.fieldName()); + } + 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 99dc933..58dab10 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.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.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/TypescriptFieldAccess.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptFieldAccess.java new file mode 100644 index 0000000..61b74fc --- /dev/null +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptFieldAccess.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.TypescriptFieldAccess imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptFieldAccess imports + +public record TypescriptFieldAccess( + org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression receiver, + org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptIdentifier fieldName +) implements org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression { + public static org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptFieldAccess.Builder arbitrary() { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptFieldAccess.Builder(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNumberLiteral.arbitrary().build(), org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptIdentifier.arbitrary().build()); + } + + public record Builder( + org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression receiver, + org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptIdentifier fieldName + ) implements org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression.Builder { + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptFieldAccess build() { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptFieldAccess(receiver, fieldName); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptFieldAccess.Builder withReceiver(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression receiver) { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptFieldAccess.Builder(receiver, fieldName); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptFieldAccess.Builder withReceiver(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression.Builder receiver) { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptFieldAccess.Builder(receiver.build(), fieldName); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptFieldAccess.Builder withFieldName(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptIdentifier fieldName) { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptFieldAccess.Builder(receiver, fieldName); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptFieldAccess.Builder withFieldName(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptIdentifier.Builder fieldName) { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptFieldAccess.Builder(receiver, fieldName.build()); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptFieldAccess.Builder body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptFieldAccess.Builder body + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptFieldAccess body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptFieldAccess 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 29781b9..5c32084 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 @@ -248,6 +248,20 @@ public class TypescriptWriterTests { assertThat(string, equalTo("x = 42.0")); } + // === Field accesses === + + @Test + public void fieldAccess() throws IOException { + var typescript = TypescriptFieldAccess.arbitrary() + .withReceiver(ref("x")) + .withFieldName(id("y")) + .build(); + + var string = write(writer -> writer.writeExpression(typescript)); + + assertThat(string, equalTo("x.y")); + } + // === Number literals === @Test diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptFieldAccessMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptFieldAccessMatcher.java new file mode 100644 index 0000000..974f485 --- /dev/null +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptFieldAccessMatcher.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.TypescriptFieldAccessMatcher imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptFieldAccessMatcher imports + +public final class TypescriptFieldAccessMatcher implements org.zwobble.precisely.Matcher<java.lang.Object> { + public static org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptFieldAccessMatcher isTypescriptFieldAccess() { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptFieldAccessMatcher(java.util.List.of()); + } + + private final java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptFieldAccess>> submatchers; + + private TypescriptFieldAccessMatcher( + java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptFieldAccess>> 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.TypescriptFieldAccess.class, this.submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptFieldAccessMatcher 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.TypescriptFieldAccess>>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("receiver", org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptFieldAccess::receiver, (org.zwobble.precisely.Matcher) receiver)); + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptFieldAccessMatcher(submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptFieldAccessMatcher withFieldName(org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptIdentifier> fieldName) { + var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptFieldAccess>>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("fieldName", org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptFieldAccess::fieldName, (org.zwobble.precisely.Matcher) fieldName)); + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptFieldAccessMatcher(submatchers); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptFieldAccessMatcher body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptFieldAccessMatcher body +} |
