108 lines
3.2 KiB
JavaScript
108 lines
3.2 KiB
JavaScript
/*---------------------------------------------------------------------------------------------
|
|
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
* Licensed under the MIT License. See License.txt in the project root for license information.
|
|
*--------------------------------------------------------------------------------------------*/
|
|
export class DebugNameData {
|
|
owner;
|
|
debugNameSource;
|
|
referenceFn;
|
|
constructor(owner, debugNameSource, referenceFn) {
|
|
this.owner = owner;
|
|
this.debugNameSource = debugNameSource;
|
|
this.referenceFn = referenceFn;
|
|
}
|
|
getDebugName(target) {
|
|
return getDebugName(target, this);
|
|
}
|
|
}
|
|
const countPerName = new Map();
|
|
const cachedDebugName = new WeakMap();
|
|
export function getDebugName(target, data) {
|
|
const cached = cachedDebugName.get(target);
|
|
if (cached) {
|
|
return cached;
|
|
}
|
|
const dbgName = computeDebugName(target, data);
|
|
if (dbgName) {
|
|
let count = countPerName.get(dbgName) ?? 0;
|
|
count++;
|
|
countPerName.set(dbgName, count);
|
|
const result = count === 1 ? dbgName : `${dbgName}#${count}`;
|
|
cachedDebugName.set(target, result);
|
|
return result;
|
|
}
|
|
return undefined;
|
|
}
|
|
function computeDebugName(self, data) {
|
|
const cached = cachedDebugName.get(self);
|
|
if (cached) {
|
|
return cached;
|
|
}
|
|
const ownerStr = data.owner ? formatOwner(data.owner) + `.` : '';
|
|
let result;
|
|
const debugNameSource = data.debugNameSource;
|
|
if (debugNameSource !== undefined) {
|
|
if (typeof debugNameSource === 'function') {
|
|
result = debugNameSource();
|
|
if (result !== undefined) {
|
|
return ownerStr + result;
|
|
}
|
|
}
|
|
else {
|
|
return ownerStr + debugNameSource;
|
|
}
|
|
}
|
|
const referenceFn = data.referenceFn;
|
|
if (referenceFn !== undefined) {
|
|
result = getFunctionName(referenceFn);
|
|
if (result !== undefined) {
|
|
return ownerStr + result;
|
|
}
|
|
}
|
|
if (data.owner !== undefined) {
|
|
const key = findKey(data.owner, self);
|
|
if (key !== undefined) {
|
|
return ownerStr + key;
|
|
}
|
|
}
|
|
return undefined;
|
|
}
|
|
function findKey(obj, value) {
|
|
for (const key in obj) {
|
|
if (obj[key] === value) {
|
|
return key;
|
|
}
|
|
}
|
|
return undefined;
|
|
}
|
|
const countPerClassName = new Map();
|
|
const ownerId = new WeakMap();
|
|
function formatOwner(owner) {
|
|
const id = ownerId.get(owner);
|
|
if (id) {
|
|
return id;
|
|
}
|
|
const className = getClassName(owner);
|
|
let count = countPerClassName.get(className) ?? 0;
|
|
count++;
|
|
countPerClassName.set(className, count);
|
|
const result = count === 1 ? className : `${className}#${count}`;
|
|
ownerId.set(owner, result);
|
|
return result;
|
|
}
|
|
function getClassName(obj) {
|
|
const ctor = obj.constructor;
|
|
if (ctor) {
|
|
return ctor.name;
|
|
}
|
|
return 'Object';
|
|
}
|
|
export function getFunctionName(fn) {
|
|
const fnSrc = fn.toString();
|
|
// Pattern: /** @description ... */
|
|
const regexp = /\/\*\*\s*@description\s*([^*]*)\*\//;
|
|
const match = regexp.exec(fnSrc);
|
|
const result = match ? match[1] : undefined;
|
|
return result?.trim();
|
|
}
|
|
//# sourceMappingURL=debugName.js.map
|