diff options
| author | Michael Williamson <mike@zwobble.org> | 2026-08-30 09:25:33 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2026-08-30 09:25:33 +0100 |
| commit | 0469bc1b4ebcc22d118b7234bc7b62d3fa2f9ca4 (patch) | |
| tree | b6b7b20b26fda5800954199a1ab7541c51f16fb7 /src/main/java | |
| parent | 93fdddf21b33fb95f249348d6fe8d38f6564243c (diff) | |
Add constructor to TypeScript AST
Diffstat (limited to 'src/main/java')
4 files changed, 124 insertions, 1 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 597c00b..6a52f73 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 @@ -102,12 +102,30 @@ public class TypescriptWriter implements AutoCloseable { void writeClassElement(TypescriptClassElement element) throws IOException { switch (element) { + case TypescriptClassConstructor constructor -> { + this.writeClassConstructor(constructor); + } + case TypescriptClassFieldDeclaration fieldDeclaration -> { this.writeClassFieldDeclaration(fieldDeclaration); } } } + private void writeClassConstructor(TypescriptClassConstructor constructor) throws IOException { + this.writer.write("constructor("); + + writeWithSeparator( + constructor.params(), + this::writeParam, + () -> this.writer.write(", ") + ); + + this.writer.write(") {"); + this.writer.newLine(); + this.writer.write("}"); + } + private void writeClassFieldDeclaration( TypescriptClassFieldDeclaration fieldDeclaration ) throws IOException { @@ -126,6 +144,17 @@ public class TypescriptWriter implements AutoCloseable { this.writer.write(";"); } + // == Parameters == + + private void writeParam(TypescriptParam param) throws IOException { + this.writeIdentifier(param.name()); + + if (param.type().isPresent()) { + this.writer.write(": "); + this.writeExpression(param.type().get()); + } + } + // == Expressions == void writeExpression(TypescriptExpression expression) throws IOException { diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptClassConstructor.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptClassConstructor.java new file mode 100644 index 0000000..a8e7042 --- /dev/null +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptClassConstructor.java @@ -0,0 +1,44 @@ +// Generated by hobgoblin. + +package org.zwobble.hobgoblin.compiler.output.lang.typescript.ast; + +// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassConstructor imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassConstructor imports + +public record TypescriptClassConstructor( + java.util.List<org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptParam> params +) implements org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassElement { + public static org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassConstructor.Builder arbitrary() { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassConstructor.Builder(java.util.List.of()); + } + + public record Builder( + java.util.List<org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptParam> params + ) implements org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassElement.Builder { + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassConstructor build() { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassConstructor(params); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassConstructor.Builder withParams(java.util.List<org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptParam> params) { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassConstructor.Builder(params); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassConstructor.Builder addParam(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptParam param) { + var params = new java.util.ArrayList<org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptParam>(this.params); + params.add(param); + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassConstructor.Builder(params); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassConstructor.Builder addParam(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptParam.Builder param) { + var params = new java.util.ArrayList<org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptParam>(this.params); + params.add(param.build()); + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassConstructor.Builder(params); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassConstructor.Builder body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassConstructor.Builder body + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassConstructor body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassConstructor body +} diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptClassElement.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptClassElement.java index f1fbe1a..f31f71b 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptClassElement.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptClassElement.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.TypescriptClassElement imports // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassElement imports -public sealed interface TypescriptClassElement permits org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassFieldDeclaration { +public sealed interface TypescriptClassElement permits org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassConstructor, org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassFieldDeclaration { public interface Builder { public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassElement build(); diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptParam.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptParam.java new file mode 100644 index 0000000..588114f --- /dev/null +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptParam.java @@ -0,0 +1,50 @@ +// Generated by hobgoblin. + +package org.zwobble.hobgoblin.compiler.output.lang.typescript.ast; + +// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptParam imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptParam imports + +public record TypescriptParam( + org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptIdentifier name, + java.util.Optional<org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression> type +) { + public static org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptParam.Builder arbitrary() { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptParam.Builder(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptIdentifier.arbitrary().build(), java.util.Optional.empty()); + } + + public record Builder( + org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptIdentifier name, + java.util.Optional<org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression> type + ) { + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptParam build() { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptParam(name, type); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptParam.Builder withName(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptIdentifier name) { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptParam.Builder(name, type); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptParam.Builder withName(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptIdentifier.Builder name) { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptParam.Builder(name.build(), type); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptParam.Builder withType(java.util.Optional<org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression> type) { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptParam.Builder(name, type); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptParam.Builder withType(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression type) { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptParam.Builder(name, java.util.Optional.of(type)); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptParam.Builder withType(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression.Builder type) { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptParam.Builder(name, java.util.Optional.of(type.build())); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptParam.Builder body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptParam.Builder body + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptParam body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptParam body +} |
