-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypedoc-plugin-default-value.js
41 lines (35 loc) · 1.14 KB
/
typedoc-plugin-default-value.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
import { Converter, TypeScript } from 'typedoc';
export function load({ application }) {
/** @type {Map<Reflection, string>} */
const defaultValues = new Map();
const printer = TypeScript.createPrinter();
application.converter.on(
Converter.EVENT_CREATE_DECLARATION,
saveDefaultValues
);
application.converter.on(Converter.EVENT_CREATE_PARAMETER, saveDefaultValues);
function saveDefaultValues(_context, reflection) {
const node =
reflection.project.getSymbolFromReflection(reflection)?.declarations?.[0];
if (!node || !node.initializer) return;
if (
node.initializer.kind === TypeScript.SyntaxKind.ObjectLiteralExpression
) {
// Unfortunately can't just set defaultValue right here, this happens before TD sets it.
defaultValues.set(
reflection,
printer.printNode(
TypeScript.EmitHint.Expression,
node.initializer,
node.getSourceFile()
)
);
}
}
application.converter.on(Converter.EVENT_RESOLVE_BEGIN, () => {
for (const [refl, init] of defaultValues) {
refl.defaultValue = init;
}
defaultValues.clear();
});
}