2021-01-11 12:13:41 -05:00
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2020-12-07 05:46:39 -05:00
|
|
|
// @ts-check
|
|
|
|
/// <reference path="./compiler.d.ts" />
|
2020-11-03 10:19:29 -05:00
|
|
|
// deno-lint-ignore-file no-undef
|
|
|
|
|
2020-07-19 13:49:44 -04:00
|
|
|
// This module is the entry point for "compiler" isolate, ie. the one
|
2020-11-02 14:41:20 -05:00
|
|
|
// that is created when Deno needs to type check TypeScript, and in some
|
|
|
|
// instances convert TypeScript to JavaScript.
|
2020-07-19 13:49:44 -04:00
|
|
|
|
|
|
|
// Removes the `__proto__` for security reasons. This intentionally makes
|
|
|
|
// Deno non compliant with ECMA-262 Annex B.2.2.1
|
|
|
|
delete Object.prototype.__proto__;
|
|
|
|
|
|
|
|
((window) => {
|
2020-12-07 05:46:39 -05:00
|
|
|
/** @type {DenoCore} */
|
2020-09-16 16:22:43 -04:00
|
|
|
const core = window.Deno.core;
|
2020-07-23 09:29:36 -04:00
|
|
|
|
2020-09-25 08:04:51 -04:00
|
|
|
let logDebug = false;
|
|
|
|
let logSource = "JS";
|
|
|
|
|
2021-02-01 03:02:02 -05:00
|
|
|
// The map from the normalized specifier to the original.
|
|
|
|
// TypeScript normalizes the specifier in its internal processing,
|
|
|
|
// but the original specifier is needed when looking up the source from the runtime.
|
|
|
|
// This map stores that relationship, and the original can be restored by the
|
|
|
|
// normalized specifier.
|
|
|
|
// See: https://github.com/denoland/deno/issues/9277#issuecomment-769653834
|
|
|
|
const normalizedToOriginalMap = new Map();
|
|
|
|
|
2020-09-25 08:04:51 -04:00
|
|
|
function setLogDebug(debug, source) {
|
|
|
|
logDebug = debug;
|
|
|
|
if (source) {
|
|
|
|
logSource = source;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-02 19:30:03 -04:00
|
|
|
function printStderr(msg) {
|
|
|
|
core.print(msg, true);
|
|
|
|
}
|
|
|
|
|
2020-10-01 06:33:15 -04:00
|
|
|
function debug(...args) {
|
2020-09-25 08:04:51 -04:00
|
|
|
if (logDebug) {
|
2020-12-07 05:46:39 -05:00
|
|
|
const stringifiedArgs = args.map((arg) =>
|
|
|
|
typeof arg === "string" ? arg : JSON.stringify(arg)
|
|
|
|
).join(" ");
|
2021-05-02 19:30:03 -04:00
|
|
|
printStderr(`DEBUG ${logSource} - ${stringifiedArgs}\n`);
|
2020-09-25 08:04:51 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-29 20:46:58 -05:00
|
|
|
function error(...args) {
|
|
|
|
const stringifiedArgs = args.map((arg) =>
|
|
|
|
typeof arg === "string" || arg instanceof Error
|
|
|
|
? String(arg)
|
|
|
|
: JSON.stringify(arg)
|
|
|
|
).join(" ");
|
2021-05-02 19:30:03 -04:00
|
|
|
printStderr(`ERROR ${logSource} = ${stringifiedArgs}\n`);
|
2020-12-29 20:46:58 -05:00
|
|
|
}
|
|
|
|
|
2020-09-25 08:04:51 -04:00
|
|
|
class AssertionError extends Error {
|
|
|
|
constructor(msg) {
|
|
|
|
super(msg);
|
|
|
|
this.name = "AssertionError";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function assert(cond, msg = "Assertion failed.") {
|
|
|
|
if (!cond) {
|
|
|
|
throw new AssertionError(msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-13 19:52:49 -04:00
|
|
|
/** @type {Map<string, ts.SourceFile>} */
|
|
|
|
const sourceFileCache = new Map();
|
|
|
|
|
2021-05-17 18:51:35 -04:00
|
|
|
/** @type {Map<string, string>} */
|
|
|
|
const scriptVersionCache = new Map();
|
|
|
|
|
2020-11-02 14:41:20 -05:00
|
|
|
/** @param {ts.DiagnosticRelatedInformation} diagnostic */
|
2020-09-12 05:53:57 -04:00
|
|
|
function fromRelatedInformation({
|
|
|
|
start,
|
|
|
|
length,
|
|
|
|
file,
|
|
|
|
messageText: msgText,
|
|
|
|
...ri
|
|
|
|
}) {
|
|
|
|
let messageText;
|
|
|
|
let messageChain;
|
|
|
|
if (typeof msgText === "object") {
|
|
|
|
messageChain = msgText;
|
|
|
|
} else {
|
|
|
|
messageText = msgText;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2020-09-12 05:53:57 -04:00
|
|
|
if (start !== undefined && length !== undefined && file) {
|
|
|
|
const startPos = file.getLineAndCharacterOfPosition(start);
|
|
|
|
const sourceLine = file.getFullText().split("\n")[startPos.line];
|
|
|
|
const fileName = file.fileName;
|
2020-07-19 13:49:44 -04:00
|
|
|
return {
|
2020-09-12 05:53:57 -04:00
|
|
|
start: startPos,
|
|
|
|
end: file.getLineAndCharacterOfPosition(start + length),
|
|
|
|
fileName,
|
|
|
|
messageChain,
|
|
|
|
messageText,
|
|
|
|
sourceLine,
|
|
|
|
...ri,
|
2020-07-19 13:49:44 -04:00
|
|
|
};
|
|
|
|
} else {
|
2020-09-12 05:53:57 -04:00
|
|
|
return {
|
|
|
|
messageChain,
|
|
|
|
messageText,
|
|
|
|
...ri,
|
|
|
|
};
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-02 14:41:20 -05:00
|
|
|
/** @param {ts.Diagnostic[]} diagnostics */
|
2020-09-12 05:53:57 -04:00
|
|
|
function fromTypeScriptDiagnostic(diagnostics) {
|
|
|
|
return diagnostics.map(({ relatedInformation: ri, source, ...diag }) => {
|
2020-12-07 05:46:39 -05:00
|
|
|
/** @type {any} */
|
2020-09-12 05:53:57 -04:00
|
|
|
const value = fromRelatedInformation(diag);
|
|
|
|
value.relatedInformation = ri
|
|
|
|
? ri.map(fromRelatedInformation)
|
|
|
|
: undefined;
|
|
|
|
value.source = source;
|
|
|
|
return value;
|
|
|
|
});
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Using incremental compile APIs requires that all
|
|
|
|
// paths must be either relative or absolute. Since
|
|
|
|
// analysis in Rust operates on fully resolved URLs,
|
|
|
|
// it makes sense to use the same scheme here.
|
2020-10-01 06:33:15 -04:00
|
|
|
const ASSETS = "asset:///";
|
2020-09-09 07:37:22 -04:00
|
|
|
const CACHE = "cache:///";
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2020-11-02 14:41:20 -05:00
|
|
|
/** Diagnostics that are intentionally ignored when compiling TypeScript in
|
|
|
|
* Deno, as they provide misleading or incorrect information. */
|
|
|
|
const IGNORED_DIAGNOSTICS = [
|
|
|
|
// TS1208: All files must be modules when the '--isolatedModules' flag is
|
2020-12-07 05:46:39 -05:00
|
|
|
// provided. We can ignore because we guarantee that all files are
|
2020-11-02 14:41:20 -05:00
|
|
|
// modules.
|
|
|
|
1208,
|
|
|
|
// TS1375: 'await' expressions are only allowed at the top level of a file
|
|
|
|
// when that file is a module, but this file has no imports or exports.
|
|
|
|
// Consider adding an empty 'export {}' to make this file a module.
|
|
|
|
1375,
|
|
|
|
// TS1103: 'for-await-of' statement is only allowed within an async function
|
|
|
|
// or async generator.
|
|
|
|
1103,
|
|
|
|
// TS2306: File 'file:///Users/rld/src/deno/cli/tests/subdir/amd_like.js' is
|
|
|
|
// not a module.
|
|
|
|
2306,
|
2021-05-18 00:09:33 -04:00
|
|
|
// TS2688: Cannot find type definition file for '...'.
|
|
|
|
// We ignore because type defintion files can end with '.ts'.
|
|
|
|
2688,
|
2020-11-02 14:41:20 -05:00
|
|
|
// TS2691: An import path cannot end with a '.ts' extension. Consider
|
|
|
|
// importing 'bad-module' instead.
|
|
|
|
2691,
|
2020-12-24 05:53:03 -05:00
|
|
|
// TS2792: Cannot find module. Did you mean to set the 'moduleResolution'
|
|
|
|
// option to 'node', or to add aliases to the 'paths' option?
|
|
|
|
2792,
|
2020-11-02 14:41:20 -05:00
|
|
|
// TS5009: Cannot find the common subdirectory path for the input files.
|
|
|
|
5009,
|
|
|
|
// TS5055: Cannot write file
|
|
|
|
// 'http://localhost:4545/cli/tests/subdir/mt_application_x_javascript.j4.js'
|
|
|
|
// because it would overwrite input file.
|
|
|
|
5055,
|
|
|
|
// TypeScript is overly opinionated that only CommonJS modules kinds can
|
|
|
|
// support JSON imports. Allegedly this was fixed in
|
|
|
|
// Microsoft/TypeScript#26825 but that doesn't seem to be working here,
|
|
|
|
// so we will ignore complaints about this compiler setting.
|
|
|
|
5070,
|
|
|
|
// TS7016: Could not find a declaration file for module '...'. '...'
|
|
|
|
// implicitly has an 'any' type. This is due to `allowJs` being off by
|
|
|
|
// default but importing of a JavaScript module.
|
|
|
|
7016,
|
|
|
|
];
|
|
|
|
|
|
|
|
const SNAPSHOT_COMPILE_OPTIONS = {
|
2020-07-19 13:49:44 -04:00
|
|
|
esModuleInterop: true,
|
|
|
|
jsx: ts.JsxEmit.React,
|
|
|
|
module: ts.ModuleKind.ESNext,
|
2020-11-02 14:41:20 -05:00
|
|
|
noEmit: true,
|
2020-07-19 13:49:44 -04:00
|
|
|
strict: true,
|
|
|
|
target: ts.ScriptTarget.ESNext,
|
|
|
|
};
|
|
|
|
|
2020-12-07 05:46:39 -05:00
|
|
|
class ScriptSnapshot {
|
|
|
|
/** @type {string} */
|
|
|
|
specifier;
|
|
|
|
/** @type {string} */
|
|
|
|
version;
|
|
|
|
/**
|
|
|
|
* @param {string} specifier
|
2021-02-01 03:02:02 -05:00
|
|
|
* @param {string} version
|
2020-12-07 05:46:39 -05:00
|
|
|
*/
|
|
|
|
constructor(specifier, version) {
|
|
|
|
this.specifier = specifier;
|
|
|
|
this.version = version;
|
|
|
|
}
|
|
|
|
/**
|
2021-02-01 03:02:02 -05:00
|
|
|
* @param {number} start
|
|
|
|
* @param {number} end
|
2020-12-07 05:46:39 -05:00
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
getText(start, end) {
|
|
|
|
const { specifier, version } = this;
|
|
|
|
debug(
|
|
|
|
`snapshot.getText(${start}, ${end}) specifier: ${specifier} version: ${version}`,
|
|
|
|
);
|
2021-04-12 15:55:05 -04:00
|
|
|
return core.opSync("op_get_text", { specifier, version, start, end });
|
2020-12-07 05:46:39 -05:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @returns {number}
|
|
|
|
*/
|
|
|
|
getLength() {
|
|
|
|
const { specifier, version } = this;
|
|
|
|
debug(`snapshot.getLength() specifier: ${specifier} version: ${version}`);
|
2021-04-12 15:55:05 -04:00
|
|
|
return core.opSync("op_get_length", { specifier, version });
|
2020-12-07 05:46:39 -05:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @param {ScriptSnapshot} oldSnapshot
|
|
|
|
* @returns {ts.TextChangeRange | undefined}
|
|
|
|
*/
|
|
|
|
getChangeRange(oldSnapshot) {
|
|
|
|
const { specifier, version } = this;
|
|
|
|
const { version: oldVersion } = oldSnapshot;
|
|
|
|
const oldLength = oldSnapshot.getLength();
|
|
|
|
debug(
|
|
|
|
`snapshot.getLength() specifier: ${specifier} oldVersion: ${oldVersion} version: ${version}`,
|
|
|
|
);
|
2021-04-12 15:55:05 -04:00
|
|
|
return core.opSync(
|
2020-12-07 05:46:39 -05:00
|
|
|
"op_get_change_range",
|
|
|
|
{ specifier, oldLength, oldVersion, version },
|
|
|
|
);
|
|
|
|
}
|
|
|
|
dispose() {
|
|
|
|
const { specifier, version } = this;
|
|
|
|
debug(`snapshot.dispose() specifier: ${specifier} version: ${version}`);
|
2021-04-12 15:55:05 -04:00
|
|
|
core.opSync("op_dispose", { specifier, version });
|
2020-12-07 05:46:39 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @type {ts.CompilerOptions} */
|
|
|
|
let compilationSettings = {};
|
|
|
|
|
|
|
|
/** @type {ts.LanguageService} */
|
|
|
|
let languageService;
|
|
|
|
|
2020-11-02 14:41:20 -05:00
|
|
|
/** An object literal of the incremental compiler host, which provides the
|
|
|
|
* specific "bindings" to the Deno environment that tsc needs to work.
|
2020-11-22 18:20:32 -05:00
|
|
|
*
|
2020-12-07 05:46:39 -05:00
|
|
|
* @type {ts.CompilerHost & ts.LanguageServiceHost} */
|
2020-10-01 06:33:15 -04:00
|
|
|
const host = {
|
2020-09-22 21:39:20 -04:00
|
|
|
fileExists(fileName) {
|
2020-10-01 06:33:15 -04:00
|
|
|
debug(`host.fileExists("${fileName}")`);
|
2020-09-22 21:39:20 -04:00
|
|
|
return false;
|
2020-10-01 06:33:15 -04:00
|
|
|
},
|
2020-10-13 19:52:49 -04:00
|
|
|
readFile(specifier) {
|
|
|
|
debug(`host.readFile("${specifier}")`);
|
2021-04-12 15:55:05 -04:00
|
|
|
return core.opSync("op_load", { specifier }).data;
|
2020-10-01 06:33:15 -04:00
|
|
|
},
|
2020-07-19 13:49:44 -04:00
|
|
|
getSourceFile(
|
2020-10-01 06:33:15 -04:00
|
|
|
specifier,
|
2020-07-19 13:49:44 -04:00
|
|
|
languageVersion,
|
2020-11-02 14:41:20 -05:00
|
|
|
_onError,
|
|
|
|
_shouldCreateNewSourceFile,
|
2020-07-19 13:49:44 -04:00
|
|
|
) {
|
2020-10-01 06:33:15 -04:00
|
|
|
debug(
|
|
|
|
`host.getSourceFile("${specifier}", ${
|
|
|
|
ts.ScriptTarget[languageVersion]
|
|
|
|
})`,
|
|
|
|
);
|
2020-11-02 14:41:20 -05:00
|
|
|
let sourceFile = sourceFileCache.get(specifier);
|
|
|
|
if (sourceFile) {
|
2020-10-13 19:52:49 -04:00
|
|
|
return sourceFile;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2020-11-02 14:41:20 -05:00
|
|
|
|
2021-02-01 03:02:02 -05:00
|
|
|
// Needs the original specifier
|
|
|
|
specifier = normalizedToOriginalMap.get(specifier) ?? specifier;
|
|
|
|
|
2020-11-02 14:41:20 -05:00
|
|
|
/** @type {{ data: string; hash?: string; scriptKind: ts.ScriptKind }} */
|
2021-04-12 15:55:05 -04:00
|
|
|
const { data, hash, scriptKind } = core.opSync(
|
2020-11-02 14:41:20 -05:00
|
|
|
"op_load",
|
|
|
|
{ specifier },
|
|
|
|
);
|
|
|
|
assert(
|
|
|
|
data != null,
|
|
|
|
`"data" is unexpectedly null for "${specifier}".`,
|
|
|
|
);
|
|
|
|
sourceFile = ts.createSourceFile(
|
|
|
|
specifier,
|
|
|
|
data,
|
|
|
|
languageVersion,
|
|
|
|
false,
|
|
|
|
scriptKind,
|
|
|
|
);
|
|
|
|
sourceFile.moduleName = specifier;
|
|
|
|
sourceFile.version = hash;
|
|
|
|
sourceFileCache.set(specifier, sourceFile);
|
|
|
|
return sourceFile;
|
2020-10-01 06:33:15 -04:00
|
|
|
},
|
|
|
|
getDefaultLibFileName() {
|
2020-11-02 14:41:20 -05:00
|
|
|
return `${ASSETS}/lib.esnext.d.ts`;
|
2020-10-01 06:33:15 -04:00
|
|
|
},
|
|
|
|
getDefaultLibLocation() {
|
|
|
|
return ASSETS;
|
|
|
|
},
|
|
|
|
writeFile(fileName, data, _writeByteOrderMark, _onError, sourceFiles) {
|
|
|
|
debug(`host.writeFile("${fileName}")`);
|
2020-11-02 14:41:20 -05:00
|
|
|
let maybeSpecifiers;
|
|
|
|
if (sourceFiles) {
|
|
|
|
maybeSpecifiers = sourceFiles.map((sf) => sf.moduleName);
|
2020-10-01 06:33:15 -04:00
|
|
|
}
|
2021-04-12 15:55:05 -04:00
|
|
|
return core.opSync(
|
2020-11-02 14:41:20 -05:00
|
|
|
"op_emit",
|
|
|
|
{ maybeSpecifiers, fileName, data },
|
|
|
|
);
|
2020-10-01 06:33:15 -04:00
|
|
|
},
|
|
|
|
getCurrentDirectory() {
|
|
|
|
return CACHE;
|
|
|
|
},
|
|
|
|
getCanonicalFileName(fileName) {
|
|
|
|
return fileName;
|
|
|
|
},
|
2020-07-19 13:49:44 -04:00
|
|
|
useCaseSensitiveFileNames() {
|
|
|
|
return true;
|
2020-10-01 06:33:15 -04:00
|
|
|
},
|
|
|
|
getNewLine() {
|
|
|
|
return "\n";
|
|
|
|
},
|
|
|
|
resolveModuleNames(specifiers, base) {
|
|
|
|
debug(`host.resolveModuleNames()`);
|
|
|
|
debug(` base: ${base}`);
|
|
|
|
debug(` specifiers: ${specifiers.join(", ")}`);
|
2020-12-07 05:46:39 -05:00
|
|
|
/** @type {Array<[string, ts.Extension] | undefined>} */
|
2021-04-12 15:55:05 -04:00
|
|
|
const resolved = core.opSync("op_resolve", {
|
2020-11-02 14:41:20 -05:00
|
|
|
specifiers,
|
|
|
|
base,
|
|
|
|
});
|
2020-12-07 05:46:39 -05:00
|
|
|
if (resolved) {
|
|
|
|
const result = resolved.map((item) => {
|
|
|
|
if (item) {
|
|
|
|
const [resolvedFileName, extension] = item;
|
|
|
|
return {
|
|
|
|
resolvedFileName,
|
|
|
|
extension,
|
|
|
|
isExternalLibraryImport: false,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return undefined;
|
|
|
|
});
|
|
|
|
result.length = specifiers.length;
|
|
|
|
return result;
|
|
|
|
} else {
|
|
|
|
return new Array(specifiers.length);
|
|
|
|
}
|
2020-10-01 06:33:15 -04:00
|
|
|
},
|
|
|
|
createHash(data) {
|
2021-04-12 15:55:05 -04:00
|
|
|
return core.opSync("op_create_hash", { data }).hash;
|
2020-10-01 06:33:15 -04:00
|
|
|
},
|
2020-12-07 05:46:39 -05:00
|
|
|
|
|
|
|
// LanguageServiceHost
|
|
|
|
getCompilationSettings() {
|
|
|
|
debug("host.getCompilationSettings()");
|
|
|
|
return compilationSettings;
|
|
|
|
},
|
|
|
|
getScriptFileNames() {
|
|
|
|
debug("host.getScriptFileNames()");
|
2021-04-12 15:55:05 -04:00
|
|
|
return core.opSync("op_script_names", undefined);
|
2020-12-07 05:46:39 -05:00
|
|
|
},
|
|
|
|
getScriptVersion(specifier) {
|
|
|
|
debug(`host.getScriptVersion("${specifier}")`);
|
|
|
|
const sourceFile = sourceFileCache.get(specifier);
|
|
|
|
if (sourceFile) {
|
|
|
|
return sourceFile.version ?? "1";
|
|
|
|
}
|
2021-05-17 18:51:35 -04:00
|
|
|
// tsc neurotically requests the script version multiple times even though
|
|
|
|
// it can't possibly have changed, so we will memoize it on a per request
|
|
|
|
// basis.
|
|
|
|
if (scriptVersionCache.has(specifier)) {
|
|
|
|
return scriptVersionCache.get(specifier);
|
|
|
|
}
|
|
|
|
const scriptVersion = core.opSync("op_script_version", { specifier });
|
|
|
|
scriptVersionCache.set(specifier, scriptVersion);
|
|
|
|
return scriptVersion;
|
2020-12-07 05:46:39 -05:00
|
|
|
},
|
|
|
|
getScriptSnapshot(specifier) {
|
|
|
|
debug(`host.getScriptSnapshot("${specifier}")`);
|
|
|
|
const sourceFile = sourceFileCache.get(specifier);
|
|
|
|
if (sourceFile) {
|
|
|
|
return {
|
|
|
|
getText(start, end) {
|
|
|
|
return sourceFile.text.substring(start, end);
|
|
|
|
},
|
|
|
|
getLength() {
|
|
|
|
return sourceFile.text.length;
|
|
|
|
},
|
|
|
|
getChangeRange() {
|
|
|
|
return undefined;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
2021-05-17 18:51:35 -04:00
|
|
|
const version = host.getScriptVersion(specifier);
|
2020-12-07 05:46:39 -05:00
|
|
|
if (version != null) {
|
|
|
|
return new ScriptSnapshot(specifier, version);
|
|
|
|
}
|
|
|
|
return undefined;
|
|
|
|
},
|
2020-10-01 06:33:15 -04:00
|
|
|
};
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2020-11-02 14:41:20 -05:00
|
|
|
/** @type {Array<[string, number]>} */
|
2020-07-19 13:49:44 -04:00
|
|
|
const stats = [];
|
|
|
|
let statsStart = 0;
|
|
|
|
|
|
|
|
function performanceStart() {
|
|
|
|
stats.length = 0;
|
2020-12-07 05:46:39 -05:00
|
|
|
statsStart = Date.now();
|
2020-07-19 13:49:44 -04:00
|
|
|
ts.performance.enable();
|
|
|
|
}
|
|
|
|
|
2020-12-07 05:46:39 -05:00
|
|
|
/**
|
2021-02-01 03:02:02 -05:00
|
|
|
* @param {{ program: ts.Program | ts.EmitAndSemanticDiagnosticsBuilderProgram, fileCount?: number }} options
|
2020-12-07 05:46:39 -05:00
|
|
|
*/
|
2020-08-05 14:44:03 -04:00
|
|
|
function performanceProgram({ program, fileCount }) {
|
2020-07-19 13:49:44 -04:00
|
|
|
if (program) {
|
|
|
|
if ("getProgram" in program) {
|
|
|
|
program = program.getProgram();
|
|
|
|
}
|
2020-11-02 14:41:20 -05:00
|
|
|
stats.push(["Files", program.getSourceFiles().length]);
|
|
|
|
stats.push(["Nodes", program.getNodeCount()]);
|
|
|
|
stats.push(["Identifiers", program.getIdentifierCount()]);
|
|
|
|
stats.push(["Symbols", program.getSymbolCount()]);
|
|
|
|
stats.push(["Types", program.getTypeCount()]);
|
|
|
|
stats.push(["Instantiations", program.getInstantiationCount()]);
|
2020-07-19 13:49:44 -04:00
|
|
|
} else if (fileCount != null) {
|
2020-11-02 14:41:20 -05:00
|
|
|
stats.push(["Files", fileCount]);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
const programTime = ts.performance.getDuration("Program");
|
|
|
|
const bindTime = ts.performance.getDuration("Bind");
|
|
|
|
const checkTime = ts.performance.getDuration("Check");
|
|
|
|
const emitTime = ts.performance.getDuration("Emit");
|
2020-11-02 14:41:20 -05:00
|
|
|
stats.push(["Parse time", programTime]);
|
|
|
|
stats.push(["Bind time", bindTime]);
|
|
|
|
stats.push(["Check time", checkTime]);
|
|
|
|
stats.push(["Emit time", emitTime]);
|
|
|
|
stats.push(
|
|
|
|
["Total TS time", programTime + bindTime + checkTime + emitTime],
|
|
|
|
);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function performanceEnd() {
|
2020-12-07 05:46:39 -05:00
|
|
|
const duration = Date.now() - statsStart;
|
2020-11-02 14:41:20 -05:00
|
|
|
stats.push(["Compile time", duration]);
|
2020-07-19 13:49:44 -04:00
|
|
|
return stats;
|
|
|
|
}
|
|
|
|
|
2020-10-13 19:52:49 -04:00
|
|
|
/**
|
|
|
|
* @typedef {object} Request
|
|
|
|
* @property {Record<string, any>} config
|
|
|
|
* @property {boolean} debug
|
|
|
|
* @property {string[]} rootNames
|
|
|
|
*/
|
2020-07-23 09:29:36 -04:00
|
|
|
|
2021-02-01 03:02:02 -05:00
|
|
|
/**
|
|
|
|
* Checks the normalized version of the root name and stores it in
|
|
|
|
* `normalizedToOriginalMap`. If the normalized specifier is already
|
|
|
|
* registered for the different root name, it throws an AssertionError.
|
|
|
|
*
|
|
|
|
* @param {string} rootName
|
|
|
|
*/
|
|
|
|
function checkNormalizedPath(rootName) {
|
|
|
|
const normalized = ts.normalizePath(rootName);
|
|
|
|
const originalRootName = normalizedToOriginalMap.get(normalized);
|
|
|
|
if (typeof originalRootName === "undefined") {
|
|
|
|
normalizedToOriginalMap.set(normalized, rootName);
|
|
|
|
} else if (originalRootName !== rootName) {
|
|
|
|
// The different root names are normalizd to the same path.
|
|
|
|
// This will cause problem when looking up the source for each.
|
|
|
|
throw new AssertionError(
|
|
|
|
`The different names for the same normalized specifier are specified: normalized=${normalized}, rootNames=${originalRootName},${rootName}`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-13 19:52:49 -04:00
|
|
|
/** The API that is called by Rust when executing a request.
|
2020-11-22 18:20:32 -05:00
|
|
|
* @param {Request} request
|
2020-10-13 19:52:49 -04:00
|
|
|
*/
|
|
|
|
function exec({ config, debug: debugFlag, rootNames }) {
|
|
|
|
setLogDebug(debugFlag, "TS");
|
|
|
|
performanceStart();
|
|
|
|
debug(">>> exec start", { rootNames });
|
|
|
|
debug(config);
|
|
|
|
|
2021-02-01 03:02:02 -05:00
|
|
|
rootNames.forEach(checkNormalizedPath);
|
|
|
|
|
2020-10-13 19:52:49 -04:00
|
|
|
const { options, errors: configFileParsingDiagnostics } = ts
|
2020-12-07 05:46:39 -05:00
|
|
|
.convertCompilerOptionsFromJson(config, "");
|
2020-11-22 18:20:32 -05:00
|
|
|
// The `allowNonTsExtensions` is a "hidden" compiler option used in VSCode
|
|
|
|
// which is not allowed to be passed in JSON, we need it to allow special
|
|
|
|
// URLs which Deno supports. So we need to either ignore the diagnostic, or
|
|
|
|
// inject it ourselves.
|
|
|
|
Object.assign(options, { allowNonTsExtensions: true });
|
2020-10-13 19:52:49 -04:00
|
|
|
const program = ts.createIncrementalProgram({
|
|
|
|
rootNames,
|
|
|
|
options,
|
|
|
|
host,
|
|
|
|
configFileParsingDiagnostics,
|
|
|
|
});
|
|
|
|
|
|
|
|
const { diagnostics: emitDiagnostics } = program.emit();
|
|
|
|
|
|
|
|
const diagnostics = [
|
|
|
|
...program.getConfigFileParsingDiagnostics(),
|
|
|
|
...program.getSyntacticDiagnostics(),
|
|
|
|
...program.getOptionsDiagnostics(),
|
|
|
|
...program.getGlobalDiagnostics(),
|
|
|
|
...program.getSemanticDiagnostics(),
|
|
|
|
...emitDiagnostics,
|
2020-11-02 14:41:20 -05:00
|
|
|
].filter(({ code }) => !IGNORED_DIAGNOSTICS.includes(code));
|
2020-10-13 19:52:49 -04:00
|
|
|
performanceProgram({ program });
|
|
|
|
|
2021-04-12 15:55:05 -04:00
|
|
|
core.opSync("op_respond", {
|
2020-10-13 19:52:49 -04:00
|
|
|
diagnostics: fromTypeScriptDiagnostic(diagnostics),
|
2020-11-02 14:41:20 -05:00
|
|
|
stats: performanceEnd(),
|
2020-10-13 19:52:49 -04:00
|
|
|
});
|
|
|
|
debug("<<< exec stop");
|
|
|
|
}
|
|
|
|
|
2020-12-07 05:46:39 -05:00
|
|
|
/**
|
2021-02-01 03:02:02 -05:00
|
|
|
* @param {number} id
|
|
|
|
* @param {any} data
|
2020-12-07 05:46:39 -05:00
|
|
|
*/
|
|
|
|
function respond(id, data = null) {
|
2021-04-12 15:55:05 -04:00
|
|
|
core.opSync("op_respond", { id, data });
|
2020-12-07 05:46:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-02-01 03:02:02 -05:00
|
|
|
* @param {LanguageServerRequest} request
|
2020-12-07 05:46:39 -05:00
|
|
|
*/
|
|
|
|
function serverRequest({ id, ...request }) {
|
|
|
|
debug(`serverRequest()`, { id, ...request });
|
2021-05-17 18:51:35 -04:00
|
|
|
// evict all memoized source file versions
|
|
|
|
scriptVersionCache.clear();
|
2020-12-07 05:46:39 -05:00
|
|
|
switch (request.method) {
|
|
|
|
case "configure": {
|
|
|
|
const { options, errors } = ts
|
|
|
|
.convertCompilerOptionsFromJson(request.compilerOptions, "");
|
|
|
|
Object.assign(options, { allowNonTsExtensions: true });
|
|
|
|
if (errors.length) {
|
|
|
|
debug(ts.formatDiagnostics(errors, host));
|
|
|
|
}
|
|
|
|
compilationSettings = options;
|
|
|
|
return respond(id, true);
|
|
|
|
}
|
2021-01-31 22:30:41 -05:00
|
|
|
case "findRenameLocations": {
|
|
|
|
return respond(
|
|
|
|
id,
|
|
|
|
languageService.findRenameLocations(
|
|
|
|
request.specifier,
|
|
|
|
request.position,
|
|
|
|
request.findInStrings,
|
|
|
|
request.findInComments,
|
|
|
|
request.providePrefixAndSuffixTextForRename,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2020-12-15 14:34:39 -05:00
|
|
|
case "getAsset": {
|
|
|
|
const sourceFile = host.getSourceFile(
|
|
|
|
request.specifier,
|
|
|
|
ts.ScriptTarget.ESNext,
|
|
|
|
);
|
2020-12-21 08:44:26 -05:00
|
|
|
return respond(id, sourceFile && sourceFile.text);
|
2020-12-15 14:34:39 -05:00
|
|
|
}
|
2021-02-04 13:53:02 -05:00
|
|
|
case "getCodeFixes": {
|
|
|
|
return respond(
|
|
|
|
id,
|
|
|
|
languageService.getCodeFixesAtPosition(
|
|
|
|
request.specifier,
|
|
|
|
request.startPosition,
|
|
|
|
request.endPosition,
|
|
|
|
request.errorCodes.map((v) => Number(v)),
|
|
|
|
{
|
|
|
|
indentSize: 2,
|
|
|
|
indentStyle: ts.IndentStyle.Block,
|
|
|
|
semicolons: ts.SemicolonPreference.Insert,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
quotePreference: "double",
|
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
case "getCombinedCodeFix": {
|
|
|
|
return respond(
|
|
|
|
id,
|
|
|
|
languageService.getCombinedCodeFix(
|
|
|
|
{
|
|
|
|
type: "file",
|
|
|
|
fileName: request.specifier,
|
|
|
|
},
|
|
|
|
request.fixId,
|
|
|
|
{
|
|
|
|
indentSize: 2,
|
|
|
|
indentStyle: ts.IndentStyle.Block,
|
|
|
|
semicolons: ts.SemicolonPreference.Insert,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
quotePreference: "double",
|
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2021-03-15 18:01:41 -04:00
|
|
|
case "getCompletionDetails": {
|
|
|
|
debug("request", request);
|
|
|
|
return respond(
|
|
|
|
id,
|
|
|
|
languageService.getCompletionEntryDetails(
|
|
|
|
request.args.specifier,
|
|
|
|
request.args.position,
|
|
|
|
request.args.name,
|
|
|
|
undefined,
|
|
|
|
request.args.source,
|
|
|
|
undefined,
|
|
|
|
// @ts-expect-error this exists in 4.3 but not part of the d.ts
|
|
|
|
request.args.data,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2021-01-31 22:30:41 -05:00
|
|
|
case "getCompletions": {
|
|
|
|
return respond(
|
|
|
|
id,
|
|
|
|
languageService.getCompletionsAtPosition(
|
|
|
|
request.specifier,
|
|
|
|
request.position,
|
|
|
|
request.preferences,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
case "getDefinition": {
|
|
|
|
return respond(
|
|
|
|
id,
|
|
|
|
languageService.getDefinitionAndBoundSpan(
|
|
|
|
request.specifier,
|
|
|
|
request.position,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2020-12-29 20:46:58 -05:00
|
|
|
case "getDiagnostics": {
|
|
|
|
try {
|
2021-01-22 05:03:16 -05:00
|
|
|
/** @type {Record<string, any[]>} */
|
|
|
|
const diagnosticMap = {};
|
|
|
|
for (const specifier of request.specifiers) {
|
|
|
|
diagnosticMap[specifier] = fromTypeScriptDiagnostic([
|
|
|
|
...languageService.getSemanticDiagnostics(specifier),
|
|
|
|
...languageService.getSuggestionDiagnostics(specifier),
|
|
|
|
...languageService.getSyntacticDiagnostics(specifier),
|
|
|
|
].filter(({ code }) => !IGNORED_DIAGNOSTICS.includes(code)));
|
|
|
|
}
|
|
|
|
return respond(id, diagnosticMap);
|
2020-12-29 20:46:58 -05:00
|
|
|
} catch (e) {
|
2021-01-22 05:03:16 -05:00
|
|
|
if ("stack" in e) {
|
|
|
|
error(e.stack);
|
|
|
|
} else {
|
|
|
|
error(e);
|
|
|
|
}
|
|
|
|
return respond(id, {});
|
2020-12-29 20:46:58 -05:00
|
|
|
}
|
2020-12-07 05:46:39 -05:00
|
|
|
}
|
|
|
|
case "getDocumentHighlights": {
|
|
|
|
return respond(
|
|
|
|
id,
|
|
|
|
languageService.getDocumentHighlights(
|
|
|
|
request.specifier,
|
|
|
|
request.position,
|
|
|
|
request.filesToSearch,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2021-04-19 21:26:36 -04:00
|
|
|
case "getEncodedSemanticClassifications": {
|
|
|
|
return respond(
|
|
|
|
id,
|
|
|
|
languageService.getEncodedSemanticClassifications(
|
|
|
|
request.specifier,
|
|
|
|
request.span,
|
|
|
|
ts.SemanticClassificationFormat.TwentyTwenty,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2021-01-31 22:30:41 -05:00
|
|
|
case "getImplementation": {
|
2020-12-07 05:46:39 -05:00
|
|
|
return respond(
|
|
|
|
id,
|
2021-01-31 22:30:41 -05:00
|
|
|
languageService.getImplementationAtPosition(
|
2020-12-07 05:46:39 -05:00
|
|
|
request.specifier,
|
|
|
|
request.position,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2021-01-31 22:30:41 -05:00
|
|
|
case "getNavigationTree": {
|
2020-12-07 05:46:39 -05:00
|
|
|
return respond(
|
|
|
|
id,
|
2021-01-31 22:30:41 -05:00
|
|
|
languageService.getNavigationTree(request.specifier),
|
2020-12-07 05:46:39 -05:00
|
|
|
);
|
|
|
|
}
|
2021-04-02 02:21:07 -04:00
|
|
|
case "getOutliningSpans": {
|
|
|
|
return respond(
|
|
|
|
id,
|
|
|
|
languageService.getOutliningSpans(
|
|
|
|
request.specifier,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2021-01-31 22:30:41 -05:00
|
|
|
case "getQuickInfo": {
|
2021-01-12 16:53:27 -05:00
|
|
|
return respond(
|
|
|
|
id,
|
2021-01-31 22:30:41 -05:00
|
|
|
languageService.getQuickInfoAtPosition(
|
2021-01-12 16:53:27 -05:00
|
|
|
request.specifier,
|
|
|
|
request.position,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2021-01-31 22:30:41 -05:00
|
|
|
case "getReferences": {
|
2020-12-29 19:58:20 -05:00
|
|
|
return respond(
|
|
|
|
id,
|
2021-01-31 22:30:41 -05:00
|
|
|
languageService.getReferencesAtPosition(
|
2020-12-29 19:58:20 -05:00
|
|
|
request.specifier,
|
|
|
|
request.position,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2021-02-15 21:34:09 -05:00
|
|
|
case "getSignatureHelpItems": {
|
|
|
|
return respond(
|
|
|
|
id,
|
|
|
|
languageService.getSignatureHelpItems(
|
|
|
|
request.specifier,
|
|
|
|
request.position,
|
|
|
|
request.options,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2021-03-23 19:33:25 -04:00
|
|
|
case "getSmartSelectionRange": {
|
|
|
|
return respond(
|
|
|
|
id,
|
|
|
|
languageService.getSmartSelectionRange(
|
|
|
|
request.specifier,
|
|
|
|
request.position,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2021-02-04 13:53:02 -05:00
|
|
|
case "getSupportedCodeFixes": {
|
|
|
|
return respond(
|
|
|
|
id,
|
|
|
|
ts.getSupportedCodeFixes(),
|
|
|
|
);
|
|
|
|
}
|
2021-04-19 01:11:26 -04:00
|
|
|
case "prepareCallHierarchy": {
|
|
|
|
return respond(
|
|
|
|
id,
|
|
|
|
languageService.prepareCallHierarchy(
|
|
|
|
request.specifier,
|
|
|
|
request.position,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
case "provideCallHierarchyIncomingCalls": {
|
|
|
|
return respond(
|
|
|
|
id,
|
|
|
|
languageService.provideCallHierarchyIncomingCalls(
|
|
|
|
request.specifier,
|
|
|
|
request.position,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
case "provideCallHierarchyOutgoingCalls": {
|
|
|
|
return respond(
|
|
|
|
id,
|
|
|
|
languageService.provideCallHierarchyOutgoingCalls(
|
|
|
|
request.specifier,
|
|
|
|
request.position,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2020-12-07 05:46:39 -05:00
|
|
|
default:
|
|
|
|
throw new TypeError(
|
|
|
|
// @ts-ignore exhausted case statement sets type to never
|
|
|
|
`Invalid request method for request: "${request.method}" (${id})`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @param {{ debug: boolean; }} init */
|
|
|
|
function serverInit({ debug: debugFlag }) {
|
|
|
|
if (hasStarted) {
|
|
|
|
throw new Error("The language server has already been initialized.");
|
|
|
|
}
|
|
|
|
hasStarted = true;
|
|
|
|
languageService = ts.createLanguageService(host);
|
|
|
|
setLogDebug(debugFlag, "TSLS");
|
|
|
|
debug("serverInit()");
|
|
|
|
}
|
|
|
|
|
2020-10-13 19:52:49 -04:00
|
|
|
let hasStarted = false;
|
|
|
|
|
|
|
|
/** Startup the runtime environment, setting various flags.
|
2020-11-22 18:20:32 -05:00
|
|
|
* @param {{ debugFlag?: boolean; legacyFlag?: boolean; }} msg
|
2020-10-13 19:52:49 -04:00
|
|
|
*/
|
2020-11-02 14:41:20 -05:00
|
|
|
function startup({ debugFlag = false }) {
|
2020-10-13 19:52:49 -04:00
|
|
|
if (hasStarted) {
|
|
|
|
throw new Error("The compiler runtime already started.");
|
2020-07-23 09:29:36 -04:00
|
|
|
}
|
2020-10-13 19:52:49 -04:00
|
|
|
hasStarted = true;
|
2020-09-26 10:33:25 -04:00
|
|
|
setLogDebug(!!debugFlag, "TS");
|
2020-07-23 09:29:36 -04:00
|
|
|
}
|
|
|
|
|
2020-11-02 14:41:20 -05:00
|
|
|
// A build time only op that provides some setup information that is used to
|
|
|
|
// ensure the snapshot is setup properly.
|
|
|
|
/** @type {{ buildSpecifier: string; libs: string[] }} */
|
2021-04-12 15:55:05 -04:00
|
|
|
const { buildSpecifier, libs } = core.opSync("op_build_info", {});
|
2020-11-02 14:41:20 -05:00
|
|
|
for (const lib of libs) {
|
2020-11-03 10:19:29 -05:00
|
|
|
const specifier = `lib.${lib}.d.ts`;
|
2020-11-02 14:41:20 -05:00
|
|
|
// we are using internal APIs here to "inject" our custom libraries into
|
|
|
|
// tsc, so things like `"lib": [ "deno.ns" ]` are supported.
|
|
|
|
if (!ts.libs.includes(lib)) {
|
|
|
|
ts.libs.push(lib);
|
|
|
|
ts.libMap.set(lib, `lib.${lib}.d.ts`);
|
|
|
|
}
|
|
|
|
// we are caching in memory common type libraries that will be re-used by
|
|
|
|
// tsc on when the snapshot is restored
|
|
|
|
assert(
|
|
|
|
host.getSourceFile(`${ASSETS}${specifier}`, ts.ScriptTarget.ESNext),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
// this helps ensure as much as possible is in memory that is re-usable
|
|
|
|
// before the snapshotting is done, which helps unsure fast "startup" for
|
|
|
|
// subsequent uses of tsc in Deno.
|
|
|
|
const TS_SNAPSHOT_PROGRAM = ts.createProgram({
|
|
|
|
rootNames: [buildSpecifier],
|
|
|
|
options: SNAPSHOT_COMPILE_OPTIONS,
|
|
|
|
host,
|
|
|
|
});
|
|
|
|
ts.getPreEmitDiagnostics(TS_SNAPSHOT_PROGRAM);
|
|
|
|
|
|
|
|
// exposes the two functions that are called by `tsc::exec()` when type
|
|
|
|
// checking TypeScript.
|
2020-10-13 19:52:49 -04:00
|
|
|
globalThis.startup = startup;
|
|
|
|
globalThis.exec = exec;
|
2020-12-07 05:46:39 -05:00
|
|
|
|
|
|
|
// exposes the functions that are called when the compiler is used as a
|
|
|
|
// language service.
|
|
|
|
globalThis.serverInit = serverInit;
|
|
|
|
globalThis.serverRequest = serverRequest;
|
2020-07-19 13:49:44 -04:00
|
|
|
})(this);
|