From 0469bc1b4ebcc22d118b7234bc7b62d3fa2f9ca4 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 30 Aug 2026 09:25:33 +0100 Subject: Add constructor to TypeScript AST --- .../lang/typescript/TypescriptWriterTests.java | 67 +++++++++++++++++----- .../ast/TypescriptClassConstructorMatcher.java | 41 +++++++++++++ .../typescript/ast/TypescriptParamMatcher.java | 47 +++++++++++++++ 3 files changed, 141 insertions(+), 14 deletions(-) create mode 100644 src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptClassConstructorMatcher.java create mode 100644 src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptParamMatcher.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 2d9b86f..5e5c57a 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 @@ -32,10 +32,10 @@ public class TypescriptWriterTests { public void moduleWithStatements() throws IOException { var typescript = TypescriptModule.arbitrary() .addBodyElement(TypescriptClassDeclaration.arbitrary() - .withName(TypescriptIdentifier.of("X")) + .withName(id("X")) ) .addBodyElement(TypescriptClassDeclaration.arbitrary() - .withName(TypescriptIdentifier.of("Y")) + .withName(id("Y")) ) .build(); @@ -60,7 +60,7 @@ public class TypescriptWriterTests { @Test public void emptyClassDeclaration() throws IOException { var typescript = TypescriptClassDeclaration.arbitrary() - .withName(TypescriptIdentifier.of("X")) + .withName(id("X")) .build(); var string = write(writer -> writer.writeStatement(typescript)); @@ -74,12 +74,12 @@ public class TypescriptWriterTests { @Test public void classDeclarationWithElements() throws IOException { var typescript = TypescriptClassDeclaration.arbitrary() - .withName(TypescriptIdentifier.of("X")) + .withName(id("X")) .addBodyElement(TypescriptClassFieldDeclaration.arbitrary() - .withName(TypescriptIdentifier.of("a")) + .withName(id("a")) ) .addBodyElement(TypescriptClassFieldDeclaration.arbitrary() - .withName(TypescriptIdentifier.of("b")) + .withName(id("b")) ) .build(); @@ -97,7 +97,7 @@ public class TypescriptWriterTests { @Test public void classDeclarationWithDocComment() throws IOException { var typescript = TypescriptClassDeclaration.arbitrary() - .withName(TypescriptIdentifier.of("X")) + .withName(id("X")) .withDocComment(new DocComment("abc\ndef")) .build(); @@ -115,12 +115,45 @@ public class TypescriptWriterTests { // == Class elements == + // === Class constructors === + + @Test + public void emptyClassConstructor() throws IOException { + var typescript = TypescriptClassConstructor.arbitrary().build(); + + var string = write(writer -> writer.writeClassElement(typescript)); + + assertThat(string, equalTo(""" + constructor() { + }""")); + } + + @Test + public void classConstructorWithParams() throws IOException { + var typescript = TypescriptClassConstructor.arbitrary() + .addParam(TypescriptParam.arbitrary() + .withName(id("a")) + .withType(TypescriptRef.arbitrary().withName(id("number"))) + ) + .addParam(TypescriptParam.arbitrary() + .withName(id("b")) + .withType(TypescriptRef.arbitrary().withName(id("string"))) + ) + .build(); + + var string = write(writer -> writer.writeClassElement(typescript)); + + assertThat(string, equalTo(""" + constructor(a: number, b: string) { + }""")); + } + // === Class field declarations === @Test public void classFieldDeclarationWithoutInitialValue() throws IOException { var typescript = TypescriptClassFieldDeclaration.arbitrary() - .withName(TypescriptIdentifier.of("a")) + .withName(id("a")) .build(); var string = write(writer -> writer.writeClassElement(typescript)); @@ -131,8 +164,8 @@ public class TypescriptWriterTests { @Test public void classFieldDeclarationWithExplicitType() throws IOException { var typescript = TypescriptClassFieldDeclaration.arbitrary() - .withName(TypescriptIdentifier.of("a")) - .withType(TypescriptRef.arbitrary().withName(TypescriptIdentifier.of("number"))) + .withName(id("a")) + .withType(TypescriptRef.arbitrary().withName(id("number"))) .build(); var string = write(writer -> writer.writeClassElement(typescript)); @@ -143,7 +176,7 @@ public class TypescriptWriterTests { @Test public void classFieldDeclarationWithInitialValue() throws IOException { var typescript = TypescriptClassFieldDeclaration.arbitrary() - .withName(TypescriptIdentifier.of("a")) + .withName(id("a")) .withInitialValue(TypescriptNumberLiteral.arbitrary().withValue(42)) .build(); @@ -155,8 +188,8 @@ public class TypescriptWriterTests { @Test public void classFieldDeclarationWithTypeAndInitialValue() throws IOException { var typescript = TypescriptClassFieldDeclaration.arbitrary() - .withName(TypescriptIdentifier.of("a")) - .withType(TypescriptRef.arbitrary().withName(TypescriptIdentifier.of("number"))) + .withName(id("a")) + .withType(TypescriptRef.arbitrary().withName(id("number"))) .withInitialValue(TypescriptNumberLiteral.arbitrary().withValue(42)) .build(); @@ -185,7 +218,7 @@ public class TypescriptWriterTests { @Test public void reference() throws IOException { var typescript = TypescriptRef.arbitrary() - .withName(TypescriptIdentifier.of("x")) + .withName(id("x")) .build(); var string = write(writer -> writer.writeExpression(typescript)); @@ -193,6 +226,12 @@ public class TypescriptWriterTests { assertThat(string, equalTo("x")); } + // == Utilities == + + private static TypescriptIdentifier id(String name) { + return TypescriptIdentifier.of(name); + } + 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/TypescriptClassConstructorMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptClassConstructorMatcher.java new file mode 100644 index 0000000..081372d --- /dev/null +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptClassConstructorMatcher.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.TypescriptClassConstructorMatcher imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassConstructorMatcher imports + +public final class TypescriptClassConstructorMatcher implements org.zwobble.precisely.Matcher { + public static org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassConstructorMatcher isTypescriptClassConstructor() { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassConstructorMatcher(java.util.List.of()); + } + + private final java.util.List> submatchers; + + private TypescriptClassConstructorMatcher( + 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.TypescriptClassConstructor.class, this.submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassConstructorMatcher withParams(org.zwobble.precisely.Matcher> params) { + var submatchers = new java.util.ArrayList>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("params", org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassConstructor::params, (org.zwobble.precisely.Matcher) params)); + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassConstructorMatcher(submatchers); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassConstructorMatcher body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassConstructorMatcher body +} diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptParamMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptParamMatcher.java new file mode 100644 index 0000000..fc921ea --- /dev/null +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptParamMatcher.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.TypescriptParamMatcher imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptParamMatcher imports + +public final class TypescriptParamMatcher implements org.zwobble.precisely.Matcher { + public static org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptParamMatcher isTypescriptParam() { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptParamMatcher(java.util.List.of()); + } + + private final java.util.List> submatchers; + + private TypescriptParamMatcher( + 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.TypescriptParam.class, this.submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptParamMatcher 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.TypescriptParam::name, (org.zwobble.precisely.Matcher) name)); + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptParamMatcher(submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptParamMatcher 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.TypescriptParam::type, (org.zwobble.precisely.Matcher) type)); + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptParamMatcher(submatchers); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptParamMatcher body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptParamMatcher body +} -- cgit v1.2.3