diff options
| author | Michael Williamson <mike@zwobble.org> | 2026-08-29 22:23:25 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2026-08-29 22:23:25 +0100 |
| commit | 47eb39b664e9e7565086c2c6f5a79dd1d1b94a86 (patch) | |
| tree | 60e58809967db8e89339e5885b11b34c4f646216 /src/main/java | |
| parent | bd664e8cffcaf997631c9a8f5a5770d5a8bbe4d7 (diff) | |
Add body to Typescript modules
Diffstat (limited to 'src/main/java')
3 files changed, 43 insertions, 8 deletions
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<org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptIdentifier> name + java.util.List<org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptIdentifier> name, + java.util.List<org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptStatement> 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<org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptIdentifier> name + java.util.List<org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptIdentifier> name, + java.util.List<org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptStatement> 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<org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptIdentifier> 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<org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptIdentifier>(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<org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptIdentifier>(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<org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptStatement> 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<org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptStatement>(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<org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptStatement>(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 |
