diff options
| author | Michael Williamson <mike@zwobble.org> | 2026-08-30 15:16:58 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2026-08-30 15:16:58 +0100 |
| commit | 119324389887cf76e7e0f52bcb9c1067ede463d4 (patch) | |
| tree | 76fb573ae03d0689702194b00a706d87f4ece1ad /src/main/java | |
| parent | 3a6771834e55a64c04337bb0ee83fbfc57524edf (diff) | |
Add type parameters to class declaration in TypeScript AST
Diffstat (limited to 'src/main/java')
4 files changed, 76 insertions, 8 deletions
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 f2c76d0..b048e8d 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 @@ -160,6 +160,7 @@ public class TypescriptTypesGenerator implements Generator { return List.of( new TypescriptClassDeclaration( typescriptTypeName, + List.of(), body, structDefinition.docComment() ), 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 79ff43f..9e52843 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 @@ -86,6 +86,15 @@ public class TypescriptWriter implements AutoCloseable { this.writeDocComment(classDeclaration.docComment()); this.writer.write("class "); this.writeIdentifier(classDeclaration.name()); + if (!classDeclaration.typeParams().isEmpty()) { + this.writer.write("<"); + writeWithSeparator( + classDeclaration.typeParams(), + this::writeTypeParam, + () -> this.writer.write(", ") + ); + this.writer.write(">"); + } this.writer.write(" {"); this.writer.indent(); this.writer.newLine(); @@ -210,6 +219,10 @@ public class TypescriptWriter implements AutoCloseable { } } + private void writeTypeParam(TypescriptTypeParam typeParam) throws IOException { + this.writeIdentifier(typeParam.name()); + } + // == Visibility == private void writeVisibility(TypescriptVisibility visibility) throws IOException { diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptClassDeclaration.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptClassDeclaration.java index d509828..3e4d928 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptClassDeclaration.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptClassDeclaration.java @@ -7,48 +7,66 @@ package org.zwobble.hobgoblin.compiler.output.lang.typescript.ast; public record TypescriptClassDeclaration( org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptIdentifier name, + java.util.List<org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeParam> typeParams, java.util.List<org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassElement> body, org.zwobble.hobgoblin.compiler.ast.DocComment docComment ) implements org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptStatement { public static org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclaration.Builder arbitrary() { - return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclaration.Builder(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptIdentifier.arbitrary().build(), java.util.List.of(), org.zwobble.hobgoblin.compiler.ast.HobgoblinNativeAst.arbitraryDocComment()); + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclaration.Builder(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptIdentifier.arbitrary().build(), java.util.List.of(), java.util.List.of(), org.zwobble.hobgoblin.compiler.ast.HobgoblinNativeAst.arbitraryDocComment()); } public record Builder( org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptIdentifier name, + java.util.List<org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeParam> typeParams, java.util.List<org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassElement> body, org.zwobble.hobgoblin.compiler.ast.DocComment docComment ) implements org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptStatement.Builder { public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclaration build() { - return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclaration(name, body, docComment); + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclaration(name, typeParams, body, docComment); } public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclaration.Builder withName(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptIdentifier name) { - return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclaration.Builder(name, body, docComment); + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclaration.Builder(name, typeParams, body, docComment); } public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclaration.Builder withName(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptIdentifier.Builder name) { - return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclaration.Builder(name.build(), body, docComment); + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclaration.Builder(name.build(), typeParams, body, docComment); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclaration.Builder withTypeParams(java.util.List<org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeParam> typeParams) { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclaration.Builder(name, typeParams, body, docComment); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclaration.Builder addTypeParam(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeParam typeParam) { + var typeParams = new java.util.ArrayList<org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeParam>(this.typeParams); + typeParams.add(typeParam); + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclaration.Builder(name, typeParams, body, docComment); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclaration.Builder addTypeParam(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeParam.Builder typeParam) { + var typeParams = new java.util.ArrayList<org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeParam>(this.typeParams); + typeParams.add(typeParam.build()); + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclaration.Builder(name, typeParams, body, docComment); } public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclaration.Builder withBody(java.util.List<org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassElement> body) { - return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclaration.Builder(name, body, docComment); + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclaration.Builder(name, typeParams, body, docComment); } public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclaration.Builder addBodyElement(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassElement bodyElement) { var body = new java.util.ArrayList<org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassElement>(this.body); body.add(bodyElement); - return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclaration.Builder(name, body, docComment); + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclaration.Builder(name, typeParams, body, docComment); } public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclaration.Builder addBodyElement(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassElement.Builder bodyElement) { var body = new java.util.ArrayList<org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassElement>(this.body); body.add(bodyElement.build()); - return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclaration.Builder(name, body, docComment); + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclaration.Builder(name, typeParams, body, docComment); } public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclaration.Builder withDocComment(org.zwobble.hobgoblin.compiler.ast.DocComment docComment) { - return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclaration.Builder(name, body, docComment); + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclaration.Builder(name, typeParams, body, docComment); } // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclaration.Builder body diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptTypeParam.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptTypeParam.java new file mode 100644 index 0000000..e7efe46 --- /dev/null +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptTypeParam.java @@ -0,0 +1,36 @@ +// Generated by hobgoblin. + +package org.zwobble.hobgoblin.compiler.output.lang.typescript.ast; + +// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeParam imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeParam imports + +public record TypescriptTypeParam( + org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptIdentifier name +) { + public static org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeParam.Builder arbitrary() { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeParam.Builder(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptIdentifier.arbitrary().build()); + } + + public record Builder( + org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptIdentifier name + ) { + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeParam build() { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeParam(name); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeParam.Builder withName(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptIdentifier name) { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeParam.Builder(name); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeParam.Builder withName(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptIdentifier.Builder name) { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeParam.Builder(name.build()); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeParam.Builder body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeParam.Builder body + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeParam body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeParam body +} |
