|
| 1 | +/* @internal */ |
| 2 | +namespace ts { |
| 3 | + export interface InspectValueOptions { |
| 4 | + readonly fileNameToRequire: string; |
| 5 | + } |
| 6 | + |
| 7 | + export const enum ValueKind { Const, Array, FunctionOrClass, Object } |
| 8 | + export interface ValueInfoBase { |
| 9 | + readonly name: string; |
| 10 | + } |
| 11 | + export type ValueInfo = ValueInfoSimple | ValueInfoArray | ValueInfoFunctionOrClass | ValueInfoObject; |
| 12 | + export interface ValueInfoSimple extends ValueInfoBase { |
| 13 | + readonly kind: ValueKind.Const; |
| 14 | + readonly typeName: string; |
| 15 | + readonly comment?: string | undefined; |
| 16 | + } |
| 17 | + export interface ValueInfoFunctionOrClass extends ValueInfoBase { |
| 18 | + readonly kind: ValueKind.FunctionOrClass; |
| 19 | + readonly source: string | number; // For a native function, this is the length. |
| 20 | + readonly prototypeMembers: ReadonlyArray<ValueInfo>; |
| 21 | + readonly namespaceMembers: ReadonlyArray<ValueInfo>; |
| 22 | + } |
| 23 | + export interface ValueInfoArray extends ValueInfoBase { |
| 24 | + readonly kind: ValueKind.Array; |
| 25 | + readonly inner: ValueInfo; |
| 26 | + } |
| 27 | + export interface ValueInfoObject extends ValueInfoBase { |
| 28 | + readonly kind: ValueKind.Object; |
| 29 | + readonly members: ReadonlyArray<ValueInfo>; |
| 30 | + } |
| 31 | + |
| 32 | + export function inspectModule(fileNameToRequire: string): ValueInfo { |
| 33 | + return inspectValue(removeFileExtension(getBaseFileName(fileNameToRequire)), tryRequire(fileNameToRequire)); |
| 34 | + } |
| 35 | + |
| 36 | + export function inspectValue(name: string, value: unknown): ValueInfo { |
| 37 | + return getValueInfo(name, value, getRecurser()); |
| 38 | + } |
| 39 | + |
| 40 | + type Recurser = <T>(obj: unknown, name: string, cbOk: () => T, cbFail: (isCircularReference: boolean, keyStack: ReadonlyArray<string>) => T) => T; |
| 41 | + function getRecurser(): Recurser { |
| 42 | + const seen = new Set<unknown>(); |
| 43 | + const nameStack: string[] = []; |
| 44 | + return (obj, name, cbOk, cbFail) => { |
| 45 | + if (seen.has(obj) || nameStack.length > 4) { |
| 46 | + return cbFail(seen.has(obj), nameStack); |
| 47 | + } |
| 48 | + |
| 49 | + seen.add(obj); |
| 50 | + nameStack.push(name); |
| 51 | + const res = cbOk(); |
| 52 | + nameStack.pop(); |
| 53 | + seen.delete(obj); |
| 54 | + return res; |
| 55 | + }; |
| 56 | + } |
| 57 | + |
| 58 | + function getValueInfo(name: string, value: unknown, recurser: Recurser): ValueInfo { |
| 59 | + return recurser(value, name, |
| 60 | + (): ValueInfo => { |
| 61 | + if (typeof value === "function") return getFunctionOrClassInfo(value as AnyFunction, name, recurser); |
| 62 | + if (typeof value === "object") { |
| 63 | + const builtin = getBuiltinType(name, value as object, recurser); |
| 64 | + if (builtin !== undefined) return builtin; |
| 65 | + const entries = getEntriesOfObject(value as object); |
| 66 | + return { kind: ValueKind.Object, name, members: flatMap(entries, ({ key, value }) => getValueInfo(key, value, recurser)) }; |
| 67 | + } |
| 68 | + return { kind: ValueKind.Const, name, typeName: isNullOrUndefined(value) ? "any" : typeof value }; |
| 69 | + }, |
| 70 | + (isCircularReference, keyStack) => anyValue(name, ` ${isCircularReference ? "Circular reference" : "Too-deep object hierarchy"} from ${keyStack.join(".")}`)); |
| 71 | + } |
| 72 | + |
| 73 | + function getFunctionOrClassInfo(fn: AnyFunction, name: string, recurser: Recurser): ValueInfoFunctionOrClass { |
| 74 | + const prototypeMembers = getPrototypeMembers(fn, recurser); |
| 75 | + const namespaceMembers = flatMap(getEntriesOfObject(fn), ({ key, value }) => getValueInfo(key, value, recurser)); |
| 76 | + const toString = cast(Function.prototype.toString.call(fn), isString); |
| 77 | + const source = stringContains(toString, "{ [native code] }") ? getFunctionLength(fn) : toString; |
| 78 | + return { kind: ValueKind.FunctionOrClass, name, source, namespaceMembers, prototypeMembers }; |
| 79 | + } |
| 80 | + |
| 81 | + const builtins: () => ReadonlyMap<AnyConstructor> = memoize(() => { |
| 82 | + const map = createMap<AnyConstructor>(); |
| 83 | + for (const { key, value } of getEntriesOfObject(global)) { |
| 84 | + if (typeof value === "function" && typeof value.prototype === "object" && value !== Object) { |
| 85 | + map.set(key, value as AnyConstructor); |
| 86 | + } |
| 87 | + } |
| 88 | + return map; |
| 89 | + }); |
| 90 | + function getBuiltinType(name: string, value: object, recurser: Recurser): ValueInfo | undefined { |
| 91 | + return isArray(value) |
| 92 | + ? { name, kind: ValueKind.Array, inner: value.length && getValueInfo("element", first(value), recurser) || anyValue(name) } |
| 93 | + : forEachEntry(builtins(), (builtin, builtinName): ValueInfo | undefined => |
| 94 | + value instanceof builtin ? { kind: ValueKind.Const, name, typeName: builtinName } : undefined); |
| 95 | + } |
| 96 | + |
| 97 | + function getPrototypeMembers(fn: AnyFunction, recurser: Recurser): ReadonlyArray<ValueInfo> { |
| 98 | + const prototype = fn.prototype as unknown; |
| 99 | + return typeof prototype !== "object" || prototype === null ? emptyArray : mapDefined(getEntriesOfObject(prototype as object), ({ key, value }) => |
| 100 | + key === "constructor" ? undefined : getValueInfo(key, value, recurser)); |
| 101 | + } |
| 102 | + |
| 103 | + const ignoredProperties: ReadonlySet<string> = new Set(["arguments", "caller", "constructor", "eval", "super_"]); |
| 104 | + const reservedFunctionProperties: ReadonlySet<string> = new Set(Object.getOwnPropertyNames(noop)); |
| 105 | + interface ObjectEntry { readonly key: string; readonly value: unknown; } |
| 106 | + function getEntriesOfObject(obj: object): ReadonlyArray<ObjectEntry> { |
| 107 | + const seen = createMap<true>(); |
| 108 | + const entries: ObjectEntry[] = []; |
| 109 | + let chain = obj; |
| 110 | + while (!isNullOrUndefined(chain) && chain !== Object.prototype && chain !== Function.prototype) { |
| 111 | + for (const key of Object.getOwnPropertyNames(chain)) { |
| 112 | + if (!isJsPrivate(key) && |
| 113 | + !ignoredProperties.has(key) && |
| 114 | + (typeof obj !== "function" || !reservedFunctionProperties.has(key)) && |
| 115 | + // Don't add property from a higher prototype if it already exists in a lower one |
| 116 | + addToSeen(seen, key)) { |
| 117 | + const value = safeGetPropertyOfObject(chain, key); |
| 118 | + // Don't repeat "toString" that matches signature from Object.prototype |
| 119 | + if (!(key === "toString" && typeof value === "function" && value.length === 0)) { |
| 120 | + entries.push({ key, value }); |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + chain = Object.getPrototypeOf(chain); |
| 125 | + } |
| 126 | + return entries.sort((e1, e2) => compareStringsCaseSensitive(e1.key, e2.key)); |
| 127 | + } |
| 128 | + |
| 129 | + function getFunctionLength(fn: AnyFunction): number { |
| 130 | + return tryCast(safeGetPropertyOfObject(fn, "length"), isNumber) || 0; |
| 131 | + } |
| 132 | + |
| 133 | + function safeGetPropertyOfObject(obj: object, key: string): unknown { |
| 134 | + const desc = Object.getOwnPropertyDescriptor(obj, key); |
| 135 | + return desc && desc.value; |
| 136 | + } |
| 137 | + |
| 138 | + function isNullOrUndefined(value: unknown): value is null | undefined { |
| 139 | + return value == null; // tslint:disable-line |
| 140 | + } |
| 141 | + |
| 142 | + function anyValue(name: string, comment?: string): ValueInfo { |
| 143 | + return { kind: ValueKind.Const, name, typeName: "any", comment }; |
| 144 | + } |
| 145 | + |
| 146 | + export function isJsPrivate(name: string): boolean { |
| 147 | + return name.startsWith("_"); |
| 148 | + } |
| 149 | + |
| 150 | + function tryRequire(fileNameToRequire: string): unknown { |
| 151 | + try { |
| 152 | + return require(fileNameToRequire); |
| 153 | + } |
| 154 | + catch { |
| 155 | + return undefined; |
| 156 | + } |
| 157 | + } |
| 158 | +} |
0 commit comments