From f83aee02e66214853a008ebe4b5141405ac3a950 Mon Sep 17 00:00:00 2001 From: Kitson Kelly Date: Sat, 1 Sep 2018 07:45:26 -0700 Subject: Bundle most types into globals.d.ts (#642) --- js/assets.ts | 33 +++------- js/compiler.ts | 8 +-- js/compiler_test.ts | 4 +- js/deno.ts | 1 + js/globals.ts | 10 +-- js/lib.globals.d.ts | 2 +- js/os.ts | 6 +- js/tsconfig.generated.json | 1 + js/types.d.ts | 147 ------------------------------------------- js/types.ts | 153 +++++++++++++++++++++++++++++++++++++++++++++ 10 files changed, 178 insertions(+), 187 deletions(-) delete mode 100644 js/types.d.ts create mode 100644 js/types.ts (limited to 'js') diff --git a/js/assets.ts b/js/assets.ts index 89b3e2645..e5ec507f7 100644 --- a/js/assets.ts +++ b/js/assets.ts @@ -1,21 +1,13 @@ // Copyright 2018 the Deno authors. All rights reserved. MIT license. // tslint:disable-next-line:no-reference -/// +/// // There is a rollup plugin that will inline any module ending with `!string` // tslint:disable:max-line-length // Generated definitions -import compilerDts from "gen/js/compiler.d.ts!string"; -import consoleDts from "gen/js/console.d.ts!string"; -import denoDts from "gen/js/deno.d.ts!string"; -import libdenoDts from "gen/js/libdeno.d.ts!string"; -import globalsDts from "gen/js/globals.d.ts!string"; -import osDts from "gen/js/os.d.ts!string"; -import fetchDts from "gen/js/fetch.d.ts!string"; -import timersDts from "gen/js/timers.d.ts!string"; -import utilDts from "gen/js/util.d.ts!string"; +import globalsDts from "gen/types/globals.d.ts!string"; // Static libraries import libEs2015Dts from "/third_party/node_modules/typescript/lib/lib.es2015.d.ts!string"; @@ -49,24 +41,16 @@ import libEsnextSymbolDts from "/third_party/node_modules/typescript/lib/lib.esn import libGlobalsDts from "/js/lib.globals.d.ts!string"; // Static definitions -import typescriptDts from "/third_party/node_modules/typescript/lib/typescript.d.ts!string"; -import typesDts from "/js/types.d.ts!string"; import fetchTypesDts from "/js/fetch_types.d.ts!string"; +import flatbuffersDts from "/third_party/node_modules/@types/flatbuffers/index.d.ts!string"; +import textEncodingDts from "/third_party/node_modules/@types/text-encoding/index.d.ts!string"; +import typescriptDts from "/third_party/node_modules/typescript/lib/typescript.d.ts!string"; // tslint:enable:max-line-length // prettier-ignore export const assetSourceCode: { [key: string]: string } = { // Generated definitions - "compiler.d.ts": compilerDts, - "console.d.ts": consoleDts, - "deno.d.ts": denoDts, - "libdeno.d.ts": libdenoDts, "globals.d.ts": globalsDts, - "os.d.ts": osDts, - "fetch.d.ts": fetchDts, - "fetch_types.d.ts": fetchTypesDts, - "timers.d.ts": timersDts, - "util.d.ts": utilDts, // Static libraries "lib.es2015.collection.d.ts": libEs2015CollectionDts, @@ -100,9 +84,8 @@ export const assetSourceCode: { [key: string]: string } = { "lib.globals.d.ts": libGlobalsDts, // Static definitions + "fetch-types.d.ts": fetchTypesDts, + "flatbuffers.d.ts": flatbuffersDts, + "text-encoding.d.ts": textEncodingDts, "typescript.d.ts": typescriptDts, - "types.d.ts": typesDts, - - // TODO Remove. - "msg_generated.d.ts": "", }; diff --git a/js/compiler.ts b/js/compiler.ts index 2e8c525b8..28fe8be8f 100644 --- a/js/compiler.ts +++ b/js/compiler.ts @@ -1,4 +1,5 @@ // Copyright 2018 the Deno authors. All rights reserved. MIT license. +/// import * as ts from "typescript"; import { assetSourceCode } from "./assets"; import * as deno from "./deno"; @@ -669,10 +670,9 @@ export class DenoCompiler implements ts.LanguageServiceHost { this._log("resolveModuleNames()", { moduleNames, containingFile }); return moduleNames.map(name => { let resolvedFileName; - if (name === "deno") { - resolvedFileName = this.resolveModuleName("deno.d.ts", ASSETS); - } else if (name === "compiler") { - resolvedFileName = this.resolveModuleName("compiler.d.ts", ASSETS); + if (name === "deno" || name === "compiler") { + // builtin modules are part of `globals.d.ts` + resolvedFileName = this.resolveModuleName("globals.d.ts", ASSETS); } else if (name === "typescript") { resolvedFileName = this.resolveModuleName("typescript.d.ts", ASSETS); } else { diff --git a/js/compiler_test.ts b/js/compiler_test.ts index 2fb67ab3b..18c2d16cc 100644 --- a/js/compiler_test.ts +++ b/js/compiler_test.ts @@ -474,7 +474,7 @@ test(function compilerFileExists() { "/root/project" ); assert(compilerInstance.fileExists(moduleMetaData.fileName)); - assert(compilerInstance.fileExists("$asset$/compiler.d.ts")); + assert(compilerInstance.fileExists("$asset$/globals.d.ts")); assertEqual( compilerInstance.fileExists("/root/project/unknown-module.ts"), false @@ -493,7 +493,7 @@ test(function compilerResolveModuleNames() { ["/root/project/foo/bar.ts", false], ["/root/project/foo/baz.ts", false], ["$asset$/lib.globals.d.ts", true], - ["$asset$/deno.d.ts", true] + ["$asset$/globals.d.ts", true] ]; for (let i = 0; i < results.length; i++) { const result = results[i]; diff --git a/js/deno.ts b/js/deno.ts index c9611d116..21766401b 100644 --- a/js/deno.ts +++ b/js/deno.ts @@ -1,5 +1,6 @@ // Copyright 2018 the Deno authors. All rights reserved. MIT license. // Public deno module. +/// export { env, exit, diff --git a/js/globals.ts b/js/globals.ts index 0364aaf98..b90aa46eb 100644 --- a/js/globals.ts +++ b/js/globals.ts @@ -2,7 +2,7 @@ import { Console } from "./console"; import * as timers from "./timers"; -import { TextDecoder, TextEncoder } from "./text_encoding"; +import * as textEncoding from "./text_encoding"; import * as fetch_ from "./fetch"; import { libdeno } from "./libdeno"; import { globalEval } from "./global-eval"; @@ -24,8 +24,8 @@ declare global { const fetch: typeof fetch_.fetch; // tslint:disable:variable-name - let TextEncoder: TextEncoder; - let TextDecoder: TextDecoder; + let TextEncoder: typeof textEncoding.TextEncoder; + let TextDecoder: typeof textEncoding.TextDecoder; // tslint:enable:variable-name } @@ -41,7 +41,7 @@ window.clearTimeout = timers.clearTimer; window.clearInterval = timers.clearTimer; window.console = new Console(libdeno.print); -window.TextEncoder = TextEncoder; -window.TextDecoder = TextDecoder; +window.TextEncoder = textEncoding.TextEncoder; +window.TextDecoder = textEncoding.TextDecoder; window.fetch = fetch_.fetch; diff --git a/js/lib.globals.d.ts b/js/lib.globals.d.ts index a1526c573..5243e4b5d 100644 --- a/js/lib.globals.d.ts +++ b/js/lib.globals.d.ts @@ -2,4 +2,4 @@ // This file contains the default TypeScript libraries for the deno runtime. /// /// -import "gen/js/globals"; +/// diff --git a/js/os.ts b/js/os.ts index db389fd66..60c5f8b1f 100644 --- a/js/os.ts +++ b/js/os.ts @@ -128,8 +128,8 @@ export function readFileSync(filename: string): Uint8Array { return new Uint8Array(dataArray!); } -function createEnv(_msg: fbs.EnvironRes): { [index:string]: string } { - const env: { [index:string]: string } = {}; +function createEnv(_msg: fbs.EnvironRes): { [index: string]: string } { + const env: { [index: string]: string } = {}; for (let i = 0; i < _msg.mapLength(); i++) { const item = _msg.map(i)!; @@ -169,7 +169,7 @@ function setEnv(key: string, value: string): void { * const newEnv = deno.env(); * console.log(env.TEST_VAR == newEnv.TEST_VAR); */ -export function env(): { [index:string]: string } { +export function env(): { [index: string]: string } { /* Ideally we could write const res = send({ command: fbs.Command.ENV, diff --git a/js/tsconfig.generated.json b/js/tsconfig.generated.json index d3cacd73e..8a8269d5b 100644 --- a/js/tsconfig.generated.json +++ b/js/tsconfig.generated.json @@ -7,6 +7,7 @@ "compilerOptions": { "declaration": true, "emitDeclarationOnly": true, + "module": "amd", "stripInternal": true }, "files": [ diff --git a/js/types.d.ts b/js/types.d.ts deleted file mode 100644 index 110873c6b..000000000 --- a/js/types.d.ts +++ /dev/null @@ -1,147 +0,0 @@ -// Copyright 2018 the Deno authors. All rights reserved. MIT license. -export type TypedArray = Uint8Array | Float32Array | Int32Array; - -export interface ModuleInfo { - moduleName: string | null; - filename: string | null; - sourceCode: string | null; - outputCode: string | null; -} - -// Following definitions adapted from: -// https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/node/index.d.ts -// Type definitions for Node.js 10.3.x -// Definitions by: Microsoft TypeScript -// DefinitelyTyped -// Parambir Singh -// Christian Vaagland Tellnes -// Wilco Bakker -// Nicolas Voigt -// Chigozirim C. -// Flarna -// Mariusz Wiktorczyk -// wwwy3y3 -// Deividas Bakanas -// Kelvin Jin -// Alvis HT Tang -// Sebastian Silbermann -// Hannes Magnusson -// Alberto Schiabel -// Klaus Meinhardt -// Huw -// Nicolas Even -// Bruno Scheufler -// Mohsen Azimi -// Hoàng Văn Khải -// Alexander T. -// Lishude -// Andrew Makarov - -export interface CallSite { - /** - * Value of "this" - */ - getThis(): any; - - /** - * Type of "this" as a string. - * This is the name of the function stored in the constructor field of - * "this", if available. Otherwise the object's [[Class]] internal - * property. - */ - getTypeName(): string | null; - - /** - * Current function - */ - getFunction(): Function | undefined; - - /** - * Name of the current function, typically its name property. - * If a name property is not available an attempt will be made to try - * to infer a name from the function's context. - */ - getFunctionName(): string | null; - - /** - * Name of the property [of "this" or one of its prototypes] that holds - * the current function - */ - getMethodName(): string | null; - - /** - * Name of the script [if this function was defined in a script] - */ - getFileName(): string | null; - - /** - * Get the script name or source URL for the source map - */ - getScriptNameOrSourceURL(): string; - - /** - * Current line number [if this function was defined in a script] - */ - getLineNumber(): number | null; - - /** - * Current column number [if this function was defined in a script] - */ - getColumnNumber(): number | null; - - /** - * A call site object representing the location where eval was called - * [if this function was created using a call to eval] - */ - getEvalOrigin(): string | undefined; - - /** - * Is this a toplevel invocation, that is, is "this" the global object? - */ - isToplevel(): boolean; - - /** - * Does this call take place in code defined by a call to eval? - */ - isEval(): boolean; - - /** - * Is this call in native V8 code? - */ - isNative(): boolean; - - /** - * Is this a constructor call? - */ - isConstructor(): boolean; -} - -export interface StartOfSourceMap { - file?: string; - sourceRoot?: string; -} - -export interface RawSourceMap extends StartOfSourceMap { - version: string; - sources: string[]; - names: string[]; - sourcesContent?: string[]; - mappings: string; -} - -declare global { - // Declare "static" methods in Error - interface ErrorConstructor { - /** Create .stack property on a target object */ - captureStackTrace(targetObject: Object, constructorOpt?: Function): void; - - /** - * Optional override for formatting stack traces - * - * @see https://github.com/v8/v8/wiki/Stack%20Trace%20API#customizing-stack-traces - */ - prepareStackTrace?: (err: Error, stackTraces: CallSite[]) => any; - - stackTraceLimit: number; - } -} diff --git a/js/types.ts b/js/types.ts new file mode 100644 index 000000000..7af0a5201 --- /dev/null +++ b/js/types.ts @@ -0,0 +1,153 @@ +// Copyright 2018 the Deno authors. All rights reserved. MIT license. +export type TypedArray = Uint8Array | Float32Array | Int32Array; + +export interface ModuleInfo { + moduleName: string | null; + filename: string | null; + sourceCode: string | null; + outputCode: string | null; +} + +// tslint:disable:max-line-length +// Following definitions adapted from: +// https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/node/index.d.ts +// Type definitions for Node.js 10.3.x +// Definitions by: Microsoft TypeScript +// DefinitelyTyped +// Parambir Singh +// Christian Vaagland Tellnes +// Wilco Bakker +// Nicolas Voigt +// Chigozirim C. +// Flarna +// Mariusz Wiktorczyk +// wwwy3y3 +// Deividas Bakanas +// Kelvin Jin +// Alvis HT Tang +// Sebastian Silbermann +// Hannes Magnusson +// Alberto Schiabel +// Klaus Meinhardt +// Huw +// Nicolas Even +// Bruno Scheufler +// Mohsen Azimi +// Hoàng Văn Khải +// Alexander T. +// Lishude +// Andrew Makarov +// tslint:enable:max-line-length + +export interface CallSite { + /** + * Value of "this" + */ + // tslint:disable-next-line:no-any + getThis(): any; + + /** + * Type of "this" as a string. + * This is the name of the function stored in the constructor field of + * "this", if available. Otherwise the object's [[Class]] internal + * property. + */ + getTypeName(): string | null; + + /** + * Current function + */ + getFunction(): Function | undefined; + + /** + * Name of the current function, typically its name property. + * If a name property is not available an attempt will be made to try + * to infer a name from the function's context. + */ + getFunctionName(): string | null; + + /** + * Name of the property [of "this" or one of its prototypes] that holds + * the current function + */ + getMethodName(): string | null; + + /** + * Name of the script [if this function was defined in a script] + */ + getFileName(): string | null; + + /** + * Get the script name or source URL for the source map + */ + getScriptNameOrSourceURL(): string; + + /** + * Current line number [if this function was defined in a script] + */ + getLineNumber(): number | null; + + /** + * Current column number [if this function was defined in a script] + */ + getColumnNumber(): number | null; + + /** + * A call site object representing the location where eval was called + * [if this function was created using a call to eval] + */ + getEvalOrigin(): string | undefined; + + /** + * Is this a toplevel invocation, that is, is "this" the global object? + */ + isToplevel(): boolean; + + /** + * Does this call take place in code defined by a call to eval? + */ + isEval(): boolean; + + /** + * Is this call in native V8 code? + */ + isNative(): boolean; + + /** + * Is this a constructor call? + */ + isConstructor(): boolean; +} + +export interface StartOfSourceMap { + file?: string; + sourceRoot?: string; +} + +export interface RawSourceMap extends StartOfSourceMap { + version: string; + sources: string[]; + names: string[]; + sourcesContent?: string[]; + mappings: string; +} + +declare global { + // Declare "static" methods in Error + interface ErrorConstructor { + /** Create .stack property on a target object */ + captureStackTrace(targetObject: object, constructorOpt?: Function): void; + + // tslint:disable:max-line-length + /** + * Optional override for formatting stack traces + * + * @see https://github.com/v8/v8/wiki/Stack%20Trace%20API#customizing-stack-traces + */ + // tslint:enable:max-line-length + // tslint:disable-next-line:no-any + prepareStackTrace?: (err: Error, stackTraces: CallSite[]) => any; + + stackTraceLimit: number; + } +} -- cgit v1.2.3