diff options
| author | Michael Williamson <mike@zwobble.org> | 2026-08-29 22:34:24 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2026-08-29 22:34:24 +0100 |
| commit | c70c89053b6ff2888607fec04144f1a06e8c2e63 (patch) | |
| tree | fcfc36c11ba1f6d68cb9f03d46158d8ff4205608 /src/main/java | |
| parent | 47eb39b664e9e7565086c2c6f5a79dd1d1b94a86 (diff) | |
Add class field declarations to TypeScript AST
Diffstat (limited to 'src/main/java')
4 files changed, 111 insertions, 10 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 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<org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassElement> 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<org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassElement> 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<org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassElement> 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<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); + } + + 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); } // 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 +} |
