diff options
| author | Michael Williamson <mike@zwobble.org> | 2026-08-29 10:15:00 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2026-08-29 10:15:00 +0100 |
| commit | 2b887718befc50679896368ae6852335ffff49a6 (patch) | |
| tree | 72d1c0a7e1a68d08727b424bd26ae3410a97964f /src | |
| parent | a0c7aa1008f31e969fe868edf1f5408f3eaa959a (diff) | |
Add stubbed TypeScript code writer
Diffstat (limited to 'src')
4 files changed, 153 insertions, 0 deletions
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 new file mode 100644 index 0000000..a031a82 --- /dev/null +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/TypescriptWriter.java @@ -0,0 +1,48 @@ +package org.zwobble.hobgoblin.compiler.output.lang.typescript; + +import org.zwobble.hobgoblin.compiler.output.CodeWriter; +import org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptModule; + +import java.io.IOException; +import java.nio.file.Path; + +public class TypescriptWriter implements AutoCloseable { + private static final String LINE_COMMENT_START = "//"; + + public interface Write { + void write(TypescriptWriter writer) throws IOException; + } + + public static void writeTo(Path path, Write write) throws IOException { + CodeWriter.writeTo( + path, + LINE_COMMENT_START, + writer -> write.write(new TypescriptWriter(writer)) + ); + } + + public static void writeTo(CodeWriter.CodeWriterFile file, Write write) throws IOException { + CodeWriter.writeTo( + file, + LINE_COMMENT_START, + writer -> write.write(new TypescriptWriter(writer)) + ); + } + + private final CodeWriter writer; + + public TypescriptWriter(CodeWriter writer) { + this.writer = writer; + } + + @Override + public void close() throws IOException { + this.writer.close(); + } + + // == Modules == + + public void writeModule(TypescriptModule module) throws IOException { + this.writer.writeHeader(); + } +} 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 new file mode 100644 index 0000000..f66626b --- /dev/null +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptModule.java @@ -0,0 +1,24 @@ +// Generated by hobgoblin. + +package org.zwobble.hobgoblin.compiler.output.lang.typescript.ast; + +// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptModule imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptModule imports + +public record TypescriptModule() { + 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(); + } + + public record Builder() { + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptModule build() { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptModule(); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptModule.Builder body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptModule.Builder body + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptModule body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptModule 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 new file mode 100644 index 0000000..8712b4a --- /dev/null +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/TypescriptWriterTests.java @@ -0,0 +1,46 @@ +package org.zwobble.hobgoblin.compiler.output.lang.typescript; + +import org.junit.jupiter.api.Test; +import org.zwobble.hobgoblin.compiler.output.CodeWriter; +import org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptModule; + +import java.io.IOException; +import java.io.StringWriter; +import java.util.Optional; + +import static org.zwobble.precisely.AssertThat.assertThat; +import static org.zwobble.precisely.Matchers.equalTo; + +public class TypescriptWriterTests { + // == Modules == + + @Test + public void emptyModule() throws IOException { + var typescript = new TypescriptModule(); + + var string = write(writer -> writer.writeModule(typescript)); + + assertThat(string, equalTo(""" + // Generated by hobgoblin. + + """ + )); + } + + private String write(TypescriptWriter.Write write) throws IOException { + return write(write, Optional.empty()); + } + + private String write(TypescriptWriter.Write write, String originalContents) throws IOException { + return write(write, Optional.of(originalContents)); + } + + private String write(TypescriptWriter.Write write, Optional<String> originalContents) throws IOException { + var stringWriter = new StringWriter(); + TypescriptWriter.writeTo( + new CodeWriter.InMemoryCodeWriterFile(originalContents, stringWriter), + write + ); + return stringWriter.toString(); + } +} 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 new file mode 100644 index 0000000..088dba0 --- /dev/null +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptModuleMatcher.java @@ -0,0 +1,35 @@ +// Generated by hobgoblin. + +package org.zwobble.hobgoblin.compiler.output.lang.typescript.ast; + +// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptModuleMatcher imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptModuleMatcher imports + +public final class TypescriptModuleMatcher implements org.zwobble.precisely.Matcher<java.lang.Object> { + public static org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptModuleMatcher isTypescriptModule() { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptModuleMatcher(java.util.List.of()); + } + + private final java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptModule>> submatchers; + + private TypescriptModuleMatcher( + java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptModule>> 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<java.lang.Object> toMatcher() { + return org.zwobble.precisely.Matchers.instanceOf(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptModule.class, this.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 +} |
