From d231df17b071f74bbcf5b600ee1fbf5152144397 Mon Sep 17 00:00:00 2001 From: Kitson Kelly Date: Fri, 13 Sep 2019 07:30:04 +1000 Subject: [PATCH] deno_typescript cleanup/improvements (#2901) --- deno_typescript/compiler_main.js | 71 ++++++++++++++++++-------------- js/compiler.ts | 3 +- js/diagnostics.ts | 2 - js/ts_global.d.ts | 14 +++++++ 4 files changed, 55 insertions(+), 35 deletions(-) create mode 100644 js/ts_global.d.ts diff --git a/deno_typescript/compiler_main.js b/deno_typescript/compiler_main.js index 85e0041173..8288306f3c 100644 --- a/deno_typescript/compiler_main.js +++ b/deno_typescript/compiler_main.js @@ -11,6 +11,7 @@ const ASSETS = "$asset$"; * @param {string} configText * @param {Array} rootNames */ +// eslint-disable-next-line @typescript-eslint/no-unused-vars function main(configText, rootNames) { println(`>>> ts version ${ts.version}`); println(`>>> rootNames ${rootNames}`); @@ -19,22 +20,26 @@ function main(configText, rootNames) { assert(rootNames.length > 0); - let { options, diagnostics } = configure(configText); + const { options, diagnostics } = configure(configText); handleDiagnostics(host, diagnostics); println(`>>> TS config: ${JSON.stringify(options)}`); const program = ts.createProgram(rootNames, options, host); - diagnostics = ts.getPreEmitDiagnostics(program).filter(({ code }) => { - // TS2691: An import path cannot end with a '.ts' extension. Consider - // importing 'bad-module' instead. - if (code === 2691) return false; - // TS5009: Cannot find the common subdirectory path for the input files. - if (code === 5009) return false; - return true; - }); - handleDiagnostics(host, diagnostics); + handleDiagnostics( + host, + ts.getPreEmitDiagnostics(program).filter(({ code }) => { + // TS1063: An export assignment cannot be used in a namespace. + if (code === 1063) return false; + // TS2691: An import path cannot end with a '.ts' extension. Consider + // importing 'bad-module' instead. + if (code === 2691) return false; + // TS5009: Cannot find the common subdirectory path for the input files. + if (code === 5009) return false; + return true; + }) + ); const emitResult = program.emit(); handleDiagnostics(host, emitResult.diagnostics); @@ -102,20 +107,25 @@ const ops = { }; /** + * This is a minimal implementation of a compiler host to be able to allow the + * creation of runtime bundles. Some of the methods are implemented in a way + * to just appease the TypeScript compiler, not to necessarily be a general + * purpose implementation. + * * @implements {ts.CompilerHost} */ class Host { /** - * @param {string} fileName + * @param {string} _fileName */ - fileExists(fileName) { + fileExists(_fileName) { return true; } /** - * @param {string} fileName + * @param {string} _fileName */ - readFile(fileName) { + readFile(_fileName) { unreachable(); return undefined; } @@ -163,18 +173,17 @@ class Host { .replace("/index.d.ts", ""); } - let { sourceCode, moduleName } = dispatch("readFile", { + const { sourceCode, moduleName } = dispatch("readFile", { fileName, languageVersion, shouldCreateNewSourceFile }); - // TODO(ry) A terrible hack. Please remove ASAP. - if (fileName.endsWith("typescript.d.ts")) { - sourceCode = sourceCode.replace("export = ts;", ""); - } - - let sourceFile = ts.createSourceFile(fileName, sourceCode, languageVersion); + const sourceFile = ts.createSourceFile( + fileName, + sourceCode, + languageVersion + ); sourceFile.moduleName = moduleName; return sourceFile; } @@ -201,18 +210,18 @@ class Host { } /** - * @param {string} fileName - * @param {ts.Path} path - * @param {ts.ScriptTarget} languageVersion - * @param {*} onError - * @param {boolean} shouldCreateNewSourceFile + * @param {string} _fileName + * @param {ts.Path} _path + * @param {ts.ScriptTarget} _languageVersion + * @param {*} _onError + * @param {boolean} _shouldCreateNewSourceFile */ getSourceFileByPath( - fileName, - path, - languageVersion, - onError, - shouldCreateNewSourceFile + _fileName, + _path, + _languageVersion, + _onError, + _shouldCreateNewSourceFile ) { unreachable(); return undefined; diff --git a/js/compiler.ts b/js/compiler.ts index 12062e63f9..41f23bcd21 100644 --- a/js/compiler.ts +++ b/js/compiler.ts @@ -1,9 +1,8 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. // TODO(ry) Combine this implementation with //deno_typescript/compiler_main.js -/// - import "./globals.ts"; +import "./ts_global.d.ts"; import { bold, cyan, yellow } from "./colors.ts"; import { Console } from "./console.ts"; diff --git a/js/diagnostics.ts b/js/diagnostics.ts index 5da91483c3..c5377f979b 100644 --- a/js/diagnostics.ts +++ b/js/diagnostics.ts @@ -4,8 +4,6 @@ // compiler, which is strongly influenced by the format of TypeScript // diagnostics. -/// - /** The log category for a diagnostic message */ export enum DiagnosticCategory { Log = 0, diff --git a/js/ts_global.d.ts b/js/ts_global.d.ts new file mode 100644 index 0000000000..d4b027926c --- /dev/null +++ b/js/ts_global.d.ts @@ -0,0 +1,14 @@ +// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. + +// This scopes the `ts` namespace globally, which is where it exists at runtime +// when building Deno, but the `typescript/lib/typescript.d.ts` is defined as a +// module. + +// eslint-disable-next-line @typescript-eslint/no-unused-vars +import * as ts_ from "../node_modules/typescript/lib/typescript.d.ts"; + +declare global { + namespace ts { + export = ts_; + } +}