deno_typescript cleanup/improvements (#2901)

This commit is contained in:
Kitson Kelly 2019-09-13 07:30:04 +10:00 committed by Ryan Dahl
parent c03cdcc939
commit d231df17b0
4 changed files with 55 additions and 35 deletions

View file

@ -11,6 +11,7 @@ const ASSETS = "$asset$";
* @param {string} configText * @param {string} configText
* @param {Array<string>} rootNames * @param {Array<string>} rootNames
*/ */
// eslint-disable-next-line @typescript-eslint/no-unused-vars
function main(configText, rootNames) { function main(configText, rootNames) {
println(`>>> ts version ${ts.version}`); println(`>>> ts version ${ts.version}`);
println(`>>> rootNames ${rootNames}`); println(`>>> rootNames ${rootNames}`);
@ -19,22 +20,26 @@ function main(configText, rootNames) {
assert(rootNames.length > 0); assert(rootNames.length > 0);
let { options, diagnostics } = configure(configText); const { options, diagnostics } = configure(configText);
handleDiagnostics(host, diagnostics); handleDiagnostics(host, diagnostics);
println(`>>> TS config: ${JSON.stringify(options)}`); println(`>>> TS config: ${JSON.stringify(options)}`);
const program = ts.createProgram(rootNames, options, host); const program = ts.createProgram(rootNames, options, host);
diagnostics = ts.getPreEmitDiagnostics(program).filter(({ code }) => { 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 // TS2691: An import path cannot end with a '.ts' extension. Consider
// importing 'bad-module' instead. // importing 'bad-module' instead.
if (code === 2691) return false; if (code === 2691) return false;
// TS5009: Cannot find the common subdirectory path for the input files. // TS5009: Cannot find the common subdirectory path for the input files.
if (code === 5009) return false; if (code === 5009) return false;
return true; return true;
}); })
handleDiagnostics(host, diagnostics); );
const emitResult = program.emit(); const emitResult = program.emit();
handleDiagnostics(host, emitResult.diagnostics); 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} * @implements {ts.CompilerHost}
*/ */
class Host { class Host {
/** /**
* @param {string} fileName * @param {string} _fileName
*/ */
fileExists(fileName) { fileExists(_fileName) {
return true; return true;
} }
/** /**
* @param {string} fileName * @param {string} _fileName
*/ */
readFile(fileName) { readFile(_fileName) {
unreachable(); unreachable();
return undefined; return undefined;
} }
@ -163,18 +173,17 @@ class Host {
.replace("/index.d.ts", ""); .replace("/index.d.ts", "");
} }
let { sourceCode, moduleName } = dispatch("readFile", { const { sourceCode, moduleName } = dispatch("readFile", {
fileName, fileName,
languageVersion, languageVersion,
shouldCreateNewSourceFile shouldCreateNewSourceFile
}); });
// TODO(ry) A terrible hack. Please remove ASAP. const sourceFile = ts.createSourceFile(
if (fileName.endsWith("typescript.d.ts")) { fileName,
sourceCode = sourceCode.replace("export = ts;", ""); sourceCode,
} languageVersion
);
let sourceFile = ts.createSourceFile(fileName, sourceCode, languageVersion);
sourceFile.moduleName = moduleName; sourceFile.moduleName = moduleName;
return sourceFile; return sourceFile;
} }
@ -201,18 +210,18 @@ class Host {
} }
/** /**
* @param {string} fileName * @param {string} _fileName
* @param {ts.Path} path * @param {ts.Path} _path
* @param {ts.ScriptTarget} languageVersion * @param {ts.ScriptTarget} _languageVersion
* @param {*} onError * @param {*} _onError
* @param {boolean} shouldCreateNewSourceFile * @param {boolean} _shouldCreateNewSourceFile
*/ */
getSourceFileByPath( getSourceFileByPath(
fileName, _fileName,
path, _path,
languageVersion, _languageVersion,
onError, _onError,
shouldCreateNewSourceFile _shouldCreateNewSourceFile
) { ) {
unreachable(); unreachable();
return undefined; return undefined;

View file

@ -1,9 +1,8 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
// TODO(ry) Combine this implementation with //deno_typescript/compiler_main.js // TODO(ry) Combine this implementation with //deno_typescript/compiler_main.js
/// <reference types="../third_party/node_modules/typescript/lib/typescript.d.ts"/>
import "./globals.ts"; import "./globals.ts";
import "./ts_global.d.ts";
import { bold, cyan, yellow } from "./colors.ts"; import { bold, cyan, yellow } from "./colors.ts";
import { Console } from "./console.ts"; import { Console } from "./console.ts";

View file

@ -4,8 +4,6 @@
// compiler, which is strongly influenced by the format of TypeScript // compiler, which is strongly influenced by the format of TypeScript
// diagnostics. // diagnostics.
/// <reference types="../third_party/node_modules/typescript/lib/typescript.d.ts"/>
/** The log category for a diagnostic message */ /** The log category for a diagnostic message */
export enum DiagnosticCategory { export enum DiagnosticCategory {
Log = 0, Log = 0,

14
js/ts_global.d.ts vendored Normal file
View file

@ -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_;
}
}