1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.TypesUsageEvaluator = void 0;
|
4 | var ts = require("typescript");
|
5 | var typescript_1 = require("./helpers/typescript");
|
6 | var TypesUsageEvaluator = (function () {
|
7 | function TypesUsageEvaluator(files, typeChecker) {
|
8 | this.nodesParentsMap = new Map();
|
9 | this.typeChecker = typeChecker;
|
10 | this.computeUsages(files);
|
11 | }
|
12 | TypesUsageEvaluator.prototype.isSymbolUsedBySymbol = function (symbol, by) {
|
13 | return this.isSymbolUsedBySymbolImpl(this.getActualSymbol(symbol), this.getActualSymbol(by), new Set());
|
14 | };
|
15 | TypesUsageEvaluator.prototype.getSymbolsUsingSymbol = function (symbol) {
|
16 | return this.nodesParentsMap.get(this.getActualSymbol(symbol)) || null;
|
17 | };
|
18 | TypesUsageEvaluator.prototype.isSymbolUsedBySymbolImpl = function (fromSymbol, toSymbol, visitedSymbols) {
|
19 | if (fromSymbol === toSymbol) {
|
20 | return true;
|
21 | }
|
22 | var reachableNodes = this.nodesParentsMap.get(fromSymbol);
|
23 | if (reachableNodes !== undefined) {
|
24 | for (var _i = 0, _a = Array.from(reachableNodes); _i < _a.length; _i++) {
|
25 | var symbol = _a[_i];
|
26 | if (visitedSymbols.has(symbol)) {
|
27 | continue;
|
28 | }
|
29 | visitedSymbols.add(symbol);
|
30 | if (this.isSymbolUsedBySymbolImpl(symbol, toSymbol, visitedSymbols)) {
|
31 | return true;
|
32 | }
|
33 | }
|
34 | }
|
35 | visitedSymbols.add(fromSymbol);
|
36 | return false;
|
37 | };
|
38 | TypesUsageEvaluator.prototype.computeUsages = function (files) {
|
39 | this.nodesParentsMap.clear();
|
40 | for (var _i = 0, files_1 = files; _i < files_1.length; _i++) {
|
41 | var file = files_1[_i];
|
42 | ts.forEachChild(file, this.computeUsageForNode.bind(this));
|
43 | }
|
44 | };
|
45 | TypesUsageEvaluator.prototype.computeUsageForNode = function (node) {
|
46 | if (typescript_1.isNodeNamedDeclaration(node) && node.name) {
|
47 | var childSymbol = this.getSymbol(node.name);
|
48 | this.computeUsagesRecursively(node, childSymbol);
|
49 | }
|
50 | else if (ts.isVariableStatement(node)) {
|
51 | for (var _i = 0, _a = node.declarationList.declarations; _i < _a.length; _i++) {
|
52 | var varDeclaration = _a[_i];
|
53 | this.computeUsageForNode(varDeclaration);
|
54 | }
|
55 | }
|
56 | };
|
57 | TypesUsageEvaluator.prototype.computeUsagesRecursively = function (parent, parentSymbol) {
|
58 | var queue = parent.getChildren();
|
59 | for (var _i = 0, queue_1 = queue; _i < queue_1.length; _i++) {
|
60 | var child = queue_1[_i];
|
61 | if (child.kind === ts.SyntaxKind.JSDocComment) {
|
62 | continue;
|
63 | }
|
64 | queue.push.apply(queue, child.getChildren());
|
65 | if (ts.isIdentifier(child)) {
|
66 | var childSymbols = typescript_1.splitTransientSymbol(this.getSymbol(child), this.typeChecker);
|
67 | for (var _a = 0, childSymbols_1 = childSymbols; _a < childSymbols_1.length; _a++) {
|
68 | var childSymbol = childSymbols_1[_a];
|
69 | var symbols = this.nodesParentsMap.get(childSymbol);
|
70 | if (symbols === undefined) {
|
71 | symbols = new Set();
|
72 | this.nodesParentsMap.set(childSymbol, symbols);
|
73 | }
|
74 |
|
75 | if (childSymbol !== parentSymbol) {
|
76 | symbols.add(parentSymbol);
|
77 | }
|
78 | }
|
79 | }
|
80 | }
|
81 | };
|
82 | TypesUsageEvaluator.prototype.getSymbol = function (node) {
|
83 | var nodeSymbol = this.typeChecker.getSymbolAtLocation(node);
|
84 | if (nodeSymbol === undefined) {
|
85 | throw new Error("Cannot find symbol for node: " + node.getText());
|
86 | }
|
87 | return this.getActualSymbol(nodeSymbol);
|
88 | };
|
89 | TypesUsageEvaluator.prototype.getActualSymbol = function (symbol) {
|
90 | return typescript_1.getActualSymbol(symbol, this.typeChecker);
|
91 | };
|
92 | return TypesUsageEvaluator;
|
93 | }());
|
94 | exports.TypesUsageEvaluator = TypesUsageEvaluator;
|