diff options
| author | Michael Williamson <mike@zwobble.org> | 2026-09-04 15:22:06 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2026-09-04 15:25:14 +0100 |
| commit | 94ca8de6bc11f194bdf3a0415aad2eb66e222c45 (patch) | |
| tree | 691279348a97897379c03d8c5f66ea63333babb1 /src | |
| parent | 8b1ee9a26a378a4456e800be314ab765be94b368 (diff) | |
Add custom code to TypeScript output
Diffstat (limited to 'src')
4 files changed, 122 insertions, 12 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 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 +} 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 a5081d0..56c51f5 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 @@ -25,6 +25,11 @@ public class TypescriptWriterTests { assertThat(string, equalTo(""" // Generated by hobgoblin. + // Custom area start: imports + // Custom area end: imports + + // Custom area start: body + // Custom area end: body """ )); } @@ -45,11 +50,21 @@ public class TypescriptWriterTests { assertThat(string, equalTo(""" // Generated by hobgoblin. + // Custom area start: imports + // Custom area end: imports + class X { + // Custom area start: X, body + // Custom area end: X, body } class Y { + // Custom area start: Y, body + // Custom area end: Y, body } + + // Custom area start: body + // Custom area end: body """ )); } @@ -68,6 +83,8 @@ public class TypescriptWriterTests { assertThat(string, equalTo(""" class X { + // Custom area start: X, body + // Custom area end: X, body }""" )); } @@ -84,6 +101,8 @@ public class TypescriptWriterTests { assertThat(string, equalTo(""" class X<Y, Z> { + // Custom area start: X, body + // Custom area end: X, body }""" )); } @@ -110,6 +129,9 @@ public class TypescriptWriterTests { public a; public b; + + // Custom area start: X, body + // Custom area end: X, body }""" )); } @@ -129,6 +151,8 @@ public class TypescriptWriterTests { * def */ class X { + // Custom area start: X, body + // Custom area end: X, body }""" )); } diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptCustomAreaMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptCustomAreaMatcher.java new file mode 100644 index 0000000..8d8a077 --- /dev/null +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptCustomAreaMatcher.java @@ -0,0 +1,41 @@ +// Generated by hobgoblin. + +package org.zwobble.hobgoblin.compiler.output.lang.typescript.ast; + +// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptCustomAreaMatcher imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptCustomAreaMatcher imports + +public final class TypescriptCustomAreaMatcher implements org.zwobble.precisely.Matcher<java.lang.Object> { + public static org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptCustomAreaMatcher isTypescriptCustomArea() { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptCustomAreaMatcher(java.util.List.of()); + } + + private final java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptCustomArea>> submatchers; + + private TypescriptCustomAreaMatcher( + java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptCustomArea>> 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.TypescriptCustomArea.class, this.submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptCustomAreaMatcher withName(org.zwobble.precisely.Matcher<? super String> name) { + var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptCustomArea>>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("name", org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptCustomArea::name, (org.zwobble.precisely.Matcher) name)); + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptCustomAreaMatcher(submatchers); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptCustomAreaMatcher body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptCustomAreaMatcher body +} |
