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 {Array<string>} 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;

View File

@ -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
/// <reference types="../third_party/node_modules/typescript/lib/typescript.d.ts"/>
import "./globals.ts";
import "./ts_global.d.ts";
import { bold, cyan, yellow } from "./colors.ts";
import { Console } from "./console.ts";

View File

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