-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator-script.ts
107 lines (97 loc) · 2.52 KB
/
generator-script.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
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
import {AJS} from "./index";
interface Indexable {
[key: string]: Indexable;
}
type AnalysisResult = ExactResult | MembersResult;
interface ExactResult {
propName: string;
type: string;
}
interface MembersResult {
propName: string;
memberResults: AnalysisResult[];
}
function analyse(thing: Indexable, name: string): AnalysisResult[] {
if (thing === null || typeof thing === 'undefined') {
return [{
propName: name,
type: 'any'
}]
}
const analysisResults: AnalysisResult[] = [];
const memberKeys = Object.keys(thing);
memberKeys.forEach((memberKey: string) => {
if (!memberKey.startsWith('_')) {
const memberValue = thing[memberKey];
const analysisResult = (() => {
switch (typeof memberValue) {
case 'function':
return analyseFunction(memberValue as any, memberKey);
case 'object':
if (Array.isArray(memberValue)) {
return {
propName: memberKey,
type: 'any[]'
}
}
return {
propName: memberKey,
memberResults: analyse(memberValue, memberKey)
};
default:
return {
propName: memberKey,
type: typeof memberValue
}
}
})();
analysisResults.push(analysisResult);
}
});
return analysisResults;
}
const ALPHABET = 'abcdefghijklmnopqrstuvwxyz'.split('');
function analyseFunction(f: Function, functionName: string): AnalysisResult {
const argumentNames = [];
for (let i = 0; i < f.length; i++) {
argumentNames.push(ALPHABET[i] + '?: any');
}
return {
propName: functionName,
type: '(' + argumentNames.join(', ') + ') => any'
};
}
const INDENTATION_MARK = ' ';
let indentationDepth = 0;
function formatResultsArray(results: AnalysisResult[], name: string): string {
let result = INDENTATION_MARK.repeat(indentationDepth);
result += `${name}: {\n`;
indentationDepth++;
results.forEach((value) => {
if (value.propName) {
let printedValuePropName = value.propName.includes('.') || value.propName.includes('-')
? `'${value.propName}'`
: value.propName;
if ((value as MembersResult).memberResults) {
result += formatResultsArray((value as MembersResult).memberResults, printedValuePropName)
} else {
result += `${INDENTATION_MARK.repeat(indentationDepth)}${printedValuePropName}: ${(value as ExactResult).type};`
}
result += '\n';
}
});
indentationDepth--;
result += `${INDENTATION_MARK.repeat(indentationDepth)}}`;
return result;
}
const AJS_NAME = 'AJS';
console.log(
formatResultsArray(
analyse(
// @tsignore
AJS as any,
AJS_NAME
),
AJS_NAME
)
);