@babel/traverse 7.0.0-beta.5 → 7.0.0-beta.53
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/README.md +9 -23
- package/lib/cache.js +5 -3
- package/lib/context.js +55 -91
- package/lib/hub.js +9 -4
- package/lib/index.js +36 -33
- package/lib/path/ancestry.js +37 -41
- package/lib/path/comments.js +22 -12
- package/lib/path/context.js +24 -48
- package/lib/path/conversion.js +170 -150
- package/lib/path/evaluation.js +108 -150
- package/lib/path/family.js +40 -43
- package/lib/path/index.js +99 -80
- package/lib/path/inference/index.js +39 -33
- package/lib/path/inference/inferer-reference.js +55 -49
- package/lib/path/inference/inferers.js +61 -49
- package/lib/path/introspection.js +105 -96
- package/lib/path/lib/hoister.js +63 -61
- package/lib/path/lib/removal-hooks.js +5 -3
- package/lib/path/lib/virtual-types.js +106 -66
- package/lib/path/modification.js +70 -68
- package/lib/path/removal.js +12 -5
- package/lib/path/replacement.js +76 -69
- package/lib/scope/binding.js +23 -23
- package/lib/scope/index.js +389 -487
- package/lib/scope/lib/renamer.js +71 -61
- package/lib/visitors.js +84 -124
- package/package.json +11 -10
package/lib/path/index.js
CHANGED
@@ -1,22 +1,58 @@
|
|
1
1
|
"use strict";
|
2
2
|
|
3
|
-
exports
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
4
|
+
value: true
|
5
|
+
});
|
4
6
|
exports.default = void 0;
|
5
7
|
|
6
8
|
var virtualTypes = _interopRequireWildcard(require("./lib/virtual-types"));
|
7
9
|
|
8
|
-
|
10
|
+
function _debug() {
|
11
|
+
const data = _interopRequireDefault(require("debug"));
|
9
12
|
|
10
|
-
|
13
|
+
_debug = function () {
|
14
|
+
return data;
|
15
|
+
};
|
16
|
+
|
17
|
+
return data;
|
18
|
+
}
|
19
|
+
|
20
|
+
function _invariant() {
|
21
|
+
const data = _interopRequireDefault(require("invariant"));
|
22
|
+
|
23
|
+
_invariant = function () {
|
24
|
+
return data;
|
25
|
+
};
|
26
|
+
|
27
|
+
return data;
|
28
|
+
}
|
11
29
|
|
12
30
|
var _index = _interopRequireDefault(require("../index"));
|
13
31
|
|
14
32
|
var _scope = _interopRequireDefault(require("../scope"));
|
15
33
|
|
16
|
-
|
34
|
+
function t() {
|
35
|
+
const data = _interopRequireWildcard(require("@babel/types"));
|
36
|
+
|
37
|
+
t = function () {
|
38
|
+
return data;
|
39
|
+
};
|
40
|
+
|
41
|
+
return data;
|
42
|
+
}
|
17
43
|
|
18
44
|
var _cache = require("../cache");
|
19
45
|
|
46
|
+
function _generator() {
|
47
|
+
const data = _interopRequireDefault(require("@babel/generator"));
|
48
|
+
|
49
|
+
_generator = function () {
|
50
|
+
return data;
|
51
|
+
};
|
52
|
+
|
53
|
+
return data;
|
54
|
+
}
|
55
|
+
|
20
56
|
var NodePath_ancestry = _interopRequireWildcard(require("./ancestry"));
|
21
57
|
|
22
58
|
var NodePath_inference = _interopRequireWildcard(require("./inference"));
|
@@ -41,12 +77,12 @@ var NodePath_comments = _interopRequireWildcard(require("./comments"));
|
|
41
77
|
|
42
78
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
43
79
|
|
44
|
-
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
|
80
|
+
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }
|
45
81
|
|
46
|
-
|
82
|
+
const debug = (0, _debug().default)("babel");
|
47
83
|
|
48
|
-
|
49
|
-
|
84
|
+
class NodePath {
|
85
|
+
constructor(hub, parent) {
|
50
86
|
this.parent = parent;
|
51
87
|
this.hub = hub;
|
52
88
|
this.contexts = [];
|
@@ -70,30 +106,30 @@ var NodePath = function () {
|
|
70
106
|
this.typeAnnotation = null;
|
71
107
|
}
|
72
108
|
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
109
|
+
static get({
|
110
|
+
hub,
|
111
|
+
parentPath,
|
112
|
+
parent,
|
113
|
+
container,
|
114
|
+
listKey,
|
115
|
+
key
|
116
|
+
}) {
|
81
117
|
if (!hub && parentPath) {
|
82
118
|
hub = parentPath.hub;
|
83
119
|
}
|
84
120
|
|
85
|
-
(0, _invariant.default)(parent, "To get a node path the parent needs to exist");
|
86
|
-
|
87
|
-
|
121
|
+
(0, _invariant().default)(parent, "To get a node path the parent needs to exist");
|
122
|
+
const targetNode = container[key];
|
123
|
+
const paths = _cache.path.get(parent) || [];
|
88
124
|
|
89
125
|
if (!_cache.path.has(parent)) {
|
90
126
|
_cache.path.set(parent, paths);
|
91
127
|
}
|
92
128
|
|
93
|
-
|
129
|
+
let path;
|
94
130
|
|
95
|
-
for (
|
96
|
-
|
131
|
+
for (let i = 0; i < paths.length; i++) {
|
132
|
+
const pathCheck = paths[i];
|
97
133
|
|
98
134
|
if (pathCheck.node === targetNode) {
|
99
135
|
path = pathCheck;
|
@@ -108,100 +144,83 @@ var NodePath = function () {
|
|
108
144
|
|
109
145
|
path.setup(parentPath, container, listKey, key);
|
110
146
|
return path;
|
111
|
-
}
|
112
|
-
|
113
|
-
var _proto = NodePath.prototype;
|
147
|
+
}
|
114
148
|
|
115
|
-
|
149
|
+
getScope(scope) {
|
116
150
|
return this.isScope() ? new _scope.default(this) : scope;
|
117
|
-
}
|
151
|
+
}
|
118
152
|
|
119
|
-
|
153
|
+
setData(key, val) {
|
120
154
|
return this.data[key] = val;
|
121
|
-
}
|
155
|
+
}
|
122
156
|
|
123
|
-
|
124
|
-
|
157
|
+
getData(key, def) {
|
158
|
+
let val = this.data[key];
|
125
159
|
if (!val && def) val = this.data[key] = def;
|
126
160
|
return val;
|
127
|
-
}
|
128
|
-
|
129
|
-
_proto.buildCodeFrameError = function buildCodeFrameError(msg, Error) {
|
130
|
-
if (Error === void 0) {
|
131
|
-
Error = SyntaxError;
|
132
|
-
}
|
161
|
+
}
|
133
162
|
|
163
|
+
buildCodeFrameError(msg, Error = SyntaxError) {
|
134
164
|
return this.hub.file.buildCodeFrameError(this.node, msg, Error);
|
135
|
-
}
|
165
|
+
}
|
136
166
|
|
137
|
-
|
167
|
+
traverse(visitor, state) {
|
138
168
|
(0, _index.default)(this.node, visitor, this.scope, state, this);
|
139
|
-
}
|
169
|
+
}
|
140
170
|
|
141
|
-
|
142
|
-
t.validate(this.node, key, node);
|
171
|
+
set(key, node) {
|
172
|
+
t().validate(this.node, key, node);
|
143
173
|
this.node[key] = node;
|
144
|
-
}
|
174
|
+
}
|
145
175
|
|
146
|
-
|
147
|
-
|
148
|
-
|
176
|
+
getPathLocation() {
|
177
|
+
const parts = [];
|
178
|
+
let path = this;
|
149
179
|
|
150
180
|
do {
|
151
|
-
|
152
|
-
if (path.inList) key = path.listKey
|
181
|
+
let key = path.key;
|
182
|
+
if (path.inList) key = `${path.listKey}[${key}]`;
|
153
183
|
parts.unshift(key);
|
154
184
|
} while (path = path.parentPath);
|
155
185
|
|
156
186
|
return parts.join(".");
|
157
|
-
}
|
187
|
+
}
|
158
188
|
|
159
|
-
|
160
|
-
if (!
|
189
|
+
debug(message) {
|
190
|
+
if (!debug.enabled) return;
|
191
|
+
debug(`${this.getPathLocation()} ${this.type}: ${message}`);
|
192
|
+
}
|
161
193
|
|
162
|
-
|
163
|
-
|
194
|
+
toString() {
|
195
|
+
return (0, _generator().default)(this.node).code;
|
196
|
+
}
|
164
197
|
|
165
|
-
|
166
|
-
}();
|
198
|
+
}
|
167
199
|
|
168
200
|
exports.default = NodePath;
|
169
201
|
Object.assign(NodePath.prototype, NodePath_ancestry, NodePath_inference, NodePath_replacement, NodePath_evaluation, NodePath_conversion, NodePath_introspection, NodePath_context, NodePath_removal, NodePath_modification, NodePath_family, NodePath_comments);
|
170
202
|
|
171
|
-
|
172
|
-
|
203
|
+
for (const type of t().TYPES) {
|
204
|
+
const typeKey = `is${type}`;
|
205
|
+
const fn = t()[typeKey];
|
173
206
|
|
174
207
|
NodePath.prototype[typeKey] = function (opts) {
|
175
|
-
return
|
208
|
+
return fn(this.node, opts);
|
176
209
|
};
|
177
210
|
|
178
|
-
NodePath.prototype[
|
179
|
-
if (!this
|
180
|
-
throw new TypeError(
|
211
|
+
NodePath.prototype[`assert${type}`] = function (opts) {
|
212
|
+
if (!fn(this.node, opts)) {
|
213
|
+
throw new TypeError(`Expected node path of type ${type}`);
|
181
214
|
}
|
182
215
|
};
|
183
|
-
};
|
184
|
-
|
185
|
-
var _arr = t.TYPES;
|
186
|
-
|
187
|
-
for (var _i = 0; _i < _arr.length; _i++) {
|
188
|
-
var type = _arr[_i];
|
189
|
-
|
190
|
-
_loop(type);
|
191
216
|
}
|
192
217
|
|
193
|
-
|
194
|
-
if (type[0] === "_")
|
195
|
-
if (t.TYPES.indexOf(type) < 0) t.TYPES.push(type);
|
196
|
-
|
218
|
+
for (const type in virtualTypes) {
|
219
|
+
if (type[0] === "_") continue;
|
220
|
+
if (t().TYPES.indexOf(type) < 0) t().TYPES.push(type);
|
221
|
+
const virtualType = virtualTypes[type];
|
197
222
|
|
198
|
-
NodePath.prototype[
|
223
|
+
NodePath.prototype[`is${type}`] = function (opts) {
|
199
224
|
return virtualType.checkPath(this, opts);
|
200
225
|
};
|
201
|
-
};
|
202
|
-
|
203
|
-
for (var type in virtualTypes) {
|
204
|
-
var _ret = _loop2(type);
|
205
|
-
|
206
|
-
if (_ret === "continue") continue;
|
207
226
|
}
|
@@ -1,6 +1,8 @@
|
|
1
1
|
"use strict";
|
2
2
|
|
3
|
-
exports
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
4
|
+
value: true
|
5
|
+
});
|
4
6
|
exports.getTypeAnnotation = getTypeAnnotation;
|
5
7
|
exports._getTypeAnnotation = _getTypeAnnotation;
|
6
8
|
exports.isBaseType = isBaseType;
|
@@ -10,34 +12,42 @@ exports.isGenericType = isGenericType;
|
|
10
12
|
|
11
13
|
var inferers = _interopRequireWildcard(require("./inferers"));
|
12
14
|
|
13
|
-
|
15
|
+
function t() {
|
16
|
+
const data = _interopRequireWildcard(require("@babel/types"));
|
14
17
|
|
15
|
-
|
18
|
+
t = function () {
|
19
|
+
return data;
|
20
|
+
};
|
21
|
+
|
22
|
+
return data;
|
23
|
+
}
|
24
|
+
|
25
|
+
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }
|
16
26
|
|
17
27
|
function getTypeAnnotation() {
|
18
28
|
if (this.typeAnnotation) return this.typeAnnotation;
|
19
|
-
|
20
|
-
if (t.isTypeAnnotation(type)) type = type.typeAnnotation;
|
29
|
+
let type = this._getTypeAnnotation() || t().anyTypeAnnotation();
|
30
|
+
if (t().isTypeAnnotation(type)) type = type.typeAnnotation;
|
21
31
|
return this.typeAnnotation = type;
|
22
32
|
}
|
23
33
|
|
24
34
|
function _getTypeAnnotation() {
|
25
|
-
|
35
|
+
const node = this.node;
|
26
36
|
|
27
37
|
if (!node) {
|
28
38
|
if (this.key === "init" && this.parentPath.isVariableDeclarator()) {
|
29
|
-
|
30
|
-
|
39
|
+
const declar = this.parentPath.parentPath;
|
40
|
+
const declarParent = declar.parentPath;
|
31
41
|
|
32
42
|
if (declar.key === "left" && declarParent.isForInStatement()) {
|
33
|
-
return t.stringTypeAnnotation();
|
43
|
+
return t().stringTypeAnnotation();
|
34
44
|
}
|
35
45
|
|
36
46
|
if (declar.key === "left" && declarParent.isForOfStatement()) {
|
37
|
-
return t.anyTypeAnnotation();
|
47
|
+
return t().anyTypeAnnotation();
|
38
48
|
}
|
39
49
|
|
40
|
-
return t.voidTypeAnnotation();
|
50
|
+
return t().voidTypeAnnotation();
|
41
51
|
} else {
|
42
52
|
return;
|
43
53
|
}
|
@@ -47,7 +57,7 @@ function _getTypeAnnotation() {
|
|
47
57
|
return node.typeAnnotation;
|
48
58
|
}
|
49
59
|
|
50
|
-
|
60
|
+
let inferer = inferers[node.type];
|
51
61
|
|
52
62
|
if (inferer) {
|
53
63
|
return inferer.call(this, node);
|
@@ -66,39 +76,35 @@ function isBaseType(baseName, soft) {
|
|
66
76
|
|
67
77
|
function _isBaseType(baseName, type, soft) {
|
68
78
|
if (baseName === "string") {
|
69
|
-
return t.isStringTypeAnnotation(type);
|
79
|
+
return t().isStringTypeAnnotation(type);
|
70
80
|
} else if (baseName === "number") {
|
71
|
-
return t.isNumberTypeAnnotation(type);
|
81
|
+
return t().isNumberTypeAnnotation(type);
|
72
82
|
} else if (baseName === "boolean") {
|
73
|
-
return t.isBooleanTypeAnnotation(type);
|
83
|
+
return t().isBooleanTypeAnnotation(type);
|
74
84
|
} else if (baseName === "any") {
|
75
|
-
return t.isAnyTypeAnnotation(type);
|
85
|
+
return t().isAnyTypeAnnotation(type);
|
76
86
|
} else if (baseName === "mixed") {
|
77
|
-
return t.isMixedTypeAnnotation(type);
|
87
|
+
return t().isMixedTypeAnnotation(type);
|
78
88
|
} else if (baseName === "empty") {
|
79
|
-
return t.isEmptyTypeAnnotation(type);
|
89
|
+
return t().isEmptyTypeAnnotation(type);
|
80
90
|
} else if (baseName === "void") {
|
81
|
-
return t.isVoidTypeAnnotation(type);
|
91
|
+
return t().isVoidTypeAnnotation(type);
|
82
92
|
} else {
|
83
93
|
if (soft) {
|
84
94
|
return false;
|
85
95
|
} else {
|
86
|
-
throw new Error(
|
96
|
+
throw new Error(`Unknown base type ${baseName}`);
|
87
97
|
}
|
88
98
|
}
|
89
99
|
}
|
90
100
|
|
91
101
|
function couldBeBaseType(name) {
|
92
|
-
|
93
|
-
if (t.isAnyTypeAnnotation(type)) return true;
|
94
|
-
|
95
|
-
if (t.isUnionTypeAnnotation(type)) {
|
96
|
-
var _arr = type.types;
|
97
|
-
|
98
|
-
for (var _i = 0; _i < _arr.length; _i++) {
|
99
|
-
var type2 = _arr[_i];
|
102
|
+
const type = this.getTypeAnnotation();
|
103
|
+
if (t().isAnyTypeAnnotation(type)) return true;
|
100
104
|
|
101
|
-
|
105
|
+
if (t().isUnionTypeAnnotation(type)) {
|
106
|
+
for (const type2 of type.types) {
|
107
|
+
if (t().isAnyTypeAnnotation(type2) || _isBaseType(name, type2, true)) {
|
102
108
|
return true;
|
103
109
|
}
|
104
110
|
}
|
@@ -110,17 +116,17 @@ function couldBeBaseType(name) {
|
|
110
116
|
}
|
111
117
|
|
112
118
|
function baseTypeStrictlyMatches(right) {
|
113
|
-
|
119
|
+
const left = this.getTypeAnnotation();
|
114
120
|
right = right.getTypeAnnotation();
|
115
121
|
|
116
|
-
if (!t.isAnyTypeAnnotation(left) && t.isFlowBaseAnnotation(left)) {
|
122
|
+
if (!t().isAnyTypeAnnotation(left) && t().isFlowBaseAnnotation(left)) {
|
117
123
|
return right.type === left.type;
|
118
124
|
}
|
119
125
|
}
|
120
126
|
|
121
127
|
function isGenericType(genericName) {
|
122
|
-
|
123
|
-
return t.isGenericTypeAnnotation(type) && t.isIdentifier(type.id, {
|
128
|
+
const type = this.getTypeAnnotation();
|
129
|
+
return t().isGenericTypeAnnotation(type) && t().isIdentifier(type.id, {
|
124
130
|
name: genericName
|
125
131
|
});
|
126
132
|
}
|
@@ -1,15 +1,25 @@
|
|
1
1
|
"use strict";
|
2
2
|
|
3
|
-
exports
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
4
|
+
value: true
|
5
|
+
});
|
4
6
|
exports.default = _default;
|
5
7
|
|
6
|
-
|
8
|
+
function t() {
|
9
|
+
const data = _interopRequireWildcard(require("@babel/types"));
|
7
10
|
|
8
|
-
|
11
|
+
t = function () {
|
12
|
+
return data;
|
13
|
+
};
|
14
|
+
|
15
|
+
return data;
|
16
|
+
}
|
17
|
+
|
18
|
+
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }
|
9
19
|
|
10
20
|
function _default(node) {
|
11
21
|
if (!this.isReferenced()) return;
|
12
|
-
|
22
|
+
const binding = this.scope.getBinding(node.name);
|
13
23
|
|
14
24
|
if (binding) {
|
15
25
|
if (binding.identifier.typeAnnotation) {
|
@@ -20,48 +30,44 @@ function _default(node) {
|
|
20
30
|
}
|
21
31
|
|
22
32
|
if (node.name === "undefined") {
|
23
|
-
return t.voidTypeAnnotation();
|
33
|
+
return t().voidTypeAnnotation();
|
24
34
|
} else if (node.name === "NaN" || node.name === "Infinity") {
|
25
|
-
return t.numberTypeAnnotation();
|
35
|
+
return t().numberTypeAnnotation();
|
26
36
|
} else if (node.name === "arguments") {}
|
27
37
|
}
|
28
38
|
|
29
39
|
function getTypeAnnotationBindingConstantViolations(binding, path, name) {
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
40
|
+
const types = [];
|
41
|
+
const functionConstantViolations = [];
|
42
|
+
let constantViolations = getConstantViolationsBefore(binding, path, functionConstantViolations);
|
43
|
+
const testType = getConditionalAnnotation(binding, path, name);
|
34
44
|
|
35
45
|
if (testType) {
|
36
|
-
|
37
|
-
constantViolations = constantViolations.filter(
|
38
|
-
return testConstantViolations.indexOf(path) < 0;
|
39
|
-
});
|
46
|
+
const testConstantViolations = getConstantViolationsBefore(binding, testType.ifStatement);
|
47
|
+
constantViolations = constantViolations.filter(path => testConstantViolations.indexOf(path) < 0);
|
40
48
|
types.push(testType.typeAnnotation);
|
41
49
|
}
|
42
50
|
|
43
51
|
if (constantViolations.length) {
|
44
52
|
constantViolations = constantViolations.concat(functionConstantViolations);
|
45
|
-
var _arr = constantViolations;
|
46
53
|
|
47
|
-
for (
|
48
|
-
var violation = _arr[_i];
|
54
|
+
for (const violation of constantViolations) {
|
49
55
|
types.push(violation.getTypeAnnotation());
|
50
56
|
}
|
51
57
|
}
|
52
58
|
|
53
59
|
if (types.length) {
|
54
|
-
return t.createUnionTypeAnnotation(types);
|
60
|
+
return t().createUnionTypeAnnotation(types);
|
55
61
|
}
|
56
62
|
}
|
57
63
|
|
58
64
|
function getConstantViolationsBefore(binding, path, functions) {
|
59
|
-
|
65
|
+
const violations = binding.constantViolations.slice();
|
60
66
|
violations.unshift(binding.path);
|
61
|
-
return violations.filter(
|
67
|
+
return violations.filter(violation => {
|
62
68
|
violation = violation.resolve();
|
63
69
|
|
64
|
-
|
70
|
+
const status = violation._guessExecutionStatusRelativeTo(path);
|
65
71
|
|
66
72
|
if (functions && status === "function") functions.push(violation);
|
67
73
|
return status === "before";
|
@@ -69,17 +75,17 @@ function getConstantViolationsBefore(binding, path, functions) {
|
|
69
75
|
}
|
70
76
|
|
71
77
|
function inferAnnotationFromBinaryExpression(name, path) {
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
78
|
+
const operator = path.node.operator;
|
79
|
+
const right = path.get("right").resolve();
|
80
|
+
const left = path.get("left").resolve();
|
81
|
+
let target;
|
76
82
|
|
77
83
|
if (left.isIdentifier({
|
78
|
-
name
|
84
|
+
name
|
79
85
|
})) {
|
80
86
|
target = right;
|
81
87
|
} else if (right.isIdentifier({
|
82
|
-
name
|
88
|
+
name
|
83
89
|
})) {
|
84
90
|
target = left;
|
85
91
|
}
|
@@ -89,16 +95,16 @@ function inferAnnotationFromBinaryExpression(name, path) {
|
|
89
95
|
return target.getTypeAnnotation();
|
90
96
|
}
|
91
97
|
|
92
|
-
if (t.BOOLEAN_NUMBER_BINARY_OPERATORS.indexOf(operator) >= 0) {
|
93
|
-
return t.numberTypeAnnotation();
|
98
|
+
if (t().BOOLEAN_NUMBER_BINARY_OPERATORS.indexOf(operator) >= 0) {
|
99
|
+
return t().numberTypeAnnotation();
|
94
100
|
}
|
95
101
|
|
96
102
|
return;
|
97
103
|
}
|
98
104
|
|
99
105
|
if (operator !== "===" && operator !== "==") return;
|
100
|
-
|
101
|
-
|
106
|
+
let typeofPath;
|
107
|
+
let typePath;
|
102
108
|
|
103
109
|
if (left.isUnaryExpression({
|
104
110
|
operator: "typeof"
|
@@ -114,17 +120,17 @@ function inferAnnotationFromBinaryExpression(name, path) {
|
|
114
120
|
|
115
121
|
if (!typeofPath) return;
|
116
122
|
if (!typeofPath.get("argument").isIdentifier({
|
117
|
-
name
|
123
|
+
name
|
118
124
|
})) return;
|
119
125
|
typePath = typePath.resolve();
|
120
126
|
if (!typePath.isLiteral()) return;
|
121
|
-
|
127
|
+
const typeValue = typePath.node.value;
|
122
128
|
if (typeof typeValue !== "string") return;
|
123
|
-
return t.createTypeAnnotationBasedOnTypeof(typeValue);
|
129
|
+
return t().createTypeAnnotationBasedOnTypeof(typeValue);
|
124
130
|
}
|
125
131
|
|
126
132
|
function getParentConditionalPath(binding, path, name) {
|
127
|
-
|
133
|
+
let parentPath;
|
128
134
|
|
129
135
|
while (parentPath = path.parentPath) {
|
130
136
|
if (parentPath.isIfStatement() || parentPath.isConditionalExpression()) {
|
@@ -144,30 +150,30 @@ function getParentConditionalPath(binding, path, name) {
|
|
144
150
|
}
|
145
151
|
|
146
152
|
function getConditionalAnnotation(binding, path, name) {
|
147
|
-
|
153
|
+
const ifStatement = getParentConditionalPath(binding, path, name);
|
148
154
|
if (!ifStatement) return;
|
149
|
-
|
150
|
-
|
151
|
-
|
155
|
+
const test = ifStatement.get("test");
|
156
|
+
const paths = [test];
|
157
|
+
const types = [];
|
152
158
|
|
153
|
-
for (
|
154
|
-
|
159
|
+
for (let i = 0; i < paths.length; i++) {
|
160
|
+
const path = paths[i];
|
155
161
|
|
156
|
-
if (
|
157
|
-
if (
|
158
|
-
paths.push(
|
159
|
-
paths.push(
|
162
|
+
if (path.isLogicalExpression()) {
|
163
|
+
if (path.node.operator === "&&") {
|
164
|
+
paths.push(path.get("left"));
|
165
|
+
paths.push(path.get("right"));
|
160
166
|
}
|
161
|
-
} else if (
|
162
|
-
|
167
|
+
} else if (path.isBinaryExpression()) {
|
168
|
+
const type = inferAnnotationFromBinaryExpression(name, path);
|
163
169
|
if (type) types.push(type);
|
164
170
|
}
|
165
171
|
}
|
166
172
|
|
167
173
|
if (types.length) {
|
168
174
|
return {
|
169
|
-
typeAnnotation: t.createUnionTypeAnnotation(types),
|
170
|
-
ifStatement
|
175
|
+
typeAnnotation: t().createUnionTypeAnnotation(types),
|
176
|
+
ifStatement
|
171
177
|
};
|
172
178
|
}
|
173
179
|
|