diff options
| author | Michael Williamson <mike@zwobble.org> | 2026-09-19 21:48:26 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2026-09-19 21:48:26 +0100 |
| commit | 3563b294bfaeb494511d6542301ea2a3b77091d5 (patch) | |
| tree | f97e823708849501b69252567315bc258b4f1ec7 /src/main/java | |
| parent | 72b49c434662cc5df5294c89eb349ae1dfb70972 (diff) | |
Add imports to TypeScript AST
Diffstat (limited to 'src/main/java')
5 files changed, 153 insertions, 9 deletions
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<TypescriptStatement> 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<org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNamedImport> 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<org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNamedImport> 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<org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNamedImport> 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<org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNamedImport>(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<org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNamedImport>(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<org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptIdentifier> name, + java.util.List<org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptImportDeclaration> imports, java.util.List<org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptStatement> 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<org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptIdentifier> name, + java.util.List<org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptImportDeclaration> imports, java.util.List<org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptStatement> 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<org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptIdentifier> 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<org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptIdentifier>(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<org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptIdentifier>(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<org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptImportDeclaration> 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<org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptImportDeclaration>(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<org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptImportDeclaration>(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<org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptStatement> 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<org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptStatement>(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<org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptStatement>(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 +} |
