summaryrefslogtreecommitdiff
path: root/tools
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
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')
-rwxr-xr-xtools/format.ts13
-rw-r--r--tools/permission_prompt_test.ts2
-rw-r--r--tools/repl_test.py6
-rw-r--r--tools/ts_library_builder/build_library.ts98
-rw-r--r--tools/ts_library_builder/test.ts40
-rw-r--r--tools/util.ts3
-rw-r--r--tools/util_test.ts3
7 files changed, 115 insertions, 50 deletions
diff --git a/tools/format.ts b/tools/format.ts
index 8f86ea979..33241162b 100755
--- a/tools/format.ts
+++ b/tools/format.ts
@@ -1,27 +1,26 @@
#!/usr/bin/env deno --allow-read --allow-run
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
-import * as deno from "deno";
import { join } from "../js/deps/https/deno.land/x/std/fs/path.ts";
import { findFiles, lookupDenoPath } from "./util.ts";
const clangFormat = join("third_party", "depot_tools", "clang-format");
const gn = join("third_party", "depot_tools", "gn");
const yapf = join("third_party", "python_packages", "bin", "yapf");
-const rustfmt = join("third_party", "rustfmt", deno.platform.os, "rustfmt");
+const rustfmt = join("third_party", "rustfmt", Deno.platform.os, "rustfmt");
const rustfmtConfig = ".rustfmt.toml";
const decoder = new TextDecoder();
async function run(...args: string[]): Promise<void> {
- if (deno.platform.os === "win") {
+ if (Deno.platform.os === "win") {
args = ["cmd.exe", "/c", ...args];
}
- const p = deno.run({ args, stdout: "piped", stderr: "piped" });
+ const p = Deno.run({ args, stdout: "piped", stderr: "piped" });
const { code } = await p.status();
if (code !== 0) {
- console.log(decoder.decode(await deno.readAll(p.stderr)));
- console.log(decoder.decode(await deno.readAll(p.stdout)));
- deno.exit(code);
+ console.log(decoder.decode(await Deno.readAll(p.stderr)));
+ console.log(decoder.decode(await Deno.readAll(p.stdout)));
+ Deno.exit(code);
}
}
diff --git a/tools/permission_prompt_test.ts b/tools/permission_prompt_test.ts
index 7e8bfee3d..0d5b86451 100644
--- a/tools/permission_prompt_test.ts
+++ b/tools/permission_prompt_test.ts
@@ -1,5 +1,5 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
-import { args, listen, env, exit, makeTempDirSync, readFile, run } from "deno";
+const { args, listen, env, exit, makeTempDirSync, readFile, run } = Deno;
const name = args[1];
const test = {
diff --git a/tools/repl_test.py b/tools/repl_test.py
index 0958c18d0..3eb0858cd 100644
--- a/tools/repl_test.py
+++ b/tools/repl_test.py
@@ -28,7 +28,7 @@ class Repl(object):
p.stdin.flush()
time.sleep(sleep_)
if exit_:
- p.stdin.write(b'deno.exit(0)\n')
+ p.stdin.write(b'Deno.exit(0)\n')
else:
time.sleep(1) # wait to be killed by js
out, err = p.communicate()
@@ -73,7 +73,7 @@ class Repl(object):
assertEqual(code, 0)
def test_function(self):
- out, err, code = self.input("deno.writeFileSync")
+ out, err, code = self.input("Deno.writeFileSync")
assertEqual(out, '[Function: writeFileSync]\n')
assertEqual(err, '')
assertEqual(code, 0)
@@ -99,7 +99,7 @@ class Repl(object):
def test_set_timeout(self):
out, err, code = self.input(
- "setTimeout(() => { console.log('b'); deno.exit(0); }, 10)",
+ "setTimeout(() => { console.log('b'); Deno.exit(0); }, 10)",
"'a'",
exit=False)
assertEqual(out, '1\na\nb\n')
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
diff --git a/tools/ts_library_builder/test.ts b/tools/ts_library_builder/test.ts
index acc2c43db..d8cbec62b 100644
--- a/tools/ts_library_builder/test.ts
+++ b/tools/ts_library_builder/test.ts
@@ -79,14 +79,16 @@ test(function buildLibraryFlatten() {
debug,
declarationProject,
filePath: `${buildPath}/api.d.ts`,
- namespaceName: `"api"`,
+ moduleName: `"api"`,
+ namespaceName: "Api",
targetSourceFile
});
assert(targetSourceFile.getNamespace(`"api"`) != null);
- assertEqual(targetSourceFile.getNamespaces().length, 1);
- const namespaceApi = targetSourceFile.getNamespaceOrThrow(`"api"`);
- const functions = namespaceApi.getFunctions();
+ assert(targetSourceFile.getNamespace("Api") != null);
+ assertEqual(targetSourceFile.getNamespaces().length, 2);
+ const moduleApi = targetSourceFile.getNamespaceOrThrow(`"api"`);
+ const functions = moduleApi.getFunctions();
assertEqual(functions[0].getName(), "foo");
assertEqual(
functions[0]
@@ -104,12 +106,38 @@ test(function buildLibraryFlatten() {
""
);
assertEqual(functions.length, 2);
- const classes = namespaceApi.getClasses();
+ const classes = moduleApi.getClasses();
assertEqual(classes[0].getName(), "Foo");
assertEqual(classes.length, 1);
- const variableDeclarations = namespaceApi.getVariableDeclarations();
+ const variableDeclarations = moduleApi.getVariableDeclarations();
assertEqual(variableDeclarations[0].getName(), "arr");
assertEqual(variableDeclarations.length, 1);
+
+ const namespaceApi = targetSourceFile.getNamespaceOrThrow(`"api"`);
+ const functionsNs = namespaceApi.getFunctions();
+ assertEqual(functionsNs[0].getName(), "foo");
+ assertEqual(
+ functionsNs[0]
+ .getJsDocs()
+ .map(jsdoc => jsdoc.getInnerText())
+ .join("\n"),
+ "jsdoc for foo"
+ );
+ assertEqual(functionsNs[1].getName(), "bar");
+ assertEqual(
+ functionsNs[1]
+ .getJsDocs()
+ .map(jsdoc => jsdoc.getInnerText())
+ .join("\n"),
+ ""
+ );
+ assertEqual(functionsNs.length, 2);
+ const classesNs = namespaceApi.getClasses();
+ assertEqual(classesNs[0].getName(), "Foo");
+ assertEqual(classesNs.length, 1);
+ const variableDeclarationsNs = namespaceApi.getVariableDeclarations();
+ assertEqual(variableDeclarationsNs[0].getName(), "arr");
+ assertEqual(variableDeclarationsNs.length, 1);
});
test(function buildLibraryMerge() {
diff --git a/tools/util.ts b/tools/util.ts
index c055959fe..9d8ad0f1f 100644
--- a/tools/util.ts
+++ b/tools/util.ts
@@ -1,7 +1,8 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
-import { platform, lstatSync, readDirSync } from "deno";
import { join } from "../js/deps/https/deno.land/x/std/fs/path/mod.ts";
+const { platform, lstatSync, readDirSync } = Deno;
+
export interface FindOptions {
skip?: string[];
depth?: number;
diff --git a/tools/util_test.ts b/tools/util_test.ts
index 5a1d33617..95def4aeb 100644
--- a/tools/util_test.ts
+++ b/tools/util_test.ts
@@ -1,5 +1,4 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
-import * as deno from "deno";
import { assert, testPerm, assertEqual } from "../js/test_util.ts";
import { findFiles } from "./util.ts";
@@ -55,7 +54,7 @@ testPerm({ read: false }, function testFindFilesPerm() {
const files = findFiles([testDir], [".ts", ".md"]);
} catch (e) {
caughtError = true;
- assertEqual(e.kind, deno.ErrorKind.PermissionDenied);
+ assertEqual(e.kind, Deno.ErrorKind.PermissionDenied);
assertEqual(e.name, "PermissionDenied");
}
assert(caughtError);