diff options
| author | Michael Williamson <mike@zwobble.org> | 2026-08-31 22:56:01 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2026-08-31 22:56:01 +0100 |
| commit | 30c7a202bbc3928cbc321974e417ef902e719692 (patch) | |
| tree | 65a835ded10f8d9f2403733404bb2fb6e8546df9 | |
| parent | 46005281330e2a001aba50a5c1358d9ae22334ec (diff) | |
Add type alias to TypeScript AST
6 files changed, 126 insertions, 1 deletions
diff --git a/hobgoblin/src/output/lang/typescript/ast.hob b/hobgoblin/src/output/lang/typescript/ast.hob index ab305c6..e421996 100644 --- a/hobgoblin/src/output/lang/typescript/ast.hob +++ b/hobgoblin/src/output/lang/typescript/ast.hob @@ -16,6 +16,7 @@ sum TypescriptStatement { variant TypescriptClassDeclaration; variant TypescriptExportNamesStatement; variant TypescriptExpressionStatement; + variant TypescriptTypeAlias; } struct TypescriptClassDeclaration { @@ -33,6 +34,11 @@ struct TypescriptExpressionStatement { field expression: TypescriptExpression; } +struct TypescriptTypeAlias { + field name: TypescriptIdentifier; + field type: TypescriptExpression; +} + // == Class elements == sum TypescriptClassElement { 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 93216bf..50d67bf 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 @@ -79,6 +79,10 @@ public class TypescriptWriter implements AutoCloseable { case TypescriptExpressionStatement expressionStatement -> { this.writeExpressionStatement(expressionStatement); } + + case TypescriptTypeAlias typeAlias -> { + this.writeTypeAlias(typeAlias); + } } } @@ -136,6 +140,14 @@ public class TypescriptWriter implements AutoCloseable { this.writer.write(";"); } + private void writeTypeAlias(TypescriptTypeAlias typeAlias) throws IOException { + this.writer.write("type "); + this.writeIdentifier(typeAlias.name()); + this.writer.write(" = "); + this.writeExpression(typeAlias.type()); + this.writer.write(";"); + } + // == Class elements == void writeClassElement(TypescriptClassElement element) throws IOException { diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptStatement.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptStatement.java index 6d24493..5d687c0 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptStatement.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptStatement.java @@ -5,7 +5,7 @@ package org.zwobble.hobgoblin.compiler.output.lang.typescript.ast; // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptStatement imports // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptStatement imports -public sealed interface TypescriptStatement permits org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclaration, org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExportNamesStatement, org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpressionStatement { +public sealed interface TypescriptStatement permits org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclaration, org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExportNamesStatement, org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpressionStatement, org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeAlias { public interface Builder { public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptStatement build(); diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptTypeAlias.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptTypeAlias.java new file mode 100644 index 0000000..0031a83 --- /dev/null +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptTypeAlias.java @@ -0,0 +1,46 @@ +// Generated by hobgoblin. + +package org.zwobble.hobgoblin.compiler.output.lang.typescript.ast; + +// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeAlias imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeAlias imports + +public record TypescriptTypeAlias( + org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptIdentifier name, + org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression type +) implements org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptStatement { + public static org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeAlias.Builder arbitrary() { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeAlias.Builder(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptIdentifier.arbitrary().build(), org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNumberLiteral.arbitrary().build()); + } + + public record Builder( + org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptIdentifier name, + org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression type + ) implements org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptStatement.Builder { + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeAlias build() { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeAlias(name, type); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeAlias.Builder withName(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptIdentifier name) { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeAlias.Builder(name, type); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeAlias.Builder withName(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptIdentifier.Builder name) { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeAlias.Builder(name.build(), type); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeAlias.Builder withType(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression type) { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeAlias.Builder(name, type); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeAlias.Builder withType(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression.Builder type) { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeAlias.Builder(name, type.build()); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeAlias.Builder body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeAlias.Builder body + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeAlias body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeAlias 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 c7d2e2d..56cbc6c 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 @@ -159,6 +159,20 @@ public class TypescriptWriterTests { assertThat(string, equalTo("42.0;")); } + // === Type aliases === + + @Test + public void typeAlias() throws IOException { + var typescript = TypescriptTypeAlias.arbitrary() + .withName(id("X")) + .withType(ref("number")) + .build(); + + var string = write(writer -> writer.writeStatement(typescript)); + + assertThat(string, equalTo("type X = number;")); + } + // == Class elements == // === Class constructors === diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptTypeAliasMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptTypeAliasMatcher.java new file mode 100644 index 0000000..039e67b --- /dev/null +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptTypeAliasMatcher.java @@ -0,0 +1,47 @@ +// Generated by hobgoblin. + +package org.zwobble.hobgoblin.compiler.output.lang.typescript.ast; + +// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeAliasMatcher imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeAliasMatcher imports + +public final class TypescriptTypeAliasMatcher implements org.zwobble.precisely.Matcher<java.lang.Object> { + public static org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeAliasMatcher isTypescriptTypeAlias() { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeAliasMatcher(java.util.List.of()); + } + + private final java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeAlias>> submatchers; + + private TypescriptTypeAliasMatcher( + java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeAlias>> 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.TypescriptTypeAlias.class, this.submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeAliasMatcher withName(org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptIdentifier> name) { + var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeAlias>>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("name", org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeAlias::name, (org.zwobble.precisely.Matcher) name)); + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeAliasMatcher(submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeAliasMatcher withType(org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression> type) { + var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeAlias>>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("type", org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeAlias::type, (org.zwobble.precisely.Matcher) type)); + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeAliasMatcher(submatchers); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeAliasMatcher body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeAliasMatcher body +} |
