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;