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 --- .../typescripttypes/TypescriptTypesGenerator.java | 2 +- .../output/lang/typescript/TypescriptWriter.java | 17 ++++++++++++ .../lang/typescript/ast/TypescriptModule.java | 32 +++++++++++++++++----- 3 files changed, 43 insertions(+), 8 deletions(-) (limited to 'src/main/java') 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 -- cgit v1.2.3