forked from NagRock/ts-mockito
-
Notifications
You must be signed in to change notification settings - Fork 9
/
MockableFunctionsFinder.ts
26 lines (24 loc) · 1.17 KB
/
MockableFunctionsFinder.ts
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
/**
* Looking for all function calls and declarations and provides an array of their names. The mechanism is greedy
* and tries to match as many function names as it can find and not only those of inspecting class.
*
* Matching occurrences are:
* - [.]functionName(
* - [.]functionName = (
* - [.]functionName = function(
* - [.]functionName = function otherName(
*/
export class MockableFunctionsFinder {
private functionNameRegex = /[.\s]([^.\s]+?)(?:\(|\s+=\s+(?:function\s*(?:[^.\s]+?\s*)?)?\()/g;
private cleanFunctionNameRegex = /^[.\s]([^.\s]+?)[\s(]/;
private excludedFunctionNames: string[] = ["hasOwnProperty", "function"];
public find(code: string): string[] {
return (code.match(this.functionNameRegex) || [])
.map((match: string) => match.match(this.cleanFunctionNameRegex)[1])
.filter((functionName: string) => this.isMockable(functionName))
.map((functionName: string) => functionName.indexOf('=') > 0 ? functionName.substr(0, functionName.indexOf('=')) : functionName);
}
private isMockable(name: string): boolean {
return this.excludedFunctionNames.indexOf(name) < 0;
}
}