From 94ca8de6bc11f194bdf3a0415aad2eb66e222c45 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Fri, 4 Sep 2026 15:22:06 +0100 Subject: Add custom code to TypeScript output --- .../output/lang/typescript/TypescriptWriter.java | 37 +++++++++++++++------- .../lang/typescript/ast/TypescriptCustomArea.java | 32 +++++++++++++++++++ 2 files changed, 57 insertions(+), 12 deletions(-) create mode 100644 src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptCustomArea.java (limited to 'src/main/java') 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 ebb7aba..d177b58 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 @@ -48,20 +48,24 @@ public class TypescriptWriter implements AutoCloseable { public void writeModule(TypescriptModule module) throws IOException { this.writer.writeHeader(); - if (module.body().isEmpty()) { - return; - } + writeCustomArea(new TypescriptCustomArea("imports")); + this.writer.newLine(); - writeWithSeparator( - module.body(), - this::writeStatement, - () -> { - writer.newLine(); - writer.newLine(); - } - ); + if (!module.body().isEmpty()) { + writeWithSeparator( + module.body(), + this::writeStatement, + () -> { + writer.newLine(); + writer.newLine(); + } + ); - this.writer.newLine(); + this.writer.newLine(); + this.writer.newLine(); + } + + writeCustomArea(new TypescriptCustomArea("body")); } // == Statements == @@ -117,8 +121,11 @@ public class TypescriptWriter implements AutoCloseable { } ); this.writer.newLine(); + this.writer.newLine(); } + writeCustomArea(new TypescriptCustomArea(classDeclaration.name().value() + ", body")); + this.writer.dedent(); this.writer.write("}"); } @@ -440,4 +447,10 @@ public class TypescriptWriter implements AutoCloseable { private void writeIdentifier(TypescriptIdentifier name) throws IOException { this.writer.write(name.value()); } + + // == Custom areas == + + private void writeCustomArea(TypescriptCustomArea customArea) throws IOException { + this.writer.writeCustomArea(customArea.name()); + } } diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptCustomArea.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptCustomArea.java new file mode 100644 index 0000000..cd3fd54 --- /dev/null +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptCustomArea.java @@ -0,0 +1,32 @@ +// Generated by hobgoblin. + +package org.zwobble.hobgoblin.compiler.output.lang.typescript.ast; + +// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptCustomArea imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptCustomArea imports + +public record TypescriptCustomArea( + String name +) { + public static org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptCustomArea.Builder arbitrary() { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptCustomArea.Builder(""); + } + + public record Builder( + String name + ) { + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptCustomArea build() { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptCustomArea(name); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptCustomArea.Builder withName(String name) { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptCustomArea.Builder(name); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptCustomArea.Builder body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptCustomArea.Builder body + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptCustomArea body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptCustomArea body +} -- cgit v1.2.3