From 3d075cb44b1902076a89e05f742b115dba19e489 Mon Sep 17 00:00:00 2001 From: Kitson Kelly Date: Fri, 9 Nov 2018 11:09:18 +1100 Subject: [PATCH] Ensure global type instances are available. --- js/compiler.ts | 3 +- js/globals.ts | 75 +++++++++++++------- js/headers_test.ts | 8 +++ js/main.ts | 3 + js/repl.ts | 4 +- tests/error_003_typescript.ts.out | 2 +- tests/error_008_checkjs.js.out | 2 +- tools/ts_library_builder/README.md | 11 ++- tools/ts_library_builder/ast_util.ts | 16 +++++ tools/ts_library_builder/build_library.ts | 13 +++- tools/ts_library_builder/test.ts | 10 ++- tools/ts_library_builder/testdata/globals.ts | 2 + 12 files changed, 112 insertions(+), 37 deletions(-) diff --git a/js/compiler.ts b/js/compiler.ts index 0462334429b9fe..d87309512375f6 100644 --- a/js/compiler.ts +++ b/js/compiler.ts @@ -6,11 +6,12 @@ import { assetSourceCode } from "./assets"; import * as deno from "./deno"; import { globalEval } from "./global_eval"; import { libdeno } from "./libdeno"; -import { window } from "./globals"; import * as os from "./os"; import { RawSourceMap } from "./types"; import { assert, log, notImplemented } from "./util"; +const window = globalEval("this"); + const EOL = "\n"; const ASSETS = "$asset$"; const LIB_RUNTIME = "lib.deno_runtime.d.ts"; diff --git a/js/globals.ts b/js/globals.ts index 5a0ca7cc6e8105..6f29d97d492555 100644 --- a/js/globals.ts +++ b/js/globals.ts @@ -1,23 +1,33 @@ // Copyright 2018 the Deno authors. All rights reserved. MIT license. +// This is a "special" module, in that it define the global runtime scope of +// Deno, and therefore it defines a lot of the runtime environemnt that code +// is evaluated in. We use this file to automatically build the runtime type +// library. + +// Modules which will make up part of the global public API surface should be +// imported as namespaces, so when the runtime tpye library is generated they +// can be expressed as a namespace in the type library. import * as blob from "./blob"; +import * as consoleTypes from "./console"; +import * as domTypes from "./dom_types"; import * as file from "./file"; -import * as formdata from "./form_data"; -import * as console_ from "./console"; -import * as fetch_ from "./fetch"; -import { Headers } from "./headers"; -import { globalEval } from "./global_eval"; -import { libdeno } from "./libdeno"; +import * as formData from "./form_data"; +import * as fetchTypes from "./fetch"; +import * as headers from "./headers"; import * as textEncoding from "./text_encoding"; import * as timers from "./timers"; import * as urlSearchParams from "./url_search_params"; -import * as domTypes from "./dom_types"; + +// These imports are not exposed and therefore are fine to just import the +// symbols required. +import { globalEval } from "./global_eval"; +import { libdeno } from "./libdeno"; // During the build process, augmentations to the variable `window` in this // file are tracked and created as part of default library that is built into -// deno, we only need to declare the enough to compile deno. - +// Deno, we only need to declare the enough to compile Deno. declare global { - const console: console_.Console; + const console: consoleTypes.Console; const setTimeout: typeof timers.setTimeout; // tslint:disable-next-line:variable-name const TextEncoder: typeof textEncoding.TextEncoder; @@ -25,26 +35,41 @@ declare global { // A reference to the global object. export const window = globalEval("this"); +// A self reference to the global object. window.window = window; -window.setTimeout = timers.setTimeout; -window.setInterval = timers.setInterval; -window.clearTimeout = timers.clearTimer; -window.clearInterval = timers.clearTimer; - -window.console = new console_.Console(libdeno.print); -window.TextEncoder = textEncoding.TextEncoder; -window.TextDecoder = textEncoding.TextDecoder; +// Globally available functions and object instances. window.atob = textEncoding.atob; window.btoa = textEncoding.btoa; +window.fetch = fetchTypes.fetch; +window.clearTimeout = timers.clearTimer; +window.clearInterval = timers.clearTimer; +window.console = new consoleTypes.Console(libdeno.print); +window.setTimeout = timers.setTimeout; +window.setInterval = timers.setInterval; +// When creating the runtime type library, we use modifications to `window` to +// determine what is in the global namespace. When we put a class in the +// namespace, we also need its global instance type as well, otherwise users +// won't be able to refer to instances. +// We have to export the type aliases, so that TypeScript _knows_ they are +// being used, which it cannot statically determine within this module. +window.Blob = blob.DenoBlob; +export type Blob = blob.DenoBlob; +window.File = file.DenoFile; +export type File = file.DenoFile; window.URLSearchParams = urlSearchParams.URLSearchParams; +export type URLSearchParams = urlSearchParams.URLSearchParams; -window.fetch = fetch_.fetch; +// Using the `as` keyword to use standard compliant interfaces as the Deno +// implementations contain some implementation details we wouldn't want to +// expose in the runtime type library. +window.Headers = headers.Headers as domTypes.HeadersConstructor; +export type Headers = domTypes.Headers; +window.FormData = formData.FormData as domTypes.FormDataConstructor; +export type FormData = domTypes.FormData; -// using the `as` keyword to mask the internal types when generating the -// runtime library -window.Headers = Headers as domTypes.HeadersConstructor; -window.Blob = blob.DenoBlob; -window.File = file.DenoFile; -window.FormData = formdata.FormData as domTypes.FormDataConstructor; +// While these are classes, they have their global instance types created in +// other type definitions, therefore we do not have to include them here. +window.TextEncoder = textEncoding.TextEncoder; +window.TextDecoder = textEncoding.TextDecoder; diff --git a/js/headers_test.ts b/js/headers_test.ts index 6980c3d7e93b34..e40efcda68a963 100644 --- a/js/headers_test.ts +++ b/js/headers_test.ts @@ -169,3 +169,11 @@ test(function headerSymbolIteratorSuccess() { assertEqual(value, headers.get(key)); } }); + +test(function headerTypesAvailable() { + function newHeaders(): Headers { + return new Headers(); + } + const headers = newHeaders(); + assert(headers instanceof Headers); +}); diff --git a/js/main.ts b/js/main.ts index 176f098c760de6..b0c7ed6d4d5a22 100644 --- a/js/main.ts +++ b/js/main.ts @@ -1,4 +1,7 @@ // Copyright 2018 the Deno authors. All rights reserved. MIT license. +// We need to make sure this module loads, for its side effects. +import "./globals"; + import * as flatbuffers from "./flatbuffers"; import * as msg from "gen/msg_generated"; import { assert, log, setLogDebug } from "./util"; diff --git a/js/repl.ts b/js/repl.ts index 3139330f6bf696..ae457e8e45ae68 100644 --- a/js/repl.ts +++ b/js/repl.ts @@ -6,7 +6,9 @@ import * as deno from "./deno"; import { close } from "./files"; import * as dispatch from "./dispatch"; import { exit } from "./os"; -import { window } from "./globals"; +import { globalEval } from "./global_eval"; + +const window = globalEval("this"); function startRepl(historyFile: string): number { const builder = flatbuffers.createBuilder(); diff --git a/tests/error_003_typescript.ts.out b/tests/error_003_typescript.ts.out index e2b5cae05f6224..20f5c95d0ddbdd 100644 --- a/tests/error_003_typescript.ts.out +++ b/tests/error_003_typescript.ts.out @@ -4,7 +4,7 @@ [WILDCARD]~~~~~~ $asset$/lib.deno_runtime.d.tsILDCARD] -[WILDCARD]declare const console: console_.Console; +[WILDCARD]declare const console: consoleTypes.Console; [WILDCARD]~~~~~~~ [WILDCARD]'console' is declared here. diff --git a/tests/error_008_checkjs.js.out b/tests/error_008_checkjs.js.out index 9220efaf1db984..793c2f68cd275c 100644 --- a/tests/error_008_checkjs.js.out +++ b/tests/error_008_checkjs.js.out @@ -4,7 +4,7 @@ [WILDCARD]~~~~~~ $asset$/lib.deno_runtime.d.tsILDCARD] -[WILDCARD]declare const console: console_.Console; +[WILDCARD]declare const console: consoleTypes.Console; [WILDCARD]~~~~~~~ [WILDCARD]'console' is declared here. diff --git a/tools/ts_library_builder/README.md b/tools/ts_library_builder/README.md index de1305dca9ce25..e2701f0fbdf871 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 528036b996c8ad..85d0a3480f9b5b 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 af3fb599d054cf..e1a64215f0c1f8 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(); diff --git a/tools/ts_library_builder/test.ts b/tools/ts_library_builder/test.ts index 3a18fe29c2f988..71b1d19a07f96c 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 e80862025a2bb6..9d117c7948eabd 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;