-
Notifications
You must be signed in to change notification settings - Fork 679
/
jsdoc.ts
146 lines (129 loc) · 3.93 KB
/
jsdoc.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
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
// Converter functions for JSDoc defined types
// @typedef
// @callback
import { ok } from "assert";
import * as ts from "typescript";
import {
DeclarationReflection,
IntrinsicType,
ReflectionKind,
ReflectionType,
SignatureReflection,
} from "../models";
import { flatMap } from "../utils/array";
import { Context } from "./context";
import { ConverterEvents } from "./converter-events";
import {
convertParameterNodes,
convertTypeParameterNodes,
} from "./factories/signature";
export function convertJsDocTypedef(
context: Context,
symbol: ts.Symbol,
declaration: ts.JSDocTypedefTag,
nameOverride?: string
) {
if (
declaration.typeExpression &&
ts.isJSDocTypeLiteral(declaration.typeExpression)
) {
convertJsDocInterface(context, declaration, symbol, nameOverride);
return;
}
const reflection = context.createDeclarationReflection(
ReflectionKind.TypeAlias,
symbol,
nameOverride
);
reflection.type = context.converter.convertType(
context.withScope(reflection),
declaration.typeExpression?.type
);
convertTemplateParameters(
context.withScope(reflection),
declaration.parent
);
}
export function convertJsDocCallback(
context: Context,
symbol: ts.Symbol,
declaration: ts.JSDocCallbackTag,
nameOverride?: string
) {
const alias = context.createDeclarationReflection(
ReflectionKind.TypeAlias,
symbol,
nameOverride
);
const ac = context.withScope(alias);
alias.type = convertJsDocSignature(ac, declaration.typeExpression);
convertTemplateParameters(ac, declaration.parent);
}
function convertJsDocInterface(
context: Context,
declaration: ts.JSDocTypedefTag,
symbol: ts.Symbol,
nameOverride?: string
) {
const reflection = context.createDeclarationReflection(
ReflectionKind.Interface,
symbol,
nameOverride
);
const rc = context.withScope(reflection);
const type = context.checker.getDeclaredTypeOfSymbol(symbol);
for (const s of type.getProperties()) {
context.converter.convertSymbol(rc, s);
}
convertTemplateParameters(rc, declaration.parent);
}
function convertJsDocSignature(context: Context, node: ts.JSDocSignature) {
const symbol = context.getSymbolAtLocation(node) ?? node.symbol;
const type = context.getTypeAtLocation(node);
if (!symbol || !type) {
return new IntrinsicType("Function");
}
const reflection = new DeclarationReflection(
"__type",
ReflectionKind.TypeLiteral,
context.scope
);
context.registerReflection(reflection, symbol);
context.trigger(ConverterEvents.CREATE_DECLARATION, reflection, node);
const signature = new SignatureReflection(
"__type",
ReflectionKind.CallSignature,
reflection
);
context.registerReflection(signature, void 0);
const signatureCtx = context.withScope(signature);
reflection.signatures = [signature];
signature.type = context.converter.convertType(
signatureCtx,
node.type?.typeExpression?.type
);
signature.parameters = convertParameterNodes(
signatureCtx,
signature,
node.parameters
);
signature.typeParameters = convertTemplateParameterNodes(
context.withScope(reflection),
node.typeParameters
);
return new ReflectionType(reflection);
}
function convertTemplateParameters(context: Context, node: ts.JSDoc) {
ok(context.scope instanceof DeclarationReflection);
context.scope.typeParameters = convertTemplateParameterNodes(
context,
node.tags?.filter(ts.isJSDocTemplateTag)
);
}
function convertTemplateParameterNodes(
context: Context,
nodes: readonly ts.JSDocTemplateTag[] | undefined
) {
const params = flatMap(nodes ?? [], (tag) => tag.typeParameters);
return convertTypeParameterNodes(context, params);
}