From 3563b294bfaeb494511d6542301ea2a3b77091d5 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 19 Sep 2026 21:48:26 +0100 Subject: Add imports to TypeScript AST --- .../typescripttypes/TypescriptTypesGenerator.java | 2 +- .../output/lang/typescript/TypescriptWriter.java | 30 +++++++++++++ .../ast/TypescriptImportDeclaration.java | 50 ++++++++++++++++++++++ .../lang/typescript/ast/TypescriptModule.java | 34 +++++++++++---- .../lang/typescript/ast/TypescriptNamedImport.java | 46 ++++++++++++++++++++ 5 files changed, 153 insertions(+), 9 deletions(-) create mode 100644 src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptImportDeclaration.java create mode 100644 src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptNamedImport.java (limited to 'src/main/java') 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 89d6d5e..ac8e93a 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 @@ -89,7 +89,7 @@ public class TypescriptTypesGenerator implements Generator { .flatMap(statement -> generateNamespaceStatement(statement, typesInfo).stream()) .toList(); - return new TypescriptModule(typescriptModuleName, statements); + return new TypescriptModule(typescriptModuleName, List.of(), statements); } private List generateNamespaceStatement( 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 d177b58..34a6d99 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 @@ -51,6 +51,14 @@ public class TypescriptWriter implements AutoCloseable { writeCustomArea(new TypescriptCustomArea("imports")); this.writer.newLine(); + for (var importDeclaration : module.imports()) { + this.writeImportDeclaration(importDeclaration); + } + + if (!module.imports().isEmpty()) { + this.writer.newLine(); + } + if (!module.body().isEmpty()) { writeWithSeparator( module.body(), @@ -68,6 +76,28 @@ public class TypescriptWriter implements AutoCloseable { writeCustomArea(new TypescriptCustomArea("body")); } + private void writeImportDeclaration(TypescriptImportDeclaration importDeclaration) throws IOException { + this.writer.write("import {"); + + writeWithSeparator( + importDeclaration.namedImports(), + namedImport -> { + this.writeIdentifier(namedImport.exportName()); + this.writer.write(" as "); + this.writeIdentifier(namedImport.importName()); + }, + () -> this.writer.write(", ") + ); + + this.writer.write("} from "); + // TODO: proper string literals + this.writer.write("\""); + this.writer.write(importDeclaration.fromModule()); + this.writer.write("\""); + this.writer.write(";"); + this.writer.newLine(); + } + // == Statements == void writeStatement(TypescriptStatement statement) throws IOException { diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptImportDeclaration.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptImportDeclaration.java new file mode 100644 index 0000000..3bed5be --- /dev/null +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptImportDeclaration.java @@ -0,0 +1,50 @@ +// Generated by hobgoblin. + +package org.zwobble.hobgoblin.compiler.output.lang.typescript.ast; + +// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptImportDeclaration imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptImportDeclaration imports + +public record TypescriptImportDeclaration( + java.util.List namedImports, + String fromModule +) { + public static org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptImportDeclaration.Builder arbitrary() { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptImportDeclaration.Builder(java.util.List.of(), ""); + } + + public record Builder( + java.util.List namedImports, + String fromModule + ) { + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptImportDeclaration build() { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptImportDeclaration(namedImports, fromModule); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptImportDeclaration.Builder withNamedImports(java.util.List namedImports) { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptImportDeclaration.Builder(namedImports, fromModule); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptImportDeclaration.Builder addNamedImport(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNamedImport namedImport) { + var namedImports = new java.util.ArrayList(this.namedImports); + namedImports.add(namedImport); + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptImportDeclaration.Builder(namedImports, fromModule); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptImportDeclaration.Builder addNamedImport(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNamedImport.Builder namedImport) { + var namedImports = new java.util.ArrayList(this.namedImports); + namedImports.add(namedImport.build()); + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptImportDeclaration.Builder(namedImports, fromModule); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptImportDeclaration.Builder withFromModule(String fromModule) { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptImportDeclaration.Builder(namedImports, fromModule); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptImportDeclaration.Builder body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptImportDeclaration.Builder body + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptImportDeclaration body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptImportDeclaration body +} diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptModule.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptModule.java index 0ddcb6a..466e362 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptModule.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptModule.java @@ -7,50 +7,68 @@ package org.zwobble.hobgoblin.compiler.output.lang.typescript.ast; public record TypescriptModule( java.util.List name, + java.util.List imports, java.util.List body ) { public static org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptModule.Builder arbitrary() { - return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptModule.Builder(java.util.List.of(), java.util.List.of()); + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptModule.Builder(java.util.List.of(), java.util.List.of(), java.util.List.of()); } public record Builder( java.util.List name, + java.util.List imports, java.util.List body ) { public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptModule build() { - return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptModule(name, body); + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptModule(name, imports, body); } public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptModule.Builder withName(java.util.List name) { - return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptModule.Builder(name, body); + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptModule.Builder(name, imports, body); } public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptModule.Builder addNameElement(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptIdentifier nameElement) { var name = new java.util.ArrayList(this.name); name.add(nameElement); - return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptModule.Builder(name, body); + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptModule.Builder(name, imports, body); } public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptModule.Builder addNameElement(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptIdentifier.Builder nameElement) { var name = new java.util.ArrayList(this.name); name.add(nameElement.build()); - return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptModule.Builder(name, body); + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptModule.Builder(name, imports, body); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptModule.Builder withImports(java.util.List imports) { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptModule.Builder(name, imports, body); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptModule.Builder addImport(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptImportDeclaration import_) { + var imports = new java.util.ArrayList(this.imports); + imports.add(import_); + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptModule.Builder(name, imports, body); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptModule.Builder addImport(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptImportDeclaration.Builder import_) { + var imports = new java.util.ArrayList(this.imports); + imports.add(import_.build()); + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptModule.Builder(name, imports, body); } public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptModule.Builder withBody(java.util.List body) { - return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptModule.Builder(name, body); + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptModule.Builder(name, imports, body); } public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptModule.Builder addBodyElement(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptStatement bodyElement) { var body = new java.util.ArrayList(this.body); body.add(bodyElement); - return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptModule.Builder(name, body); + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptModule.Builder(name, imports, body); } public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptModule.Builder addBodyElement(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptStatement.Builder bodyElement) { var body = new java.util.ArrayList(this.body); body.add(bodyElement.build()); - return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptModule.Builder(name, body); + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptModule.Builder(name, imports, body); } // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptModule.Builder body diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptNamedImport.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptNamedImport.java new file mode 100644 index 0000000..0954db2 --- /dev/null +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptNamedImport.java @@ -0,0 +1,46 @@ +// Generated by hobgoblin. + +package org.zwobble.hobgoblin.compiler.output.lang.typescript.ast; + +// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNamedImport imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNamedImport imports + +public record TypescriptNamedImport( + org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptIdentifier exportName, + org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptIdentifier importName +) { + public static org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNamedImport.Builder arbitrary() { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNamedImport.Builder(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptIdentifier.arbitrary().build(), org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptIdentifier.arbitrary().build()); + } + + public record Builder( + org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptIdentifier exportName, + org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptIdentifier importName + ) { + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNamedImport build() { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNamedImport(exportName, importName); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNamedImport.Builder withExportName(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptIdentifier exportName) { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNamedImport.Builder(exportName, importName); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNamedImport.Builder withExportName(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptIdentifier.Builder exportName) { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNamedImport.Builder(exportName.build(), importName); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNamedImport.Builder withImportName(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptIdentifier importName) { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNamedImport.Builder(exportName, importName); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNamedImport.Builder withImportName(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptIdentifier.Builder importName) { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNamedImport.Builder(exportName, importName.build()); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNamedImport.Builder body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNamedImport.Builder body + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNamedImport body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNamedImport body +} -- cgit v1.2.3