@babel/traverse 7.14.8 → 7.16.0
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/context.js +6 -2
- package/lib/index.js +18 -12
- package/lib/path/ancestry.js +11 -7
- package/lib/path/comments.js +9 -4
- package/lib/path/context.js +26 -14
- package/lib/path/conversion.js +60 -35
- package/lib/path/evaluation.js +1 -1
- package/lib/path/family.js +68 -38
- package/lib/path/index.js +9 -3
- package/lib/path/inference/index.js +39 -21
- package/lib/path/inference/inferer-reference.js +28 -17
- package/lib/path/inference/inferers.js +89 -65
- package/lib/path/introspection.js +37 -25
- package/lib/path/lib/hoister.js +17 -7
- package/lib/path/lib/virtual-types.js +47 -23
- package/lib/path/modification.js +25 -14
- package/lib/path/removal.js +4 -4
- package/lib/path/replacement.js +50 -27
- package/lib/scope/index.js +101 -55
- package/lib/scope/lib/renamer.js +16 -8
- package/lib/types.js +0 -2
- package/lib/visitors.js +11 -5
- package/package.json +9 -9
- package/scripts/generators/validators.js +9 -0
package/lib/path/family.js
CHANGED
@@ -3,25 +3,32 @@
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
4
4
|
value: true
|
5
5
|
});
|
6
|
-
exports.getOpposite = getOpposite;
|
7
|
-
exports.getCompletionRecords = getCompletionRecords;
|
8
|
-
exports.getSibling = getSibling;
|
9
|
-
exports.getPrevSibling = getPrevSibling;
|
10
|
-
exports.getNextSibling = getNextSibling;
|
11
|
-
exports.getAllNextSiblings = getAllNextSiblings;
|
12
|
-
exports.getAllPrevSiblings = getAllPrevSiblings;
|
13
|
-
exports.get = get;
|
14
6
|
exports._getKey = _getKey;
|
15
7
|
exports._getPattern = _getPattern;
|
16
|
-
exports.
|
17
|
-
exports.
|
8
|
+
exports.get = get;
|
9
|
+
exports.getAllNextSiblings = getAllNextSiblings;
|
10
|
+
exports.getAllPrevSiblings = getAllPrevSiblings;
|
18
11
|
exports.getBindingIdentifierPaths = getBindingIdentifierPaths;
|
12
|
+
exports.getBindingIdentifiers = getBindingIdentifiers;
|
13
|
+
exports.getCompletionRecords = getCompletionRecords;
|
14
|
+
exports.getNextSibling = getNextSibling;
|
15
|
+
exports.getOpposite = getOpposite;
|
19
16
|
exports.getOuterBindingIdentifierPaths = getOuterBindingIdentifierPaths;
|
17
|
+
exports.getOuterBindingIdentifiers = getOuterBindingIdentifiers;
|
18
|
+
exports.getPrevSibling = getPrevSibling;
|
19
|
+
exports.getSibling = getSibling;
|
20
20
|
|
21
21
|
var _index = require("./index");
|
22
22
|
|
23
|
-
var
|
23
|
+
var _t = require("@babel/types");
|
24
24
|
|
25
|
+
const {
|
26
|
+
getBindingIdentifiers: _getBindingIdentifiers,
|
27
|
+
getOuterBindingIdentifiers: _getOuterBindingIdentifiers,
|
28
|
+
isDeclaration,
|
29
|
+
numericLiteral,
|
30
|
+
unaryExpression
|
31
|
+
} = _t;
|
25
32
|
const NORMAL_COMPLETION = 0;
|
26
33
|
const BREAK_COMPLETION = 1;
|
27
34
|
|
@@ -50,7 +57,10 @@ function getOpposite() {
|
|
50
57
|
}
|
51
58
|
|
52
59
|
function addCompletionRecords(path, records, context) {
|
53
|
-
if (path)
|
60
|
+
if (path) {
|
61
|
+
records.push(..._getCompletionRecords(path, context));
|
62
|
+
}
|
63
|
+
|
54
64
|
return records;
|
55
65
|
}
|
56
66
|
|
@@ -79,10 +89,10 @@ function completionRecordForSwitch(cases, records, context) {
|
|
79
89
|
lastNormalCompletions = normalCompletions;
|
80
90
|
}
|
81
91
|
|
82
|
-
records
|
92
|
+
records.push(...breakCompletions);
|
83
93
|
}
|
84
94
|
|
85
|
-
records
|
95
|
+
records.push(...lastNormalCompletions);
|
86
96
|
return records;
|
87
97
|
}
|
88
98
|
|
@@ -98,7 +108,7 @@ function replaceBreakStatementInBreakCompletion(completions, reachable) {
|
|
98
108
|
label: null
|
99
109
|
})) {
|
100
110
|
if (reachable) {
|
101
|
-
c.path.replaceWith(
|
111
|
+
c.path.replaceWith(unaryExpression("void", numericLiteral(0)));
|
102
112
|
} else {
|
103
113
|
c.path.remove();
|
104
114
|
}
|
@@ -107,7 +117,7 @@ function replaceBreakStatementInBreakCompletion(completions, reachable) {
|
|
107
117
|
}
|
108
118
|
|
109
119
|
function getStatementListCompletion(paths, context) {
|
110
|
-
|
120
|
+
const completions = [];
|
111
121
|
|
112
122
|
if (context.canHaveBreak) {
|
113
123
|
let lastNormalCompletions = [];
|
@@ -119,8 +129,8 @@ function getStatementListCompletion(paths, context) {
|
|
119
129
|
});
|
120
130
|
|
121
131
|
if (path.isBlockStatement() && (context.inCaseClause || context.shouldPopulateBreak)) {
|
122
|
-
|
123
|
-
|
132
|
+
newContext.shouldPopulateBreak = true;
|
133
|
+
} else {
|
124
134
|
newContext.shouldPopulateBreak = false;
|
125
135
|
}
|
126
136
|
|
@@ -131,16 +141,16 @@ function getStatementListCompletion(paths, context) {
|
|
131
141
|
label: null
|
132
142
|
}))) {
|
133
143
|
normalCompletionToBreak(lastNormalCompletions);
|
134
|
-
completions
|
144
|
+
completions.push(...lastNormalCompletions);
|
135
145
|
|
136
146
|
if (lastNormalCompletions.some(c => c.path.isDeclaration())) {
|
137
|
-
completions
|
147
|
+
completions.push(...statementCompletions);
|
138
148
|
replaceBreakStatementInBreakCompletion(statementCompletions, true);
|
139
149
|
}
|
140
150
|
|
141
151
|
replaceBreakStatementInBreakCompletion(statementCompletions, false);
|
142
152
|
} else {
|
143
|
-
completions
|
153
|
+
completions.push(...statementCompletions);
|
144
154
|
|
145
155
|
if (!context.shouldPopulateBreak) {
|
146
156
|
replaceBreakStatementInBreakCompletion(statementCompletions, true);
|
@@ -151,14 +161,32 @@ function getStatementListCompletion(paths, context) {
|
|
151
161
|
}
|
152
162
|
|
153
163
|
if (i === paths.length - 1) {
|
154
|
-
completions
|
164
|
+
completions.push(...statementCompletions);
|
155
165
|
} else {
|
156
|
-
|
157
|
-
|
166
|
+
lastNormalCompletions = [];
|
167
|
+
|
168
|
+
for (let i = 0; i < statementCompletions.length; i++) {
|
169
|
+
const c = statementCompletions[i];
|
170
|
+
|
171
|
+
if (c.type === BREAK_COMPLETION) {
|
172
|
+
completions.push(c);
|
173
|
+
}
|
174
|
+
|
175
|
+
if (c.type === NORMAL_COMPLETION) {
|
176
|
+
lastNormalCompletions.push(c);
|
177
|
+
}
|
178
|
+
}
|
158
179
|
}
|
159
180
|
}
|
160
181
|
} else if (paths.length) {
|
161
|
-
|
182
|
+
for (let i = paths.length - 1; i >= 0; i--) {
|
183
|
+
const pathCompletions = _getCompletionRecords(paths[i], context);
|
184
|
+
|
185
|
+
if (pathCompletions.length > 1 || pathCompletions.length === 1 && !pathCompletions[0].path.isVariableDeclaration()) {
|
186
|
+
completions.push(...pathCompletions);
|
187
|
+
break;
|
188
|
+
}
|
189
|
+
}
|
162
190
|
}
|
163
191
|
|
164
192
|
return completions;
|
@@ -171,24 +199,24 @@ function _getCompletionRecords(path, context) {
|
|
171
199
|
records = addCompletionRecords(path.get("consequent"), records, context);
|
172
200
|
records = addCompletionRecords(path.get("alternate"), records, context);
|
173
201
|
} else if (path.isDoExpression() || path.isFor() || path.isWhile() || path.isLabeledStatement()) {
|
174
|
-
|
202
|
+
return addCompletionRecords(path.get("body"), records, context);
|
175
203
|
} else if (path.isProgram() || path.isBlockStatement()) {
|
176
|
-
|
204
|
+
return getStatementListCompletion(path.get("body"), context);
|
177
205
|
} else if (path.isFunction()) {
|
178
206
|
return _getCompletionRecords(path.get("body"), context);
|
179
207
|
} else if (path.isTryStatement()) {
|
180
208
|
records = addCompletionRecords(path.get("block"), records, context);
|
181
209
|
records = addCompletionRecords(path.get("handler"), records, context);
|
182
210
|
} else if (path.isCatchClause()) {
|
183
|
-
|
211
|
+
return addCompletionRecords(path.get("body"), records, context);
|
184
212
|
} else if (path.isSwitchStatement()) {
|
185
|
-
|
213
|
+
return completionRecordForSwitch(path.get("cases"), records, context);
|
186
214
|
} else if (path.isSwitchCase()) {
|
187
|
-
|
215
|
+
return getStatementListCompletion(path.get("consequent"), {
|
188
216
|
canHaveBreak: true,
|
189
217
|
shouldPopulateBreak: false,
|
190
218
|
inCaseClause: true
|
191
|
-
})
|
219
|
+
});
|
192
220
|
} else if (path.isBreakStatement()) {
|
193
221
|
records.push(BreakCompletion(path));
|
194
222
|
} else {
|
@@ -306,23 +334,23 @@ function _getPattern(parts, context) {
|
|
306
334
|
}
|
307
335
|
|
308
336
|
function getBindingIdentifiers(duplicates) {
|
309
|
-
return
|
337
|
+
return _getBindingIdentifiers(this.node, duplicates);
|
310
338
|
}
|
311
339
|
|
312
340
|
function getOuterBindingIdentifiers(duplicates) {
|
313
|
-
return
|
341
|
+
return _getOuterBindingIdentifiers(this.node, duplicates);
|
314
342
|
}
|
315
343
|
|
316
344
|
function getBindingIdentifierPaths(duplicates = false, outerOnly = false) {
|
317
345
|
const path = this;
|
318
|
-
|
346
|
+
const search = [path];
|
319
347
|
const ids = Object.create(null);
|
320
348
|
|
321
349
|
while (search.length) {
|
322
350
|
const id = search.shift();
|
323
351
|
if (!id) continue;
|
324
352
|
if (!id.node) continue;
|
325
|
-
const keys =
|
353
|
+
const keys = _getBindingIdentifiers.keys[id.node.type];
|
326
354
|
|
327
355
|
if (id.isIdentifier()) {
|
328
356
|
if (duplicates) {
|
@@ -339,7 +367,7 @@ function getBindingIdentifierPaths(duplicates = false, outerOnly = false) {
|
|
339
367
|
if (id.isExportDeclaration()) {
|
340
368
|
const declaration = id.get("declaration");
|
341
369
|
|
342
|
-
if (
|
370
|
+
if (isDeclaration(declaration)) {
|
343
371
|
search.push(declaration);
|
344
372
|
}
|
345
373
|
|
@@ -362,8 +390,10 @@ function getBindingIdentifierPaths(duplicates = false, outerOnly = false) {
|
|
362
390
|
const key = keys[i];
|
363
391
|
const child = id.get(key);
|
364
392
|
|
365
|
-
if (Array.isArray(child)
|
366
|
-
search
|
393
|
+
if (Array.isArray(child)) {
|
394
|
+
search.push(...child);
|
395
|
+
} else if (child.node) {
|
396
|
+
search.push(child);
|
367
397
|
}
|
368
398
|
}
|
369
399
|
}
|
package/lib/path/index.js
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
4
4
|
value: true
|
5
5
|
});
|
6
|
-
exports.default = exports.
|
6
|
+
exports.default = exports.SHOULD_STOP = exports.SHOULD_SKIP = exports.REMOVED = void 0;
|
7
7
|
|
8
8
|
var virtualTypes = require("./lib/virtual-types");
|
9
9
|
|
@@ -13,7 +13,9 @@ var _index = require("../index");
|
|
13
13
|
|
14
14
|
var _scope = require("../scope");
|
15
15
|
|
16
|
-
var
|
16
|
+
var _t = require("@babel/types");
|
17
|
+
|
18
|
+
var t = _t;
|
17
19
|
|
18
20
|
var _cache = require("../cache");
|
19
21
|
|
@@ -41,6 +43,10 @@ var NodePath_family = require("./family");
|
|
41
43
|
|
42
44
|
var NodePath_comments = require("./comments");
|
43
45
|
|
46
|
+
const {
|
47
|
+
validate
|
48
|
+
} = _t;
|
49
|
+
|
44
50
|
const debug = _debug("babel");
|
45
51
|
|
46
52
|
const REMOVED = 1 << 0;
|
@@ -138,7 +144,7 @@ class NodePath {
|
|
138
144
|
}
|
139
145
|
|
140
146
|
set(key, node) {
|
141
|
-
|
147
|
+
validate(this.node, key, node);
|
142
148
|
this.node[key] = node;
|
143
149
|
}
|
144
150
|
|
@@ -3,21 +3,39 @@
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
4
4
|
value: true
|
5
5
|
});
|
6
|
-
exports.getTypeAnnotation = getTypeAnnotation;
|
7
6
|
exports._getTypeAnnotation = _getTypeAnnotation;
|
8
|
-
exports.isBaseType = isBaseType;
|
9
|
-
exports.couldBeBaseType = couldBeBaseType;
|
10
7
|
exports.baseTypeStrictlyMatches = baseTypeStrictlyMatches;
|
8
|
+
exports.couldBeBaseType = couldBeBaseType;
|
9
|
+
exports.getTypeAnnotation = getTypeAnnotation;
|
10
|
+
exports.isBaseType = isBaseType;
|
11
11
|
exports.isGenericType = isGenericType;
|
12
12
|
|
13
13
|
var inferers = require("./inferers");
|
14
14
|
|
15
|
-
var
|
15
|
+
var _t = require("@babel/types");
|
16
|
+
|
17
|
+
const {
|
18
|
+
anyTypeAnnotation,
|
19
|
+
isAnyTypeAnnotation,
|
20
|
+
isBooleanTypeAnnotation,
|
21
|
+
isEmptyTypeAnnotation,
|
22
|
+
isFlowBaseAnnotation,
|
23
|
+
isGenericTypeAnnotation,
|
24
|
+
isIdentifier,
|
25
|
+
isMixedTypeAnnotation,
|
26
|
+
isNumberTypeAnnotation,
|
27
|
+
isStringTypeAnnotation,
|
28
|
+
isTypeAnnotation,
|
29
|
+
isUnionTypeAnnotation,
|
30
|
+
isVoidTypeAnnotation,
|
31
|
+
stringTypeAnnotation,
|
32
|
+
voidTypeAnnotation
|
33
|
+
} = _t;
|
16
34
|
|
17
35
|
function getTypeAnnotation() {
|
18
36
|
if (this.typeAnnotation) return this.typeAnnotation;
|
19
|
-
let type = this._getTypeAnnotation() ||
|
20
|
-
if (
|
37
|
+
let type = this._getTypeAnnotation() || anyTypeAnnotation();
|
38
|
+
if (isTypeAnnotation(type)) type = type.typeAnnotation;
|
21
39
|
return this.typeAnnotation = type;
|
22
40
|
}
|
23
41
|
|
@@ -32,14 +50,14 @@ function _getTypeAnnotation() {
|
|
32
50
|
const declarParent = declar.parentPath;
|
33
51
|
|
34
52
|
if (declar.key === "left" && declarParent.isForInStatement()) {
|
35
|
-
return
|
53
|
+
return stringTypeAnnotation();
|
36
54
|
}
|
37
55
|
|
38
56
|
if (declar.key === "left" && declarParent.isForOfStatement()) {
|
39
|
-
return
|
57
|
+
return anyTypeAnnotation();
|
40
58
|
}
|
41
59
|
|
42
|
-
return
|
60
|
+
return voidTypeAnnotation();
|
43
61
|
} else {
|
44
62
|
return;
|
45
63
|
}
|
@@ -80,19 +98,19 @@ function isBaseType(baseName, soft) {
|
|
80
98
|
|
81
99
|
function _isBaseType(baseName, type, soft) {
|
82
100
|
if (baseName === "string") {
|
83
|
-
return
|
101
|
+
return isStringTypeAnnotation(type);
|
84
102
|
} else if (baseName === "number") {
|
85
|
-
return
|
103
|
+
return isNumberTypeAnnotation(type);
|
86
104
|
} else if (baseName === "boolean") {
|
87
|
-
return
|
105
|
+
return isBooleanTypeAnnotation(type);
|
88
106
|
} else if (baseName === "any") {
|
89
|
-
return
|
107
|
+
return isAnyTypeAnnotation(type);
|
90
108
|
} else if (baseName === "mixed") {
|
91
|
-
return
|
109
|
+
return isMixedTypeAnnotation(type);
|
92
110
|
} else if (baseName === "empty") {
|
93
|
-
return
|
111
|
+
return isEmptyTypeAnnotation(type);
|
94
112
|
} else if (baseName === "void") {
|
95
|
-
return
|
113
|
+
return isVoidTypeAnnotation(type);
|
96
114
|
} else {
|
97
115
|
if (soft) {
|
98
116
|
return false;
|
@@ -104,11 +122,11 @@ function _isBaseType(baseName, type, soft) {
|
|
104
122
|
|
105
123
|
function couldBeBaseType(name) {
|
106
124
|
const type = this.getTypeAnnotation();
|
107
|
-
if (
|
125
|
+
if (isAnyTypeAnnotation(type)) return true;
|
108
126
|
|
109
|
-
if (
|
127
|
+
if (isUnionTypeAnnotation(type)) {
|
110
128
|
for (const type2 of type.types) {
|
111
|
-
if (
|
129
|
+
if (isAnyTypeAnnotation(type2) || _isBaseType(name, type2, true)) {
|
112
130
|
return true;
|
113
131
|
}
|
114
132
|
}
|
@@ -123,7 +141,7 @@ function baseTypeStrictlyMatches(rightArg) {
|
|
123
141
|
const left = this.getTypeAnnotation();
|
124
142
|
const right = rightArg.getTypeAnnotation();
|
125
143
|
|
126
|
-
if (!
|
144
|
+
if (!isAnyTypeAnnotation(left) && isFlowBaseAnnotation(left)) {
|
127
145
|
return right.type === left.type;
|
128
146
|
}
|
129
147
|
|
@@ -132,7 +150,7 @@ function baseTypeStrictlyMatches(rightArg) {
|
|
132
150
|
|
133
151
|
function isGenericType(genericName) {
|
134
152
|
const type = this.getTypeAnnotation();
|
135
|
-
return
|
153
|
+
return isGenericTypeAnnotation(type) && isIdentifier(type.id, {
|
136
154
|
name: genericName
|
137
155
|
});
|
138
156
|
}
|
@@ -5,7 +5,18 @@ Object.defineProperty(exports, "__esModule", {
|
|
5
5
|
});
|
6
6
|
exports.default = _default;
|
7
7
|
|
8
|
-
var
|
8
|
+
var _t = require("@babel/types");
|
9
|
+
|
10
|
+
const {
|
11
|
+
BOOLEAN_NUMBER_BINARY_OPERATORS,
|
12
|
+
createFlowUnionType,
|
13
|
+
createTSUnionType,
|
14
|
+
createTypeAnnotationBasedOnTypeof,
|
15
|
+
createUnionTypeAnnotation,
|
16
|
+
isTSTypeAnnotation,
|
17
|
+
numberTypeAnnotation,
|
18
|
+
voidTypeAnnotation
|
19
|
+
} = _t;
|
9
20
|
|
10
21
|
function _default(node) {
|
11
22
|
if (!this.isReferenced()) return;
|
@@ -20,9 +31,9 @@ function _default(node) {
|
|
20
31
|
}
|
21
32
|
|
22
33
|
if (node.name === "undefined") {
|
23
|
-
return
|
34
|
+
return voidTypeAnnotation();
|
24
35
|
} else if (node.name === "NaN" || node.name === "Infinity") {
|
25
|
-
return
|
36
|
+
return numberTypeAnnotation();
|
26
37
|
} else if (node.name === "arguments") {}
|
27
38
|
}
|
28
39
|
|
@@ -39,7 +50,7 @@ function getTypeAnnotationBindingConstantViolations(binding, path, name) {
|
|
39
50
|
}
|
40
51
|
|
41
52
|
if (constantViolations.length) {
|
42
|
-
constantViolations
|
53
|
+
constantViolations.push(...functionConstantViolations);
|
43
54
|
|
44
55
|
for (const violation of constantViolations) {
|
45
56
|
types.push(violation.getTypeAnnotation());
|
@@ -50,15 +61,15 @@ function getTypeAnnotationBindingConstantViolations(binding, path, name) {
|
|
50
61
|
return;
|
51
62
|
}
|
52
63
|
|
53
|
-
if (
|
54
|
-
return
|
64
|
+
if (isTSTypeAnnotation(types[0]) && createTSUnionType) {
|
65
|
+
return createTSUnionType(types);
|
55
66
|
}
|
56
67
|
|
57
|
-
if (
|
58
|
-
return
|
68
|
+
if (createFlowUnionType) {
|
69
|
+
return createFlowUnionType(types);
|
59
70
|
}
|
60
71
|
|
61
|
-
return
|
72
|
+
return createUnionTypeAnnotation(types);
|
62
73
|
}
|
63
74
|
|
64
75
|
function getConstantViolationsBefore(binding, path, functions) {
|
@@ -95,8 +106,8 @@ function inferAnnotationFromBinaryExpression(name, path) {
|
|
95
106
|
return target.getTypeAnnotation();
|
96
107
|
}
|
97
108
|
|
98
|
-
if (
|
99
|
-
return
|
109
|
+
if (BOOLEAN_NUMBER_BINARY_OPERATORS.indexOf(operator) >= 0) {
|
110
|
+
return numberTypeAnnotation();
|
100
111
|
}
|
101
112
|
|
102
113
|
return;
|
@@ -126,7 +137,7 @@ function inferAnnotationFromBinaryExpression(name, path) {
|
|
126
137
|
if (!typePath.isLiteral()) return;
|
127
138
|
const typeValue = typePath.node.value;
|
128
139
|
if (typeof typeValue !== "string") return;
|
129
|
-
return
|
140
|
+
return createTypeAnnotationBasedOnTypeof(typeValue);
|
130
141
|
}
|
131
142
|
|
132
143
|
function getParentConditionalPath(binding, path, name) {
|
@@ -171,22 +182,22 @@ function getConditionalAnnotation(binding, path, name) {
|
|
171
182
|
}
|
172
183
|
|
173
184
|
if (types.length) {
|
174
|
-
if (
|
185
|
+
if (isTSTypeAnnotation(types[0]) && createTSUnionType) {
|
175
186
|
return {
|
176
|
-
typeAnnotation:
|
187
|
+
typeAnnotation: createTSUnionType(types),
|
177
188
|
ifStatement
|
178
189
|
};
|
179
190
|
}
|
180
191
|
|
181
|
-
if (
|
192
|
+
if (createFlowUnionType) {
|
182
193
|
return {
|
183
|
-
typeAnnotation:
|
194
|
+
typeAnnotation: createFlowUnionType(types),
|
184
195
|
ifStatement
|
185
196
|
};
|
186
197
|
}
|
187
198
|
|
188
199
|
return {
|
189
|
-
typeAnnotation:
|
200
|
+
typeAnnotation: createUnionTypeAnnotation(types),
|
190
201
|
ifStatement
|
191
202
|
};
|
192
203
|
}
|