/
virtual-types-validator.ts
214 lines (193 loc) 路 5.99 KB
/
virtual-types-validator.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import type NodePath from "../index";
import {
isBinding,
isBlockScoped as nodeIsBlockScoped,
isExportDeclaration,
isExpression as nodeIsExpression,
isFlow as nodeIsFlow,
isForStatement,
isForXStatement,
isIdentifier,
isImportDeclaration,
isImportSpecifier,
isJSXIdentifier,
isJSXMemberExpression,
isMemberExpression,
isRestElement as nodeIsRestElement,
isReferenced as nodeIsReferenced,
isScope as nodeIsScope,
isStatement as nodeIsStatement,
isVar as nodeIsVar,
isVariableDeclaration,
react,
isForOfStatement,
} from "@babel/types";
import type * as t from "@babel/types";
const { isCompatTag } = react;
import type { VirtualTypeAliases } from "./virtual-types";
export interface VirtualTypeNodePathValidators {
isBindingIdentifier<T extends t.Node>(
this: NodePath<T>,
opts?: object,
): this is NodePath<T & VirtualTypeAliases["BindingIdentifier"]>;
isBlockScoped(opts?: object): boolean;
/**
* @deprecated
*/
isExistentialTypeParam<T extends t.Node>(
this: NodePath<T>,
opts?: object,
): this is NodePath<T & VirtualTypeAliases["ExistentialTypeParam"]>;
isExpression<T extends t.Node>(
this: NodePath<T>,
opts?: object,
): this is NodePath<T & t.Expression>;
isFlow<T extends t.Node>(
this: NodePath<T>,
opts?: object,
): this is NodePath<T & t.Flow>;
isForAwaitStatement<T extends t.Node>(
this: NodePath<T>,
opts?: object,
): this is NodePath<T & VirtualTypeAliases["ForAwaitStatement"]>;
isGenerated(opts?: object): boolean;
/**
* @deprecated
*/
isNumericLiteralTypeAnnotation(opts?: object): void;
isPure(opts?: object): boolean;
isReferenced(opts?: object): boolean;
isReferencedIdentifier<T extends t.Node>(
this: NodePath<T>,
opts?: object,
): this is NodePath<T & VirtualTypeAliases["ReferencedIdentifier"]>;
isReferencedMemberExpression<T extends t.Node>(
this: NodePath<T>,
opts?: object,
): this is NodePath<T & VirtualTypeAliases["ReferencedMemberExpression"]>;
isRestProperty<T extends t.Node>(
this: NodePath<T>,
opts?: object,
): this is NodePath<T & t.RestProperty>;
isScope<T extends t.Node>(
this: NodePath<T>,
opts?: object,
): this is NodePath<T & VirtualTypeAliases["Scope"]>;
isSpreadProperty<T extends t.Node>(
this: NodePath<T>,
opts?: object,
): this is NodePath<T & t.SpreadProperty>;
isStatement<T extends t.Node>(
this: NodePath<T>,
opts?: object,
): this is NodePath<T & t.Statement>;
isUser(opts?: object): boolean;
isVar<T extends t.Node>(
this: NodePath<T>,
opts?: object,
): this is NodePath<T & VirtualTypeAliases["Var"]>;
}
export function isReferencedIdentifier(this: NodePath, opts?: any): boolean {
const { node, parent } = this;
if (!isIdentifier(node, opts) && !isJSXMemberExpression(parent, opts)) {
if (isJSXIdentifier(node, opts)) {
if (isCompatTag(node.name)) return false;
} else {
// not a JSXIdentifier or an Identifier
return false;
}
}
// check if node is referenced
return nodeIsReferenced(node, parent, this.parentPath.parent);
}
export function isReferencedMemberExpression(this: NodePath): boolean {
const { node, parent } = this;
return isMemberExpression(node) && nodeIsReferenced(node, parent);
}
export function isBindingIdentifier(this: NodePath): boolean {
const { node, parent } = this;
const grandparent = this.parentPath.parent;
return isIdentifier(node) && isBinding(node, parent, grandparent);
}
export function isStatement(this: NodePath): boolean {
const { node, parent } = this;
if (nodeIsStatement(node)) {
if (isVariableDeclaration(node)) {
if (isForXStatement(parent, { left: node })) return false;
if (isForStatement(parent, { init: node })) return false;
}
return true;
} else {
return false;
}
}
export function isExpression(this: NodePath): boolean {
if (this.isIdentifier()) {
return this.isReferencedIdentifier();
} else {
return nodeIsExpression(this.node);
}
}
export function isScope(this: NodePath): boolean {
return nodeIsScope(this.node, this.parent);
}
export function isReferenced(this: NodePath): boolean {
return nodeIsReferenced(this.node, this.parent);
}
export function isBlockScoped(this: NodePath): boolean {
return nodeIsBlockScoped(this.node);
}
export function isVar(this: NodePath): boolean {
return nodeIsVar(this.node);
}
export function isUser(this: NodePath): boolean {
return this.node && !!this.node.loc;
}
export function isGenerated(this: NodePath): boolean {
return !this.isUser();
}
export function isPure(this: NodePath, constantsOnly?: boolean): boolean {
return this.scope.isPure(this.node, constantsOnly);
}
export function isFlow(this: NodePath): boolean {
const { node } = this;
if (nodeIsFlow(node)) {
return true;
} else if (isImportDeclaration(node)) {
return node.importKind === "type" || node.importKind === "typeof";
} else if (isExportDeclaration(node)) {
return node.exportKind === "type";
} else if (isImportSpecifier(node)) {
return node.importKind === "type" || node.importKind === "typeof";
} else {
return false;
}
}
// TODO: 7.0 Backwards Compat
export function isRestProperty(this: NodePath): boolean {
return (
nodeIsRestElement(this.node) &&
this.parentPath &&
this.parentPath.isObjectPattern()
);
}
export function isSpreadProperty(this: NodePath): boolean {
return (
nodeIsRestElement(this.node) &&
this.parentPath &&
this.parentPath.isObjectExpression()
);
}
export function isForAwaitStatement(this: NodePath): boolean {
return isForOfStatement(this.node, { await: true });
}
export function isExistentialTypeParam(this: NodePath): void {
throw new Error(
"`path.isExistentialTypeParam` has been renamed to `path.isExistsTypeAnnotation()` in Babel 7.",
);
}
export function isNumericLiteralTypeAnnotation(this: NodePath): void {
throw new Error(
"`path.isNumericLiteralTypeAnnotation()` has been renamed to `path.isNumberLiteralTypeAnnotation()` in Babel 7.",
);
}