From 47eb39b664e9e7565086c2c6f5a79dd1d1b94a86 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 29 Aug 2026 22:23:25 +0100 Subject: Add body to Typescript modules --- hobgoblin/src/output/lang/typescript/ast.hob | 2 ++ .../typescripttypes/TypescriptTypesGenerator.java | 2 +- .../output/lang/typescript/TypescriptWriter.java | 17 ++++++++++++ .../lang/typescript/ast/TypescriptModule.java | 32 +++++++++++++++++----- .../lang/typescript/TypescriptWriterTests.java | 25 +++++++++++++++++ .../typescript/ast/TypescriptModuleMatcher.java | 6 ++++ 6 files changed, 76 insertions(+), 8 deletions(-) diff --git a/hobgoblin/src/output/lang/typescript/ast.hob b/hobgoblin/src/output/lang/typescript/ast.hob index 1af6aca..fbb9f5b 100644 --- a/hobgoblin/src/output/lang/typescript/ast.hob +++ b/hobgoblin/src/output/lang/typescript/ast.hob @@ -4,6 +4,8 @@ struct TypescriptModule { // The paths of TypeScript modules don't need to be valid TypeScript // identifiers, but we have this constraint for simplicity. field name: List[TypescriptIdentifier]; + + field body: List[TypescriptStatement]; } // == Statements == diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/typescripttypes/TypescriptTypesGenerator.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/typescripttypes/TypescriptTypesGenerator.java index df308ab..aef1653 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/typescripttypes/TypescriptTypesGenerator.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/typescripttypes/TypescriptTypesGenerator.java @@ -74,6 +74,6 @@ public class TypescriptTypesGenerator implements Generator { .map(part -> this.typescriptGenerator.generateModuleName(part)) .toList(); - return new TypescriptModule(typescriptModuleName); + return new TypescriptModule(typescriptModuleName, List.of()); } } 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 c230705..837de82 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 @@ -9,6 +9,8 @@ import org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptState import java.io.IOException; import java.nio.file.Path; +import static org.zwobble.hobgoblin.compiler.output.CodeWriter.writeWithSeparator; + public class TypescriptWriter implements AutoCloseable { private static final String LINE_COMMENT_START = "//"; @@ -47,6 +49,21 @@ public class TypescriptWriter implements AutoCloseable { public void writeModule(TypescriptModule module) throws IOException { this.writer.writeHeader(); + + if (module.body().isEmpty()) { + return; + } + + writeWithSeparator( + module.body(), + this::writeStatement, + () -> { + writer.newLine(); + writer.newLine(); + } + ); + + this.writer.newLine(); } // == Statements == diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptModule.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptModule.java index cff3dd5..0ddcb6a 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptModule.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptModule.java @@ -6,33 +6,51 @@ package org.zwobble.hobgoblin.compiler.output.lang.typescript.ast; // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptModule imports public record TypescriptModule( - java.util.List name + java.util.List name, + java.util.List body ) { public static org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptModule.Builder arbitrary() { - return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptModule.Builder(java.util.List.of()); + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptModule.Builder(java.util.List.of(), java.util.List.of()); } public record Builder( - java.util.List name + java.util.List name, + java.util.List body ) { public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptModule build() { - return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptModule(name); + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptModule(name, body); } public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptModule.Builder withName(java.util.List name) { - return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptModule.Builder(name); + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptModule.Builder(name, body); } public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptModule.Builder addNameElement(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptIdentifier nameElement) { var name = new java.util.ArrayList(this.name); name.add(nameElement); - return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptModule.Builder(name); + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptModule.Builder(name, body); } public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptModule.Builder addNameElement(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptIdentifier.Builder nameElement) { var name = new java.util.ArrayList(this.name); name.add(nameElement.build()); - return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptModule.Builder(name); + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptModule.Builder(name, body); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptModule.Builder withBody(java.util.List body) { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptModule.Builder(name, body); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptModule.Builder addBodyElement(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptStatement bodyElement) { + var body = new java.util.ArrayList(this.body); + body.add(bodyElement); + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptModule.Builder(name, body); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptModule.Builder addBodyElement(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptStatement.Builder bodyElement) { + var body = new java.util.ArrayList(this.body); + body.add(bodyElement.build()); + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptModule.Builder(name, body); } // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptModule.Builder 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 a5cf539..a375b13 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 @@ -29,6 +29,31 @@ public class TypescriptWriterTests { )); } + @Test + public void moduleWithStatements() throws IOException { + var typescript = TypescriptModule.arbitrary() + .addBodyElement(TypescriptClassDeclaration.arbitrary() + .withName(TypescriptIdentifier.of("X")) + ) + .addBodyElement(TypescriptClassDeclaration.arbitrary() + .withName(TypescriptIdentifier.of("Y")) + ) + .build(); + + var string = write(writer -> writer.writeModule(typescript)); + + assertThat(string, equalTo(""" + // Generated by hobgoblin. + + class X { + } + + class Y { + } + """ + )); + } + // == Statements == // === Class declarations === diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptModuleMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptModuleMatcher.java index 2a9f2f5..c1bc764 100644 --- a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptModuleMatcher.java +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptModuleMatcher.java @@ -36,6 +36,12 @@ public final class TypescriptModuleMatcher implements org.zwobble.precisely.Matc return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptModuleMatcher(submatchers); } + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptModuleMatcher withBody(org.zwobble.precisely.Matcher> body) { + var submatchers = new java.util.ArrayList>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("body", org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptModule::body, body)); + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptModuleMatcher(submatchers); + } + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptModuleMatcher body // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptModuleMatcher body } -- cgit v1.2.3