-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathextract.js
145 lines (145 loc) · 5.33 KB
/
extract.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
"use strict";
exports.__esModule = true;
exports.processFiles = void 0;
var ts = require("typescript");
var fs = require('fs');
var _a = require('kleur'), green = _a.green, red = _a.red;
var functionsToIgnore = []; // optionally ['require', 'parseInt', 'exec', 'reject', 'resolve'];
var allFunctions = [];
var calledFunctions = new Map();
var currentFunction = undefined; // to keep track of which function we are inside
// =================================================================================================
/**
* Recursively walk through TypeScript code extracting
* - function declarations and
* - function calls within each function
*
* Code modified from https://convincedcoder.com/2019/01/19/Processing-TypeScript-using-TypeScript/
* @param node
* @param sourceFile
* @param indentLevel -- helpful for logging
*/
function extractFunctionCalls(node, sourceFile, indentLevel) {
// e.g `function hello()`
if (ts.isFunctionDeclaration(node)) {
node.forEachChild(function (child) {
if (ts.isIdentifier(child)) {
var declaredFunction = child.getText(sourceFile);
updateDeclaredFunctions(declaredFunction);
}
});
}
// Arrow function
if (ts.isVariableDeclaration(node) &&
node.initializer &&
ts.isArrowFunction(node.initializer) &&
indentLevel === 3) {
var child = node.getChildAt(0, sourceFile);
if (ts.isIdentifier(child)) {
var declaredFunction = child.getText(sourceFile);
updateDeclaredFunctions(declaredFunction);
}
}
// First child must be `Identifier`
// examples of what gets skipped: `fs.readFile('lol.json')` or `ipc.on('something', () => {})`
if (ts.isCallExpression(node)) {
var child = node.getChildAt(0, sourceFile);
if (ts.isIdentifier(child)) {
var calledFunction = child.getText(sourceFile);
updateCalledFunctions(calledFunction);
}
}
// logNode(node, sourceFile, indentLevel);
node.forEachChild(function (child) { return extractFunctionCalls(child, sourceFile, indentLevel + 1); });
}
/**
* Log stuff if needed
* @param node
* @param sourceFile
* @param indentLevel
*/
function logNode(node, sourceFile, indentLevel) {
var indentation = "-".repeat(indentLevel);
var syntaxKind = ts.SyntaxKind[node.kind];
var nodeText = node.getText(sourceFile).split('\n')[0];
console.log("".concat(indentation).concat(syntaxKind, ": ").concat(nodeText));
}
/**
* Update `allFunctions` and `currentFunction`
* @param declaredFunction
*/
function updateDeclaredFunctions(declaredFunction) {
currentFunction = declaredFunction;
allFunctions.push(declaredFunction);
}
/**
* Update `calledFunctions` map with current called function name
* @param calledFunction - name of the function getting called
*/
function updateCalledFunctions(calledFunction) {
if (!functionsToIgnore.includes(calledFunction)) {
if (calledFunctions.has(currentFunction)) {
var pastCalls = calledFunctions.get(currentFunction);
pastCalls.push(calledFunction);
calledFunctions.set(currentFunction, pastCalls);
}
else {
calledFunctions.set(currentFunction, [calledFunction]);
}
}
}
function processFiles(filenames) {
// =================================================================================================
// instead of: extractFunctionCalls(sourceFile, 0, sourceFile);
// grab all the root nodes first
// then do recursion for each
filenames.forEach(function (filename) {
var rootNodes = [];
var codeAsString;
var skipFile = false;
try {
codeAsString = fs.readFileSync(filename).toString();
}
catch (err) {
console.log('File', green(filename), red('not found!'), ' - skipping');
skipFile = true;
}
if (!skipFile) {
var sourceFile_1 = ts.createSourceFile(filename, codeAsString, ts.ScriptTarget.Latest);
sourceFile_1.forEachChild(function (child) {
rootNodes.push(child);
});
rootNodes.forEach(function (node) {
currentFunction = undefined;
extractFunctionCalls(node, sourceFile_1, 1);
});
}
});
calledFunctions["delete"](undefined);
// Output
console.log('');
console.log('======================================');
console.log(allFunctions);
console.log('--------------------------------------');
console.log(calledFunctions);
console.log('--------------------------------------');
console.log('Functions: \t\t\t', allFunctions.length);
console.log('Functions that call others: \t', calledFunctions.size);
console.log('--------------------------------------');
// Only include functions that exist in the `allFunctions` list
calledFunctions.forEach(function (value, key) {
calledFunctions.set(key, value.filter(function (calledFunc) {
return allFunctions.includes(calledFunc);
}));
if (!calledFunctions.get(key).length) {
calledFunctions["delete"](key);
}
});
console.log(calledFunctions);
var functions = {
all: allFunctions,
called: calledFunctions
};
return functions;
}
exports.processFiles = processFiles;