@babel/traverse 7.14.9 → 7.16.3
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 +98 -46
- package/lib/path/evaluation.js +1 -1
- package/lib/path/family.js +61 -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/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,10 +161,21 @@ 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) {
|
@@ -162,7 +183,7 @@ function getStatementListCompletion(paths, context) {
|
|
162
183
|
const pathCompletions = _getCompletionRecords(paths[i], context);
|
163
184
|
|
164
185
|
if (pathCompletions.length > 1 || pathCompletions.length === 1 && !pathCompletions[0].path.isVariableDeclaration()) {
|
165
|
-
completions
|
186
|
+
completions.push(...pathCompletions);
|
166
187
|
break;
|
167
188
|
}
|
168
189
|
}
|
@@ -178,24 +199,24 @@ function _getCompletionRecords(path, context) {
|
|
178
199
|
records = addCompletionRecords(path.get("consequent"), records, context);
|
179
200
|
records = addCompletionRecords(path.get("alternate"), records, context);
|
180
201
|
} else if (path.isDoExpression() || path.isFor() || path.isWhile() || path.isLabeledStatement()) {
|
181
|
-
|
202
|
+
return addCompletionRecords(path.get("body"), records, context);
|
182
203
|
} else if (path.isProgram() || path.isBlockStatement()) {
|
183
|
-
|
204
|
+
return getStatementListCompletion(path.get("body"), context);
|
184
205
|
} else if (path.isFunction()) {
|
185
206
|
return _getCompletionRecords(path.get("body"), context);
|
186
207
|
} else if (path.isTryStatement()) {
|
187
208
|
records = addCompletionRecords(path.get("block"), records, context);
|
188
209
|
records = addCompletionRecords(path.get("handler"), records, context);
|
189
210
|
} else if (path.isCatchClause()) {
|
190
|
-
|
211
|
+
return addCompletionRecords(path.get("body"), records, context);
|
191
212
|
} else if (path.isSwitchStatement()) {
|
192
|
-
|
213
|
+
return completionRecordForSwitch(path.get("cases"), records, context);
|
193
214
|
} else if (path.isSwitchCase()) {
|
194
|
-
|
215
|
+
return getStatementListCompletion(path.get("consequent"), {
|
195
216
|
canHaveBreak: true,
|
196
217
|
shouldPopulateBreak: false,
|
197
218
|
inCaseClause: true
|
198
|
-
})
|
219
|
+
});
|
199
220
|
} else if (path.isBreakStatement()) {
|
200
221
|
records.push(BreakCompletion(path));
|
201
222
|
} else {
|
@@ -313,23 +334,23 @@ function _getPattern(parts, context) {
|
|
313
334
|
}
|
314
335
|
|
315
336
|
function getBindingIdentifiers(duplicates) {
|
316
|
-
return
|
337
|
+
return _getBindingIdentifiers(this.node, duplicates);
|
317
338
|
}
|
318
339
|
|
319
340
|
function getOuterBindingIdentifiers(duplicates) {
|
320
|
-
return
|
341
|
+
return _getOuterBindingIdentifiers(this.node, duplicates);
|
321
342
|
}
|
322
343
|
|
323
344
|
function getBindingIdentifierPaths(duplicates = false, outerOnly = false) {
|
324
345
|
const path = this;
|
325
|
-
|
346
|
+
const search = [path];
|
326
347
|
const ids = Object.create(null);
|
327
348
|
|
328
349
|
while (search.length) {
|
329
350
|
const id = search.shift();
|
330
351
|
if (!id) continue;
|
331
352
|
if (!id.node) continue;
|
332
|
-
const keys =
|
353
|
+
const keys = _getBindingIdentifiers.keys[id.node.type];
|
333
354
|
|
334
355
|
if (id.isIdentifier()) {
|
335
356
|
if (duplicates) {
|
@@ -346,7 +367,7 @@ function getBindingIdentifierPaths(duplicates = false, outerOnly = false) {
|
|
346
367
|
if (id.isExportDeclaration()) {
|
347
368
|
const declaration = id.get("declaration");
|
348
369
|
|
349
|
-
if (
|
370
|
+
if (isDeclaration(declaration)) {
|
350
371
|
search.push(declaration);
|
351
372
|
}
|
352
373
|
|
@@ -369,8 +390,10 @@ function getBindingIdentifierPaths(duplicates = false, outerOnly = false) {
|
|
369
390
|
const key = keys[i];
|
370
391
|
const child = id.get(key);
|
371
392
|
|
372
|
-
if (Array.isArray(child)
|
373
|
-
search
|
393
|
+
if (Array.isArray(child)) {
|
394
|
+
search.push(...child);
|
395
|
+
} else if (child.node) {
|
396
|
+
search.push(child);
|
374
397
|
}
|
375
398
|
}
|
376
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
|
}
|