From 844903922facb51292735c6577296c7d5f17c1a9 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 30 Aug 2026 09:28:09 +0100 Subject: Add expression statement to TypeScript AST --- .../output/lang/typescript/TypescriptWriter.java | 11 ++++++ .../ast/TypescriptExpressionStatement.java | 36 +++++++++++++++++++ .../lang/typescript/ast/TypescriptStatement.java | 2 +- .../lang/typescript/TypescriptWriterTests.java | 13 +++++++ .../ast/TypescriptExpressionStatementMatcher.java | 41 ++++++++++++++++++++++ 5 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptExpressionStatement.java create mode 100644 src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptExpressionStatementMatcher.java (limited to 'src') 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 6a52f73..6cd116e 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 @@ -71,6 +71,10 @@ public class TypescriptWriter implements AutoCloseable { case TypescriptClassDeclaration classDeclaration -> { this.writeClassDeclaration(classDeclaration); } + + case TypescriptExpressionStatement expressionStatement -> { + this.writeExpressionStatement(expressionStatement); + } } } @@ -98,6 +102,13 @@ public class TypescriptWriter implements AutoCloseable { this.writer.write("}"); } + private void writeExpressionStatement( + TypescriptExpressionStatement expressionStatement + ) throws IOException { + this.writeExpression(expressionStatement.expression()); + this.writer.write(";"); + } + // == Class elements == void writeClassElement(TypescriptClassElement element) throws IOException { diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptExpressionStatement.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptExpressionStatement.java new file mode 100644 index 0000000..28c2f1e --- /dev/null +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptExpressionStatement.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.TypescriptExpressionStatement imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpressionStatement imports + +public record TypescriptExpressionStatement( + org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression expression +) implements org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptStatement { + public static org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpressionStatement.Builder arbitrary() { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpressionStatement.Builder(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNumberLiteral.arbitrary().build()); + } + + public record Builder( + org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression expression + ) implements org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptStatement.Builder { + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpressionStatement build() { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpressionStatement(expression); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpressionStatement.Builder withExpression(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression expression) { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpressionStatement.Builder(expression); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpressionStatement.Builder withExpression(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression.Builder expression) { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpressionStatement.Builder(expression.build()); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpressionStatement.Builder body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpressionStatement.Builder body + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpressionStatement body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpressionStatement body +} diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptStatement.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptStatement.java index 8bb978b..9a47c84 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptStatement.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptStatement.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.TypescriptStatement imports // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptStatement imports -public sealed interface TypescriptStatement permits org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclaration { +public sealed interface TypescriptStatement permits org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclaration, org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpressionStatement { public interface Builder { public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptStatement build(); 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 5e5c57a..82b76f0 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 @@ -113,6 +113,19 @@ public class TypescriptWriterTests { )); } + // === Expression statements === + + @Test + public void expressionStatement() throws IOException { + var typescript = TypescriptExpressionStatement.arbitrary() + .withExpression(TypescriptNumberLiteral.arbitrary().withValue(42)) + .build(); + + var string = write(writer -> writer.writeStatement(typescript)); + + assertThat(string, equalTo("42.0;")); + } + // == Class elements == // === Class constructors === diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptExpressionStatementMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptExpressionStatementMatcher.java new file mode 100644 index 0000000..043fb1c --- /dev/null +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptExpressionStatementMatcher.java @@ -0,0 +1,41 @@ +// Generated by hobgoblin. + +package org.zwobble.hobgoblin.compiler.output.lang.typescript.ast; + +// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpressionStatementMatcher imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpressionStatementMatcher imports + +public final class TypescriptExpressionStatementMatcher implements org.zwobble.precisely.Matcher { + public static org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpressionStatementMatcher isTypescriptExpressionStatement() { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpressionStatementMatcher(java.util.List.of()); + } + + private final java.util.List> submatchers; + + private TypescriptExpressionStatementMatcher( + java.util.List> submatchers + ) { + this.submatchers = submatchers; + } + + public org.zwobble.precisely.MatchResult match(java.lang.Object actual) { + return this.toMatcher().match(actual); + } + + public org.zwobble.precisely.TextTree describe() { + return this.toMatcher().describe(); + } + + private org.zwobble.precisely.Matcher toMatcher() { + return org.zwobble.precisely.Matchers.instanceOf(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpressionStatement.class, this.submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpressionStatementMatcher withExpression(org.zwobble.precisely.Matcher expression) { + var submatchers = new java.util.ArrayList>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("expression", org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpressionStatement::expression, (org.zwobble.precisely.Matcher) expression)); + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpressionStatementMatcher(submatchers); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpressionStatementMatcher body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpressionStatementMatcher body +} -- cgit v1.2.3