dts-bundle-generator
Version:
DTS Bundle Generator
98 lines (97 loc) • 3.94 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var ts = require("typescript");
var declarationKinds = [
ts.SyntaxKind.InterfaceDeclaration,
ts.SyntaxKind.ClassDeclaration,
ts.SyntaxKind.EnumDeclaration,
ts.SyntaxKind.TypeAliasDeclaration,
ts.SyntaxKind.ModuleDeclaration,
ts.SyntaxKind.FunctionDeclaration,
ts.SyntaxKind.VariableDeclaration,
];
function isNodeDeclaration(node) {
return declarationKinds.indexOf(node.kind) !== -1;
}
exports.isNodeDeclaration = isNodeDeclaration;
var TypesUsageEvaluator = (function () {
function TypesUsageEvaluator(files, typeChecker) {
this.nodesParentsMap = new Map();
this.typeChecker = typeChecker;
this.computeUsages(files);
}
TypesUsageEvaluator.prototype.isTypeUsedBySymbol = function (typeNode, by) {
if (!typeNode.name) {
// anon?
return false;
}
return this.isSymbolUsedBySymbol(this.getSymbol(typeNode.name), this.getActualSymbol(by));
};
TypesUsageEvaluator.prototype.isSymbolUsedBySymbol = function (fromSymbol, toSymbol) {
if (fromSymbol === toSymbol) {
return true;
}
var reachableNodes = this.nodesParentsMap.get(fromSymbol);
if (reachableNodes) {
for (var _i = 0, _a = Array.from(reachableNodes); _i < _a.length; _i++) {
var symbol = _a[_i];
if (this.isSymbolUsedBySymbol(symbol, toSymbol)) {
return true;
}
}
}
return false;
};
TypesUsageEvaluator.prototype.computeUsages = function (files) {
this.nodesParentsMap.clear();
for (var _i = 0, files_1 = files; _i < files_1.length; _i++) {
var file = files_1[_i];
ts.forEachChild(file, this.computeUsageForNode.bind(this));
}
};
TypesUsageEvaluator.prototype.computeUsageForNode = function (node) {
if (isNodeDeclaration(node) && node.name) {
var childSymbol = this.getSymbol(node.name);
this.computeUsagesRecursively(node, childSymbol);
}
else if (node.kind === ts.SyntaxKind.VariableStatement) {
for (var _i = 0, _a = node.declarationList.declarations; _i < _a.length; _i++) {
var varDeclaration = _a[_i];
this.computeUsageForNode(varDeclaration);
}
}
};
TypesUsageEvaluator.prototype.computeUsagesRecursively = function (parent, parentSymbol) {
var queue = parent.getChildren();
for (var i = 0; i < queue.length; ++i) {
var child = queue[i];
if (child.kind === ts.SyntaxKind.JSDocComment) {
continue;
}
queue.push.apply(queue, child.getChildren());
if (child.kind === ts.SyntaxKind.Identifier) {
var childSymbol = this.getSymbol(child);
var symbols = this.nodesParentsMap.get(childSymbol);
if (!symbols) {
symbols = new Set();
this.nodesParentsMap.set(childSymbol, symbols);
}
// to avoid infinite recursion
if (childSymbol !== parentSymbol) {
symbols.add(parentSymbol);
}
}
}
};
TypesUsageEvaluator.prototype.getSymbol = function (node) {
return this.getActualSymbol(this.typeChecker.getSymbolAtLocation(node));
};
TypesUsageEvaluator.prototype.getActualSymbol = function (symbol) {
if (symbol.flags & ts.SymbolFlags.Alias) {
return this.getActualSymbol(this.typeChecker.getAliasedSymbol(symbol));
}
return symbol;
};
return TypesUsageEvaluator;
}());
exports.TypesUsageEvaluator = TypesUsageEvaluator;