@typescript/analyze-trace
Version:
Analyze the output of tsc --generatetrace
41 lines • 1.39 kB
JavaScript
;
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
const simplify = require("./simplify-type");
function getTypeTree(id, simplifiedTypes, types) {
const tree = {};
addTypeToTree(tree, id, []);
return tree;
function addTypeToTree(tree, id, ancestorIds) {
if (typeof id !== "number")
return;
let type = simplifiedTypes.get(id);
if (!type) {
type = types && simplify(types[id - 1]);
if (!type)
return;
simplifiedTypes.set(id, type);
}
const children = {};
// If there's a cycle, suppress the children, but not the type itself
if (ancestorIds.indexOf(id) < 0) {
ancestorIds.push(id);
for (const prop in type) {
if (prop.match(/type/i)) {
if (Array.isArray(type[prop])) {
for (const t of type[prop]) {
addTypeToTree(children, t, ancestorIds);
}
}
else {
addTypeToTree(children, type[prop], ancestorIds);
}
}
}
ancestorIds.pop();
}
tree[JSON.stringify(type)] = children;
}
}
module.exports = getTypeTree;
//# sourceMappingURL=get-type-tree.js.map