Skip to content

Commit 51c28a0

Browse files
committed
cleanup
1 parent a9a9e04 commit 51c28a0

File tree

6 files changed

+55
-20
lines changed

6 files changed

+55
-20
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Inside any directory, run:
1616
tcg
1717
```
1818

19-
It will remind you how to use the CLI: you need to provide specific files, or globs:
19+
It will remind you how to use the CLI: you need to provide specific files, or globs (wildcard paths):
2020

2121
```sh
2222
tcg myFile.ts folder/*.ts anotherFolder/**/*.ts

bin/extract.js

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,8 @@ function extractFunctionCalls(node, sourceFile, indentLevel) {
2323
if (ts.isFunctionDeclaration(node)) {
2424
node.forEachChild(function (child) {
2525
if (ts.isIdentifier(child)) {
26-
var functionName = child.getText(sourceFile);
27-
currentFunction = functionName;
28-
allFunctions.push(functionName);
26+
var declaredFunction = child.getText(sourceFile);
27+
updateDeclaredFunctions(declaredFunction);
2928
}
3029
});
3130
}
@@ -38,13 +37,29 @@ function extractFunctionCalls(node, sourceFile, indentLevel) {
3837
updateCalledFunctions(calledFunction);
3938
}
4039
}
41-
// Optional logging:
42-
// const indentation = "-".repeat(indentLevel);
43-
// const syntaxKind = ts.SyntaxKind[node.kind];
44-
// const nodeText = node.getText(sourceFile).split('\n')[0];
45-
// console.log(`${indentation}${syntaxKind}: ${nodeText}`);
40+
logThings(node, sourceFile, indentLevel);
4641
node.forEachChild(function (child) { return extractFunctionCalls(child, sourceFile, indentLevel + 1); });
4742
}
43+
/**
44+
* Log stuff if needed
45+
* @param node
46+
* @param sourceFile
47+
* @param indentLevel
48+
*/
49+
function logThings(node, sourceFile, indentLevel) {
50+
var indentation = "-".repeat(indentLevel);
51+
var syntaxKind = ts.SyntaxKind[node.kind];
52+
var nodeText = node.getText(sourceFile).split('\n')[0];
53+
console.log("" + indentation + syntaxKind + ": " + nodeText);
54+
}
55+
/**
56+
* Update `allFunctions` and `currentFunction`
57+
* @param declaredFunction
58+
*/
59+
function updateDeclaredFunctions(declaredFunction) {
60+
currentFunction = declaredFunction;
61+
allFunctions.push(declaredFunction);
62+
}
4863
/**
4964
* Update `calledFunctions` map with current called function name
5065
* @param calledFunction - name of the function getting called

bin/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,5 @@ function showHelpMessage() {
3838
console.log('or any combination of the above, like `' + green('myFile.ts myFolder/*.ts') + '`');
3939
}
4040
function proceed() {
41-
console.log('do some stuff');
4241
extract_1.processFiles(myArgs);
4342
}

extract.ts

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,8 @@ function extractFunctionCalls(node: ts.Node, sourceFile: ts.SourceFile, indentLe
2626
if (ts.isFunctionDeclaration(node)) {
2727
node.forEachChild(child => {
2828
if (ts.isIdentifier(child)) {
29-
const functionName: string = child.getText(sourceFile);
30-
currentFunction = functionName;
31-
allFunctions.push(functionName);
29+
const declaredFunction: string = child.getText(sourceFile);
30+
updateDeclaredFunctions(declaredFunction);
3231
}
3332
});
3433
}
@@ -43,15 +42,34 @@ function extractFunctionCalls(node: ts.Node, sourceFile: ts.SourceFile, indentLe
4342
}
4443
}
4544

46-
// Optional logging:
47-
// const indentation = "-".repeat(indentLevel);
48-
// const syntaxKind = ts.SyntaxKind[node.kind];
49-
// const nodeText = node.getText(sourceFile).split('\n')[0];
50-
// console.log(`${indentation}${syntaxKind}: ${nodeText}`);
45+
// logThings(node, sourceFile, indentLevel);
5146

5247
node.forEachChild(child => extractFunctionCalls(child, sourceFile, indentLevel + 1));
5348
}
5449

50+
/**
51+
* Log stuff if needed
52+
* @param node
53+
* @param sourceFile
54+
* @param indentLevel
55+
*/
56+
function logThings(node: ts.Node, sourceFile: ts.SourceFile, indentLevel: number) {
57+
const indentation = "-".repeat(indentLevel);
58+
const syntaxKind = ts.SyntaxKind[node.kind];
59+
const nodeText = node.getText(sourceFile).split('\n')[0];
60+
console.log(`${indentation}${syntaxKind}: ${nodeText}`);
61+
}
62+
63+
64+
/**
65+
* Update `allFunctions` and `currentFunction`
66+
* @param declaredFunction
67+
*/
68+
function updateDeclaredFunctions(declaredFunction: string): void {
69+
currentFunction = declaredFunction;
70+
allFunctions.push(declaredFunction);
71+
}
72+
5573
/**
5674
* Update `calledFunctions` map with current called function name
5775
* @param calledFunction - name of the function getting called

index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,5 @@ function showHelpMessage(): void {
4646
}
4747

4848
function proceed(): void {
49-
console.log('do some stuff');
5049
processFiles(myArgs);
5150
}

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,16 @@
99
"author": "Boris Yakubchik",
1010
"homepage": "https://yboris.dev",
1111
"license": "MIT",
12+
"repository": {
13+
"type": "git",
14+
"url": "https://github.com/whyboris/TypeScript-Call-Graph"
15+
},
1216
"main": "bin/index.js",
1317
"bin": {
1418
"tcg": "./bin/index.js"
1519
},
1620
"scripts": {
17-
"start": "tsc && mv index.js bin/index.js && mv extract.js bin/extract.js && node bin/index.js",
21+
"start": "tsc && mv index.js bin/index.js && mv extract.js bin/extract.js && node bin/index.js *.ts",
1822
"test": "node bin/index.js *.ts",
1923
"global": "npm install -g ."
2024
},

0 commit comments

Comments
 (0)