-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
/
index.ts
195 lines (175 loc) 路 4.8 KB
/
index.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import type NodePath from "../index";
import * as inferers from "./inferers";
import {
anyTypeAnnotation,
isAnyTypeAnnotation,
isArrayTypeAnnotation,
isBooleanTypeAnnotation,
isEmptyTypeAnnotation,
isFlowBaseAnnotation,
isGenericTypeAnnotation,
isIdentifier,
isMixedTypeAnnotation,
isNumberTypeAnnotation,
isStringTypeAnnotation,
isTSArrayType,
isTSTypeAnnotation,
isTSTypeReference,
isTupleTypeAnnotation,
isTypeAnnotation,
isUnionTypeAnnotation,
isVoidTypeAnnotation,
stringTypeAnnotation,
voidTypeAnnotation,
} from "@babel/types";
import type * as t from "@babel/types";
/**
* Infer the type of the current `NodePath`.
*/
export function getTypeAnnotation(this: NodePath): t.FlowType | t.TSType {
let type = this.getData("typeAnnotation");
if (type != null) {
return type;
}
type = this._getTypeAnnotation() || anyTypeAnnotation();
if (isTypeAnnotation(type) || isTSTypeAnnotation(type)) {
type = type.typeAnnotation;
}
this.setData("typeAnnotation", type);
return type;
}
// Used to avoid infinite recursion in cases like
// var b, c; if (0) { c = 1; b = c; } c = b;
// It also works with indirect recursion.
const typeAnnotationInferringNodes = new WeakSet();
/**
* todo: split up this method
*/
export function _getTypeAnnotation(this: NodePath): any {
const node = this.node;
if (!node) {
// handle initializerless variables, add in checks for loop initializers too
if (this.key === "init" && this.parentPath.isVariableDeclarator()) {
const declar = this.parentPath.parentPath;
const declarParent = declar.parentPath;
// for (let NODE in bar) {}
if (declar.key === "left" && declarParent.isForInStatement()) {
return stringTypeAnnotation();
}
// for (let NODE of bar) {}
if (declar.key === "left" && declarParent.isForOfStatement()) {
return anyTypeAnnotation();
}
return voidTypeAnnotation();
} else {
return;
}
}
// @ts-ignore
if (node.typeAnnotation) {
// @ts-ignore
return node.typeAnnotation;
}
if (typeAnnotationInferringNodes.has(node)) {
// Bail out from type inference to avoid infinite loops
return;
}
typeAnnotationInferringNodes.add(node);
try {
let inferer =
// @ts-expect-error inferers do not cover all AST types
inferers[node.type];
if (inferer) {
return inferer.call(this, node);
}
// @ts-expect-error inferers do not cover all AST types
inferer = inferers[this.parentPath.type];
if (inferer?.validParent) {
return this.parentPath.getTypeAnnotation();
}
} finally {
typeAnnotationInferringNodes.delete(node);
}
}
export function isBaseType(
this: NodePath,
baseName: string,
soft?: boolean,
): boolean {
return _isBaseType(baseName, this.getTypeAnnotation(), soft);
}
function _isBaseType(
baseName: string,
type?: t.FlowType | t.TSType,
soft?: boolean,
): boolean {
if (baseName === "string") {
return isStringTypeAnnotation(type);
} else if (baseName === "number") {
return isNumberTypeAnnotation(type);
} else if (baseName === "boolean") {
return isBooleanTypeAnnotation(type);
} else if (baseName === "any") {
return isAnyTypeAnnotation(type);
} else if (baseName === "mixed") {
return isMixedTypeAnnotation(type);
} else if (baseName === "empty") {
return isEmptyTypeAnnotation(type);
} else if (baseName === "void") {
return isVoidTypeAnnotation(type);
} else {
if (soft) {
return false;
} else {
throw new Error(`Unknown base type ${baseName}`);
}
}
}
export function couldBeBaseType(this: NodePath, name: string): boolean {
const type = this.getTypeAnnotation();
if (isAnyTypeAnnotation(type)) return true;
if (isUnionTypeAnnotation(type)) {
for (const type2 of type.types) {
if (isAnyTypeAnnotation(type2) || _isBaseType(name, type2, true)) {
return true;
}
}
return false;
} else {
return _isBaseType(name, type, true);
}
}
export function baseTypeStrictlyMatches(
this: NodePath,
rightArg: NodePath,
): boolean {
const left = this.getTypeAnnotation();
const right = rightArg.getTypeAnnotation();
if (!isAnyTypeAnnotation(left) && isFlowBaseAnnotation(left)) {
return right.type === left.type;
}
return false;
}
export function isGenericType(this: NodePath, genericName: string): boolean {
const type = this.getTypeAnnotation();
if (genericName === "Array") {
// T[]
if (
isTSArrayType(type) ||
isArrayTypeAnnotation(type) ||
isTupleTypeAnnotation(type)
) {
return true;
}
}
return (
(isGenericTypeAnnotation(type) &&
isIdentifier(type.id, {
name: genericName,
})) ||
(isTSTypeReference(type) &&
isIdentifier(type.typeName, {
name: genericName,
}))
);
}