forked from babel/babel
/
virtual-types.ts
177 lines (157 loc) 路 4.16 KB
/
virtual-types.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
import type NodePath from "../index";
import {
isBinding,
isBlockScoped,
isExportDeclaration,
isExpression,
isFlow,
isForStatement,
isForXStatement,
isIdentifier,
isImportDeclaration,
isImportSpecifier,
isJSXIdentifier,
isJSXMemberExpression,
isMemberExpression,
isReferenced,
isScope,
isStatement,
isVar,
isVariableDeclaration,
react,
} from "@babel/types";
import type * as t from "@babel/types";
const { isCompatTag } = react;
export const ReferencedIdentifier = {
types: ["Identifier", "JSXIdentifier"],
checkPath(path: NodePath, opts?: any): boolean {
const { node, parent } = path;
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 isReferenced(node, parent, path.parentPath.parent);
},
};
export const ReferencedMemberExpression = {
types: ["MemberExpression"],
checkPath({ node, parent }) {
return isMemberExpression(node) && isReferenced(node, parent);
},
};
export const BindingIdentifier = {
types: ["Identifier"],
checkPath(path: NodePath): boolean {
const { node, parent } = path;
const grandparent = path.parentPath.parent;
return isIdentifier(node) && isBinding(node, parent, grandparent);
},
};
export const Statement = {
types: ["Statement"],
checkPath({ node, parent }: NodePath): boolean {
if (isStatement(node)) {
if (isVariableDeclaration(node)) {
if (isForXStatement(parent, { left: node })) return false;
if (isForStatement(parent, { init: node })) return false;
}
return true;
} else {
return false;
}
},
};
export const Expression = {
types: ["Expression"],
checkPath(path: NodePath): boolean {
if (path.isIdentifier()) {
return path.isReferencedIdentifier();
} else {
return isExpression(path.node);
}
},
};
export const Scope = {
// When pattern is inside the function params, it is a scope
types: ["Scopable", "Pattern"],
checkPath(path) {
return isScope(path.node, path.parent);
},
};
export const Referenced = {
checkPath(path: NodePath): boolean {
return isReferenced(path.node, path.parent);
},
};
export const BlockScoped = {
checkPath(path: NodePath): boolean {
return isBlockScoped(path.node);
},
};
export const Var = {
types: ["VariableDeclaration"],
checkPath(path: NodePath): boolean {
return isVar(path.node);
},
};
export const User = {
checkPath(path: NodePath): boolean {
return path.node && !!path.node.loc;
},
};
export const Generated = {
checkPath(path: NodePath): boolean {
return !path.isUser();
},
};
export const Pure = {
checkPath(path: NodePath, constantsOnly?: boolean): boolean {
return path.scope.isPure(path.node, constantsOnly);
},
};
export const Flow = {
types: ["Flow", "ImportDeclaration", "ExportDeclaration", "ImportSpecifier"],
checkPath({ node }: NodePath): boolean {
if (isFlow(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 const RestProperty = {
types: ["RestElement"],
checkPath(path: NodePath): boolean {
return path.parentPath && path.parentPath.isObjectPattern();
},
};
export const SpreadProperty = {
types: ["RestElement"],
checkPath(path: NodePath): boolean {
return path.parentPath && path.parentPath.isObjectExpression();
},
};
export const ExistentialTypeParam = {
types: ["ExistsTypeAnnotation"],
};
export const NumericLiteralTypeAnnotation = {
types: ["NumberLiteralTypeAnnotation"],
};
export const ForAwaitStatement = {
types: ["ForOfStatement"],
checkPath({ node }: NodePath<t.ForOfStatement>): boolean {
return node.await === true;
},
};