summaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2026-09-04 09:47:27 +0100
committerMichael Williamson <mike@zwobble.org>2026-09-04 09:47:27 +0100
commit4bf4004fc90073d816d1431df5bc5845547b814e (patch)
tree00fadff43a80381e1e4ab2f63960373f03b8879c /src/main/java
parent8dd50423a53f87f15464c467874684300b129ba8 (diff)
Add const declaration to TypeScript AST
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/TypescriptWriter.java20
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptConstDeclaration.java60
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptStatement.java2
3 files changed, 81 insertions, 1 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 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<org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression> 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<org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression> 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<org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression> 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();