forked from robturtle/ts-transformer-interface
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathruntime-schema.ts
112 lines (107 loc) · 3.42 KB
/
runtime-schema.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
import * as ts from 'typescript';
import { runtime } from './index.d';
export function buildInterface(
name: string,
type: ts.Type,
typeChecker: ts.TypeChecker,
): runtime.Schema {
const symbols = typeChecker.getPropertiesOfType(type);
return {
name: name,
props: symbols.map(s => buildInterfaceProperty(s, typeChecker)),
};
}
function buildInterfaceProperty(symbol: ts.Symbol, typeChecker: ts.TypeChecker): runtime.Property {
return {
name: symbol.getName(),
optional: propertyOptional(symbol),
type: propertyType(symbol, typeChecker),
};
}
function propertyOptional(symbol: ts.Symbol): boolean {
return !symbol.declarations?.some(d => (d as ts.PropertySignature).questionToken === undefined);
}
function propertyType(symbol: ts.Symbol, typeChecker: ts.TypeChecker): runtime.Type {
const declarations = symbol.declarations as ts.Declaration[];
if (declarations?.length === 0) {
return null;
}
const declaration = declarations[0];
if (!declaration) {
return null;
}
const parent = (declaration as any).parent;
if (!parent) {
return null;
}
const typeParameters = parent.typeParameters;
const propertySignature = (declaration as any).type;
if (typeParameters && typeParameters.length > 0) {
const typeParam = typeParameters[0];
return {
genericParameterName: typeParam.getText(),
genericParameterType: getTypeFromSignature(propertySignature, typeChecker),
};
} else {
return getTypeFromSignature(propertySignature, typeChecker);
}
}
function getTypeFromSignature(
propertySignature: ts.PropertySignature,
typeChecker: ts.TypeChecker,
): runtime.Type {
const kind = propertySignature.kind as ts.SyntaxKind;
switch (kind) {
case ts.SyntaxKind.StringKeyword:
return 'string';
case ts.SyntaxKind.NumberKeyword:
return 'number';
case ts.SyntaxKind.BooleanKeyword:
return 'boolean';
case ts.SyntaxKind.FunctionKeyword:
return 'function';
case ts.SyntaxKind.ObjectKeyword:
return 'object';
case ts.SyntaxKind.NullKeyword:
return 'null';
case ts.SyntaxKind.AnyKeyword:
return 'any';
case ts.SyntaxKind.UnknownKeyword:
return 'unknown';
case ts.SyntaxKind.TypeReference:
const typeArgs: ts.Node[] = (propertySignature as any).typeArguments;
if (typeArgs && typeArgs.length > 0) {
const typeName = (propertySignature as any).typeName;
const typeArg = typeArgs[0] as ts.PropertySignature;
return {
selfType: typeName.escapedText,
typeArgumentType: getTypeFromSignature(typeArg, typeChecker),
};
} else {
return {
referenceName: propertySignature.getText(),
};
}
case ts.SyntaxKind.ArrayType:
return {
arrayElementType: getTypeFromSignature(
(<ts.ArrayTypeNode>(propertySignature as any)).elementType as any,
typeChecker,
),
};
case ts.SyntaxKind.TypeLiteral:
const members: Map<string, ts.Symbol> = (propertySignature as any).symbol.members;
return {
props: Array.from(members.values()).map(m =>
buildInterfaceProperty(m as ts.Symbol, typeChecker),
),
};
case ts.SyntaxKind.UnionType:
const union = ((propertySignature as any) as ts.UnionTypeNode).types.map(t => {
return getTypeFromSignature(t as any, typeChecker);
});
return { union };
default:
return null;
}
}