summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2026-08-30 09:43:56 +0100
committerMichael Williamson <mike@zwobble.org>2026-08-30 09:43:56 +0100
commit83f64c3f47c95b746b2bb14ef26ee5533e5ed579 (patch)
treeea84b36a88df78c0f629eadc48a11eea43294066 /src
parent5257c230be9bbbb1dad4d053d80405d29369a1c7 (diff)
Add body to constructors in TypeScript AST
Diffstat (limited to 'src')
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/TypescriptWriter.java14
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptClassConstructor.java32
-rw-r--r--src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/TypescriptWriterTests.java16
-rw-r--r--src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptClassConstructorMatcher.java6
4 files changed, 61 insertions, 7 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 ddad0b1..0c841de 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
@@ -133,6 +133,20 @@ public class TypescriptWriter implements AutoCloseable {
);
this.writer.write(") {");
+
+ if (!constructor.body().isEmpty()) {
+ this.writer.indent();
+ this.writer.newLine();
+
+ writeWithSeparator(
+ constructor.body(),
+ this::writeStatement,
+ () -> this.writer.newLine()
+ );
+
+ this.writer.dedent();
+ }
+
this.writer.newLine();
this.writer.write("}");
}
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
index a8e7042..76fc97c 100644
--- 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
@@ -6,33 +6,51 @@ package org.zwobble.hobgoblin.compiler.output.lang.typescript.ast;
// 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
+ java.util.List<org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptParam> params,
+ java.util.List<org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptStatement> body
) 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());
+ return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassConstructor.Builder(java.util.List.of(), java.util.List.of());
}
public record Builder(
- java.util.List<org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptParam> params
+ java.util.List<org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptParam> params,
+ java.util.List<org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptStatement> body
) 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);
+ return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassConstructor(params, body);
}
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);
+ return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassConstructor.Builder(params, body);
}
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);
+ return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassConstructor.Builder(params, body);
}
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);
+ return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassConstructor.Builder(params, body);
+ }
+
+ public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassConstructor.Builder withBody(java.util.List<org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptStatement> body) {
+ return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassConstructor.Builder(params, body);
+ }
+
+ public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassConstructor.Builder addBodyElement(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptStatement bodyElement) {
+ var body = new java.util.ArrayList<org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptStatement>(this.body);
+ body.add(bodyElement);
+ return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassConstructor.Builder(params, body);
+ }
+
+ public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassConstructor.Builder addBodyElement(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptStatement.Builder bodyElement) {
+ var body = new java.util.ArrayList<org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptStatement>(this.body);
+ body.add(bodyElement.build());
+ return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassConstructor.Builder(params, body);
}
// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassConstructor.Builder 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 690a676..29781b9 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
@@ -161,6 +161,22 @@ public class TypescriptWriterTests {
}"""));
}
+ @Test
+ public void classConstructorWithBody() throws IOException {
+ var typescript = TypescriptClassConstructor.arbitrary()
+ .addBodyElement(TypescriptExpressionStatement.arbitrary().withExpression(num(1)))
+ .addBodyElement(TypescriptExpressionStatement.arbitrary().withExpression(num(2)))
+ .build();
+
+ var string = write(writer -> writer.writeClassElement(typescript));
+
+ assertThat(string, equalTo("""
+ constructor() {
+ 1.0;
+ 2.0;
+ }"""));
+ }
+
// === Class field declarations ===
@Test
diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptClassConstructorMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptClassConstructorMatcher.java
index 081372d..7157675 100644
--- a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptClassConstructorMatcher.java
+++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptClassConstructorMatcher.java
@@ -36,6 +36,12 @@ public final class TypescriptClassConstructorMatcher implements org.zwobble.prec
return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassConstructorMatcher(submatchers);
}
+ public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassConstructorMatcher withBody(org.zwobble.precisely.Matcher<? super java.util.List<org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptStatement>> body) {
+ var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassConstructor>>(this.submatchers);
+ submatchers.add(org.zwobble.precisely.Matchers.has("body", org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassConstructor::body, (org.zwobble.precisely.Matcher) body));
+ return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassConstructorMatcher(submatchers);
+ }
+
// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassConstructorMatcher body
// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassConstructorMatcher body
}