From 4bf4004fc90073d816d1431df5bc5845547b814e Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Fri, 4 Sep 2026 09:47:27 +0100 Subject: Add const declaration to TypeScript AST --- .../output/lang/typescript/TypescriptWriter.java | 20 ++++++++ .../typescript/ast/TypescriptConstDeclaration.java | 60 ++++++++++++++++++++++ .../lang/typescript/ast/TypescriptStatement.java | 2 +- 3 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptConstDeclaration.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 7719474..7523fe0 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 @@ -72,6 +72,10 @@ public class TypescriptWriter implements AutoCloseable { this.writeClassDeclaration(classDeclaration); } + case TypescriptConstDeclaration constDeclaration -> { + this.writeConstDeclaration(constDeclaration); + } + case TypescriptExportNamesStatement exportNamesStatement -> { this.writeExportNamesStatement(exportNamesStatement); } @@ -119,6 +123,22 @@ public class TypescriptWriter implements AutoCloseable { this.writer.write("}"); } + private void writeConstDeclaration( + TypescriptConstDeclaration constDeclaration + ) throws IOException { + this.writer.write("const "); + this.writeIdentifier(constDeclaration.name()); + + if (constDeclaration.type().isPresent()) { + this.writer.write(": "); + this.writeExpression(constDeclaration.type().get()); + } + + this.writer.write(" = "); + this.writeExpression(constDeclaration.value()); + this.writer.write(";"); + } + private void writeExportNamesStatement( TypescriptExportNamesStatement exportNamesStatement ) throws IOException { diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptConstDeclaration.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptConstDeclaration.java new file mode 100644 index 0000000..ee34043 --- /dev/null +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptConstDeclaration.java @@ -0,0 +1,60 @@ +// Generated by hobgoblin. + +package org.zwobble.hobgoblin.compiler.output.lang.typescript.ast; + +// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptConstDeclaration imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptConstDeclaration imports + +public record TypescriptConstDeclaration( + org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptIdentifier name, + java.util.Optional type, + org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression value +) implements org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptStatement { + public static org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptConstDeclaration.Builder arbitrary() { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptConstDeclaration.Builder(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptIdentifier.arbitrary().build(), java.util.Optional.empty(), org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNumberLiteral.arbitrary().build()); + } + + public record Builder( + org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptIdentifier name, + java.util.Optional type, + org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression value + ) implements org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptStatement.Builder { + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptConstDeclaration build() { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptConstDeclaration(name, type, value); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptConstDeclaration.Builder withName(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptIdentifier name) { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptConstDeclaration.Builder(name, type, value); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptConstDeclaration.Builder withName(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptIdentifier.Builder name) { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptConstDeclaration.Builder(name.build(), type, value); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptConstDeclaration.Builder withType(java.util.Optional type) { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptConstDeclaration.Builder(name, type, value); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptConstDeclaration.Builder withType(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression type) { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptConstDeclaration.Builder(name, java.util.Optional.of(type), value); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptConstDeclaration.Builder withType(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression.Builder type) { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptConstDeclaration.Builder(name, java.util.Optional.of(type.build()), value); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptConstDeclaration.Builder withValue(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression value) { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptConstDeclaration.Builder(name, type, value); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptConstDeclaration.Builder withValue(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression.Builder value) { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptConstDeclaration.Builder(name, type, value.build()); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptConstDeclaration.Builder body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptConstDeclaration.Builder body + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptConstDeclaration body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptConstDeclaration 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 5d687c0..d16a160 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, org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExportNamesStatement, org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpressionStatement, org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeAlias { +public sealed interface TypescriptStatement permits org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclaration, org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptConstDeclaration, org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExportNamesStatement, org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpressionStatement, org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeAlias { public interface Builder { public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptStatement build(); -- cgit v1.2.3