Unconditionally use WeakMap in debug (#51785)

This commit is contained in:
Jake Bailey 2022-12-06 10:41:39 -08:00 committed by GitHub
parent c2fa967bff
commit bb42b5c1a6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -588,23 +588,8 @@ export namespace Debug {
if (isDebugInfoEnabled) return;
// avoid recomputing
let weakTypeTextMap: WeakMap<Type, string> | undefined;
let weakNodeTextMap: WeakMap<Node, string> | undefined;
function getWeakTypeTextMap() {
if (weakTypeTextMap === undefined) {
if (typeof WeakMap === "function") weakTypeTextMap = new WeakMap();
}
return weakTypeTextMap;
}
function getWeakNodeTextMap() {
if (weakNodeTextMap === undefined) {
if (typeof WeakMap === "function") weakNodeTextMap = new WeakMap();
}
return weakNodeTextMap;
}
const weakTypeTextMap = new WeakMap<Type, string>();
const weakNodeTextMap = new WeakMap<Node, string>();
// Add additional properties in debug mode to assist with debugging.
Object.defineProperties(objectAllocator.getSymbolConstructor().prototype, {
@ -658,11 +643,10 @@ export namespace Debug {
__debugTypeToString: {
value(this: Type) {
// avoid recomputing
const map = getWeakTypeTextMap();
let text = map?.get(this);
let text = weakTypeTextMap.get(this);
if (text === undefined) {
text = this.checker.typeToString(this);
map?.set(this, text);
weakTypeTextMap.set(this, text);
}
return text;
}
@ -738,13 +722,12 @@ export namespace Debug {
value(this: Node, includeTrivia?: boolean) {
if (nodeIsSynthesized(this)) return "";
// avoid recomputing
const map = getWeakNodeTextMap();
let text = map?.get(this);
let text = weakNodeTextMap.get(this);
if (text === undefined) {
const parseNode = getParseTreeNode(this);
const sourceFile = parseNode && getSourceFileOfNode(parseNode);
text = sourceFile ? getSourceTextOfNodeFromSourceFile(sourceFile, parseNode, includeTrivia) : "";
map?.set(this, text);
weakNodeTextMap.set(this, text);
}
return text;
}