-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathobject-definition.ts
More file actions
156 lines (147 loc) · 5.02 KB
/
object-definition.ts
File metadata and controls
156 lines (147 loc) · 5.02 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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import ts from 'typescript';
import type { TypeDefinition, UnionTypeDefinition, ValueDescription } from './interfaces';
import { extractDeclaration, isOptional, stringifyType } from '../shared/type-utils';
import { extractValueDescriptions } from './extract-value-descriptions';
function isArrayType(type: ts.Type) {
const symbol = type.getSymbol();
if (!symbol) {
return false;
}
return symbol.getName() === 'Array' || symbol.getName() === 'ReadonlyArray';
}
export function getObjectDefinition(
type: string,
rawType: ts.Type,
rawTypeNode: ts.TypeNode | undefined,
checker: ts.TypeChecker
): { type: string; inlineType?: TypeDefinition } {
const realType = rawType.getNonNullableType();
const realTypeName = stringifyType(realType, checker);
if (
realType.flags & ts.TypeFlags.String ||
realType.flags & ts.TypeFlags.Literal ||
realType.flags & ts.TypeFlags.Boolean ||
realType.flags & ts.TypeFlags.Number ||
isArrayType(realType) ||
realTypeName === 'HTMLElement' ||
realTypeName.split('.')[0] === 'Highcharts' ||
type === 'React.ReactNode'
) {
// do not expand built-in Javascript methods or primitive values
return { type };
}
if (realType.isUnionOrIntersection()) {
return getUnionTypeDefinition(realTypeName, realType, rawTypeNode, checker);
}
if (realType.getProperties().length > 0) {
const properties = realType
.getProperties()
.map(prop => {
const propNode = extractDeclaration(prop) as ts.PropertyDeclaration;
const propType = checker.getTypeAtLocation(propNode);
const typeString = stringifyType(propType, checker);
return {
name: prop.getName(),
optional: isOptional(propType),
...getObjectDefinition(typeString, propType, propNode.type, checker),
};
})
.sort((a, b) => a.name.localeCompare(b.name));
return {
type: type,
inlineType: {
name: realTypeName.length < 100 ? realTypeName : 'object',
type: 'object',
properties: properties,
},
};
}
if (realType.getCallSignatures().length > 0) {
if (realType.getCallSignatures().length > 1) {
throw new Error('Multiple call signatures are not supported');
}
const signature = realType.getCallSignatures()[0];
return {
type,
inlineType: {
name: realTypeName,
type: 'function',
returnType: stringifyType(signature.getReturnType(), checker),
parameters: signature.getParameters().map(param => {
const paramType = checker.getTypeAtLocation(extractDeclaration(param));
return {
name: param.getName(),
type: stringifyType(paramType, checker),
};
}),
},
};
}
return { type };
}
function isStringLiteralOrStringIntersection(subtype: ts.Type, checker: ts.TypeChecker): boolean {
// Check if it's a string literal
if (subtype.isStringLiteral() || subtype.flags & ts.TypeFlags.StringLiteral) {
return true;
}
if (subtype.isIntersection()) {
const stringified = stringifyType(subtype, checker);
if (stringified.startsWith('string &')) {
return true;
}
}
return false;
}
function getPrimitiveType(type: ts.UnionOrIntersectionType, checker: ts.TypeChecker) {
if (type.types.every(subtype => isStringLiteralOrStringIntersection(subtype, checker))) {
return 'string';
}
if (type.types.every(subtype => subtype.isNumberLiteral() || subtype.flags & ts.TypeFlags.NumberLiteral)) {
return 'number';
}
return undefined;
}
function getUnionTypeDefinition(
realTypeName: string,
realType: ts.UnionOrIntersectionType,
typeNode: ts.TypeNode | undefined,
checker: ts.TypeChecker
): { type: string; inlineType: UnionTypeDefinition } {
const valueDescriptions = extractValueDescriptions(realType, typeNode);
const primitiveType = getPrimitiveType(realType, checker);
const values = realType.types.map(subtype => {
if (primitiveType === 'string') {
if (subtype.isStringLiteral()) {
return (subtype as ts.LiteralType).value.toString();
}
if (subtype.isIntersection()) {
return 'string';
}
}
if (primitiveType === 'number' && subtype.isNumberLiteral()) {
return (subtype as ts.LiteralType).value.toString();
}
return stringifyType(subtype, checker);
});
return {
type: primitiveType ?? realTypeName,
inlineType: {
name: realTypeName,
type: 'union',
valueDescriptions: valueDescriptions.length > 0 ? zipValueDescriptions(values, valueDescriptions) : undefined,
values: values,
},
};
}
function zipValueDescriptions(values: Array<string>, descriptions: Array<ValueDescription | undefined>) {
const descriptionsMap: Record<string, ValueDescription> = {};
values.forEach((value, index) => {
const description = descriptions[index];
if (description) {
descriptionsMap[value] = description;
}
});
return descriptionsMap;
}