From c70c89053b6ff2888607fec04144f1a06e8c2e63 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 29 Aug 2026 22:34:24 +0100 Subject: Add class field declarations to TypeScript AST --- .../output/lang/typescript/TypescriptWriter.java | 37 +++++++++++++++++++--- .../typescript/ast/TypescriptClassDeclaration.java | 30 ++++++++++++++---- .../typescript/ast/TypescriptClassElement.java | 18 +++++++++++ .../ast/TypescriptClassFieldDeclaration.java | 36 +++++++++++++++++++++ 4 files changed, 111 insertions(+), 10 deletions(-) create mode 100644 src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptClassElement.java create mode 100644 src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptClassFieldDeclaration.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 837de82..80ce51b 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 @@ -1,10 +1,7 @@ package org.zwobble.hobgoblin.compiler.output.lang.typescript; import org.zwobble.hobgoblin.compiler.output.CodeWriter; -import org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclaration; -import org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptIdentifier; -import org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptModule; -import org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptStatement; +import org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.*; import java.io.IOException; import java.nio.file.Path; @@ -80,10 +77,42 @@ public class TypescriptWriter implements AutoCloseable { this.writer.write("class "); this.writeIdentifier(classDeclaration.name()); this.writer.write(" {"); + this.writer.indent(); this.writer.newLine(); + + if (!classDeclaration.body().isEmpty()) { + writeWithSeparator( + classDeclaration.body(), + this::writeClassElement, + () -> { + this.writer.newLine(); + this.writer.newLine(); + } + ); + this.writer.newLine(); + } + + this.writer.dedent(); this.writer.write("}"); } + // == Class elements == + + void writeClassElement(TypescriptClassElement element) throws IOException { + switch (element) { + case TypescriptClassFieldDeclaration fieldDeclaration -> { + this.writeClassFieldDeclaration(fieldDeclaration); + } + } + } + + private void writeClassFieldDeclaration( + TypescriptClassFieldDeclaration fieldDeclaration + ) throws IOException { + this.writeIdentifier(fieldDeclaration.name()); + this.writer.write(";"); + } + // == Identifiers == private void writeIdentifier(TypescriptIdentifier name) 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 a2e992c..2b5a951 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 @@ -6,25 +6,43 @@ package org.zwobble.hobgoblin.compiler.output.lang.typescript.ast; // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclaration imports public record TypescriptClassDeclaration( - org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptIdentifier name + org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptIdentifier name, + java.util.List body ) 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()); + 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()); } public record Builder( - org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptIdentifier name + org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptIdentifier name, + java.util.List body ) 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); + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclaration(name, body); } 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); + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclaration.Builder(name, body); } 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()); + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclaration.Builder(name.build(), body); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclaration.Builder withBody(java.util.List body) { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclaration.Builder(name, body); + } + + 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(this.body); + body.add(bodyElement); + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclaration.Builder(name, body); + } + + 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(this.body); + body.add(bodyElement.build()); + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclaration.Builder(name, body); } // 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/TypescriptClassElement.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptClassElement.java new file mode 100644 index 0000000..f1fbe1a --- /dev/null +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptClassElement.java @@ -0,0 +1,18 @@ +// Generated by hobgoblin. + +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 interface Builder { + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassElement build(); + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassElement.Builder body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassElement.Builder body + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassElement body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassElement body +} diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptClassFieldDeclaration.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptClassFieldDeclaration.java new file mode 100644 index 0000000..19fa7ff --- /dev/null +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptClassFieldDeclaration.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.TypescriptClassFieldDeclaration imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassFieldDeclaration imports + +public record TypescriptClassFieldDeclaration( + org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptIdentifier name +) implements org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassElement { + public static org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassFieldDeclaration.Builder arbitrary() { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassFieldDeclaration.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 + ) implements org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassElement.Builder { + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassFieldDeclaration build() { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassFieldDeclaration(name); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassFieldDeclaration.Builder withName(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptIdentifier name) { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassFieldDeclaration.Builder(name); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassFieldDeclaration.Builder withName(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptIdentifier.Builder name) { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassFieldDeclaration.Builder(name.build()); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassFieldDeclaration.Builder body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassFieldDeclaration.Builder body + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassFieldDeclaration body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassFieldDeclaration body +} -- cgit v1.2.3