summaryrefslogtreecommitdiff
path: root/tools/ts_library_builder/build_library.ts
diff options
context:
space:
mode:
authorKitson Kelly <me@kitsonkelly.com>2019-02-13 02:08:56 +1100
committerRyan Dahl <ry@tinyclouds.org>2019-02-12 10:08:56 -0500
commita21a5ad2fa4dcbad58fe63c298c69f8607705bf4 (patch)
tree03e0092f46813ffdf84f53ab58f71b8a0276207e /tools/ts_library_builder/build_library.ts
parent1e5e091cb074896c7550b1b6f802582f12629048 (diff)
Add Deno global namespace (#1748)
Resolves #1705 This PR adds the Deno APIs as a global namespace named `Deno`. For backwards compatibility, the ability to `import * from "deno"` is preserved. I have tried to convert every test and internal code the references the module to use the namespace instead, but because I didn't break compatibility I am not sure. On the REPL, `deno` no longer exists, replaced only with `Deno` to align with the regular runtime. The runtime type library includes both the namespace and module. This means it duplicates the whole type information. When we remove the functionality from the runtime, it will be a one line change to the library generator to remove the module definition from the type library. I marked a `TODO` in a couple places where to remove the `"deno"` module, but there are additional places I know I didn't mark.
Diffstat (limited to 'tools/ts_library_builder/build_library.ts')
-rw-r--r--tools/ts_library_builder/build_library.ts98
1 files changed, 68 insertions, 30 deletions
diff --git a/tools/ts_library_builder/build_library.ts b/tools/ts_library_builder/build_library.ts
index 93d2da661..98a51f63f 100644
--- a/tools/ts_library_builder/build_library.ts
+++ b/tools/ts_library_builder/build_library.ts
@@ -98,7 +98,9 @@ interface FlattenOptions {
filePath: string;
debug?: boolean;
declarationProject: Project;
- namespaceName: string;
+ globalInterfaceName?: string;
+ moduleName?: string;
+ namespaceName?: string;
targetSourceFile: SourceFile;
}
@@ -109,10 +111,12 @@ export function flatten({
filePath,
debug,
declarationProject,
+ globalInterfaceName,
+ moduleName,
namespaceName,
targetSourceFile
}: FlattenOptions): void {
- // Flatten the source file into a single module declaration
+ // Flatten the source file into a single set of statements
const statements = flattenNamespace({
sourceFile: declarationProject.getSourceFileOrThrow(filePath),
rootPath: basePath,
@@ -120,15 +124,42 @@ export function flatten({
debug
});
- // Create the module in the target file
- const namespace = targetSourceFile.addNamespace({
- name: namespaceName,
- hasDeclareKeyword: true,
- declarationKind: NamespaceDeclarationKind.Module
- });
+ // If a module name is specified create the module in the target file
+ if (moduleName) {
+ const namespace = targetSourceFile.addNamespace({
+ name: moduleName,
+ hasDeclareKeyword: true,
+ declarationKind: NamespaceDeclarationKind.Module
+ });
+
+ // Add the output of the flattening to the namespace
+ namespace.addStatements(statements);
+ }
+
+ if (namespaceName) {
+ const namespace = targetSourceFile.insertNamespace(0, {
+ name: namespaceName,
+ hasDeclareKeyword: true,
+ declarationKind: NamespaceDeclarationKind.Namespace
+ });
+
+ // Add the output of the flattening to the namespace
+ namespace.addStatements(statements);
+
+ if (globalInterfaceName) {
+ // Retrieve the global interface
+ const interfaceDeclaration = targetSourceFile.getInterfaceOrThrow(
+ globalInterfaceName
+ );
- // Add the output of the flattening to the namespace
- namespace.addStatements(statements);
+ // Add the namespace to the global interface
+ addInterfaceProperty(
+ interfaceDeclaration,
+ namespaceName,
+ `typeof ${namespaceName}`
+ );
+ }
+ }
}
interface MergeGlobalOptions {
@@ -137,6 +168,7 @@ interface MergeGlobalOptions {
declarationProject: Project;
filePath: string;
globalVarName: string;
+ ignore?: string[];
inputProject: Project;
interfaceName: string;
targetSourceFile: SourceFile;
@@ -149,6 +181,7 @@ export function mergeGlobal({
declarationProject,
filePath,
globalVarName,
+ ignore,
inputProject,
interfaceName,
targetSourceFile
@@ -214,16 +247,18 @@ export function mergeGlobal({
// Create a global variable and add the property to the `Window` interface
// for each mutation of the `window` variable we observed in `globals.ts`
for (const [property, info] of globalVariables) {
- const type = info.type.getText(info.node);
- const typeSymbol = info.type.getSymbol();
- if (typeSymbol) {
- const valueDeclaration = typeSymbol.getValueDeclaration();
- if (valueDeclaration) {
- dependentSourceFiles.add(valueDeclaration.getSourceFile());
+ if (!(ignore && ignore.includes(property))) {
+ const type = info.type.getText(info.node);
+ const typeSymbol = info.type.getSymbol();
+ if (typeSymbol) {
+ const valueDeclaration = typeSymbol.getValueDeclaration();
+ if (valueDeclaration) {
+ dependentSourceFiles.add(valueDeclaration.getSourceFile());
+ }
}
+ addVariableDeclaration(targetSourceFile, property, type, true);
+ addInterfaceProperty(interfaceDeclaration, property, type);
}
- addVariableDeclaration(targetSourceFile, property, type, true);
- addInterfaceProperty(interfaceDeclaration, property, type);
}
// We need to copy over any type aliases
@@ -288,7 +323,7 @@ export function main({
debug,
outFile,
silent
-}: BuildLibraryOptions) {
+}: BuildLibraryOptions): void {
if (!silent) {
console.log("-----");
console.log("build_lib");
@@ -415,33 +450,36 @@ export function main({
}${msgGeneratedDtsText}\n`
};
- flatten({
+ mergeGlobal({
basePath,
- customSources,
debug,
declarationProject,
- filePath: `${basePath}/js/deno.d.ts`,
- namespaceName: `"deno"`,
+ filePath: `${basePath}/js/globals.ts`,
+ globalVarName: "window",
+ inputProject,
+ ignore: ["Deno"],
+ interfaceName: "Window",
targetSourceFile: libDTs
});
if (!silent) {
- console.log(`Created module "deno".`);
+ console.log(`Merged "globals" into global scope.`);
}
- mergeGlobal({
+ flatten({
basePath,
+ customSources,
debug,
declarationProject,
- filePath: `${basePath}/js/globals.ts`,
- globalVarName: "window",
- inputProject,
- interfaceName: "Window",
+ filePath: `${basePath}/js/deno.d.ts`,
+ globalInterfaceName: "Window",
+ moduleName: `"deno"`,
+ namespaceName: "Deno",
targetSourceFile: libDTs
});
if (!silent) {
- console.log(`Merged "globals" into global scope.`);
+ console.log(`Created module "deno" and namespace Deno.`);
}
// Inline any files that were passed in, to be used to add additional libs