@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/lib/hoister.js
CHANGED
@@ -1,20 +1,30 @@
|
|
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
|
+
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
|
-
|
11
|
-
ReferencedIdentifier
|
12
|
-
if (path.isJSXIdentifier() && t.react.isCompatTag(path.node.name) && !path.parentPath.isJSXMemberExpression()) {
|
20
|
+
const referenceVisitor = {
|
21
|
+
ReferencedIdentifier(path, state) {
|
22
|
+
if (path.isJSXIdentifier() && t().react.isCompatTag(path.node.name) && !path.parentPath.isJSXMemberExpression()) {
|
13
23
|
return;
|
14
24
|
}
|
15
25
|
|
16
26
|
if (path.node.name === "this") {
|
17
|
-
|
27
|
+
let scope = path.scope;
|
18
28
|
|
19
29
|
do {
|
20
30
|
if (scope.path.isFunction() && !scope.path.isArrowFunctionExpression()) {
|
@@ -25,15 +35,16 @@ var referenceVisitor = {
|
|
25
35
|
if (scope) state.breakOnScopePaths.push(scope.path);
|
26
36
|
}
|
27
37
|
|
28
|
-
|
38
|
+
const binding = path.scope.getBinding(path.node.name);
|
29
39
|
if (!binding) return;
|
30
40
|
if (binding !== state.scope.getBinding(path.node.name)) return;
|
31
41
|
state.bindings[path.node.name] = binding;
|
32
42
|
}
|
43
|
+
|
33
44
|
};
|
34
45
|
|
35
|
-
|
36
|
-
|
46
|
+
class PathHoister {
|
47
|
+
constructor(path, scope) {
|
37
48
|
this.breakOnScopePaths = [];
|
38
49
|
this.bindings = {};
|
39
50
|
this.scopes = [];
|
@@ -42,11 +53,9 @@ var PathHoister = function () {
|
|
42
53
|
this.attachAfter = false;
|
43
54
|
}
|
44
55
|
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
for (var key in this.bindings) {
|
49
|
-
var binding = this.bindings[key];
|
56
|
+
isCompatibleScope(scope) {
|
57
|
+
for (const key in this.bindings) {
|
58
|
+
const binding = this.bindings[key];
|
50
59
|
|
51
60
|
if (!scope.bindingIdentifierEquals(key, binding.identifier)) {
|
52
61
|
return false;
|
@@ -54,10 +63,10 @@ var PathHoister = function () {
|
|
54
63
|
}
|
55
64
|
|
56
65
|
return true;
|
57
|
-
}
|
66
|
+
}
|
58
67
|
|
59
|
-
|
60
|
-
|
68
|
+
getCompatibleScopes() {
|
69
|
+
let scope = this.path.scope;
|
61
70
|
|
62
71
|
do {
|
63
72
|
if (this.isCompatibleScope(scope)) {
|
@@ -70,37 +79,34 @@ var PathHoister = function () {
|
|
70
79
|
break;
|
71
80
|
}
|
72
81
|
} while (scope = scope.parent);
|
73
|
-
}
|
82
|
+
}
|
74
83
|
|
75
|
-
|
76
|
-
|
84
|
+
getAttachmentPath() {
|
85
|
+
let path = this._getAttachmentPath();
|
77
86
|
|
78
87
|
if (!path) return;
|
79
|
-
|
88
|
+
let targetScope = path.scope;
|
80
89
|
|
81
90
|
if (targetScope.path === path) {
|
82
91
|
targetScope = path.scope.parent;
|
83
92
|
}
|
84
93
|
|
85
94
|
if (targetScope.path.isProgram() || targetScope.path.isFunction()) {
|
86
|
-
for (
|
95
|
+
for (const name in this.bindings) {
|
87
96
|
if (!targetScope.hasOwnBinding(name)) continue;
|
88
|
-
|
97
|
+
const binding = this.bindings[name];
|
89
98
|
|
90
99
|
if (binding.kind === "param" || binding.path.parentKey === "params") {
|
91
100
|
continue;
|
92
101
|
}
|
93
102
|
|
94
|
-
|
103
|
+
const bindingParentPath = this.getAttachmentParentForPath(binding.path);
|
95
104
|
|
96
105
|
if (bindingParentPath.key >= path.key) {
|
97
106
|
this.attachAfter = true;
|
98
107
|
path = binding.path;
|
99
|
-
var _arr = binding.constantViolations;
|
100
|
-
|
101
|
-
for (var _i = 0; _i < _arr.length; _i++) {
|
102
|
-
var violationPath = _arr[_i];
|
103
108
|
|
109
|
+
for (const violationPath of binding.constantViolations) {
|
104
110
|
if (this.getAttachmentParentForPath(violationPath).key > path.key) {
|
105
111
|
path = violationPath;
|
106
112
|
}
|
@@ -109,24 +115,20 @@ var PathHoister = function () {
|
|
109
115
|
}
|
110
116
|
}
|
111
117
|
|
112
|
-
if (path.parentPath.isExportDeclaration()) {
|
113
|
-
path = path.parentPath;
|
114
|
-
}
|
115
|
-
|
116
118
|
return path;
|
117
|
-
}
|
119
|
+
}
|
118
120
|
|
119
|
-
|
120
|
-
|
121
|
-
|
121
|
+
_getAttachmentPath() {
|
122
|
+
const scopes = this.scopes;
|
123
|
+
const scope = scopes.pop();
|
122
124
|
if (!scope) return;
|
123
125
|
|
124
126
|
if (scope.path.isFunction()) {
|
125
127
|
if (this.hasOwnParamBindings(scope)) {
|
126
128
|
if (this.scope === scope) return;
|
127
|
-
|
129
|
+
const bodies = scope.path.get("body").get("body");
|
128
130
|
|
129
|
-
for (
|
131
|
+
for (let i = 0; i < bodies.length; i++) {
|
130
132
|
if (bodies[i].node._blockHoist) continue;
|
131
133
|
return bodies[i];
|
132
134
|
}
|
@@ -136,51 +138,51 @@ var PathHoister = function () {
|
|
136
138
|
} else if (scope.path.isProgram()) {
|
137
139
|
return this.getNextScopeAttachmentParent();
|
138
140
|
}
|
139
|
-
}
|
141
|
+
}
|
140
142
|
|
141
|
-
|
142
|
-
|
143
|
+
getNextScopeAttachmentParent() {
|
144
|
+
const scope = this.scopes.pop();
|
143
145
|
if (scope) return this.getAttachmentParentForPath(scope.path);
|
144
|
-
}
|
146
|
+
}
|
145
147
|
|
146
|
-
|
148
|
+
getAttachmentParentForPath(path) {
|
147
149
|
do {
|
148
150
|
if (!path.parentPath || Array.isArray(path.container) && path.isStatement()) {
|
149
151
|
return path;
|
150
152
|
}
|
151
153
|
} while (path = path.parentPath);
|
152
|
-
}
|
154
|
+
}
|
153
155
|
|
154
|
-
|
155
|
-
for (
|
156
|
+
hasOwnParamBindings(scope) {
|
157
|
+
for (const name in this.bindings) {
|
156
158
|
if (!scope.hasOwnBinding(name)) continue;
|
157
|
-
|
159
|
+
const binding = this.bindings[name];
|
158
160
|
if (binding.kind === "param" && binding.constant) return true;
|
159
161
|
}
|
160
162
|
|
161
163
|
return false;
|
162
|
-
}
|
164
|
+
}
|
163
165
|
|
164
|
-
|
166
|
+
run() {
|
165
167
|
this.path.traverse(referenceVisitor, this);
|
166
168
|
this.getCompatibleScopes();
|
167
|
-
|
169
|
+
const attachTo = this.getAttachmentPath();
|
168
170
|
if (!attachTo) return;
|
169
171
|
if (attachTo.getFunctionParent() === this.path.getFunctionParent()) return;
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
attachTo[insertFn]([attachTo.isVariableDeclarator() ? declarator : t.variableDeclaration("var", [declarator])]);
|
174
|
-
|
172
|
+
let uid = attachTo.scope.generateUidIdentifier("ref");
|
173
|
+
const declarator = t().variableDeclarator(uid, this.path.node);
|
174
|
+
const insertFn = this.attachAfter ? "insertAfter" : "insertBefore";
|
175
|
+
const [attached] = attachTo[insertFn]([attachTo.isVariableDeclarator() ? declarator : t().variableDeclaration("var", [declarator])]);
|
176
|
+
const parent = this.path.parentPath;
|
175
177
|
|
176
178
|
if (parent.isJSXElement() && this.path.container === parent.node.children) {
|
177
|
-
uid = t.JSXExpressionContainer(uid);
|
179
|
+
uid = t().JSXExpressionContainer(uid);
|
178
180
|
}
|
179
181
|
|
180
|
-
this.path.replaceWith(uid);
|
181
|
-
|
182
|
+
this.path.replaceWith(t().cloneNode(uid));
|
183
|
+
return attachTo.isVariableDeclarator() ? attached.get("init") : attached.get("declarations.0.init");
|
184
|
+
}
|
182
185
|
|
183
|
-
|
184
|
-
}();
|
186
|
+
}
|
185
187
|
|
186
188
|
exports.default = PathHoister;
|
@@ -1,9 +1,11 @@
|
|
1
1
|
"use strict";
|
2
2
|
|
3
|
-
exports
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
4
|
+
value: true
|
5
|
+
});
|
4
6
|
exports.hooks = void 0;
|
5
|
-
|
6
|
-
|
7
|
+
const hooks = [function (self, parent) {
|
8
|
+
const removeParent = self.key === "test" && (parent.isWhile() || parent.isSwitchCase()) || self.key === "declaration" && parent.isExportDeclaration() || self.key === "body" && parent.isLabeledStatement() || self.listKey === "declarations" && parent.isVariableDeclaration() && parent.node.declarations.length === 1 || self.key === "expression" && parent.isExpressionStatement();
|
7
9
|
|
8
10
|
if (removeParent) {
|
9
11
|
parent.remove();
|
@@ -1,60 +1,79 @@
|
|
1
1
|
"use strict";
|
2
2
|
|
3
|
-
exports
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
4
|
+
value: true
|
5
|
+
});
|
4
6
|
exports.ForAwaitStatement = exports.NumericLiteralTypeAnnotation = exports.ExistentialTypeParam = exports.SpreadProperty = exports.RestProperty = exports.Flow = exports.Pure = exports.Generated = exports.User = exports.Var = exports.BlockScoped = exports.Referenced = exports.Scope = exports.Expression = exports.Statement = exports.BindingIdentifier = exports.ReferencedMemberExpression = exports.ReferencedIdentifier = void 0;
|
5
7
|
|
6
|
-
|
8
|
+
function t() {
|
9
|
+
const data = _interopRequireWildcard(require("@babel/types"));
|
7
10
|
|
8
|
-
|
11
|
+
t = function () {
|
12
|
+
return data;
|
13
|
+
};
|
9
14
|
|
10
|
-
|
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; } }
|
19
|
+
|
20
|
+
const ReferencedIdentifier = {
|
11
21
|
types: ["Identifier", "JSXIdentifier"],
|
12
|
-
checkPath: function checkPath(_ref, opts) {
|
13
|
-
var node = _ref.node,
|
14
|
-
parent = _ref.parent;
|
15
22
|
|
16
|
-
|
17
|
-
|
18
|
-
|
23
|
+
checkPath({
|
24
|
+
node,
|
25
|
+
parent
|
26
|
+
}, opts) {
|
27
|
+
if (!t().isIdentifier(node, opts) && !t().isJSXMemberExpression(parent, opts)) {
|
28
|
+
if (t().isJSXIdentifier(node, opts)) {
|
29
|
+
if (t().react.isCompatTag(node.name)) return false;
|
19
30
|
} else {
|
20
31
|
return false;
|
21
32
|
}
|
22
33
|
}
|
23
34
|
|
24
|
-
return t.isReferenced(node, parent);
|
35
|
+
return t().isReferenced(node, parent);
|
25
36
|
}
|
37
|
+
|
26
38
|
};
|
27
39
|
exports.ReferencedIdentifier = ReferencedIdentifier;
|
28
|
-
|
40
|
+
const ReferencedMemberExpression = {
|
29
41
|
types: ["MemberExpression"],
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
42
|
+
|
43
|
+
checkPath({
|
44
|
+
node,
|
45
|
+
parent
|
46
|
+
}) {
|
47
|
+
return t().isMemberExpression(node) && t().isReferenced(node, parent);
|
34
48
|
}
|
49
|
+
|
35
50
|
};
|
36
51
|
exports.ReferencedMemberExpression = ReferencedMemberExpression;
|
37
|
-
|
52
|
+
const BindingIdentifier = {
|
38
53
|
types: ["Identifier"],
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
54
|
+
|
55
|
+
checkPath({
|
56
|
+
node,
|
57
|
+
parent
|
58
|
+
}) {
|
59
|
+
return t().isIdentifier(node) && t().isBinding(node, parent);
|
43
60
|
}
|
61
|
+
|
44
62
|
};
|
45
63
|
exports.BindingIdentifier = BindingIdentifier;
|
46
|
-
|
64
|
+
const Statement = {
|
47
65
|
types: ["Statement"],
|
48
|
-
checkPath: function checkPath(_ref4) {
|
49
|
-
var node = _ref4.node,
|
50
|
-
parent = _ref4.parent;
|
51
66
|
|
52
|
-
|
53
|
-
|
54
|
-
|
67
|
+
checkPath({
|
68
|
+
node,
|
69
|
+
parent
|
70
|
+
}) {
|
71
|
+
if (t().isStatement(node)) {
|
72
|
+
if (t().isVariableDeclaration(node)) {
|
73
|
+
if (t().isForXStatement(parent, {
|
55
74
|
left: node
|
56
75
|
})) return false;
|
57
|
-
if (t.isForStatement(parent, {
|
76
|
+
if (t().isForStatement(parent, {
|
58
77
|
init: node
|
59
78
|
})) return false;
|
60
79
|
}
|
@@ -64,109 +83,130 @@ var Statement = {
|
|
64
83
|
return false;
|
65
84
|
}
|
66
85
|
}
|
86
|
+
|
67
87
|
};
|
68
88
|
exports.Statement = Statement;
|
69
|
-
|
89
|
+
const Expression = {
|
70
90
|
types: ["Expression"],
|
71
|
-
|
91
|
+
|
92
|
+
checkPath(path) {
|
72
93
|
if (path.isIdentifier()) {
|
73
94
|
return path.isReferencedIdentifier();
|
74
95
|
} else {
|
75
|
-
return t.isExpression(path.node);
|
96
|
+
return t().isExpression(path.node);
|
76
97
|
}
|
77
98
|
}
|
99
|
+
|
78
100
|
};
|
79
101
|
exports.Expression = Expression;
|
80
|
-
|
102
|
+
const Scope = {
|
81
103
|
types: ["Scopable"],
|
82
|
-
|
83
|
-
|
104
|
+
|
105
|
+
checkPath(path) {
|
106
|
+
return t().isScope(path.node, path.parent);
|
84
107
|
}
|
108
|
+
|
85
109
|
};
|
86
110
|
exports.Scope = Scope;
|
87
|
-
|
88
|
-
checkPath
|
89
|
-
return t.isReferenced(path.node, path.parent);
|
111
|
+
const Referenced = {
|
112
|
+
checkPath(path) {
|
113
|
+
return t().isReferenced(path.node, path.parent);
|
90
114
|
}
|
115
|
+
|
91
116
|
};
|
92
117
|
exports.Referenced = Referenced;
|
93
|
-
|
94
|
-
checkPath
|
95
|
-
return t.isBlockScoped(path.node);
|
118
|
+
const BlockScoped = {
|
119
|
+
checkPath(path) {
|
120
|
+
return t().isBlockScoped(path.node);
|
96
121
|
}
|
122
|
+
|
97
123
|
};
|
98
124
|
exports.BlockScoped = BlockScoped;
|
99
|
-
|
125
|
+
const Var = {
|
100
126
|
types: ["VariableDeclaration"],
|
101
|
-
|
102
|
-
|
127
|
+
|
128
|
+
checkPath(path) {
|
129
|
+
return t().isVar(path.node);
|
103
130
|
}
|
131
|
+
|
104
132
|
};
|
105
133
|
exports.Var = Var;
|
106
|
-
|
107
|
-
checkPath
|
134
|
+
const User = {
|
135
|
+
checkPath(path) {
|
108
136
|
return path.node && !!path.node.loc;
|
109
137
|
}
|
138
|
+
|
110
139
|
};
|
111
140
|
exports.User = User;
|
112
|
-
|
113
|
-
checkPath
|
141
|
+
const Generated = {
|
142
|
+
checkPath(path) {
|
114
143
|
return !path.isUser();
|
115
144
|
}
|
145
|
+
|
116
146
|
};
|
117
147
|
exports.Generated = Generated;
|
118
|
-
|
119
|
-
checkPath
|
148
|
+
const Pure = {
|
149
|
+
checkPath(path, opts) {
|
120
150
|
return path.scope.isPure(path.node, opts);
|
121
151
|
}
|
152
|
+
|
122
153
|
};
|
123
154
|
exports.Pure = Pure;
|
124
|
-
|
155
|
+
const Flow = {
|
125
156
|
types: ["Flow", "ImportDeclaration", "ExportDeclaration", "ImportSpecifier"],
|
126
|
-
checkPath: function checkPath(_ref5) {
|
127
|
-
var node = _ref5.node;
|
128
157
|
|
129
|
-
|
158
|
+
checkPath({
|
159
|
+
node
|
160
|
+
}) {
|
161
|
+
if (t().isFlow(node)) {
|
130
162
|
return true;
|
131
|
-
} else if (t.isImportDeclaration(node)) {
|
163
|
+
} else if (t().isImportDeclaration(node)) {
|
132
164
|
return node.importKind === "type" || node.importKind === "typeof";
|
133
|
-
} else if (t.isExportDeclaration(node)) {
|
165
|
+
} else if (t().isExportDeclaration(node)) {
|
134
166
|
return node.exportKind === "type";
|
135
|
-
} else if (t.isImportSpecifier(node)) {
|
167
|
+
} else if (t().isImportSpecifier(node)) {
|
136
168
|
return node.importKind === "type" || node.importKind === "typeof";
|
137
169
|
} else {
|
138
170
|
return false;
|
139
171
|
}
|
140
172
|
}
|
173
|
+
|
141
174
|
};
|
142
175
|
exports.Flow = Flow;
|
143
|
-
|
176
|
+
const RestProperty = {
|
144
177
|
types: ["RestElement"],
|
145
|
-
|
178
|
+
|
179
|
+
checkPath(path) {
|
146
180
|
return path.parentPath && path.parentPath.isObjectPattern();
|
147
181
|
}
|
182
|
+
|
148
183
|
};
|
149
184
|
exports.RestProperty = RestProperty;
|
150
|
-
|
185
|
+
const SpreadProperty = {
|
151
186
|
types: ["RestElement"],
|
152
|
-
|
187
|
+
|
188
|
+
checkPath(path) {
|
153
189
|
return path.parentPath && path.parentPath.isObjectExpression();
|
154
190
|
}
|
191
|
+
|
155
192
|
};
|
156
193
|
exports.SpreadProperty = SpreadProperty;
|
157
|
-
|
194
|
+
const ExistentialTypeParam = {
|
158
195
|
types: ["ExistsTypeAnnotation"]
|
159
196
|
};
|
160
197
|
exports.ExistentialTypeParam = ExistentialTypeParam;
|
161
|
-
|
198
|
+
const NumericLiteralTypeAnnotation = {
|
162
199
|
types: ["NumberLiteralTypeAnnotation"]
|
163
200
|
};
|
164
201
|
exports.NumericLiteralTypeAnnotation = NumericLiteralTypeAnnotation;
|
165
|
-
|
202
|
+
const ForAwaitStatement = {
|
166
203
|
types: ["ForOfStatement"],
|
167
|
-
|
168
|
-
|
204
|
+
|
205
|
+
checkPath({
|
206
|
+
node
|
207
|
+
}) {
|
169
208
|
return node.await === true;
|
170
209
|
}
|
210
|
+
|
171
211
|
};
|
172
212
|
exports.ForAwaitStatement = ForAwaitStatement;
|