From d8771a35c842daab25d6e02df99855ef1e41b740 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 30 Aug 2026 08:58:39 +0100 Subject: Add optional type to TypeScript class field declarations --- .../lang/typescript/TypescriptWriterTests.java | 38 ++++++++++++++++++++ .../TypescriptClassFieldDeclarationMatcher.java | 6 ++++ .../lang/typescript/ast/TypescriptRefMatcher.java | 41 ++++++++++++++++++++++ 3 files changed, 85 insertions(+) create mode 100644 src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptRefMatcher.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 acb4fea..2d9b86f 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 @@ -128,6 +128,18 @@ public class TypescriptWriterTests { assertThat(string, equalTo("a;")); } + @Test + public void classFieldDeclarationWithExplicitType() throws IOException { + var typescript = TypescriptClassFieldDeclaration.arbitrary() + .withName(TypescriptIdentifier.of("a")) + .withType(TypescriptRef.arbitrary().withName(TypescriptIdentifier.of("number"))) + .build(); + + var string = write(writer -> writer.writeClassElement(typescript)); + + assertThat(string, equalTo("a: number;")); + } + @Test public void classFieldDeclarationWithInitialValue() throws IOException { var typescript = TypescriptClassFieldDeclaration.arbitrary() @@ -140,6 +152,19 @@ public class TypescriptWriterTests { assertThat(string, equalTo("a = 42.0;")); } + @Test + public void classFieldDeclarationWithTypeAndInitialValue() throws IOException { + var typescript = TypescriptClassFieldDeclaration.arbitrary() + .withName(TypescriptIdentifier.of("a")) + .withType(TypescriptRef.arbitrary().withName(TypescriptIdentifier.of("number"))) + .withInitialValue(TypescriptNumberLiteral.arbitrary().withValue(42)) + .build(); + + var string = write(writer -> writer.writeClassElement(typescript)); + + assertThat(string, equalTo("a: number = 42.0;")); + } + // == Expressions == // === Number literals === @@ -155,6 +180,19 @@ public class TypescriptWriterTests { assertThat(string, equalTo("42.0")); } + // === References === + + @Test + public void reference() throws IOException { + var typescript = TypescriptRef.arbitrary() + .withName(TypescriptIdentifier.of("x")) + .build(); + + var string = write(writer -> writer.writeExpression(typescript)); + + assertThat(string, equalTo("x")); + } + 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/TypescriptClassFieldDeclarationMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptClassFieldDeclarationMatcher.java index 558ad00..3942fb4 100644 --- a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptClassFieldDeclarationMatcher.java +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptClassFieldDeclarationMatcher.java @@ -36,6 +36,12 @@ public final class TypescriptClassFieldDeclarationMatcher implements org.zwobble return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassFieldDeclarationMatcher(submatchers); } + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassFieldDeclarationMatcher withType(org.zwobble.precisely.Matcher> type) { + var submatchers = new java.util.ArrayList>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("type", org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassFieldDeclaration::type, (org.zwobble.precisely.Matcher) type)); + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassFieldDeclarationMatcher(submatchers); + } + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassFieldDeclarationMatcher withInitialValue(org.zwobble.precisely.Matcher> initialValue) { var submatchers = new java.util.ArrayList>(this.submatchers); submatchers.add(org.zwobble.precisely.Matchers.has("initialValue", org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassFieldDeclaration::initialValue, (org.zwobble.precisely.Matcher) initialValue)); diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptRefMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptRefMatcher.java new file mode 100644 index 0000000..de86aff --- /dev/null +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptRefMatcher.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.TypescriptRefMatcher imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptRefMatcher imports + +public final class TypescriptRefMatcher implements org.zwobble.precisely.Matcher { + public static org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptRefMatcher isTypescriptRef() { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptRefMatcher(java.util.List.of()); + } + + private final java.util.List> submatchers; + + private TypescriptRefMatcher( + 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.TypescriptRef.class, this.submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptRefMatcher 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.TypescriptRef::name, (org.zwobble.precisely.Matcher) name)); + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptRefMatcher(submatchers); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptRefMatcher body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptRefMatcher body +} -- cgit v1.2.3