@workday/canvas-kit-docs 9.0.0-alpha.368-next.6 → 9.0.0-alpha.382-next.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/es6/docgen/createTraversals.js +41 -22
- package/dist/es6/docgen/docParser.js +285 -257
- package/dist/es6/docgen/getExternalSymbol.js +2 -2
- package/dist/es6/docgen/plugins/componentParser.js +28 -33
- package/dist/es6/docgen/plugins/enhancedComponentParser.js +211 -150
- package/dist/es6/docgen/plugins/modelParser.js +139 -79
- package/dist/es6/docgen/traversals.js +396 -397
- package/dist/es6/docgen/traverse.js +8 -9
- package/dist/es6/lib/DescriptionTooltip.js +1 -1
- package/dist/es6/lib/MDXElements.js +13 -39
- package/dist/es6/lib/MoreTooltip.js +21 -41
- package/dist/es6/lib/Specifications.js +30 -34
- package/dist/es6/lib/StylePropsTable.js +8 -9
- package/dist/es6/lib/Table.js +19 -30
- package/dist/es6/lib/Value.js +13 -17
- package/dist/es6/lib/docs.js +701 -397
- package/dist/es6/lib/widgetUtils.js +44 -76
- package/dist/es6/lib/widgets/array.js +1 -2
- package/dist/es6/lib/widgets/callExpression.js +4 -5
- package/dist/es6/lib/widgets/canvasColor.js +2 -2
- package/dist/es6/lib/widgets/component.js +1 -2
- package/dist/es6/lib/widgets/conditional.js +1 -2
- package/dist/es6/lib/widgets/enhancedComponent.js +19 -24
- package/dist/es6/lib/widgets/external.js +1 -4
- package/dist/es6/lib/widgets/function.js +5 -7
- package/dist/es6/lib/widgets/intersection.js +3 -4
- package/dist/es6/lib/widgets/model.js +4 -6
- package/dist/es6/lib/widgets/object.js +2 -3
- package/dist/es6/lib/widgets/parenthesis.js +1 -2
- package/dist/es6/lib/widgets/primitives.js +17 -39
- package/dist/es6/lib/widgets/qualifiedName.js +1 -2
- package/dist/es6/lib/widgets/symbol.js +1 -2
- package/dist/es6/lib/widgets/tuple.js +2 -3
- package/dist/es6/lib/widgets/typeParameter.js +1 -2
- package/dist/es6/lib/widgets/union.js +3 -4
- package/dist/es6/mdx/installBlock.js +6 -7
- package/dist/es6/mdx/style-props/examples/Background.js +5 -16
- package/dist/es6/mdx/style-props/examples/Border.js +5 -16
- package/dist/es6/mdx/style-props/examples/Color.js +5 -16
- package/dist/es6/mdx/style-props/examples/Depth.js +5 -16
- package/dist/es6/mdx/style-props/examples/Flex.js +11 -22
- package/dist/es6/mdx/style-props/examples/FlexItem.js +10 -21
- package/dist/es6/mdx/style-props/examples/Grid.js +6 -17
- package/dist/es6/mdx/style-props/examples/GridItem.js +6 -17
- package/dist/es6/mdx/style-props/examples/Layout.js +5 -16
- package/dist/es6/mdx/style-props/examples/Other.js +5 -16
- package/dist/es6/mdx/style-props/examples/Position.js +5 -16
- package/dist/es6/mdx/style-props/examples/Space.js +6 -17
- package/dist/es6/mdx/style-props/examples/Text.js +2 -2
- package/dist/es6/mdx/welcomePage.js +3 -3
- package/dist/mdx/9.0-UPGRADE-GUIDE.mdx +30 -3
- package/dist/mdx/preview-react/text-area/examples/Alert.tsx +1 -1
- package/dist/mdx/preview-react/text-input/examples/Alert.tsx +1 -1
- package/dist/mdx/preview-react/text-input/examples/ThemedAlert.tsx +5 -2
- package/dist/mdx/react/banner/examples/StickyAnimation.tsx +7 -5
- package/dist/mdx/react/common/examples/ResponsiveViewport.tsx +2 -3
- package/package.json +5 -5
|
@@ -1,20 +1,8 @@
|
|
|
1
|
-
var __assign = (this && this.__assign) || function () {
|
|
2
|
-
__assign = Object.assign || function(t) {
|
|
3
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
4
|
-
s = arguments[i];
|
|
5
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
6
|
-
t[p] = s[p];
|
|
7
|
-
}
|
|
8
|
-
return t;
|
|
9
|
-
};
|
|
10
|
-
return __assign.apply(this, arguments);
|
|
11
|
-
};
|
|
12
1
|
import ts from 'typescript';
|
|
13
2
|
import { getExternalSymbol } from './getExternalSymbol';
|
|
14
3
|
import t, { find } from './traverse';
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
if (plugins === void 0) { plugins = []; }
|
|
4
|
+
export class DocParser {
|
|
5
|
+
constructor(program, plugins = []) {
|
|
18
6
|
this.program = program;
|
|
19
7
|
this.plugins = plugins;
|
|
20
8
|
/**
|
|
@@ -28,14 +16,13 @@ var DocParser = /** @class */ (function () {
|
|
|
28
16
|
/**
|
|
29
17
|
* Get all {@link ExportedSymbol}s from a file.
|
|
30
18
|
*/
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
var sourceFile = this.program.getSourceFile(fileName);
|
|
19
|
+
getExportedSymbols(fileName) {
|
|
20
|
+
const symbols = [];
|
|
21
|
+
const sourceFile = this.program.getSourceFile(fileName);
|
|
35
22
|
if (!sourceFile)
|
|
36
23
|
return symbols;
|
|
37
|
-
find(sourceFile,
|
|
38
|
-
|
|
24
|
+
find(sourceFile, node => {
|
|
25
|
+
const kind = node.kind;
|
|
39
26
|
return ([
|
|
40
27
|
'VariableDeclaration',
|
|
41
28
|
'InterfaceDeclaration',
|
|
@@ -44,35 +31,38 @@ var DocParser = /** @class */ (function () {
|
|
|
44
31
|
'EnumDeclaration',
|
|
45
32
|
'ClassDeclaration',
|
|
46
33
|
]
|
|
47
|
-
.map(
|
|
48
|
-
.includes(kind) && isNodeExported(
|
|
49
|
-
}).forEach(
|
|
50
|
-
|
|
51
|
-
|
|
34
|
+
.map(k => ts.SyntaxKind[k])
|
|
35
|
+
.includes(kind) && isNodeExported(this.checker, node));
|
|
36
|
+
}).forEach(node => {
|
|
37
|
+
const symbol = getSymbolFromNode(this.checker, node);
|
|
38
|
+
const previousSymbolsLength = this.symbols.length;
|
|
52
39
|
if (symbol) {
|
|
53
|
-
|
|
40
|
+
const exportedSymbol = {
|
|
41
|
+
name: symbol.name,
|
|
42
|
+
fileName,
|
|
43
|
+
...findDocComment(this.checker, symbol),
|
|
44
|
+
type: this.getValueFromNode(node),
|
|
45
|
+
};
|
|
54
46
|
symbols.push(exportedSymbol);
|
|
55
|
-
|
|
47
|
+
const addedSymbolsLength = previousSymbolsLength - this.symbols.length;
|
|
56
48
|
// add all symbols added by the parser
|
|
57
49
|
if (addedSymbolsLength) {
|
|
58
|
-
|
|
50
|
+
this.symbols.slice(addedSymbolsLength).forEach(symbol => {
|
|
59
51
|
symbols.push(symbol);
|
|
60
52
|
});
|
|
61
53
|
}
|
|
62
|
-
|
|
54
|
+
this.symbols.push(exportedSymbol);
|
|
63
55
|
}
|
|
64
56
|
});
|
|
65
57
|
return symbols;
|
|
66
|
-
}
|
|
67
|
-
|
|
58
|
+
}
|
|
59
|
+
getValueFromNode(node) {
|
|
68
60
|
return getValueFromNode(this, node);
|
|
69
|
-
}
|
|
70
|
-
|
|
61
|
+
}
|
|
62
|
+
getValueFromType(type, node) {
|
|
71
63
|
return getValueFromType(this, type, node);
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
}());
|
|
75
|
-
export { DocParser };
|
|
64
|
+
}
|
|
65
|
+
}
|
|
76
66
|
/**
|
|
77
67
|
* This is the main recursive function for creating docs from the Typescript AST. The AST is
|
|
78
68
|
* traversed and type information is extracted from source code. The Typescript type checker is used
|
|
@@ -90,7 +80,7 @@ function getValueFromNode(parser, node) {
|
|
|
90
80
|
// This shouldn't happen, but we'd rather see `???` in the output than crash
|
|
91
81
|
return unknownValue('???');
|
|
92
82
|
}
|
|
93
|
-
return (parser.plugins.reduce(
|
|
83
|
+
return (parser.plugins.reduce((result, fn) => {
|
|
94
84
|
return result || fn(node, parser);
|
|
95
85
|
}, undefined) || _getValueFromNode(parser, node));
|
|
96
86
|
}
|
|
@@ -99,7 +89,7 @@ function getValueFromNode(parser, node) {
|
|
|
99
89
|
*/
|
|
100
90
|
function _getValueFromNode(parser, node) {
|
|
101
91
|
var _a, _b, _c, _d, _e, _f;
|
|
102
|
-
|
|
92
|
+
const { checker } = parser;
|
|
103
93
|
// Uncomment for debugging
|
|
104
94
|
// console.log(
|
|
105
95
|
// t.getKindNameFromNode(node) || node.kind,
|
|
@@ -119,7 +109,7 @@ function _getValueFromNode(parser, node) {
|
|
|
119
109
|
if (t.isTupleType(node)) {
|
|
120
110
|
return {
|
|
121
111
|
kind: 'tuple',
|
|
122
|
-
value: node.elements.map(
|
|
112
|
+
value: node.elements.map(e => getValueFromNode(parser, e)),
|
|
123
113
|
};
|
|
124
114
|
}
|
|
125
115
|
/**
|
|
@@ -140,22 +130,22 @@ function _getValueFromNode(parser, node) {
|
|
|
140
130
|
if ((t.isTypeAliasDeclaration(node) && t.isTypeLiteral(node.type)) ||
|
|
141
131
|
t.isInterfaceDeclaration(node)) {
|
|
142
132
|
// Treat Interfaces and Types with TypeLiterals as interfaces
|
|
143
|
-
|
|
144
|
-
|
|
133
|
+
const type = checker.getTypeAtLocation(node);
|
|
134
|
+
const properties = type
|
|
145
135
|
.getProperties()
|
|
146
|
-
.map(
|
|
136
|
+
.map(p => {
|
|
147
137
|
return getValueFromNode(parser, getValueDeclaration(p));
|
|
148
138
|
})
|
|
149
139
|
.filter(filterObjectProperties);
|
|
150
140
|
// get index signature...
|
|
151
|
-
|
|
141
|
+
const indexType = getIndexSignatureFromType(parser, type);
|
|
152
142
|
if (isObject(type) && (properties.length || indexType)) {
|
|
153
|
-
|
|
154
|
-
node.parent.typeParameters)) === null || _a === void 0 ? void 0 : _a.map(
|
|
143
|
+
const typeParameters = (_a = (node.typeParameters ||
|
|
144
|
+
node.parent.typeParameters)) === null || _a === void 0 ? void 0 : _a.map(p => getValueFromNode(parser, p));
|
|
155
145
|
return {
|
|
156
146
|
kind: 'object',
|
|
157
|
-
properties
|
|
158
|
-
typeParameters
|
|
147
|
+
properties,
|
|
148
|
+
typeParameters,
|
|
159
149
|
indexSignature: indexType,
|
|
160
150
|
};
|
|
161
151
|
}
|
|
@@ -176,7 +166,7 @@ function _getValueFromNode(parser, node) {
|
|
|
176
166
|
* ```
|
|
177
167
|
*/
|
|
178
168
|
if (t.isTypeAliasDeclaration(node)) {
|
|
179
|
-
|
|
169
|
+
const typeParameters = (_b = node.typeParameters) === null || _b === void 0 ? void 0 : _b.map(p => getValueFromNode(parser, p));
|
|
180
170
|
// We need to test if the `node.type` is a TypeReference and if that TypeReference is an exported
|
|
181
171
|
// symbol. If the TypeReference is exported, we can continue with recursing on the `node.type`. If
|
|
182
172
|
// it is not an exported symbol, we need to instead evaluate the type of the declaration directly using
|
|
@@ -190,15 +180,15 @@ function _getValueFromNode(parser, node) {
|
|
|
190
180
|
// ```
|
|
191
181
|
// If `ValueOf` was exported, the type would be documented as `ValueOf<{a: 'first', b:
|
|
192
182
|
// 'second'}>`, but if it isn't exported, the value is `'first' | 'second'`
|
|
193
|
-
|
|
194
|
-
|
|
183
|
+
const isLocalTypeReference = t.isTypeReference(node.type) && !isExportedSymbol(checker, node.type.typeName);
|
|
184
|
+
const value = isLocalTypeReference
|
|
195
185
|
? getValueFromType(parser, checker.getTypeAtLocation(node), node) ||
|
|
196
186
|
unknownValue(safeGetText(checker, node))
|
|
197
187
|
: getValueFromNode(parser, node.type);
|
|
198
188
|
return {
|
|
199
189
|
kind: 'type',
|
|
200
190
|
typeParameters: typeParameters || [],
|
|
201
|
-
value
|
|
191
|
+
value,
|
|
202
192
|
};
|
|
203
193
|
}
|
|
204
194
|
/**
|
|
@@ -208,10 +198,10 @@ function _getValueFromNode(parser, node) {
|
|
|
208
198
|
* ```
|
|
209
199
|
*/
|
|
210
200
|
if (t.isClassDeclaration(node)) {
|
|
211
|
-
|
|
212
|
-
|
|
201
|
+
const type = checker.getTypeAtLocation(node);
|
|
202
|
+
const value = getObjectValueFromType(parser, type);
|
|
213
203
|
if (value.kind === 'object') {
|
|
214
|
-
value.typeParameters = (_c = node.typeParameters) === null || _c === void 0 ? void 0 : _c.map(
|
|
204
|
+
value.typeParameters = (_c = node.typeParameters) === null || _c === void 0 ? void 0 : _c.map(p => {
|
|
215
205
|
return getValueFromNode(parser, p);
|
|
216
206
|
}).filter(filterObjectTypeParameters);
|
|
217
207
|
}
|
|
@@ -231,12 +221,12 @@ function _getValueFromNode(parser, node) {
|
|
|
231
221
|
* In this example, the TypeLiteral is `{ C: 'c' }`.
|
|
232
222
|
*/
|
|
233
223
|
if (t.isTypeLiteral(node)) {
|
|
234
|
-
|
|
235
|
-
.map(
|
|
224
|
+
const properties = node.members
|
|
225
|
+
.map(member => {
|
|
236
226
|
return getValueFromNode(parser, member);
|
|
237
227
|
})
|
|
238
228
|
.filter(filterObjectProperties);
|
|
239
|
-
return { kind: 'object', properties
|
|
229
|
+
return { kind: 'object', properties };
|
|
240
230
|
}
|
|
241
231
|
/**
|
|
242
232
|
* A ArrayType is a special notation for arrays. For example:
|
|
@@ -265,13 +255,13 @@ function _getValueFromNode(parser, node) {
|
|
|
265
255
|
* The constraint is `string` and the default is `'a'`
|
|
266
256
|
*/
|
|
267
257
|
if (t.isTypeParameter(node)) {
|
|
268
|
-
|
|
269
|
-
|
|
258
|
+
const constraint = node.constraint ? getValueFromNode(parser, node.constraint) : undefined;
|
|
259
|
+
const defaultValue = node.default ? getValueFromNode(parser, node.default) : undefined;
|
|
270
260
|
return {
|
|
271
261
|
kind: 'typeParameter',
|
|
272
262
|
name: node.name.text,
|
|
273
|
-
defaultValue
|
|
274
|
-
constraint
|
|
263
|
+
defaultValue,
|
|
264
|
+
constraint,
|
|
275
265
|
required: !defaultValue,
|
|
276
266
|
};
|
|
277
267
|
}
|
|
@@ -295,13 +285,19 @@ function _getValueFromNode(parser, node) {
|
|
|
295
285
|
* as a `parameter` instead of a `function`
|
|
296
286
|
*/
|
|
297
287
|
if (t.isMethodDeclaration(node)) {
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
288
|
+
const signature = getValueFromSignatureNode(parser, node);
|
|
289
|
+
const symbol = getSymbolFromNode(checker, node);
|
|
290
|
+
const type = checker.getTypeAtLocation(node);
|
|
291
|
+
const jsDoc = findDocComment(checker, symbol);
|
|
302
292
|
if (jsDoc.tags.default) {
|
|
303
293
|
}
|
|
304
|
-
return
|
|
294
|
+
return {
|
|
295
|
+
kind: 'property',
|
|
296
|
+
name: (symbol === null || symbol === void 0 ? void 0 : symbol.name) || '',
|
|
297
|
+
type: signature,
|
|
298
|
+
required: symbol ? !isOptional(symbol) && !includesUndefined(type) : false,
|
|
299
|
+
...jsDoc,
|
|
300
|
+
};
|
|
305
301
|
}
|
|
306
302
|
/**
|
|
307
303
|
* A MethodSignature is a type of property declaration within a TS type. It is a special syntax
|
|
@@ -321,17 +317,22 @@ function _getValueFromNode(parser, node) {
|
|
|
321
317
|
* of a `function`.
|
|
322
318
|
*/
|
|
323
319
|
if (t.isMethodSignature(node)) {
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
return
|
|
320
|
+
const signature = getValueFromSignatureNode(parser, node);
|
|
321
|
+
const symbol = getSymbolFromNode(checker, node);
|
|
322
|
+
const jsDoc = findDocComment(checker, symbol);
|
|
323
|
+
return {
|
|
324
|
+
kind: 'property',
|
|
325
|
+
name: (symbol === null || symbol === void 0 ? void 0 : symbol.name) || node.name.text || '',
|
|
326
|
+
type: signature,
|
|
327
|
+
...jsDoc,
|
|
328
|
+
};
|
|
328
329
|
}
|
|
329
330
|
/**
|
|
330
331
|
* A FunctionType is a function declaration in Typescript's type annotation
|
|
331
332
|
*/
|
|
332
333
|
if (t.isFunctionType(node)) {
|
|
333
|
-
|
|
334
|
-
|
|
334
|
+
const declaration = node;
|
|
335
|
+
const signature = checker.getSignatureFromDeclaration(declaration);
|
|
335
336
|
if (signature) {
|
|
336
337
|
return getValueFromSignature(parser, node, signature);
|
|
337
338
|
}
|
|
@@ -368,11 +369,11 @@ function _getValueFromNode(parser, node) {
|
|
|
368
369
|
if (node.initializer &&
|
|
369
370
|
t.isIdentifier(node.initializer) &&
|
|
370
371
|
isExportedSymbol(checker, node.initializer)) {
|
|
371
|
-
|
|
372
|
+
const symbol = getSymbolFromNode(checker, node.initializer);
|
|
372
373
|
return {
|
|
373
374
|
kind: 'symbol',
|
|
374
375
|
name: node.initializer.text,
|
|
375
|
-
fileName: (_d =
|
|
376
|
+
fileName: (_d = symbol === null || symbol === void 0 ? void 0 : symbol.valueDeclaration) === null || _d === void 0 ? void 0 : _d.getSourceFile().fileName,
|
|
376
377
|
};
|
|
377
378
|
}
|
|
378
379
|
if (node.initializer && ts.isFunctionLike(node.initializer)) {
|
|
@@ -380,7 +381,7 @@ function _getValueFromNode(parser, node) {
|
|
|
380
381
|
}
|
|
381
382
|
// We have no type information in the AST. We'll get the Type from the type checker and run some
|
|
382
383
|
// tests on what we have
|
|
383
|
-
|
|
384
|
+
const type = checker.getTypeAtLocation(node.initializer || node);
|
|
384
385
|
// Both functions and objects are considered objects to Typescript
|
|
385
386
|
if (isObject(type)) {
|
|
386
387
|
if (type.objectFlags & ts.ObjectFlags.ArrayLiteral) {
|
|
@@ -388,7 +389,7 @@ function _getValueFromNode(parser, node) {
|
|
|
388
389
|
}
|
|
389
390
|
return getObjectValueFromType(parser, type);
|
|
390
391
|
}
|
|
391
|
-
|
|
392
|
+
const value = getValueFromType(parser, type);
|
|
392
393
|
if (value)
|
|
393
394
|
return value;
|
|
394
395
|
}
|
|
@@ -401,18 +402,24 @@ function _getValueFromNode(parser, node) {
|
|
|
401
402
|
* }
|
|
402
403
|
*/
|
|
403
404
|
if (t.isPropertySignature(node)) {
|
|
404
|
-
|
|
405
|
-
|
|
405
|
+
const symbol = getSymbolFromNode(checker, node);
|
|
406
|
+
const jsDoc = findDocComment(checker, symbol);
|
|
406
407
|
// Get the name of the property - it could be a symbol or have a `name` that is an identifier or string literal
|
|
407
|
-
|
|
408
|
+
const name = (symbol === null || symbol === void 0 ? void 0 : symbol.name) ||
|
|
408
409
|
(t.isIdentifier(node.name)
|
|
409
410
|
? node.name.text
|
|
410
411
|
: t.isStringLiteral(node.name)
|
|
411
412
|
? node.name.text
|
|
412
413
|
: '');
|
|
413
|
-
return
|
|
414
|
+
return {
|
|
415
|
+
kind: 'property',
|
|
416
|
+
name,
|
|
417
|
+
required: node.questionToken ? false : true,
|
|
418
|
+
type: node.type
|
|
414
419
|
? getValueFromNode(parser, node.type)
|
|
415
|
-
: unknownValue(safeGetText(checker, node))
|
|
420
|
+
: unknownValue(safeGetText(checker, node)),
|
|
421
|
+
...jsDoc,
|
|
422
|
+
};
|
|
416
423
|
}
|
|
417
424
|
/**
|
|
418
425
|
* A PropertyDeclaration is a property declared in a class
|
|
@@ -426,11 +433,16 @@ function _getValueFromNode(parser, node) {
|
|
|
426
433
|
* In this example, the PropertyDeclaration is `a = 'b'`
|
|
427
434
|
*/
|
|
428
435
|
if (t.isPropertyDeclaration(node)) {
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
return
|
|
436
|
+
const name = getNodeName(node);
|
|
437
|
+
const symbol = getSymbolFromNode(checker, node);
|
|
438
|
+
const jsDoc = findDocComment(checker, symbol);
|
|
439
|
+
const type = checker.getTypeAtLocation(node);
|
|
440
|
+
return {
|
|
441
|
+
kind: 'property',
|
|
442
|
+
name: name || (symbol === null || symbol === void 0 ? void 0 : symbol.name) || 'unknown',
|
|
443
|
+
type: getValueFromType(parser, type) || unknownValue(safeGetText(checker, node)),
|
|
444
|
+
...jsDoc,
|
|
445
|
+
};
|
|
434
446
|
}
|
|
435
447
|
/**
|
|
436
448
|
* An ObjectLiteralExpression is a JS value of an object literal.
|
|
@@ -448,8 +460,8 @@ function _getValueFromNode(parser, node) {
|
|
|
448
460
|
return {
|
|
449
461
|
kind: 'object',
|
|
450
462
|
properties: node.properties
|
|
451
|
-
.flatMap(
|
|
452
|
-
|
|
463
|
+
.flatMap(property => {
|
|
464
|
+
const value = getValueFromNode(parser, property);
|
|
453
465
|
if (value.kind === 'object') {
|
|
454
466
|
return value.properties;
|
|
455
467
|
}
|
|
@@ -465,35 +477,35 @@ function _getValueFromNode(parser, node) {
|
|
|
465
477
|
};
|
|
466
478
|
}
|
|
467
479
|
if (t.isSpreadAssignment(node)) {
|
|
468
|
-
|
|
469
|
-
|
|
480
|
+
const symbol = getSymbolFromNode(checker, node.expression);
|
|
481
|
+
const declaration = getValueDeclaration(symbol);
|
|
470
482
|
if (declaration) {
|
|
471
483
|
return getValueFromNode(parser, declaration);
|
|
472
484
|
}
|
|
473
485
|
}
|
|
474
486
|
if (t.isArrayLiteralExpression(node)) {
|
|
475
|
-
|
|
476
|
-
node.elements.forEach(
|
|
487
|
+
let values = [];
|
|
488
|
+
node.elements.forEach(element => {
|
|
477
489
|
if (t.isSpreadElement(element)) {
|
|
478
490
|
t.getKindNameFromNode(element);
|
|
479
|
-
|
|
491
|
+
const value = getValueFromNode(parser, element);
|
|
480
492
|
if (value.kind === 'array' || value.kind === 'tuple') {
|
|
481
|
-
|
|
493
|
+
values = values.concat(value.value);
|
|
482
494
|
}
|
|
483
495
|
}
|
|
484
496
|
else {
|
|
485
|
-
|
|
486
|
-
|
|
497
|
+
const value = getValueFromNode(parser, element);
|
|
498
|
+
values.push(value);
|
|
487
499
|
}
|
|
488
500
|
});
|
|
489
501
|
return {
|
|
490
502
|
kind: 'tuple',
|
|
491
|
-
value:
|
|
503
|
+
value: values,
|
|
492
504
|
};
|
|
493
505
|
}
|
|
494
506
|
if (t.isSpreadElement(node)) {
|
|
495
|
-
|
|
496
|
-
|
|
507
|
+
const symbol = getSymbolFromNode(checker, node.expression);
|
|
508
|
+
const declaration = getValueDeclaration(symbol);
|
|
497
509
|
if (declaration) {
|
|
498
510
|
return getValueFromNode(parser, declaration);
|
|
499
511
|
}
|
|
@@ -510,20 +522,27 @@ function _getValueFromNode(parser, node) {
|
|
|
510
522
|
*
|
|
511
523
|
* */
|
|
512
524
|
if (t.isPropertyAssignment(node)) {
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
525
|
+
const type = checker.getTypeAtLocation(node);
|
|
526
|
+
const symbol = getSymbolFromNode(checker, node);
|
|
527
|
+
const jsDoc = findDocComment(checker, symbol);
|
|
516
528
|
// For default values, we want the value and not the type. An `AsExpression` redirects to types,
|
|
517
529
|
// so we want to bypass it to get the value node
|
|
518
|
-
|
|
530
|
+
const defaultValueNode = t.isAsExpression(node.initializer)
|
|
519
531
|
? node.initializer.expression
|
|
520
532
|
: node.initializer;
|
|
521
|
-
return
|
|
533
|
+
return {
|
|
534
|
+
kind: 'property',
|
|
535
|
+
name: (symbol === null || symbol === void 0 ? void 0 : symbol.name) || '',
|
|
536
|
+
defaultValue: getValueFromNode(parser, defaultValueNode),
|
|
537
|
+
type: getValueFromType(parser, type) || unknownValue(safeGetText(checker, node)),
|
|
538
|
+
required: symbol ? !isOptional(symbol) && !includesUndefined(type) : false,
|
|
539
|
+
...jsDoc,
|
|
540
|
+
};
|
|
522
541
|
}
|
|
523
542
|
// as A
|
|
524
543
|
if (t.isAsExpression(node)) {
|
|
525
544
|
if (safeGetText(checker, node) === 'const') {
|
|
526
|
-
|
|
545
|
+
const type = checker.getTypeAtLocation(node.parent);
|
|
527
546
|
return getValueFromType(parser, type) || unknownValue(safeGetText(checker, node));
|
|
528
547
|
}
|
|
529
548
|
return getValueFromNode(parser, node.type);
|
|
@@ -533,16 +552,16 @@ function _getValueFromNode(parser, node) {
|
|
|
533
552
|
// something like `keyof A`. In this case, we'll get the symbol and ask for the properties of
|
|
534
553
|
// the symbol's declaration.
|
|
535
554
|
if (t.isTypeReference(node.type)) {
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
if (
|
|
555
|
+
const symbol = getSymbolFromNode(checker, node.type.typeName);
|
|
556
|
+
const declaration = getValueDeclaration(symbol);
|
|
557
|
+
if (symbol && declaration && isExportedSymbol(checker, declaration)) {
|
|
539
558
|
return {
|
|
540
559
|
kind: 'keyof',
|
|
541
560
|
name: {
|
|
542
561
|
kind: 'symbol',
|
|
543
|
-
name:
|
|
562
|
+
name: symbol.name,
|
|
544
563
|
fileName: declaration.getSourceFile().fileName,
|
|
545
|
-
value:
|
|
564
|
+
value: `keyof ${symbol.name}`,
|
|
546
565
|
},
|
|
547
566
|
};
|
|
548
567
|
}
|
|
@@ -620,21 +639,21 @@ function _getValueFromNode(parser, node) {
|
|
|
620
639
|
}
|
|
621
640
|
// type A = `{anything}`
|
|
622
641
|
if (t.isTemplateLiteralType(node)) {
|
|
623
|
-
|
|
642
|
+
const type = checker.getTypeAtLocation(node);
|
|
624
643
|
return getValueFromType(parser, type, node) || unknownValue(checker.typeToString(type));
|
|
625
644
|
}
|
|
626
645
|
// A | B
|
|
627
646
|
if (t.isUnionType(node)) {
|
|
628
647
|
return {
|
|
629
648
|
kind: 'union',
|
|
630
|
-
value: node.types.map(
|
|
649
|
+
value: node.types.map(type => getValueFromNode(parser, type)),
|
|
631
650
|
};
|
|
632
651
|
}
|
|
633
652
|
// A & B
|
|
634
653
|
if (t.isIntersectionType(node)) {
|
|
635
654
|
return {
|
|
636
655
|
kind: 'intersection',
|
|
637
|
-
value: node.types.map(
|
|
656
|
+
value: node.types.map(type => getValueFromNode(parser, type)),
|
|
638
657
|
};
|
|
639
658
|
}
|
|
640
659
|
// ()
|
|
@@ -643,7 +662,7 @@ function _getValueFromNode(parser, node) {
|
|
|
643
662
|
}
|
|
644
663
|
// type A = B['C']
|
|
645
664
|
if (t.isIndexedAccessType(node)) {
|
|
646
|
-
|
|
665
|
+
const type = checker.getTypeAtLocation(node);
|
|
647
666
|
return getValueFromType(parser, type, node) || unknownValue(safeGetText(checker, node));
|
|
648
667
|
}
|
|
649
668
|
/**
|
|
@@ -684,15 +703,15 @@ function _getValueFromNode(parser, node) {
|
|
|
684
703
|
*/
|
|
685
704
|
if (t.isQualifiedName(node)) {
|
|
686
705
|
if (isExportedSymbol(checker, node.left)) {
|
|
687
|
-
|
|
706
|
+
const value = checker.typeToString(checker.getTypeAtLocation(node.left));
|
|
688
707
|
return {
|
|
689
708
|
kind: 'qualifiedName',
|
|
690
|
-
left: { kind: 'symbol', name: safeGetText(checker, node.left), value
|
|
709
|
+
left: { kind: 'symbol', name: safeGetText(checker, node.left), value },
|
|
691
710
|
right: { kind: 'string', value: safeGetText(checker, node.right) },
|
|
692
711
|
};
|
|
693
712
|
}
|
|
694
713
|
// if the node.left is not exported, we'll reduce to a type
|
|
695
|
-
|
|
714
|
+
const type = checker.getTypeAtLocation(node);
|
|
696
715
|
return getValueFromType(parser, type, node) || unknownValue(checker.typeToString(type));
|
|
697
716
|
}
|
|
698
717
|
/**
|
|
@@ -707,12 +726,12 @@ function _getValueFromNode(parser, node) {
|
|
|
707
726
|
*/
|
|
708
727
|
if (t.isTypeQuery(node)) {
|
|
709
728
|
if (isExportedSymbol(checker, node.exprName)) {
|
|
710
|
-
|
|
711
|
-
return { kind: 'symbol', name: node.exprName.getText(), value
|
|
729
|
+
const value = checker.typeToString(checker.getTypeAtLocation(node.exprName));
|
|
730
|
+
return { kind: 'symbol', name: node.exprName.getText(), value };
|
|
712
731
|
}
|
|
713
|
-
|
|
714
|
-
if (
|
|
715
|
-
|
|
732
|
+
const symbol = getSymbolFromNode(checker, node.exprName);
|
|
733
|
+
if (symbol) {
|
|
734
|
+
const declaration = getValueDeclaration(symbol);
|
|
716
735
|
return getValueFromNode(parser, declaration);
|
|
717
736
|
}
|
|
718
737
|
}
|
|
@@ -728,11 +747,11 @@ function _getValueFromNode(parser, node) {
|
|
|
728
747
|
* add additional properties on the function.
|
|
729
748
|
*/
|
|
730
749
|
if (t.isPropertyAccessExpression(node)) {
|
|
731
|
-
|
|
750
|
+
let typeInfo;
|
|
732
751
|
if (t.isAsExpression(node.name)) {
|
|
733
752
|
typeInfo = getValueFromNode(parser, node.name);
|
|
734
753
|
}
|
|
735
|
-
|
|
754
|
+
const type = checker.getTypeAtLocation(node);
|
|
736
755
|
typeInfo = getValueFromType(parser, type) || unknownValue(checker.typeToString(type));
|
|
737
756
|
return { kind: 'property', name: node.name.getText(), type: typeInfo };
|
|
738
757
|
}
|
|
@@ -753,25 +772,25 @@ function _getValueFromNode(parser, node) {
|
|
|
753
772
|
if (t.isTypeReference(node)) {
|
|
754
773
|
// handle `as const` specially. If we don't do this, we'll get into an infinite loop
|
|
755
774
|
if (safeGetText(checker, node) === 'const') {
|
|
756
|
-
|
|
757
|
-
return getValueFromType(parser,
|
|
775
|
+
const type = checker.getTypeAtLocation(node.parent.parent);
|
|
776
|
+
return getValueFromType(parser, type) || unknownValue(node.parent.parent.getText());
|
|
758
777
|
}
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
778
|
+
const typeParameters = (_e = node.typeArguments) === null || _e === void 0 ? void 0 : _e.map(p => getValueFromNode(parser, p));
|
|
779
|
+
const symbolNode = t.isQualifiedName(node.typeName) ? node.typeName.right : node.typeName;
|
|
780
|
+
const symbol = getSymbolFromNode(checker, symbolNode);
|
|
781
|
+
const fileName = (_f = getValueDeclaration(symbol)) === null || _f === void 0 ? void 0 : _f.getSourceFile().fileName;
|
|
782
|
+
const externalSymbol = getExternalSymbol((symbol === null || symbol === void 0 ? void 0 : symbol.name) || safeGetText(checker, node), fileName);
|
|
764
783
|
if (externalSymbol) {
|
|
765
784
|
return {
|
|
766
785
|
kind: 'external',
|
|
767
|
-
name: (
|
|
786
|
+
name: (symbol === null || symbol === void 0 ? void 0 : symbol.name) || '',
|
|
768
787
|
url: externalSymbol,
|
|
769
|
-
typeParameters
|
|
788
|
+
typeParameters,
|
|
770
789
|
};
|
|
771
790
|
}
|
|
772
791
|
if (isExportedSymbol(checker, symbolNode)) {
|
|
773
|
-
|
|
774
|
-
return { kind: 'symbol', name: safeGetText(checker, node.typeName), typeParameters
|
|
792
|
+
const value = checker.typeToString(checker.getTypeAtLocation(node.typeName));
|
|
793
|
+
return { kind: 'symbol', name: safeGetText(checker, node.typeName), typeParameters, value };
|
|
775
794
|
}
|
|
776
795
|
// If it is a qualified name, handle that specially. The `left` might be a symbol
|
|
777
796
|
if (t.isQualifiedName(node.typeName)) {
|
|
@@ -779,15 +798,15 @@ function _getValueFromNode(parser, node) {
|
|
|
779
798
|
}
|
|
780
799
|
// The TypeReference isn't exported, so we'll return the type of the
|
|
781
800
|
// symbol's value declaration directly
|
|
782
|
-
|
|
783
|
-
if (
|
|
801
|
+
const type = checker.getTypeAtLocation(node);
|
|
802
|
+
if (symbol) {
|
|
784
803
|
if (type.getFlags() & ts.TypeFlags.Instantiable) {
|
|
785
804
|
// It is a generic type
|
|
786
|
-
return { kind: 'generic', name:
|
|
805
|
+
return { kind: 'generic', name: symbol === null || symbol === void 0 ? void 0 : symbol.name };
|
|
787
806
|
}
|
|
788
|
-
|
|
807
|
+
const declaration = getValueDeclaration(symbol);
|
|
789
808
|
if (declaration) {
|
|
790
|
-
|
|
809
|
+
const typeInfo = getValueFromNode(parser, declaration);
|
|
791
810
|
// we want to embed objects
|
|
792
811
|
if (typeInfo.kind === 'object') {
|
|
793
812
|
return typeInfo;
|
|
@@ -818,11 +837,16 @@ function _getValueFromNode(parser, node) {
|
|
|
818
837
|
* points to the VariableDeclaration.
|
|
819
838
|
*/
|
|
820
839
|
if (t.isShorthandPropertyAssignment(node)) {
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
840
|
+
const type = checker.getTypeAtLocation(node);
|
|
841
|
+
const symbol = getSymbolFromNode(checker, node);
|
|
842
|
+
const jsDoc = findDocComment(checker, symbol);
|
|
824
843
|
// see if the declaration is assigned to something exported
|
|
825
|
-
return
|
|
844
|
+
return {
|
|
845
|
+
kind: 'property',
|
|
846
|
+
name: (symbol === null || symbol === void 0 ? void 0 : symbol.name) || '',
|
|
847
|
+
type: getValueFromType(parser, type) || unknownValue(safeGetText(checker, node)),
|
|
848
|
+
...jsDoc,
|
|
849
|
+
};
|
|
826
850
|
}
|
|
827
851
|
/**
|
|
828
852
|
* Declaration of a enum
|
|
@@ -839,7 +863,7 @@ function _getValueFromNode(parser, node) {
|
|
|
839
863
|
return {
|
|
840
864
|
kind: 'object',
|
|
841
865
|
typeParameters: [],
|
|
842
|
-
properties: node.members.map(
|
|
866
|
+
properties: node.members.map((m, index) => {
|
|
843
867
|
return {
|
|
844
868
|
kind: 'property',
|
|
845
869
|
name: safeGetText(checker, m.name),
|
|
@@ -865,8 +889,8 @@ function _getValueFromNode(parser, node) {
|
|
|
865
889
|
*/
|
|
866
890
|
if (t.isIdentifier(node)) {
|
|
867
891
|
if (isExportedSymbol(checker, node)) {
|
|
868
|
-
|
|
869
|
-
|
|
892
|
+
const symbol = getSymbolFromNode(checker, node);
|
|
893
|
+
const declaration = getValueDeclaration(symbol);
|
|
870
894
|
return {
|
|
871
895
|
kind: 'symbol',
|
|
872
896
|
name: node.text,
|
|
@@ -882,20 +906,20 @@ function _getValueFromNode(parser, node) {
|
|
|
882
906
|
*
|
|
883
907
|
*/
|
|
884
908
|
if (t.isParameter(node)) {
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
909
|
+
const type = checker.getTypeAtLocation(node);
|
|
910
|
+
const symbol = getSymbolFromNode(checker, node);
|
|
911
|
+
const jsDoc = findDocComment(checker, symbol);
|
|
912
|
+
const isRequired = node.questionToken
|
|
889
913
|
? false
|
|
890
914
|
: node.initializer
|
|
891
915
|
? false
|
|
892
|
-
:
|
|
893
|
-
? !isOptional(
|
|
916
|
+
: symbol
|
|
917
|
+
? !isOptional(symbol) && !includesUndefined(type)
|
|
894
918
|
: false;
|
|
895
|
-
|
|
919
|
+
const typeInfo = node.type
|
|
896
920
|
? getValueFromNode(parser, node.type)
|
|
897
921
|
: getValueFromType(parser, type) || unknownValue(safeGetText(checker, node));
|
|
898
|
-
|
|
922
|
+
const defaultValue = node.initializer ? getValueFromNode(parser, node.initializer) : undefined;
|
|
899
923
|
/**
|
|
900
924
|
* Set default values if an object binding pattern is found. We do this at the Parameter level,
|
|
901
925
|
* because we have all the info here.
|
|
@@ -908,29 +932,37 @@ function _getValueFromNode(parser, node) {
|
|
|
908
932
|
* In this example, the ObjectBindingPattern is `{ a = 'a', b }`
|
|
909
933
|
*/
|
|
910
934
|
if (t.isObjectBindingPattern(node.name)) {
|
|
911
|
-
|
|
935
|
+
const defaults = getDefaultsFromObjectBindingParameter(parser, node);
|
|
912
936
|
if (typeInfo.kind === 'object') {
|
|
913
|
-
typeInfo.properties.forEach(
|
|
914
|
-
if (!p.defaultValue &&
|
|
915
|
-
p.defaultValue =
|
|
937
|
+
typeInfo.properties.forEach(p => {
|
|
938
|
+
if (!p.defaultValue && defaults[p.name]) {
|
|
939
|
+
p.defaultValue = defaults[p.name];
|
|
916
940
|
}
|
|
917
941
|
});
|
|
918
942
|
}
|
|
919
943
|
}
|
|
920
|
-
return
|
|
944
|
+
return {
|
|
945
|
+
kind: 'parameter',
|
|
946
|
+
name: (symbol === null || symbol === void 0 ? void 0 : symbol.name) || '',
|
|
947
|
+
defaultValue,
|
|
948
|
+
type: typeInfo,
|
|
949
|
+
required: isRequired,
|
|
950
|
+
rest: !!node.dotDotDotToken,
|
|
951
|
+
...jsDoc,
|
|
952
|
+
};
|
|
921
953
|
}
|
|
922
954
|
/**
|
|
923
955
|
* A call expression is an expression calling a function. In this case, we want to get the signature
|
|
924
956
|
* and get a type for the return type
|
|
925
957
|
*/
|
|
926
958
|
if (t.isCallExpression(node)) {
|
|
927
|
-
|
|
928
|
-
|
|
959
|
+
const type = checker.getTypeAtLocation(node);
|
|
960
|
+
const value = getValueFromType(parser, type);
|
|
929
961
|
if (value) {
|
|
930
962
|
return value;
|
|
931
963
|
}
|
|
932
964
|
}
|
|
933
|
-
|
|
965
|
+
const symbol = getSymbolFromNode(checker, node);
|
|
934
966
|
if (!symbol) {
|
|
935
967
|
return unknownValue(safeGetText(checker, node));
|
|
936
968
|
}
|
|
@@ -940,7 +972,7 @@ function _getValueFromNode(parser, node) {
|
|
|
940
972
|
function isNodeExported(checker, node) {
|
|
941
973
|
return (ts.getCombinedModifierFlags(node) & ts.ModifierFlags.Export) !== 0;
|
|
942
974
|
}
|
|
943
|
-
export
|
|
975
|
+
export const defaultJSDoc = {
|
|
944
976
|
description: '',
|
|
945
977
|
tags: {},
|
|
946
978
|
declarations: [],
|
|
@@ -950,37 +982,37 @@ export function getFullJsDocComment(checker, symbol) {
|
|
|
950
982
|
if (symbol.getDocumentationComment === undefined) {
|
|
951
983
|
return defaultJSDoc;
|
|
952
984
|
}
|
|
953
|
-
|
|
985
|
+
let mainComment = ts.displayPartsToString(symbol.getDocumentationComment(checker));
|
|
954
986
|
if (mainComment) {
|
|
955
987
|
mainComment = mainComment.replace(/\r\n/g, '\n');
|
|
956
988
|
}
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
tags.forEach(
|
|
960
|
-
|
|
961
|
-
|
|
989
|
+
const tags = symbol.getJsDocTags() || [];
|
|
990
|
+
const tagMap = {};
|
|
991
|
+
tags.forEach(tag => {
|
|
992
|
+
const trimmedText = (tag.text || '').trim();
|
|
993
|
+
const currentValue = tagMap[tag.name];
|
|
962
994
|
tagMap[tag.name] = currentValue ? currentValue + '\n' + trimmedText : trimmedText;
|
|
963
995
|
});
|
|
964
996
|
return {
|
|
965
997
|
description: mainComment,
|
|
966
|
-
declarations: ((_a = symbol === null || symbol === void 0 ? void 0 : symbol.declarations) !== null && _a !== void 0 ? _a : []).map(
|
|
998
|
+
declarations: ((_a = symbol === null || symbol === void 0 ? void 0 : symbol.declarations) !== null && _a !== void 0 ? _a : []).map(d => ({
|
|
967
999
|
name: (symbol === null || symbol === void 0 ? void 0 : symbol.name) || '',
|
|
968
1000
|
filePath: d.getSourceFile().fileName,
|
|
969
|
-
})
|
|
1001
|
+
})),
|
|
970
1002
|
tags: tagMap,
|
|
971
1003
|
};
|
|
972
1004
|
}
|
|
973
1005
|
export function findDocComment(checker, symbol) {
|
|
974
1006
|
if (symbol) {
|
|
975
|
-
|
|
976
|
-
if (
|
|
977
|
-
return
|
|
1007
|
+
const comment = getFullJsDocComment(checker, symbol);
|
|
1008
|
+
if (comment.description || comment.declarations.length || comment.tags.default) {
|
|
1009
|
+
return comment;
|
|
978
1010
|
}
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
.filter(
|
|
982
|
-
.map(
|
|
983
|
-
.filter(
|
|
1011
|
+
const rootSymbols = checker.getRootSymbols(symbol);
|
|
1012
|
+
const commentsOnRootSymbols = rootSymbols
|
|
1013
|
+
.filter(x => x !== symbol)
|
|
1014
|
+
.map(x => getFullJsDocComment(checker, x))
|
|
1015
|
+
.filter(x => !!x.description || !!comment.tags.default);
|
|
984
1016
|
if (commentsOnRootSymbols.length) {
|
|
985
1017
|
return commentsOnRootSymbols[0];
|
|
986
1018
|
}
|
|
@@ -991,7 +1023,7 @@ export function findDocComment(checker, symbol) {
|
|
|
991
1023
|
* Attempt to get the name of a declaration or expression
|
|
992
1024
|
*/
|
|
993
1025
|
export function getNodeName(node) {
|
|
994
|
-
|
|
1026
|
+
const name = ts.getNameOfDeclaration(node);
|
|
995
1027
|
if (name && 'text' in name) {
|
|
996
1028
|
return name.text;
|
|
997
1029
|
}
|
|
@@ -1014,7 +1046,7 @@ export function isObject(type) {
|
|
|
1014
1046
|
return !!(type.flags & ts.TypeFlags.Object);
|
|
1015
1047
|
}
|
|
1016
1048
|
function includesUndefined(type) {
|
|
1017
|
-
return type.isUnion() ? type.types.some(
|
|
1049
|
+
return type.isUnion() ? type.types.some(t => t.flags & ts.TypeFlags.Undefined) : false;
|
|
1018
1050
|
}
|
|
1019
1051
|
function isTupleType(type) {
|
|
1020
1052
|
var _a;
|
|
@@ -1051,9 +1083,9 @@ export function getValidDefaultFromNode(parser, node) {
|
|
|
1051
1083
|
*/
|
|
1052
1084
|
export function getDefaultsFromObjectBindingParameter(parser, node) {
|
|
1053
1085
|
if (t.isObjectBindingPattern(node.name)) {
|
|
1054
|
-
return node.name.elements.reduce(
|
|
1086
|
+
return node.name.elements.reduce((result, element) => {
|
|
1055
1087
|
if (t.isBindingElement(element) && t.isIdentifier(element.name) && element.initializer) {
|
|
1056
|
-
|
|
1088
|
+
const defaultValue = getValidDefaultFromNode(parser, element.initializer);
|
|
1057
1089
|
if (defaultValue) {
|
|
1058
1090
|
result[element.name.text] = defaultValue;
|
|
1059
1091
|
}
|
|
@@ -1081,11 +1113,11 @@ export function getDefaultsFromObjectBindingParameter(parser, node) {
|
|
|
1081
1113
|
*/
|
|
1082
1114
|
function getIndexSignatureFromType(parser, type) {
|
|
1083
1115
|
var _a;
|
|
1084
|
-
|
|
1085
|
-
|
|
1116
|
+
const { checker } = parser;
|
|
1117
|
+
const indexSignature = checker.getIndexInfoOfType(type, ts.IndexKind.String) ||
|
|
1086
1118
|
checker.getIndexInfoOfType(type, ts.IndexKind.Number);
|
|
1087
1119
|
if (indexSignature) {
|
|
1088
|
-
|
|
1120
|
+
const parameter = (_a = indexSignature.declaration) === null || _a === void 0 ? void 0 : _a.parameters[0];
|
|
1089
1121
|
return {
|
|
1090
1122
|
kind: 'indexSignature',
|
|
1091
1123
|
name: (parameter === null || parameter === void 0 ? void 0 : parameter.name) ? safeGetText(checker, parameter === null || parameter === void 0 ? void 0 : parameter.name) : '',
|
|
@@ -1107,23 +1139,23 @@ function getIndexSignatureFromType(parser, type) {
|
|
|
1107
1139
|
* infinite loops.
|
|
1108
1140
|
*/
|
|
1109
1141
|
export function getValueFromType(parser, type, node) {
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1142
|
+
const { checker } = parser;
|
|
1143
|
+
const originalNodeKind = node === null || node === void 0 ? void 0 : node.kind;
|
|
1144
|
+
const typeToString = checker.typeToString(type);
|
|
1113
1145
|
// If the type is `any`, we want to bail now
|
|
1114
1146
|
if (type.flags & ts.TypeFlags.Any) {
|
|
1115
1147
|
return { kind: 'primitive', value: 'any' };
|
|
1116
1148
|
}
|
|
1117
1149
|
// check if the node is an external symbol
|
|
1118
1150
|
// TODO: This won't work if the symbol contains a generic
|
|
1119
|
-
|
|
1151
|
+
const externalSymbol = getExternalSymbol(typeToString);
|
|
1120
1152
|
if (externalSymbol) {
|
|
1121
1153
|
return { kind: 'external', name: typeToString, url: externalSymbol };
|
|
1122
1154
|
}
|
|
1123
1155
|
if (isTupleType(type)) {
|
|
1124
1156
|
return {
|
|
1125
1157
|
kind: 'union',
|
|
1126
|
-
value: checker.getTypeArguments(type).map(
|
|
1158
|
+
value: checker.getTypeArguments(type).map(t => getValueFromType(parser, t) || unknownValue('')) ||
|
|
1127
1159
|
[],
|
|
1128
1160
|
};
|
|
1129
1161
|
}
|
|
@@ -1150,20 +1182,20 @@ export function getValueFromType(parser, type, node) {
|
|
|
1150
1182
|
// service uses for the tooltips when hovering over text in your IDE. We try to go from Type to
|
|
1151
1183
|
// Node whenever possible, but there are some cases where interacting directly with the types is
|
|
1152
1184
|
// preferred. The union example is one of such examples.
|
|
1153
|
-
|
|
1185
|
+
const typeNode = checker.typeToTypeNode(type, node, ts.NodeBuilderFlags.NoTruncation);
|
|
1154
1186
|
// We try to extract useful type information from the TypeNode and go back to recursing the AST.
|
|
1155
1187
|
// But, if the typeNode and original node have the same kind, we've actually lost information and
|
|
1156
1188
|
// should skip processing the TypeNode.
|
|
1157
1189
|
if (typeNode && originalNodeKind !== typeNode.kind) {
|
|
1158
1190
|
// find the symbol
|
|
1159
1191
|
if (t.isTypeReference(typeNode)) {
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1192
|
+
const symbol = getSymbolFromNode(checker, typeNode.typeName);
|
|
1193
|
+
const declaration = getValueDeclaration(symbol);
|
|
1194
|
+
const fileName = declaration === null || declaration === void 0 ? void 0 : declaration.getSourceFile().fileName;
|
|
1163
1195
|
if (symbol) {
|
|
1164
|
-
|
|
1165
|
-
if (
|
|
1166
|
-
return { kind: 'external', name: symbol.name, url:
|
|
1196
|
+
const externalSymbol = getExternalSymbol(symbol.name, fileName);
|
|
1197
|
+
if (externalSymbol) {
|
|
1198
|
+
return { kind: 'external', name: symbol.name, url: externalSymbol };
|
|
1167
1199
|
}
|
|
1168
1200
|
if (declaration && declaration !== node) {
|
|
1169
1201
|
if (isExportedSymbol(checker, declaration)) {
|
|
@@ -1176,18 +1208,16 @@ export function getValueFromType(parser, type, node) {
|
|
|
1176
1208
|
// Figure out if we should recurse back into AST nodes with our synthetic TypeNode. There are
|
|
1177
1209
|
// exceptions to using the TypeNode. Exceptions where we actually lose type information. Some
|
|
1178
1210
|
// examples are large unions or `keyof`:
|
|
1179
|
-
|
|
1211
|
+
let exceptions = false;
|
|
1180
1212
|
// The typeNode is a UnionType, but the number of items has overflowed with an identifier `...
|
|
1181
1213
|
// {N} more ...`, so we don't want to use the typeNode and will use `type.types` instead which
|
|
1182
1214
|
// is not shortened. In this case we want to do nothing and use the type-based union check
|
|
1183
1215
|
if (t.isUnionType(typeNode) &&
|
|
1184
1216
|
type.isUnion() &&
|
|
1185
|
-
typeNode.types.some(
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
v.typeName.escapedText.includes('more'));
|
|
1190
|
-
})) {
|
|
1217
|
+
typeNode.types.some(v => t.isIdentifier(v) ||
|
|
1218
|
+
(t.isTypeReference(v) &&
|
|
1219
|
+
t.isIdentifier(v.typeName) &&
|
|
1220
|
+
v.typeName.escapedText.includes('more')))) {
|
|
1191
1221
|
exceptions = true;
|
|
1192
1222
|
}
|
|
1193
1223
|
// A function TypeNode looses a lot of information. We'll fallback to using the type. A Function
|
|
@@ -1230,10 +1260,10 @@ export function getValueFromType(parser, type, node) {
|
|
|
1230
1260
|
if (type.isUnion()) {
|
|
1231
1261
|
// If we got here, it means a TypeNode was a TypeReference that wasn't exported or a synthetic
|
|
1232
1262
|
// TypeNode `keyof *` that `getValueFromNode` couldn't properly parse.
|
|
1233
|
-
|
|
1263
|
+
let filteredTypes = type.types;
|
|
1234
1264
|
return {
|
|
1235
1265
|
kind: 'union',
|
|
1236
|
-
value: filteredTypes.map(
|
|
1266
|
+
value: filteredTypes.map(t => getValueFromType(parser, t) || unknownValue(checker.typeToString(t))),
|
|
1237
1267
|
};
|
|
1238
1268
|
}
|
|
1239
1269
|
if (type.isStringLiteral()) {
|
|
@@ -1268,9 +1298,9 @@ export function getValueFromType(parser, type, node) {
|
|
|
1268
1298
|
}
|
|
1269
1299
|
if (isObject(type)) {
|
|
1270
1300
|
if (type.objectFlags & ts.ObjectFlags.ArrayLiteral) {
|
|
1271
|
-
|
|
1272
|
-
if (
|
|
1273
|
-
return getValueFromNode(parser,
|
|
1301
|
+
const typeNode = checker.typeToTypeNode(type, undefined, undefined);
|
|
1302
|
+
if (typeNode) {
|
|
1303
|
+
return getValueFromNode(parser, typeNode);
|
|
1274
1304
|
}
|
|
1275
1305
|
}
|
|
1276
1306
|
return getObjectValueFromType(parser, type);
|
|
@@ -1282,12 +1312,12 @@ export function unknownValue(nodeText) {
|
|
|
1282
1312
|
}
|
|
1283
1313
|
export function isExportedSymbol(checker, node) {
|
|
1284
1314
|
var _a;
|
|
1285
|
-
|
|
1315
|
+
const sourceFile = (_a = node.getSourceFile) === null || _a === void 0 ? void 0 : _a.call(node);
|
|
1286
1316
|
if (sourceFile === null || sourceFile === void 0 ? void 0 : sourceFile.isDeclarationFile) {
|
|
1287
1317
|
return true;
|
|
1288
1318
|
}
|
|
1289
|
-
|
|
1290
|
-
|
|
1319
|
+
const symbol = getSymbolFromNode(checker, node);
|
|
1320
|
+
const declarationNode = getValueDeclaration(symbol);
|
|
1291
1321
|
if (declarationNode) {
|
|
1292
1322
|
t.getKindNameFromNode(declarationNode);
|
|
1293
1323
|
// Check if the declaration's sourcefile is a declaration file
|
|
@@ -1322,8 +1352,8 @@ function safeGetText(checker, node) {
|
|
|
1322
1352
|
return node.text;
|
|
1323
1353
|
}
|
|
1324
1354
|
// Try a symbol
|
|
1325
|
-
|
|
1326
|
-
return (symbol === null || symbol === void 0 ? void 0 : symbol.name) ||
|
|
1355
|
+
const symbol = getSymbolFromNode(checker, node);
|
|
1356
|
+
return (symbol === null || symbol === void 0 ? void 0 : symbol.name) || `SyntheticNode - ${t.getKindNameFromNode(node)}`;
|
|
1327
1357
|
}
|
|
1328
1358
|
/**
|
|
1329
1359
|
* Get export members of a node. Export members are additional members of a declaration.
|
|
@@ -1337,64 +1367,64 @@ function safeGetText(checker, node) {
|
|
|
1337
1367
|
*/
|
|
1338
1368
|
function getExportMembers(parser, node) {
|
|
1339
1369
|
var _a;
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
.map(
|
|
1344
|
-
|
|
1370
|
+
const { checker } = parser;
|
|
1371
|
+
const exports = ((_a = getSymbolFromNode(checker, node)) === null || _a === void 0 ? void 0 : _a.exports) || new Map();
|
|
1372
|
+
const members = Array.from(exports.values())
|
|
1373
|
+
.map(symbol => {
|
|
1374
|
+
const declaration = getValueDeclaration(symbol);
|
|
1345
1375
|
if (declaration) {
|
|
1346
1376
|
return getValueFromNode(parser, declaration);
|
|
1347
1377
|
}
|
|
1348
1378
|
return;
|
|
1349
1379
|
})
|
|
1350
|
-
.filter(
|
|
1380
|
+
.filter((i) => !!i);
|
|
1351
1381
|
return members;
|
|
1352
1382
|
}
|
|
1353
1383
|
function getValueFromSignature(parser, declaration, signature) {
|
|
1354
1384
|
var _a;
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1385
|
+
const members = getExportMembers(parser, declaration);
|
|
1386
|
+
const typeParameters = (_a = signature.typeParameters) === null || _a === void 0 ? void 0 : _a.map(p => getValueFromType(parser, p)).filter((v) => (v === null || v === void 0 ? void 0 : v.kind) === 'typeParameter');
|
|
1387
|
+
const parameters = signature.parameters.map(s => {
|
|
1358
1388
|
return getValueFromNode(parser, getValueDeclaration(s));
|
|
1359
1389
|
});
|
|
1360
|
-
|
|
1390
|
+
const returnType = declaration.type
|
|
1361
1391
|
? getValueFromNode(parser, declaration.type)
|
|
1362
1392
|
: getValueFromType(parser, signature.getReturnType()) || unknownValue(declaration.getText());
|
|
1363
1393
|
return {
|
|
1364
1394
|
kind: 'function',
|
|
1365
|
-
typeParameters
|
|
1366
|
-
parameters
|
|
1367
|
-
members
|
|
1368
|
-
returnType
|
|
1395
|
+
typeParameters,
|
|
1396
|
+
parameters,
|
|
1397
|
+
members,
|
|
1398
|
+
returnType,
|
|
1369
1399
|
};
|
|
1370
1400
|
}
|
|
1371
1401
|
function generateSignatureFromTypeNode(parser, node) {
|
|
1372
1402
|
var _a;
|
|
1373
1403
|
if (t.isFunctionType(node) || t.isMethodSignature(node)) {
|
|
1374
|
-
|
|
1375
|
-
|
|
1404
|
+
const parameters = node.parameters.map(p => (getSymbolFromNode(parser.checker, p) || getSymbolFromNode(parser.checker, p.name)));
|
|
1405
|
+
const typeParameters = (_a = node.typeParameters) === null || _a === void 0 ? void 0 : _a.map(t => parser.checker.getTypeAtLocation(t));
|
|
1376
1406
|
return {
|
|
1377
|
-
parameters
|
|
1407
|
+
parameters,
|
|
1378
1408
|
declaration: node,
|
|
1379
|
-
typeParameters
|
|
1380
|
-
getDeclaration
|
|
1409
|
+
typeParameters,
|
|
1410
|
+
getDeclaration() {
|
|
1381
1411
|
return node;
|
|
1382
1412
|
},
|
|
1383
|
-
getParameters
|
|
1384
|
-
return
|
|
1413
|
+
getParameters() {
|
|
1414
|
+
return parameters;
|
|
1385
1415
|
},
|
|
1386
|
-
getTypeParameters
|
|
1387
|
-
return
|
|
1416
|
+
getTypeParameters() {
|
|
1417
|
+
return typeParameters;
|
|
1388
1418
|
},
|
|
1389
|
-
getReturnType
|
|
1419
|
+
getReturnType() {
|
|
1390
1420
|
return parser.checker.getTypeAtLocation(node.type || node);
|
|
1391
1421
|
},
|
|
1392
|
-
getDocumentationComment
|
|
1393
|
-
|
|
1422
|
+
getDocumentationComment() {
|
|
1423
|
+
const symbol = getSymbolFromNode(parser.checker, node);
|
|
1394
1424
|
return symbol.getDocumentationComment(parser.checker);
|
|
1395
1425
|
},
|
|
1396
|
-
getJsDocTags
|
|
1397
|
-
|
|
1426
|
+
getJsDocTags() {
|
|
1427
|
+
const symbol = getSymbolFromNode(parser.checker, node);
|
|
1398
1428
|
return symbol.getJsDocTags();
|
|
1399
1429
|
},
|
|
1400
1430
|
};
|
|
@@ -1402,8 +1432,8 @@ function generateSignatureFromTypeNode(parser, node) {
|
|
|
1402
1432
|
return undefined;
|
|
1403
1433
|
}
|
|
1404
1434
|
export function getValueFromSignatureNode(parser, declaration) {
|
|
1405
|
-
|
|
1406
|
-
|
|
1435
|
+
const { checker } = parser;
|
|
1436
|
+
const signature = checker.getSignatureFromDeclaration(declaration) ||
|
|
1407
1437
|
generateSignatureFromTypeNode(parser, declaration);
|
|
1408
1438
|
if (signature) {
|
|
1409
1439
|
return getValueFromSignature(parser, declaration, signature);
|
|
@@ -1411,13 +1441,13 @@ export function getValueFromSignatureNode(parser, declaration) {
|
|
|
1411
1441
|
return unknownValue(safeGetText(checker, declaration));
|
|
1412
1442
|
}
|
|
1413
1443
|
function getObjectValueFromType(parser, type) {
|
|
1414
|
-
|
|
1444
|
+
const properties = type
|
|
1415
1445
|
.getProperties()
|
|
1416
|
-
.map(
|
|
1446
|
+
.map(symbol => {
|
|
1417
1447
|
return getValueFromNode(parser, getValueDeclaration(symbol));
|
|
1418
1448
|
})
|
|
1419
1449
|
.filter(filterObjectProperties);
|
|
1420
|
-
|
|
1450
|
+
const callSignatures = type.getCallSignatures().map(s => {
|
|
1421
1451
|
return getValueFromSignatureNode(parser, s.getDeclaration());
|
|
1422
1452
|
});
|
|
1423
1453
|
if (callSignatures.length) {
|
|
@@ -1427,17 +1457,16 @@ function getObjectValueFromType(parser, type) {
|
|
|
1427
1457
|
}
|
|
1428
1458
|
return callSignatures[0];
|
|
1429
1459
|
}
|
|
1430
|
-
return { kind: 'object', properties
|
|
1460
|
+
return { kind: 'object', properties };
|
|
1431
1461
|
}
|
|
1432
1462
|
/**
|
|
1433
1463
|
* Get defaults from JSDoc tags if available and do some simple processing to extract useful type
|
|
1434
1464
|
* information. JSDoc tags are not type checked, so our processing is limited.
|
|
1435
1465
|
*/
|
|
1436
1466
|
export function getDefaultFromTags(tags) {
|
|
1437
|
-
for (
|
|
1438
|
-
var tag = tags_1[_i];
|
|
1467
|
+
for (const tag of tags) {
|
|
1439
1468
|
if (tag.name === 'default') {
|
|
1440
|
-
|
|
1469
|
+
const text = (tag.text || '').replace('{', '').replace('}', '');
|
|
1441
1470
|
if ([
|
|
1442
1471
|
'string',
|
|
1443
1472
|
'number',
|
|
@@ -1486,8 +1515,7 @@ export function createParserPlugin(fn) {
|
|
|
1486
1515
|
* new parser per file. Instead use the parser directly and call `parser.getExportedSymbols` for
|
|
1487
1516
|
* each file to share memory.
|
|
1488
1517
|
*/
|
|
1489
|
-
export function parse(program, fileName, plugins) {
|
|
1490
|
-
|
|
1491
|
-
var parser = new DocParser(program, plugins);
|
|
1518
|
+
export function parse(program, fileName, plugins = []) {
|
|
1519
|
+
const parser = new DocParser(program, plugins);
|
|
1492
1520
|
return parser.getExportedSymbols(fileName);
|
|
1493
1521
|
}
|