-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.ts
143 lines (129 loc) · 4.74 KB
/
util.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
import ts, { isHeritageClause, isTypeNode, TypeChecker } from "typescript";
import { Modifier, Param, Type, Member } from "./types";
const toStringModifier = (m: ts.ModifierLike): Modifier => {
switch (m.kind) {
case ts.SyntaxKind.StaticKeyword: return "static";
case ts.SyntaxKind.PublicKeyword: return "public";
case ts.SyntaxKind.PrivateKeyword: return "private";
case ts.SyntaxKind.ProtectedKeyword: return "protected";
case ts.SyntaxKind.ReadonlyKeyword: return "readonly";
case ts.SyntaxKind.ExportKeyword: return "export";
case ts.SyntaxKind.DefaultKeyword: return "default";
case ts.SyntaxKind.ConstKeyword: return "const";
case ts.SyntaxKind.AsyncKeyword: return "async";
case ts.SyntaxKind.DeclareKeyword: return "declare";
default:
throw new Error(`I don't know about ${ts.SyntaxKind[m.kind]}`);
}
}
const getNodeType = (node: ts.TypeNode) => node.getText();
export const modifiers = (n: ts.Node) =>
(ts.canHaveModifiers(n) && (ts.getModifiers(n)?.map(toStringModifier)) || []);
export const params = (
n: ts.FunctionDeclaration |
ts.ConstructorDeclaration |
ts.MethodDeclaration |
ts.CallSignatureDeclaration |
ts.MethodSignature): Param[] =>
n.parameters
.filter(({ name }) => ts.isIdentifier(name))
.map(({ name, type, questionToken, ...v }) => (
{
name: name.getText(),
type: describeType(type!),
question: !!questionToken,
modifiers: modifiers(v)
}));
const describeType = (type: ts.TypeNode): Type => {
if (ts.isFunctionTypeNode(type) || ts.isConstructorTypeNode(type))
return {
name: type.name?.getText(),
parameters: type.parameters.map(
t => ({
name: t.name.getText(),
question: !!t.questionToken,
modifiers: modifiers(t),
type: describeType(t.type!)
})),
type: describeType(type.type),
kind: "callable"
};
if (ts.isUnionTypeNode(type) || ts.isIntersectionTypeNode(type))
return {
members: type.types.map(describeType),
kind: (ts.isUnionTypeNode(type) ?
"union" : "intersection")
};
if (ts.isArrayTypeNode(type))
return {
members: describeType(type.elementType),
kind: "array"
};
if (ts.isTypeLiteralNode(type))
return {
members: type.members.map(describeMember),
kind: "literal"
};
return getNodeType(type);
}
function isNodeArray<T extends ts.Node>(node?: any): node is ts.NodeArray<T> {
return (node as ts.NodeArray<T>).hasTrailingComma !== undefined;
}
export function describe<T extends ts.HeritageClause>(node: ts.NodeArray<T>, tc: TypeChecker): string[][];
export function describe<T extends ts.HeritageClause>(node: T, tc: TypeChecker): string[];
export function describe<T extends ts.TypeNode>(node: ts.NodeArray<T>, tc?: TypeChecker): Type[];
export function describe<T extends ts.TypeNode>(node: T, tc?: TypeChecker): Type;
export function describe<T extends ts.Node>(node: ts.NodeArray<T>, tc?: TypeChecker): Member[];
export function describe<T extends ts.Node>(node: T, tc?: TypeChecker): Member;
export function describe(node: any, tc?: TypeChecker): unknown {
if (isHeritageClause(node))
return describeHeritageClause(node, tc!);
if (isTypeNode(node))
return describeType(node);
if (isNodeArray(node))
return node.map(x => describe(x, tc));
return describeMember(node);
}
const describeMember = (node: ts.Node): Member => {
if (ts.isPropertyDeclaration(node)
|| ts.isPropertySignature(node)
|| ts.isConstructorDeclaration(node)
|| ts.isMethodDeclaration(node)
|| ts.isCallSignatureDeclaration(node)
|| ts.isIndexSignatureDeclaration(node)
|| ts.isMethodSignature(node)
|| ts.isConstructorDeclaration(node)) {
const m = {
type: node.type && describeType(node.type!),
question: !!node.questionToken,
modifiers: modifiers(node)
};
if (ts.isIndexSignatureDeclaration(node))
return { ...m, member: "index-sig" };
if (ts.isPropertyDeclaration(node) || ts.isPropertySignature(node))
return {
...m,
name: node.name.getText(),
member: "property"
};
return {
...m,
name: node.name?.getText(),
params: params(node),
member: (ts.isCallSignatureDeclaration(node) ?
"call-sig" :
(ts.isConstructorDeclaration(node) ?
"constructor" :
(ts.isMethodDeclaration(node) ?
"method" : "generic")))
};
}
throw new Error(`Don't know how to handle the given member ${ts.SyntaxKind[node.kind]}`);
}
const describeHeritageClause = (c: ts.HeritageClause, tc: ts.TypeChecker) => (
c.types
.map(
e => {
const t = tc.getTypeFromTypeNode(e);
return tc.getFullyQualifiedName(t.symbol).split('.');
}));