summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2026-08-31 23:05:34 +0100
committerMichael Williamson <mike@zwobble.org>2026-08-31 23:05:34 +0100
commit9ae0aa5e9fe3ae8e52c48fc1ad23ee047752ff89 (patch)
treeaad16ffcaf395fe1fd0051d552b3617528f11624
parentb853399ce5ab5adb1a8899265d106ae1ab96bb35 (diff)
Add typeof to TypeScript AST
-rw-r--r--hobgoblin/src/output/lang/typescript/ast.hob5
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/TypescriptWriter.java9
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptExpression.java2
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptTypeof.java36
-rw-r--r--src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/TypescriptWriterTests.java13
-rw-r--r--src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptTypeofMatcher.java41
6 files changed, 105 insertions, 1 deletions
diff --git a/hobgoblin/src/output/lang/typescript/ast.hob b/hobgoblin/src/output/lang/typescript/ast.hob
index 64cf249..5438b8f 100644
--- a/hobgoblin/src/output/lang/typescript/ast.hob
+++ b/hobgoblin/src/output/lang/typescript/ast.hob
@@ -90,6 +90,7 @@ sum TypescriptExpression {
variant TypescriptNewCall;
variant TypescriptNumberLiteral;
variant TypescriptRef;
+ variant TypescriptTypeof;
variant TypescriptTypeUnion;
}
@@ -131,6 +132,10 @@ struct TypescriptRef {
field name: TypescriptIdentifier;
}
+struct TypescriptTypeof {
+ field expression: TypescriptExpression;
+}
+
struct TypescriptTypeUnion {
field variants: List[TypescriptExpression];
}
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 7931855..63e4e72 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
@@ -277,6 +277,10 @@ public class TypescriptWriter implements AutoCloseable {
this.writeRef(ref);
}
+ case TypescriptTypeof typeof -> {
+ this.writeTypeof(typeof);
+ }
+
case TypescriptTypeUnion typeUnion -> {
this.writeTypeUnion(typeUnion);
}
@@ -345,6 +349,11 @@ public class TypescriptWriter implements AutoCloseable {
this.writeIdentifier(ref.name());
}
+ private void writeTypeof(TypescriptTypeof typeof) throws IOException {
+ this.writer.write("typeof ");
+ this.writeExpression(typeof.expression());
+ }
+
private void writeTypeUnion(TypescriptTypeUnion typeUnion) throws IOException {
writeWithSeparator(
typeUnion.variants(),
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptExpression.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptExpression.java
index b8e79d3..0e5fe02 100644
--- a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptExpression.java
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptExpression.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.TypescriptExpression imports
// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression imports
-public sealed interface TypescriptExpression permits org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptBinaryOperation, org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptCall, org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptConstructedType, org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptFieldAccess, org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNewCall, org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNumberLiteral, org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptRef, org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeUnion {
+public sealed interface TypescriptExpression permits org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptBinaryOperation, org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptCall, org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptConstructedType, org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptFieldAccess, org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNewCall, org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNumberLiteral, org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptRef, org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeof, org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeUnion {
public interface Builder {
public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression build();
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptTypeof.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptTypeof.java
new file mode 100644
index 0000000..7410c1d
--- /dev/null
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptTypeof.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.TypescriptTypeof imports
+// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeof imports
+
+public record TypescriptTypeof(
+ org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression expression
+) implements org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression {
+ public static org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeof.Builder arbitrary() {
+ return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeof.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.TypescriptExpression.Builder {
+ public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeof build() {
+ return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeof(expression);
+ }
+
+ public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeof.Builder withExpression(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression expression) {
+ return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeof.Builder(expression);
+ }
+
+ public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeof.Builder withExpression(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression.Builder expression) {
+ return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeof.Builder(expression.build());
+ }
+
+ // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeof.Builder body
+ // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeof.Builder body
+ }
+
+ // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeof body
+ // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeof 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 b6f666e..8348743 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
@@ -432,6 +432,19 @@ public class TypescriptWriterTests {
assertThat(string, equalTo("x"));
}
+ // === typeof ===
+
+ @Test
+ public void typeof() throws IOException {
+ var typescript = TypescriptTypeof.arbitrary()
+ .withExpression(ref("x"))
+ .build();
+
+ var string = write(writer -> writer.writeExpression(typescript));
+
+ assertThat(string, equalTo("typeof x"));
+ }
+
// === Type unions ===
@Test
diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptTypeofMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptTypeofMatcher.java
new file mode 100644
index 0000000..88feb0b
--- /dev/null
+++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptTypeofMatcher.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.TypescriptTypeofMatcher imports
+// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeofMatcher imports
+
+public final class TypescriptTypeofMatcher implements org.zwobble.precisely.Matcher<java.lang.Object> {
+ public static org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeofMatcher isTypescriptTypeof() {
+ return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeofMatcher(java.util.List.of());
+ }
+
+ private final java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeof>> submatchers;
+
+ private TypescriptTypeofMatcher(
+ java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeof>> 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.TypescriptTypeof.class, this.submatchers);
+ }
+
+ public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeofMatcher withExpression(org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression> expression) {
+ var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeof>>(this.submatchers);
+ submatchers.add(org.zwobble.precisely.Matchers.has("expression", org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeof::expression, (org.zwobble.precisely.Matcher) expression));
+ return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeofMatcher(submatchers);
+ }
+
+ // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeofMatcher body
+ // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeofMatcher body
+}