@babel/traverse 7.18.6 → 7.18.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of @babel/traverse might be problematic. Click here for more details.
- package/lib/path/conversion.js +8 -1
- package/lib/path/generated/validators.js +0 -5
- package/lib/path/index.js +5 -6
- package/lib/path/inference/index.js +19 -1
- package/lib/path/inference/inferer-reference.js +4 -28
- package/lib/path/inference/inferers.js +32 -45
- package/lib/path/inference/util.js +32 -0
- package/lib/path/introspection.js +9 -2
- package/lib/path/lib/virtual-types-validator.js +183 -0
- package/lib/path/lib/virtual-types.js +18 -206
- package/lib/types.js +1 -3
- package/lib/visitors.js +11 -7
- package/package.json +6 -6
- package/scripts/generators/validators.js +8 -28
- package/lib/path/generated/virtual-types.js +0 -3
- package/scripts/generators/virtual-types.js +0 -27
package/lib/path/conversion.js
CHANGED
@@ -119,6 +119,10 @@ function unwrapFunctionEnvironment() {
|
|
119
119
|
hoistFunctionEnvironment(this);
|
120
120
|
}
|
121
121
|
|
122
|
+
function setType(path, type) {
|
123
|
+
path.node.type = type;
|
124
|
+
}
|
125
|
+
|
122
126
|
function arrowFunctionToExpression({
|
123
127
|
allowInsertArrow = true,
|
124
128
|
specCompliant = false,
|
@@ -133,7 +137,7 @@ function arrowFunctionToExpression({
|
|
133
137
|
fnPath: fn
|
134
138
|
} = hoistFunctionEnvironment(this, noNewArrows, allowInsertArrow);
|
135
139
|
fn.ensureBlock();
|
136
|
-
fn
|
140
|
+
setType(fn, "FunctionExpression");
|
137
141
|
|
138
142
|
if (!noNewArrows) {
|
139
143
|
const checkBinding = thisBinding ? null : fn.scope.generateUidIdentifier("arrowCheckId");
|
@@ -147,7 +151,10 @@ function arrowFunctionToExpression({
|
|
147
151
|
|
148
152
|
fn.get("body").unshiftContainer("body", expressionStatement(callExpression(this.hub.addHelper("newArrowCheck"), [thisExpression(), checkBinding ? identifier(checkBinding.name) : identifier(thisBinding)])));
|
149
153
|
fn.replaceWith(callExpression(memberExpression((0, _helperFunctionName.default)(this, true) || fn.node, identifier("bind")), [checkBinding ? identifier(checkBinding.name) : thisExpression()]));
|
154
|
+
return fn.get("callee.object");
|
150
155
|
}
|
156
|
+
|
157
|
+
return fn;
|
151
158
|
}
|
152
159
|
|
153
160
|
const getSuperCallsVisitor = (0, _visitors.merge)([{
|
package/lib/path/index.js
CHANGED
@@ -43,6 +43,8 @@ var NodePath_family = require("./family");
|
|
43
43
|
|
44
44
|
var NodePath_comments = require("./comments");
|
45
45
|
|
46
|
+
var NodePath_virtual_types_validator = require("./lib/virtual-types-validator");
|
47
|
+
|
46
48
|
const {
|
47
49
|
validate
|
48
50
|
} = _t;
|
@@ -246,14 +248,11 @@ for (const type of t.TYPES) {
|
|
246
248
|
};
|
247
249
|
}
|
248
250
|
|
251
|
+
Object.assign(NodePath.prototype, NodePath_virtual_types_validator);
|
252
|
+
|
249
253
|
for (const type of Object.keys(virtualTypes)) {
|
250
254
|
if (type[0] === "_") continue;
|
251
|
-
if (t.TYPES.
|
252
|
-
const virtualType = virtualTypes[type];
|
253
|
-
|
254
|
-
NodePath.prototype[`is${type}`] = function (opts) {
|
255
|
-
return virtualType.checkPath(this, opts);
|
256
|
-
};
|
255
|
+
if (!t.TYPES.includes(type)) t.TYPES.push(type);
|
257
256
|
}
|
258
257
|
|
259
258
|
var _default = NodePath;
|
@@ -17,6 +17,7 @@ var _t = require("@babel/types");
|
|
17
17
|
const {
|
18
18
|
anyTypeAnnotation,
|
19
19
|
isAnyTypeAnnotation,
|
20
|
+
isArrayTypeAnnotation,
|
20
21
|
isBooleanTypeAnnotation,
|
21
22
|
isEmptyTypeAnnotation,
|
22
23
|
isFlowBaseAnnotation,
|
@@ -25,6 +26,10 @@ const {
|
|
25
26
|
isMixedTypeAnnotation,
|
26
27
|
isNumberTypeAnnotation,
|
27
28
|
isStringTypeAnnotation,
|
29
|
+
isTSArrayType,
|
30
|
+
isTSTypeAnnotation,
|
31
|
+
isTSTypeReference,
|
32
|
+
isTupleTypeAnnotation,
|
28
33
|
isTypeAnnotation,
|
29
34
|
isUnionTypeAnnotation,
|
30
35
|
isVoidTypeAnnotation,
|
@@ -40,7 +45,11 @@ function getTypeAnnotation() {
|
|
40
45
|
}
|
41
46
|
|
42
47
|
type = this._getTypeAnnotation() || anyTypeAnnotation();
|
43
|
-
|
48
|
+
|
49
|
+
if (isTypeAnnotation(type) || isTSTypeAnnotation(type)) {
|
50
|
+
type = type.typeAnnotation;
|
51
|
+
}
|
52
|
+
|
44
53
|
this.setData("typeAnnotation", type);
|
45
54
|
return type;
|
46
55
|
}
|
@@ -156,7 +165,16 @@ function baseTypeStrictlyMatches(rightArg) {
|
|
156
165
|
|
157
166
|
function isGenericType(genericName) {
|
158
167
|
const type = this.getTypeAnnotation();
|
168
|
+
|
169
|
+
if (genericName === "Array") {
|
170
|
+
if (isTSArrayType(type) || isArrayTypeAnnotation(type) || isTupleTypeAnnotation(type)) {
|
171
|
+
return true;
|
172
|
+
}
|
173
|
+
}
|
174
|
+
|
159
175
|
return isGenericTypeAnnotation(type) && isIdentifier(type.id, {
|
160
176
|
name: genericName
|
177
|
+
}) || isTSTypeReference(type) && isIdentifier(type.typeName, {
|
178
|
+
name: genericName
|
161
179
|
});
|
162
180
|
}
|
@@ -7,13 +7,11 @@ exports.default = _default;
|
|
7
7
|
|
8
8
|
var _t = require("@babel/types");
|
9
9
|
|
10
|
+
var _util = require("./util");
|
11
|
+
|
10
12
|
const {
|
11
13
|
BOOLEAN_NUMBER_BINARY_OPERATORS,
|
12
|
-
createFlowUnionType,
|
13
|
-
createTSUnionType,
|
14
14
|
createTypeAnnotationBasedOnTypeof,
|
15
|
-
createUnionTypeAnnotation,
|
16
|
-
isTSTypeAnnotation,
|
17
15
|
numberTypeAnnotation,
|
18
16
|
voidTypeAnnotation
|
19
17
|
} = _t;
|
@@ -61,15 +59,7 @@ function getTypeAnnotationBindingConstantViolations(binding, path, name) {
|
|
61
59
|
return;
|
62
60
|
}
|
63
61
|
|
64
|
-
|
65
|
-
return createTSUnionType(types);
|
66
|
-
}
|
67
|
-
|
68
|
-
if (createFlowUnionType) {
|
69
|
-
return createFlowUnionType(types);
|
70
|
-
}
|
71
|
-
|
72
|
-
return createUnionTypeAnnotation(types);
|
62
|
+
return (0, _util.createUnionType)(types);
|
73
63
|
}
|
74
64
|
|
75
65
|
function getConstantViolationsBefore(binding, path, functions) {
|
@@ -182,22 +172,8 @@ function getConditionalAnnotation(binding, path, name) {
|
|
182
172
|
}
|
183
173
|
|
184
174
|
if (types.length) {
|
185
|
-
if (isTSTypeAnnotation(types[0]) && createTSUnionType) {
|
186
|
-
return {
|
187
|
-
typeAnnotation: createTSUnionType(types),
|
188
|
-
ifStatement
|
189
|
-
};
|
190
|
-
}
|
191
|
-
|
192
|
-
if (createFlowUnionType) {
|
193
|
-
return {
|
194
|
-
typeAnnotation: createFlowUnionType(types),
|
195
|
-
ifStatement
|
196
|
-
};
|
197
|
-
}
|
198
|
-
|
199
175
|
return {
|
200
|
-
typeAnnotation:
|
176
|
+
typeAnnotation: (0, _util.createUnionType)(types),
|
201
177
|
ifStatement
|
202
178
|
};
|
203
179
|
}
|
@@ -26,6 +26,8 @@ exports.RegExpLiteral = RegExpLiteral;
|
|
26
26
|
exports.RestElement = RestElement;
|
27
27
|
exports.SequenceExpression = SequenceExpression;
|
28
28
|
exports.StringLiteral = StringLiteral;
|
29
|
+
exports.TSAsExpression = TSAsExpression;
|
30
|
+
exports.TSNonNullExpression = TSNonNullExpression;
|
29
31
|
exports.TaggedTemplateExpression = TaggedTemplateExpression;
|
30
32
|
exports.TemplateLiteral = TemplateLiteral;
|
31
33
|
exports.TypeCastExpression = TypeCastExpression;
|
@@ -37,6 +39,8 @@ var _t = require("@babel/types");
|
|
37
39
|
|
38
40
|
var _infererReference = require("./inferer-reference");
|
39
41
|
|
42
|
+
var _util = require("./util");
|
43
|
+
|
40
44
|
const {
|
41
45
|
BOOLEAN_BINARY_OPERATORS,
|
42
46
|
BOOLEAN_UNARY_OPERATORS,
|
@@ -47,37 +51,20 @@ const {
|
|
47
51
|
arrayTypeAnnotation,
|
48
52
|
booleanTypeAnnotation,
|
49
53
|
buildMatchMemberExpression,
|
50
|
-
createFlowUnionType,
|
51
|
-
createTSUnionType,
|
52
|
-
createUnionTypeAnnotation,
|
53
54
|
genericTypeAnnotation,
|
54
55
|
identifier,
|
55
|
-
isTSTypeAnnotation,
|
56
56
|
nullLiteralTypeAnnotation,
|
57
57
|
numberTypeAnnotation,
|
58
58
|
stringTypeAnnotation,
|
59
59
|
tupleTypeAnnotation,
|
60
60
|
unionTypeAnnotation,
|
61
|
-
voidTypeAnnotation
|
61
|
+
voidTypeAnnotation,
|
62
|
+
isIdentifier
|
62
63
|
} = _t;
|
63
64
|
|
64
65
|
function VariableDeclarator() {
|
65
|
-
|
66
|
-
|
67
|
-
const id = this.get("id");
|
68
|
-
if (!id.isIdentifier()) return;
|
69
|
-
const init = this.get("init");
|
70
|
-
let type = init.getTypeAnnotation();
|
71
|
-
|
72
|
-
if (((_type = type) == null ? void 0 : _type.type) === "AnyTypeAnnotation") {
|
73
|
-
if (init.isCallExpression() && init.get("callee").isIdentifier({
|
74
|
-
name: "Array"
|
75
|
-
}) && !init.scope.hasBinding("Array", true)) {
|
76
|
-
type = ArrayExpression();
|
77
|
-
}
|
78
|
-
}
|
79
|
-
|
80
|
-
return type;
|
66
|
+
if (!this.get("id").isIdentifier()) return;
|
67
|
+
return this.get("init").getTypeAnnotation();
|
81
68
|
}
|
82
69
|
|
83
70
|
function TypeCastExpression(node) {
|
@@ -86,6 +73,16 @@ function TypeCastExpression(node) {
|
|
86
73
|
|
87
74
|
TypeCastExpression.validParent = true;
|
88
75
|
|
76
|
+
function TSAsExpression(node) {
|
77
|
+
return node.typeAnnotation;
|
78
|
+
}
|
79
|
+
|
80
|
+
TSAsExpression.validParent = true;
|
81
|
+
|
82
|
+
function TSNonNullExpression() {
|
83
|
+
return this.get("expression").getTypeAnnotation();
|
84
|
+
}
|
85
|
+
|
89
86
|
function NewExpression(node) {
|
90
87
|
if (node.callee.type === "Identifier") {
|
91
88
|
return genericTypeAnnotation(node.callee);
|
@@ -133,30 +130,12 @@ function BinaryExpression(node) {
|
|
133
130
|
|
134
131
|
function LogicalExpression() {
|
135
132
|
const argumentTypes = [this.get("left").getTypeAnnotation(), this.get("right").getTypeAnnotation()];
|
136
|
-
|
137
|
-
if (isTSTypeAnnotation(argumentTypes[0]) && createTSUnionType) {
|
138
|
-
return createTSUnionType(argumentTypes);
|
139
|
-
}
|
140
|
-
|
141
|
-
if (createFlowUnionType) {
|
142
|
-
return createFlowUnionType(argumentTypes);
|
143
|
-
}
|
144
|
-
|
145
|
-
return createUnionTypeAnnotation(argumentTypes);
|
133
|
+
return (0, _util.createUnionType)(argumentTypes);
|
146
134
|
}
|
147
135
|
|
148
136
|
function ConditionalExpression() {
|
149
137
|
const argumentTypes = [this.get("consequent").getTypeAnnotation(), this.get("alternate").getTypeAnnotation()];
|
150
|
-
|
151
|
-
if (isTSTypeAnnotation(argumentTypes[0]) && createTSUnionType) {
|
152
|
-
return createTSUnionType(argumentTypes);
|
153
|
-
}
|
154
|
-
|
155
|
-
if (createFlowUnionType) {
|
156
|
-
return createFlowUnionType(argumentTypes);
|
157
|
-
}
|
158
|
-
|
159
|
-
return createUnionTypeAnnotation(argumentTypes);
|
138
|
+
return (0, _util.createUnionType)(argumentTypes);
|
160
139
|
}
|
161
140
|
|
162
141
|
function SequenceExpression() {
|
@@ -229,7 +208,9 @@ function CallExpression() {
|
|
229
208
|
|
230
209
|
if (isObjectKeys(callee)) {
|
231
210
|
return arrayTypeAnnotation(stringTypeAnnotation());
|
232
|
-
} else if (isArrayFrom(callee) || isObjectValues(callee)
|
211
|
+
} else if (isArrayFrom(callee) || isObjectValues(callee) || isIdentifier(callee, {
|
212
|
+
name: "Array"
|
213
|
+
})) {
|
233
214
|
return arrayTypeAnnotation(anyTypeAnnotation());
|
234
215
|
} else if (isObjectEntries(callee)) {
|
235
216
|
return arrayTypeAnnotation(tupleTypeAnnotation([stringTypeAnnotation(), anyTypeAnnotation()]));
|
@@ -246,14 +227,20 @@ function resolveCall(callee) {
|
|
246
227
|
callee = callee.resolve();
|
247
228
|
|
248
229
|
if (callee.isFunction()) {
|
249
|
-
|
250
|
-
|
230
|
+
const {
|
231
|
+
node
|
232
|
+
} = callee;
|
233
|
+
|
234
|
+
if (node.async) {
|
235
|
+
if (node.generator) {
|
251
236
|
return genericTypeAnnotation(identifier("AsyncIterator"));
|
252
237
|
} else {
|
253
238
|
return genericTypeAnnotation(identifier("Promise"));
|
254
239
|
}
|
255
240
|
} else {
|
256
|
-
if (
|
241
|
+
if (node.generator) {
|
242
|
+
return genericTypeAnnotation(identifier("Iterator"));
|
243
|
+
} else if (callee.node.returnType) {
|
257
244
|
return callee.node.returnType;
|
258
245
|
} else {}
|
259
246
|
}
|
@@ -0,0 +1,32 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
4
|
+
value: true
|
5
|
+
});
|
6
|
+
exports.createUnionType = createUnionType;
|
7
|
+
|
8
|
+
var _t = require("@babel/types");
|
9
|
+
|
10
|
+
const {
|
11
|
+
createFlowUnionType,
|
12
|
+
createTSUnionType,
|
13
|
+
createUnionTypeAnnotation,
|
14
|
+
isFlowType,
|
15
|
+
isTSType
|
16
|
+
} = _t;
|
17
|
+
|
18
|
+
function createUnionType(types) {
|
19
|
+
{
|
20
|
+
if (isFlowType(types[0])) {
|
21
|
+
if (createFlowUnionType) {
|
22
|
+
return createFlowUnionType(types);
|
23
|
+
}
|
24
|
+
|
25
|
+
return createUnionTypeAnnotation(types);
|
26
|
+
} else {
|
27
|
+
if (createTSUnionType) {
|
28
|
+
return createTSUnionType(types);
|
29
|
+
}
|
30
|
+
}
|
31
|
+
}
|
32
|
+
}
|
@@ -441,13 +441,20 @@ function isInStrictMode() {
|
|
441
441
|
sourceType: "module"
|
442
442
|
})) return true;
|
443
443
|
if (path.isClass()) return true;
|
444
|
-
if (!path.isProgram() && !path.isFunction()) return false;
|
445
444
|
|
446
445
|
if (path.isArrowFunctionExpression() && !path.get("body").isBlockStatement()) {
|
447
446
|
return false;
|
448
447
|
}
|
449
448
|
|
450
|
-
|
449
|
+
let body;
|
450
|
+
|
451
|
+
if (path.isFunction()) {
|
452
|
+
body = path.node.body;
|
453
|
+
} else if (path.isProgram()) {
|
454
|
+
body = path.node;
|
455
|
+
} else {
|
456
|
+
return false;
|
457
|
+
}
|
451
458
|
|
452
459
|
for (const directive of body.directives) {
|
453
460
|
if (directive.value.value === "use strict") {
|
@@ -0,0 +1,183 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
4
|
+
value: true
|
5
|
+
});
|
6
|
+
exports.isBindingIdentifier = isBindingIdentifier;
|
7
|
+
exports.isBlockScoped = isBlockScoped;
|
8
|
+
exports.isExistentialTypeParam = isExistentialTypeParam;
|
9
|
+
exports.isExpression = isExpression;
|
10
|
+
exports.isFlow = isFlow;
|
11
|
+
exports.isForAwaitStatement = isForAwaitStatement;
|
12
|
+
exports.isGenerated = isGenerated;
|
13
|
+
exports.isNumericLiteralTypeAnnotation = isNumericLiteralTypeAnnotation;
|
14
|
+
exports.isPure = isPure;
|
15
|
+
exports.isReferenced = isReferenced;
|
16
|
+
exports.isReferencedIdentifier = isReferencedIdentifier;
|
17
|
+
exports.isReferencedMemberExpression = isReferencedMemberExpression;
|
18
|
+
exports.isRestProperty = isRestProperty;
|
19
|
+
exports.isScope = isScope;
|
20
|
+
exports.isSpreadProperty = isSpreadProperty;
|
21
|
+
exports.isStatement = isStatement;
|
22
|
+
exports.isUser = isUser;
|
23
|
+
exports.isVar = isVar;
|
24
|
+
|
25
|
+
var _t = require("@babel/types");
|
26
|
+
|
27
|
+
const {
|
28
|
+
isBinding,
|
29
|
+
isBlockScoped: nodeIsBlockScoped,
|
30
|
+
isExportDeclaration,
|
31
|
+
isExpression: nodeIsExpression,
|
32
|
+
isFlow: nodeIsFlow,
|
33
|
+
isForStatement,
|
34
|
+
isForXStatement,
|
35
|
+
isIdentifier,
|
36
|
+
isImportDeclaration,
|
37
|
+
isImportSpecifier,
|
38
|
+
isJSXIdentifier,
|
39
|
+
isJSXMemberExpression,
|
40
|
+
isMemberExpression,
|
41
|
+
isRestElement: nodeIsRestElement,
|
42
|
+
isReferenced: nodeIsReferenced,
|
43
|
+
isScope: nodeIsScope,
|
44
|
+
isStatement: nodeIsStatement,
|
45
|
+
isVar: nodeIsVar,
|
46
|
+
isVariableDeclaration,
|
47
|
+
react
|
48
|
+
} = _t;
|
49
|
+
const {
|
50
|
+
isCompatTag
|
51
|
+
} = react;
|
52
|
+
|
53
|
+
function isReferencedIdentifier(opts) {
|
54
|
+
const {
|
55
|
+
node,
|
56
|
+
parent
|
57
|
+
} = this;
|
58
|
+
|
59
|
+
if (!isIdentifier(node, opts) && !isJSXMemberExpression(parent, opts)) {
|
60
|
+
if (isJSXIdentifier(node, opts)) {
|
61
|
+
if (isCompatTag(node.name)) return false;
|
62
|
+
} else {
|
63
|
+
return false;
|
64
|
+
}
|
65
|
+
}
|
66
|
+
|
67
|
+
return nodeIsReferenced(node, parent, this.parentPath.parent);
|
68
|
+
}
|
69
|
+
|
70
|
+
function isReferencedMemberExpression() {
|
71
|
+
const {
|
72
|
+
node,
|
73
|
+
parent
|
74
|
+
} = this;
|
75
|
+
return isMemberExpression(node) && nodeIsReferenced(node, parent);
|
76
|
+
}
|
77
|
+
|
78
|
+
function isBindingIdentifier() {
|
79
|
+
const {
|
80
|
+
node,
|
81
|
+
parent
|
82
|
+
} = this;
|
83
|
+
const grandparent = this.parentPath.parent;
|
84
|
+
return isIdentifier(node) && isBinding(node, parent, grandparent);
|
85
|
+
}
|
86
|
+
|
87
|
+
function isStatement() {
|
88
|
+
const {
|
89
|
+
node,
|
90
|
+
parent
|
91
|
+
} = this;
|
92
|
+
|
93
|
+
if (nodeIsStatement(node)) {
|
94
|
+
if (isVariableDeclaration(node)) {
|
95
|
+
if (isForXStatement(parent, {
|
96
|
+
left: node
|
97
|
+
})) return false;
|
98
|
+
if (isForStatement(parent, {
|
99
|
+
init: node
|
100
|
+
})) return false;
|
101
|
+
}
|
102
|
+
|
103
|
+
return true;
|
104
|
+
} else {
|
105
|
+
return false;
|
106
|
+
}
|
107
|
+
}
|
108
|
+
|
109
|
+
function isExpression() {
|
110
|
+
if (this.isIdentifier()) {
|
111
|
+
return this.isReferencedIdentifier();
|
112
|
+
} else {
|
113
|
+
return nodeIsExpression(this.node);
|
114
|
+
}
|
115
|
+
}
|
116
|
+
|
117
|
+
function isScope() {
|
118
|
+
return nodeIsScope(this.node, this.parent);
|
119
|
+
}
|
120
|
+
|
121
|
+
function isReferenced() {
|
122
|
+
return nodeIsReferenced(this.node, this.parent);
|
123
|
+
}
|
124
|
+
|
125
|
+
function isBlockScoped() {
|
126
|
+
return nodeIsBlockScoped(this.node);
|
127
|
+
}
|
128
|
+
|
129
|
+
function isVar() {
|
130
|
+
return nodeIsVar(this.node);
|
131
|
+
}
|
132
|
+
|
133
|
+
function isUser() {
|
134
|
+
return this.node && !!this.node.loc;
|
135
|
+
}
|
136
|
+
|
137
|
+
function isGenerated() {
|
138
|
+
return !this.isUser();
|
139
|
+
}
|
140
|
+
|
141
|
+
function isPure(constantsOnly) {
|
142
|
+
return this.scope.isPure(this.node, constantsOnly);
|
143
|
+
}
|
144
|
+
|
145
|
+
function isFlow() {
|
146
|
+
const {
|
147
|
+
node
|
148
|
+
} = this;
|
149
|
+
|
150
|
+
if (nodeIsFlow(node)) {
|
151
|
+
return true;
|
152
|
+
} else if (isImportDeclaration(node)) {
|
153
|
+
return node.importKind === "type" || node.importKind === "typeof";
|
154
|
+
} else if (isExportDeclaration(node)) {
|
155
|
+
return node.exportKind === "type";
|
156
|
+
} else if (isImportSpecifier(node)) {
|
157
|
+
return node.importKind === "type" || node.importKind === "typeof";
|
158
|
+
} else {
|
159
|
+
return false;
|
160
|
+
}
|
161
|
+
}
|
162
|
+
|
163
|
+
function isRestProperty() {
|
164
|
+
return nodeIsRestElement(this.node) && this.parentPath && this.parentPath.isObjectPattern();
|
165
|
+
}
|
166
|
+
|
167
|
+
function isSpreadProperty() {
|
168
|
+
return nodeIsRestElement(this.node) && this.parentPath && this.parentPath.isObjectExpression();
|
169
|
+
}
|
170
|
+
|
171
|
+
function isForAwaitStatement() {
|
172
|
+
return isForStatement(this.node, {
|
173
|
+
await: true
|
174
|
+
});
|
175
|
+
}
|
176
|
+
|
177
|
+
function isExistentialTypeParam() {
|
178
|
+
throw new Error("`path.isExistentialTypeParam` has been renamed to `path.isExistsTypeAnnotation()` in Babel 7.");
|
179
|
+
}
|
180
|
+
|
181
|
+
function isNumericLiteralTypeAnnotation() {
|
182
|
+
throw new Error("`path.isNumericLiteralTypeAnnotation()` has been renamed to `path.isNumberLiteralTypeAnnotation()` in Babel 7.");
|
183
|
+
}
|
@@ -4,227 +4,39 @@ Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
5
5
|
});
|
6
6
|
exports.Var = exports.User = exports.Statement = exports.SpreadProperty = exports.Scope = exports.RestProperty = exports.ReferencedMemberExpression = exports.ReferencedIdentifier = exports.Referenced = exports.Pure = exports.NumericLiteralTypeAnnotation = exports.Generated = exports.ForAwaitStatement = exports.Flow = exports.Expression = exports.ExistentialTypeParam = exports.BlockScoped = exports.BindingIdentifier = void 0;
|
7
|
-
|
8
|
-
var _t = require("@babel/types");
|
9
|
-
|
10
|
-
const {
|
11
|
-
isBinding,
|
12
|
-
isBlockScoped,
|
13
|
-
isExportDeclaration,
|
14
|
-
isExpression,
|
15
|
-
isFlow,
|
16
|
-
isForStatement,
|
17
|
-
isForXStatement,
|
18
|
-
isIdentifier,
|
19
|
-
isImportDeclaration,
|
20
|
-
isImportSpecifier,
|
21
|
-
isJSXIdentifier,
|
22
|
-
isJSXMemberExpression,
|
23
|
-
isMemberExpression,
|
24
|
-
isReferenced,
|
25
|
-
isScope,
|
26
|
-
isStatement,
|
27
|
-
isVar,
|
28
|
-
isVariableDeclaration,
|
29
|
-
react
|
30
|
-
} = _t;
|
31
|
-
const {
|
32
|
-
isCompatTag
|
33
|
-
} = react;
|
34
|
-
const ReferencedIdentifier = {
|
35
|
-
types: ["Identifier", "JSXIdentifier"],
|
36
|
-
|
37
|
-
checkPath(path, opts) {
|
38
|
-
const {
|
39
|
-
node,
|
40
|
-
parent
|
41
|
-
} = path;
|
42
|
-
|
43
|
-
if (!isIdentifier(node, opts) && !isJSXMemberExpression(parent, opts)) {
|
44
|
-
if (isJSXIdentifier(node, opts)) {
|
45
|
-
if (isCompatTag(node.name)) return false;
|
46
|
-
} else {
|
47
|
-
return false;
|
48
|
-
}
|
49
|
-
}
|
50
|
-
|
51
|
-
return isReferenced(node, parent, path.parentPath.parent);
|
52
|
-
}
|
53
|
-
|
54
|
-
};
|
7
|
+
const ReferencedIdentifier = ["Identifier", "JSXIdentifier"];
|
55
8
|
exports.ReferencedIdentifier = ReferencedIdentifier;
|
56
|
-
const ReferencedMemberExpression =
|
57
|
-
types: ["MemberExpression"],
|
58
|
-
|
59
|
-
checkPath({
|
60
|
-
node,
|
61
|
-
parent
|
62
|
-
}) {
|
63
|
-
return isMemberExpression(node) && isReferenced(node, parent);
|
64
|
-
}
|
65
|
-
|
66
|
-
};
|
9
|
+
const ReferencedMemberExpression = ["MemberExpression"];
|
67
10
|
exports.ReferencedMemberExpression = ReferencedMemberExpression;
|
68
|
-
const BindingIdentifier =
|
69
|
-
types: ["Identifier"],
|
70
|
-
|
71
|
-
checkPath(path) {
|
72
|
-
const {
|
73
|
-
node,
|
74
|
-
parent
|
75
|
-
} = path;
|
76
|
-
const grandparent = path.parentPath.parent;
|
77
|
-
return isIdentifier(node) && isBinding(node, parent, grandparent);
|
78
|
-
}
|
79
|
-
|
80
|
-
};
|
11
|
+
const BindingIdentifier = ["Identifier"];
|
81
12
|
exports.BindingIdentifier = BindingIdentifier;
|
82
|
-
const Statement =
|
83
|
-
types: ["Statement"],
|
84
|
-
|
85
|
-
checkPath({
|
86
|
-
node,
|
87
|
-
parent
|
88
|
-
}) {
|
89
|
-
if (isStatement(node)) {
|
90
|
-
if (isVariableDeclaration(node)) {
|
91
|
-
if (isForXStatement(parent, {
|
92
|
-
left: node
|
93
|
-
})) return false;
|
94
|
-
if (isForStatement(parent, {
|
95
|
-
init: node
|
96
|
-
})) return false;
|
97
|
-
}
|
98
|
-
|
99
|
-
return true;
|
100
|
-
} else {
|
101
|
-
return false;
|
102
|
-
}
|
103
|
-
}
|
104
|
-
|
105
|
-
};
|
13
|
+
const Statement = ["Statement"];
|
106
14
|
exports.Statement = Statement;
|
107
|
-
const Expression =
|
108
|
-
types: ["Expression"],
|
109
|
-
|
110
|
-
checkPath(path) {
|
111
|
-
if (path.isIdentifier()) {
|
112
|
-
return path.isReferencedIdentifier();
|
113
|
-
} else {
|
114
|
-
return isExpression(path.node);
|
115
|
-
}
|
116
|
-
}
|
117
|
-
|
118
|
-
};
|
15
|
+
const Expression = ["Expression"];
|
119
16
|
exports.Expression = Expression;
|
120
|
-
const Scope =
|
121
|
-
types: ["Scopable", "Pattern"],
|
122
|
-
|
123
|
-
checkPath(path) {
|
124
|
-
return isScope(path.node, path.parent);
|
125
|
-
}
|
126
|
-
|
127
|
-
};
|
17
|
+
const Scope = ["Scopable", "Pattern"];
|
128
18
|
exports.Scope = Scope;
|
129
|
-
const Referenced =
|
130
|
-
checkPath(path) {
|
131
|
-
return isReferenced(path.node, path.parent);
|
132
|
-
}
|
133
|
-
|
134
|
-
};
|
19
|
+
const Referenced = null;
|
135
20
|
exports.Referenced = Referenced;
|
136
|
-
const BlockScoped =
|
137
|
-
checkPath(path) {
|
138
|
-
return isBlockScoped(path.node);
|
139
|
-
}
|
140
|
-
|
141
|
-
};
|
21
|
+
const BlockScoped = null;
|
142
22
|
exports.BlockScoped = BlockScoped;
|
143
|
-
const Var =
|
144
|
-
types: ["VariableDeclaration"],
|
145
|
-
|
146
|
-
checkPath(path) {
|
147
|
-
return isVar(path.node);
|
148
|
-
}
|
149
|
-
|
150
|
-
};
|
23
|
+
const Var = ["VariableDeclaration"];
|
151
24
|
exports.Var = Var;
|
152
|
-
const User =
|
153
|
-
checkPath(path) {
|
154
|
-
return path.node && !!path.node.loc;
|
155
|
-
}
|
156
|
-
|
157
|
-
};
|
25
|
+
const User = null;
|
158
26
|
exports.User = User;
|
159
|
-
const Generated =
|
160
|
-
checkPath(path) {
|
161
|
-
return !path.isUser();
|
162
|
-
}
|
163
|
-
|
164
|
-
};
|
27
|
+
const Generated = null;
|
165
28
|
exports.Generated = Generated;
|
166
|
-
const Pure =
|
167
|
-
checkPath(path, constantsOnly) {
|
168
|
-
return path.scope.isPure(path.node, constantsOnly);
|
169
|
-
}
|
170
|
-
|
171
|
-
};
|
29
|
+
const Pure = null;
|
172
30
|
exports.Pure = Pure;
|
173
|
-
const Flow =
|
174
|
-
types: ["Flow", "ImportDeclaration", "ExportDeclaration", "ImportSpecifier"],
|
175
|
-
|
176
|
-
checkPath({
|
177
|
-
node
|
178
|
-
}) {
|
179
|
-
if (isFlow(node)) {
|
180
|
-
return true;
|
181
|
-
} else if (isImportDeclaration(node)) {
|
182
|
-
return node.importKind === "type" || node.importKind === "typeof";
|
183
|
-
} else if (isExportDeclaration(node)) {
|
184
|
-
return node.exportKind === "type";
|
185
|
-
} else if (isImportSpecifier(node)) {
|
186
|
-
return node.importKind === "type" || node.importKind === "typeof";
|
187
|
-
} else {
|
188
|
-
return false;
|
189
|
-
}
|
190
|
-
}
|
191
|
-
|
192
|
-
};
|
31
|
+
const Flow = ["Flow", "ImportDeclaration", "ExportDeclaration", "ImportSpecifier"];
|
193
32
|
exports.Flow = Flow;
|
194
|
-
const RestProperty =
|
195
|
-
types: ["RestElement"],
|
196
|
-
|
197
|
-
checkPath(path) {
|
198
|
-
return path.parentPath && path.parentPath.isObjectPattern();
|
199
|
-
}
|
200
|
-
|
201
|
-
};
|
33
|
+
const RestProperty = ["RestElement"];
|
202
34
|
exports.RestProperty = RestProperty;
|
203
|
-
const SpreadProperty =
|
204
|
-
types: ["RestElement"],
|
205
|
-
|
206
|
-
checkPath(path) {
|
207
|
-
return path.parentPath && path.parentPath.isObjectExpression();
|
208
|
-
}
|
209
|
-
|
210
|
-
};
|
35
|
+
const SpreadProperty = ["RestElement"];
|
211
36
|
exports.SpreadProperty = SpreadProperty;
|
212
|
-
const ExistentialTypeParam =
|
213
|
-
types: ["ExistsTypeAnnotation"]
|
214
|
-
};
|
37
|
+
const ExistentialTypeParam = ["ExistsTypeAnnotation"];
|
215
38
|
exports.ExistentialTypeParam = ExistentialTypeParam;
|
216
|
-
const NumericLiteralTypeAnnotation =
|
217
|
-
types: ["NumberLiteralTypeAnnotation"]
|
218
|
-
};
|
39
|
+
const NumericLiteralTypeAnnotation = ["NumberLiteralTypeAnnotation"];
|
219
40
|
exports.NumericLiteralTypeAnnotation = NumericLiteralTypeAnnotation;
|
220
|
-
const ForAwaitStatement =
|
221
|
-
types: ["ForOfStatement"],
|
222
|
-
|
223
|
-
checkPath({
|
224
|
-
node
|
225
|
-
}) {
|
226
|
-
return node.await === true;
|
227
|
-
}
|
228
|
-
|
229
|
-
};
|
41
|
+
const ForAwaitStatement = ["ForOfStatement"];
|
230
42
|
exports.ForAwaitStatement = ForAwaitStatement;
|
package/lib/types.js
CHANGED
package/lib/visitors.js
CHANGED
@@ -17,6 +17,10 @@ const {
|
|
17
17
|
TYPES
|
18
18
|
} = _t;
|
19
19
|
|
20
|
+
function isVirtualType(type) {
|
21
|
+
return type in virtualTypes;
|
22
|
+
}
|
23
|
+
|
20
24
|
function explode(visitor) {
|
21
25
|
if (visitor._exploded) return visitor;
|
22
26
|
visitor._exploded = true;
|
@@ -40,18 +44,18 @@ function explode(visitor) {
|
|
40
44
|
|
41
45
|
for (const nodeType of Object.keys(visitor)) {
|
42
46
|
if (shouldIgnoreKey(nodeType)) continue;
|
43
|
-
|
44
|
-
if (!wrapper) continue;
|
47
|
+
if (!isVirtualType(nodeType)) continue;
|
45
48
|
const fns = visitor[nodeType];
|
46
49
|
|
47
50
|
for (const type of Object.keys(fns)) {
|
48
|
-
fns[type] = wrapCheck(
|
51
|
+
fns[type] = wrapCheck(nodeType, fns[type]);
|
49
52
|
}
|
50
53
|
|
51
54
|
delete visitor[nodeType];
|
55
|
+
const types = virtualTypes[nodeType];
|
52
56
|
|
53
|
-
if (
|
54
|
-
for (const type of
|
57
|
+
if (types !== null) {
|
58
|
+
for (const type of types) {
|
55
59
|
if (visitor[type]) {
|
56
60
|
mergePair(visitor[type], fns);
|
57
61
|
} else {
|
@@ -212,9 +216,9 @@ function ensureCallbackArrays(obj) {
|
|
212
216
|
if (obj.exit && !Array.isArray(obj.exit)) obj.exit = [obj.exit];
|
213
217
|
}
|
214
218
|
|
215
|
-
function wrapCheck(
|
219
|
+
function wrapCheck(nodeType, fn) {
|
216
220
|
const newFn = function (path) {
|
217
|
-
if (
|
221
|
+
if (path[`is${nodeType}`]()) {
|
218
222
|
return fn.apply(this, arguments);
|
219
223
|
}
|
220
224
|
};
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@babel/traverse",
|
3
|
-
"version": "7.18.
|
3
|
+
"version": "7.18.10",
|
4
4
|
"description": "The Babel Traverse module maintains the overall tree state, and is responsible for replacing, removing, and adding nodes",
|
5
5
|
"author": "The Babel Team (https://babel.dev/team)",
|
6
6
|
"homepage": "https://babel.dev/docs/en/next/babel-traverse",
|
@@ -17,13 +17,13 @@
|
|
17
17
|
"main": "./lib/index.js",
|
18
18
|
"dependencies": {
|
19
19
|
"@babel/code-frame": "^7.18.6",
|
20
|
-
"@babel/generator": "^7.18.
|
21
|
-
"@babel/helper-environment-visitor": "^7.18.
|
22
|
-
"@babel/helper-function-name": "^7.18.
|
20
|
+
"@babel/generator": "^7.18.10",
|
21
|
+
"@babel/helper-environment-visitor": "^7.18.9",
|
22
|
+
"@babel/helper-function-name": "^7.18.9",
|
23
23
|
"@babel/helper-hoist-variables": "^7.18.6",
|
24
24
|
"@babel/helper-split-export-declaration": "^7.18.6",
|
25
|
-
"@babel/parser": "^7.18.
|
26
|
-
"@babel/types": "^7.18.
|
25
|
+
"@babel/parser": "^7.18.10",
|
26
|
+
"@babel/types": "^7.18.10",
|
27
27
|
"debug": "^4.1.0",
|
28
28
|
"globals": "^11.1.0"
|
29
29
|
},
|
@@ -1,46 +1,26 @@
|
|
1
1
|
import * as t from "@babel/types";
|
2
|
-
import * as virtualTypes from "../../lib/path/lib/virtual-types.js";
|
3
2
|
|
4
3
|
export default function generateValidators() {
|
5
4
|
let output = `/*
|
6
5
|
* This file is auto-generated! Do not modify it directly.
|
7
6
|
* To re-generate run 'make build'
|
8
7
|
*/
|
9
|
-
import * as t from "@babel/types";
|
10
|
-
import NodePath from "../index";
|
11
|
-
import type {
|
8
|
+
import type * as t from "@babel/types";
|
9
|
+
import type NodePath from "../index";
|
10
|
+
import type { VirtualTypeNodePathValidators } from "../lib/virtual-types-validator";
|
12
11
|
|
13
|
-
|
12
|
+
interface BaseNodePathValidators {
|
14
13
|
`;
|
15
14
|
|
16
15
|
for (const type of [...t.TYPES].sort()) {
|
17
|
-
output += `is${type}(opts?: object): this is NodePath<t.${type}>;`;
|
18
|
-
}
|
19
|
-
|
20
|
-
for (const type of Object.keys(virtualTypes)) {
|
21
|
-
// TODO: Remove this check once we stop compiling to CJS
|
22
|
-
if (type === "default" || type === "__esModule") continue;
|
23
|
-
|
24
|
-
const { types } = virtualTypes[type];
|
25
|
-
if (type[0] === "_") continue;
|
26
|
-
if (t.NODE_FIELDS[type] || t.FLIPPED_ALIAS_KEYS[type]) {
|
27
|
-
output += `is${type}(opts?: object): this is NodePath<t.${type}>;`;
|
28
|
-
} else if (types /* in VirtualTypeAliases */) {
|
29
|
-
output += `is${type}(opts?: object): this is NodePath<VirtualTypeAliases["${type}"]>;`;
|
30
|
-
} else if (type === "Pure") {
|
31
|
-
output += `isPure(constantsOnly?: boolean): boolean;`;
|
32
|
-
} else {
|
33
|
-
// if it don't have types, then VirtualTypeAliases[type] is t.Node
|
34
|
-
// which TS marked as always true
|
35
|
-
// eg. if (path.isBlockScope()) return;
|
36
|
-
// path resolved to `never` here
|
37
|
-
// so we have to return boolean instead of this is NodePath<t.Node> here
|
38
|
-
output += `is${type}(opts?: object): boolean;`;
|
39
|
-
}
|
16
|
+
output += `is${type}<T extends t.Node>(this: NodePath<T>, opts?: object): this is NodePath<T & t.${type}>;`;
|
40
17
|
}
|
41
18
|
|
42
19
|
output += `
|
43
20
|
}
|
21
|
+
|
22
|
+
export interface NodePathValidators
|
23
|
+
extends BaseNodePathValidators, VirtualTypeNodePathValidators {}
|
44
24
|
`;
|
45
25
|
|
46
26
|
return output;
|
@@ -1,27 +0,0 @@
|
|
1
|
-
import * as virtualTypes from "../../lib/path/lib/virtual-types.js";
|
2
|
-
|
3
|
-
export default function generateValidators() {
|
4
|
-
let output = `/*
|
5
|
-
* This file is auto-generated! Do not modify it directly.
|
6
|
-
* To re-generate run 'make build'
|
7
|
-
*/
|
8
|
-
import * as t from "@babel/types";
|
9
|
-
|
10
|
-
export interface VirtualTypeAliases {
|
11
|
-
`;
|
12
|
-
|
13
|
-
for (const type of Object.keys(virtualTypes)) {
|
14
|
-
// TODO: Remove this check once we stop compiling to CJS
|
15
|
-
if (type === "default" || type === "__esModule") continue;
|
16
|
-
|
17
|
-
output += ` ${type}: ${(virtualTypes[type].types || ["Node"])
|
18
|
-
.map(t => `t.${t}`)
|
19
|
-
.join(" | ")};`;
|
20
|
-
}
|
21
|
-
|
22
|
-
output += `
|
23
|
-
}
|
24
|
-
`;
|
25
|
-
|
26
|
-
return output;
|
27
|
-
}
|