summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--hobgoblin/src/output/lang/typescript/ast.hob7
-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
-rw-r--r--src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/TypescriptWriterTests.java27
-rw-r--r--src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptConstDeclarationMatcher.java53
6 files changed, 168 insertions, 1 deletions
diff --git a/hobgoblin/src/output/lang/typescript/ast.hob b/hobgoblin/src/output/lang/typescript/ast.hob
index 4a9057e..1af5ff2 100644
--- a/hobgoblin/src/output/lang/typescript/ast.hob
+++ b/hobgoblin/src/output/lang/typescript/ast.hob
@@ -14,6 +14,7 @@ struct TypescriptModule {
sum TypescriptStatement {
variant TypescriptClassDeclaration;
+ variant TypescriptConstDeclaration;
variant TypescriptExportNamesStatement;
variant TypescriptExpressionStatement;
variant TypescriptTypeAlias;
@@ -26,6 +27,12 @@ struct TypescriptClassDeclaration {
field docComment: DocComment;
}
+struct TypescriptConstDeclaration {
+ field name: TypescriptIdentifier;
+ field type: Option[TypescriptExpression];
+ field value: TypescriptExpression;
+}
+
struct TypescriptExportNamesStatement {
field names: List[TypescriptIdentifier];
}
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();
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 b45e5c4..5a7c488 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
@@ -133,6 +133,33 @@ public class TypescriptWriterTests {
));
}
+ // === Class declarations ===
+
+ @Test
+ public void constDeclarationWithoutExplicitType() throws IOException {
+ var typescript = TypescriptConstDeclaration.arbitrary()
+ .withName(id("x"))
+ .withValue(num(42))
+ .build();
+
+ var string = write(writer -> writer.writeStatement(typescript));
+
+ assertThat(string, equalTo("const x = 42.0;"));
+ }
+
+ @Test
+ public void constDeclarationWithExplicitType() throws IOException {
+ var typescript = TypescriptConstDeclaration.arbitrary()
+ .withName(id("x"))
+ .withType(ref("number"))
+ .withValue(num(42))
+ .build();
+
+ var string = write(writer -> writer.writeStatement(typescript));
+
+ assertThat(string, equalTo("const x: number = 42.0;"));
+ }
+
// === Export statements ===
@Test
diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptConstDeclarationMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptConstDeclarationMatcher.java
new file mode 100644
index 0000000..278d885
--- /dev/null
+++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptConstDeclarationMatcher.java
@@ -0,0 +1,53 @@
+// Generated by hobgoblin.
+
+package org.zwobble.hobgoblin.compiler.output.lang.typescript.ast;
+
+// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptConstDeclarationMatcher imports
+// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptConstDeclarationMatcher imports
+
+public final class TypescriptConstDeclarationMatcher implements org.zwobble.precisely.Matcher<java.lang.Object> {
+ public static org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptConstDeclarationMatcher isTypescriptConstDeclaration() {
+ return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptConstDeclarationMatcher(java.util.List.of());
+ }
+
+ private final java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptConstDeclaration>> submatchers;
+
+ private TypescriptConstDeclarationMatcher(
+ java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptConstDeclaration>> 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<java.lang.Object> toMatcher() {
+ return org.zwobble.precisely.Matchers.instanceOf(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptConstDeclaration.class, this.submatchers);
+ }
+
+ public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptConstDeclarationMatcher withName(org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptIdentifier> name) {
+ var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptConstDeclaration>>(this.submatchers);
+ submatchers.add(org.zwobble.precisely.Matchers.has("name", org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptConstDeclaration::name, (org.zwobble.precisely.Matcher) name));
+ return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptConstDeclarationMatcher(submatchers);
+ }
+
+ public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptConstDeclarationMatcher withType(org.zwobble.precisely.Matcher<? super java.util.Optional<org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression>> type) {
+ var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptConstDeclaration>>(this.submatchers);
+ submatchers.add(org.zwobble.precisely.Matchers.has("type", org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptConstDeclaration::type, (org.zwobble.precisely.Matcher) type));
+ return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptConstDeclarationMatcher(submatchers);
+ }
+
+ public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptConstDeclarationMatcher withValue(org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression> value) {
+ var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptConstDeclaration>>(this.submatchers);
+ submatchers.add(org.zwobble.precisely.Matchers.has("value", org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptConstDeclaration::value, (org.zwobble.precisely.Matcher) value));
+ return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptConstDeclarationMatcher(submatchers);
+ }
+
+ // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptConstDeclarationMatcher body
+ // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptConstDeclarationMatcher body
+}