diff options
| author | Michael Williamson <mike@zwobble.org> | 2026-09-04 10:30:23 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2026-09-04 10:30:23 +0100 |
| commit | 329b73a28689faa776c82a4e33195a05ef8337ad (patch) | |
| tree | 76dff8387f424c88be860bcd01156a96cadf4e9b /src | |
| parent | bc68be40f1486ed2f450b14cbc3991c36e754456 (diff) | |
Support enums in typescript-types
Diffstat (limited to 'src')
2 files changed, 71 insertions, 6 deletions
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/typescript/TypescriptGenerator.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/typescript/TypescriptGenerator.java index c26b73d..f19a641 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/typescript/TypescriptGenerator.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/typescript/TypescriptGenerator.java @@ -4,6 +4,7 @@ import org.zwobble.hobgoblin.compiler.builtins.NativeTypes; import org.zwobble.hobgoblin.compiler.output.lang.typescript.TypescriptWriter; import org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.*; import org.zwobble.hobgoblin.compiler.types.*; +import org.zwobble.hobgoblin.compiler.util.Casing; import java.io.File; import java.io.IOException; @@ -41,6 +42,10 @@ public class TypescriptGenerator { return new TypescriptIdentifier(name); } + public TypescriptIdentifier generateEnumVariantName(String name) { + return new TypescriptIdentifier(Casing.lowerCamelCaseToUpperCamelCase(name)); + } + public TypescriptExpression generateTypeExpression(Type value) { return switch (collapseType(value)) { case ConstructedType constructedType -> { diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/typescripttypes/TypescriptTypesGenerator.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/typescripttypes/TypescriptTypesGenerator.java index 16c0003..d7309eb 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/typescripttypes/TypescriptTypesGenerator.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/typescripttypes/TypescriptTypesGenerator.java @@ -11,6 +11,7 @@ import org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.*; import org.zwobble.hobgoblin.compiler.typechecker.TypesInfo; import org.zwobble.hobgoblin.compiler.types.SimpleStructType; import org.zwobble.hobgoblin.compiler.types.TypeOrConstructor; +import org.zwobble.hobgoblin.compiler.util.Lists; import org.zwobble.json5.reader.Json5ObjectReader; import java.io.IOException; @@ -117,7 +118,62 @@ public class TypescriptTypesGenerator implements Generator { private List<TypescriptStatement> generateEnumDefinition( TypedEnumDefinitionNode enumDefinition ) { - return List.of(); + var typescriptTypeName = this.typescriptGenerator.generateTypeName(enumDefinition.name()); + + return Lists.concat(List.of( + enumDefinition.variants().stream() + .<TypescriptStatement>map(variant -> new TypescriptConstDeclaration( + generateEnumVariantConstName(enumDefinition, variant), + Optional.of(new TypescriptRef(TypescriptIdentifier.of("unique symbol"))), + generateSymbol(this.typescriptGenerator.generateEnumVariantName(variant.name())) + )) + .toList(), + List.of( + new TypescriptConstDeclaration( + typescriptTypeName, + Optional.empty(), + new TypescriptObjectLiteral( + enumDefinition.variants().stream() + .map(variant -> { + var typescriptVariantName = this.typescriptGenerator.generateEnumVariantName(variant.name()); + var typescriptVariantConstRef = new TypescriptRef(this.generateEnumVariantConstName(enumDefinition, variant)); + return new TypescriptObjectLiteralProperty( + typescriptVariantName, + new TypescriptCast( + typescriptVariantConstRef, + new TypescriptTypeof(typescriptVariantConstRef) + ) + ); + }) + .toList() + ) + ), + new TypescriptTypeAlias( + typescriptTypeName, + new TypescriptTypeUnion( + enumDefinition.variants().stream() + .<TypescriptExpression>map(variant -> new TypescriptTypeof(new TypescriptRef( + this.generateEnumVariantConstName(enumDefinition, variant) + ))) + .toList() + ) + ), + new TypescriptExportNamesStatement(List.of( + typescriptTypeName + )) + ) + )); + } + + private TypescriptIdentifier generateEnumVariantConstName( + TypedEnumDefinitionNode enumDefinition, + TypedEnumVariantDefinitionNode variant + ) { + var typescriptEnumName = this.typescriptGenerator.generateTypeName(enumDefinition.name()); + + return TypescriptIdentifier.of( + typescriptEnumName.value() + "$" + this.typescriptGenerator.generateEnumVariantName(variant.name()).value() + ); } private List<TypescriptStatement> generateStructDefinition( @@ -147,11 +203,7 @@ public class TypescriptTypesGenerator implements Generator { true, true, Optional.empty(), - Optional.of(new TypescriptCall( - TypescriptTypes.SYMBOL, - // TODO: add type name as string - List.of() - )) + Optional.of(generateSymbol(typescriptTypeName)) )); body.add(new TypescriptClassFieldDeclaration( @@ -249,4 +301,12 @@ public class TypescriptTypesGenerator implements Generator { new TypescriptExportNamesStatement(List.of(TypescriptIdentifier.of("type " + typescriptTypeName.value()))) ); } + + private TypescriptExpression generateSymbol(TypescriptIdentifier name) { + return new TypescriptCall( + TypescriptTypes.SYMBOL, + // TODO: add type name as string + List.of() + ); + } } |
