diff options
Diffstat (limited to 'tools/ts_library_builder')
-rw-r--r-- | tools/ts_library_builder/README.md | 11 | ||||
-rw-r--r-- | tools/ts_library_builder/ast_util.ts | 16 | ||||
-rw-r--r-- | tools/ts_library_builder/build_library.ts | 13 | ||||
-rw-r--r-- | tools/ts_library_builder/test.ts | 10 | ||||
-rw-r--r-- | tools/ts_library_builder/testdata/globals.ts | 2 |
5 files changed, 44 insertions, 8 deletions
diff --git a/tools/ts_library_builder/README.md b/tools/ts_library_builder/README.md index de1305dca..e2701f0fb 100644 --- a/tools/ts_library_builder/README.md +++ b/tools/ts_library_builder/README.md @@ -72,17 +72,16 @@ like this: - This process assumes that all the modules that feed `js/deno.ts` will have a public type API that does not have name conflicts. - We process the `js/globals.ts` file to generate the global namespace. - - Currently we create a `"globals"` module which will contain the type - definitions. - We create a `Window` interface and a `global` scope augmentation namespace. - We iterate over augmentations to the `window` variable declared in the file, extract the type information and apply it to both a global variable declaration and a property on the `Window` interface. + - We identify any type aliases in the module and declare them globally. - We take each namespace import to `js/globals.ts`, we resolve the emitted - declaration `.d.ts` file and create it as its own namespace withing the - `"globals"` module. It is unsafe to just flatten these, because there is a - high risk of collisions, but also, it makes authoring the types easier within - the generated interface and variable declarations. + declaration `.d.ts` file and create it as its own namespace within the global + scope. It is unsafe to just flatten these, because there is a high risk of + collisions, but also, it makes authoring the types easier within the generated + interface and variable declarations. - We then validate the resulting definition file and write it out to the appropriate build path. diff --git a/tools/ts_library_builder/ast_util.ts b/tools/ts_library_builder/ast_util.ts index 528036b99..85d0a3480 100644 --- a/tools/ts_library_builder/ast_util.ts +++ b/tools/ts_library_builder/ast_util.ts @@ -42,6 +42,22 @@ export function addSourceComment( ); } +/** Add a declaration of a type alias to a node */ +export function addTypeAlias( + node: StatementedNode, + name: string, + type: string, + hasDeclareKeyword = false, + jsdocs?: JSDoc[] +) { + return node.addTypeAlias({ + name, + type, + docs: jsdocs && jsdocs.map(jsdoc => jsdoc.getText()), + hasDeclareKeyword + }); +} + /** Add a declaration of a variable to a node */ export function addVariableDeclaration( node: StatementedNode, diff --git a/tools/ts_library_builder/build_library.ts b/tools/ts_library_builder/build_library.ts index af3fb599d..e1a64215f 100644 --- a/tools/ts_library_builder/build_library.ts +++ b/tools/ts_library_builder/build_library.ts @@ -21,7 +21,8 @@ import { loadFiles, logDiagnostics, namespaceSourceFile, - normalizeSlashes + normalizeSlashes, + addTypeAlias } from "./ast_util"; export interface BuildLibraryOptions { @@ -216,6 +217,16 @@ export function mergeGlobal({ addInterfaceProperty(interfaceDeclaration, property, type); } + // We need to copy over any type aliases + for (const typeAlias of sourceFile.getTypeAliases()) { + addTypeAlias( + targetSourceFile, + typeAlias.getName(), + typeAlias.getType().getText(sourceFile), + true + ); + } + // We need to ensure that we only namespace each source file once, so we // will use this map for tracking that. const sourceFileMap = new Map<SourceFile, string>(); diff --git a/tools/ts_library_builder/test.ts b/tools/ts_library_builder/test.ts index 3a18fe29c..71b1d19a0 100644 --- a/tools/ts_library_builder/test.ts +++ b/tools/ts_library_builder/test.ts @@ -149,7 +149,15 @@ test(function buildLibraryMerge() { variableDeclarations[4].getType().getText(), `typeof moduleD.reprocess` ); - assertEqual(variableDeclarations.length, 5); + assertEqual( + variableDeclarations[5].getType().getText(), + `typeof moduleC.Bar` + ); + assertEqual(variableDeclarations.length, 6); + const typeAliases = targetSourceFile.getTypeAliases(); + assertEqual(typeAliases[0].getName(), "Bar"); + assertEqual(typeAliases[0].getType().getText(), "moduleC.Bar"); + assertEqual(typeAliases.length, 1); }); // TODO author unit tests for `ast_util.ts` diff --git a/tools/ts_library_builder/testdata/globals.ts b/tools/ts_library_builder/testdata/globals.ts index e80862025..9d117c794 100644 --- a/tools/ts_library_builder/testdata/globals.ts +++ b/tools/ts_library_builder/testdata/globals.ts @@ -8,3 +8,5 @@ foobarbaz.bar = new moduleC.Bar(); foobarbaz.qat = moduleC.qat; foobarbaz.process = moduleE.process; foobarbaz.reprocess = moduleD.reprocess; +foobarbaz.Bar = moduleC.Bar; +export type Bar = moduleC.Bar; |