diff options
| author | Michael Williamson <mike@zwobble.org> | 2026-08-30 11:17:45 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2026-08-30 11:17:45 +0100 |
| commit | a7d5133ac7cc82e3826f90d2e5f1ca5cd3f08676 (patch) | |
| tree | da8fa4c132bef9bc34788c2dd817bf7b13c87767 | |
| parent | 9de4242330fe137133327533f24073613da8d201 (diff) | |
Export generated TypeScript classes in typescript-types
6 files changed, 49 insertions, 9 deletions
diff --git a/examples/01-struct/output/typescript/package-lock.json b/examples/01-struct/output/typescript/package-lock.json index e2f6d78..7de1562 100644 --- a/examples/01-struct/output/typescript/package-lock.json +++ b/examples/01-struct/output/typescript/package-lock.json @@ -9,6 +9,7 @@ "version": "0.1.0", "devDependencies": { "@types/mocha": "^10.0.10", + "@types/node": "^26.4.0", "mocha": "^11.8.0", "typescript": "^7.0.2" } @@ -49,6 +50,16 @@ "dev": true, "license": "MIT" }, + "node_modules/@types/node": { + "version": "26.4.0", + "resolved": "https://registry.npmjs.org/@types/node/-/node-26.4.0.tgz", + "integrity": "sha512-faiGnoIrLH/V8cibOMEAZ8pMw6oXqSukl29ra4mN8GdaB2ZewzeaLj+INpV5N+Z1eKWzY+IzaIZH2EIR6YZRNQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "undici-types": "~8.3.0" + } + }, "node_modules/@typescript/typescript-aix-ppc64": { "version": "7.0.2", "resolved": "https://registry.npmjs.org/@typescript/typescript-aix-ppc64/-/typescript-aix-ppc64-7.0.2.tgz", @@ -1346,6 +1357,13 @@ "@typescript/typescript-win32-x64": "7.0.2" } }, + "node_modules/undici-types": { + "version": "8.3.0", + "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-8.3.0.tgz", + "integrity": "sha512-j375ScV60dom+YkPFIfTLcOiPxkN/buHz5GobjLhixFuANaNs3C9l4GmrWqejgXWJ7BbJcFYpTEUkS1Ge8bpZQ==", + "dev": true, + "license": "MIT" + }, "node_modules/which": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/which/-/which-2.0.2.tgz", diff --git a/examples/01-struct/output/typescript/src/point.test.ts b/examples/01-struct/output/typescript/src/point.test.ts index e69de29..d5e8533 100644 --- a/examples/01-struct/output/typescript/src/point.test.ts +++ b/examples/01-struct/output/typescript/src/point.test.ts @@ -0,0 +1,13 @@ +import {strictEqual} from "node:assert"; + +import {test} from "mocha"; + +import {Point} from "./point.ts"; + +test("pointFields", () => { + var point = new Point(10, 25); + var x: number = point.x; + var y: number = point.y; + strictEqual(x, 10); + strictEqual(y, 25); +}); diff --git a/examples/01-struct/output/typescript/src/point.ts b/examples/01-struct/output/typescript/src/point.ts index 81f1b83..0e843ce 100644 --- a/examples/01-struct/output/typescript/src/point.ts +++ b/examples/01-struct/output/typescript/src/point.ts @@ -14,6 +14,8 @@ class Line { } } +export {Line}; + /** * A 2D point. */ @@ -27,3 +29,5 @@ class Point { this.y = y; } } + +export {Point}; diff --git a/examples/templates/typescript/package.json b/examples/templates/typescript/package.json index b2bd443..9732b10 100644 --- a/examples/templates/typescript/package.json +++ b/examples/templates/typescript/package.json @@ -10,6 +10,7 @@ }, "devDependencies": { "@types/mocha": "^10.0.10", + "@types/node": "^26.4.0", "mocha": "^11.8.0", "typescript": "^7.0.2" } diff --git a/examples/templates/typescript/tsconfig.json b/examples/templates/typescript/tsconfig.json index 6a4848a..da0f363 100644 --- a/examples/templates/typescript/tsconfig.json +++ b/examples/templates/typescript/tsconfig.json @@ -3,7 +3,7 @@ "compilerOptions": { "module": "nodenext", "target": "esnext", - "types": [], + "types": ["node"], "sourceMap": true, "declaration": true, "declarationMap": true, @@ -16,6 +16,7 @@ "noUncheckedSideEffectImports": true, "moduleDetection": "force", "skipLibCheck": true, - "erasableSyntaxOnly": true + "erasableSyntaxOnly": true, + "allowImportingTsExtensions": true } } 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 c737d1c..68fe74d 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 @@ -77,13 +77,13 @@ public class TypescriptTypesGenerator implements Generator { .toList(); var statements = namespace.body().stream() - .map(statement -> generateNamespaceStatement(statement)) + .flatMap(statement -> generateNamespaceStatement(statement).stream()) .toList(); return new TypescriptModule(typescriptModuleName, statements); } - private TypescriptStatement generateNamespaceStatement(TypedNamespaceStatementNode statement) { + private List<TypescriptStatement> generateNamespaceStatement(TypedNamespaceStatementNode statement) { switch (statement) { case TypedEnumDefinitionNode _ -> { throw new UnsupportedOperationException("TODO"); @@ -103,7 +103,7 @@ public class TypescriptTypesGenerator implements Generator { } } - private TypescriptStatement generateStructDefinition(TypedStructDefinitionNode structDefinition) { + private List<TypescriptStatement> generateStructDefinition(TypedStructDefinitionNode structDefinition) { var typescriptTypeName = this.typescriptGenerator.generateTypeName(structDefinition.name()); var body = new ArrayList<TypescriptClassElement>(); @@ -135,10 +135,13 @@ public class TypescriptTypesGenerator implements Generator { .toList() )); - return new TypescriptClassDeclaration( - typescriptTypeName, - body, - structDefinition.docComment() + return List.of( + new TypescriptClassDeclaration( + typescriptTypeName, + body, + structDefinition.docComment() + ), + new TypescriptExportNamesStatement(List.of(typescriptTypeName)) ); } } |
