-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathts-file-summary.js
More file actions
202 lines (180 loc) · 5.17 KB
/
Copy pathts-file-summary.js
File metadata and controls
202 lines (180 loc) · 5.17 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import ts from "typescript";
const {
createProgram,
forEachChild,
SyntaxKind,
displayPartsToString,
NodeFlags,
} = ts;
/**
* Originally copied from https://stackoverflow.com/a/39331761
* Author: Kostya Shkryob <https://stackoverflow.com/users/2169630/kostya-shkryob>
*/
const cache = {};
/**
* Generate documentation for all classes in a set of .ts files, async
*
* @param {string[]} fileNames
* @param {ts.CompilerOptions} options
* @param {boolean} includeImported Whether to include summaries for the files that the given files import
*/
export function generateSummary(fileNames, options, includeImported = false) {
return new Promise((res, rej) => {
try {
res(generateSummarySync(fileNames, options, includeImported));
} catch (e) {
rej(e);
}
});
}
/**
* Generate documention for all classes in a set of .ts files
*
* @param {string[]} fileNames
* @param {ts.CompilerOptions} options
* @param {boolean} includeImported Whether to include summaries for the files that the given files import
*
*/
export function generateSummarySync(
fileNames,
options,
includeImported = false
) {
if (cache[JSON.stringify(fileNames)]) {
return cache[JSON.stringify(fileNames)];
}
// Build a program using the set of root file names in fileNames
let program = createProgram(fileNames, options);
// Get the checker, we will use it to find more about classes
let checker = program.getTypeChecker();
let output /*: DocEntry[]*/ = [];
if (includeImported) {
// Visit every sourceFile in the program
for (const sourceFile of program.getSourceFiles()) {
// Walk the tree to search for classes
forEachChild(sourceFile, visit);
}
} else {
for (const fileName of fileNames) {
forEachChild(program.getSourceFile(fileName), visit);
}
}
return output;
/** visit nodes finding exported classes
* @param {ts.Node} node
*/
function visit(node) {
// Only consider exported nodes
if (!isNodeExported(node)) {
return;
}
if (node.kind === SyntaxKind.ClassDeclaration) {
// This is a top level class, get its symbol
output.push(serializeClass(node));
// No need to walk any further, class expressions/inner declarations
// cannot be exported
} else if (node.kind === SyntaxKind.ModuleDeclaration) {
// This is a namespace, visit its children
forEachChild(node, visit);
}
}
/** Serialize a symbol into a json object
* @param {ts.Symbol} symbol
*/
function serializeSymbol(symbol) {
return {
name: symbol.getName(),
documentation: displayPartsToString(symbol.getDocumentationComment()),
type: checker.typeToString(
checker.getTypeOfSymbolAtLocation(symbol, symbol.valueDeclaration)
),
};
}
/**
* Serialize a class symbol information
* @param {ts.Node} node
*/
function serializeClass(node) {
let symbol = checker.getSymbolAtLocation(node.name);
let details = serializeSymbol(symbol);
// Get the construct signatures
details.decorators =
node.decorators && node.decorators.map(serializeDecorator);
let constructorType = checker.getTypeOfSymbolAtLocation(
symbol,
symbol.valueDeclaration
);
details.constructors = constructorType
.getConstructSignatures()
.map(serializeSignature);
return details;
}
/**
* @param {ts.Decorator} decorator
*/
function serializeDecorator(decorator) {
let symbol = checker.getSymbolAtLocation(
decorator.expression.getFirstToken()
);
let decoratorType = checker.getTypeOfSymbolAtLocation(
symbol,
symbol.valueDeclaration
);
let details = serializeSymbol(symbol);
details.constructors = decoratorType
.getCallSignatures()
.map(serializeSignature);
details.param = getDecoratorParam(decorator);
return details;
}
/** Serialize a signature (call or construct)
* @param {ts.Signature} signature
*/
function serializeSignature(signature) {
return {
parameters: signature.parameters.map(serializeSymbol),
returnType: checker.typeToString(signature.getReturnType()),
documentation: displayPartsToString(signature.getDocumentationComment()),
};
}
/** True if this is visible outside this file, false otherwise
* @param {ts.Node} node
*/
function isNodeExported(node) {
return (
(node.flags & NodeFlags.Export) !== 0 ||
(node.parent && node.parent.kind === SyntaxKind.SourceFile)
);
}
}
/**
*
* @param {ts.Decorator} decorator
*/
function getDecoratorParam(decorator) {
try {
return decorator.expression
.getChildren()[2]
.getChildren()[0]
.getChildren()[1]
.getChildren()
.filter((c) => c.getChildren().length)
.map((p) => ({
key: p.getChildren()[0].getText(),
value: tryJsonParse(p.getChildren()[2].getText()),
}))
.reduce(function (map, obj) {
map[obj.key] = obj.value;
return map;
}, {});
} catch (e) {
console.warn("Couldn't find decorator for: " + decorator.getText());
}
}
function tryJsonParse(thing) {
try {
return JSON.parse(thing).replace(/'/g, "");
} catch (e) {
return thing;
}
}