summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2026-08-29 22:23:25 +0100
committerMichael Williamson <mike@zwobble.org>2026-08-29 22:23:25 +0100
commit47eb39b664e9e7565086c2c6f5a79dd1d1b94a86 (patch)
tree60e58809967db8e89339e5885b11b34c4f646216
parentbd664e8cffcaf997631c9a8f5a5770d5a8bbe4d7 (diff)
Add body to Typescript modules
-rw-r--r--hobgoblin/src/output/lang/typescript/ast.hob2
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/output/generators/typescripttypes/TypescriptTypesGenerator.java2
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/TypescriptWriter.java17
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptModule.java32
-rw-r--r--src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/TypescriptWriterTests.java25
-rw-r--r--src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptModuleMatcher.java6
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<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
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<? super java.util.List<org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptStatement>> body) {
+ var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptModule>>(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
}