index.js
6.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var virtualTypes = _interopRequireWildcard(require("./lib/virtual-types"));
function _debug2() {
var data = _interopRequireDefault(require("debug"));
_debug2 = function _debug2() {
return data;
};
return data;
}
function _invariant() {
var data = _interopRequireDefault(require("invariant"));
_invariant = function _invariant() {
return data;
};
return data;
}
var _index = _interopRequireDefault(require("../index"));
var _scope = _interopRequireDefault(require("../scope"));
function t() {
var data = _interopRequireWildcard(require("@babel/types"));
t = function t() {
return data;
};
return data;
}
var _cache = require("../cache");
function _generator() {
var data = _interopRequireDefault(require("@babel/generator"));
_generator = function _generator() {
return data;
};
return data;
}
var NodePath_ancestry = _interopRequireWildcard(require("./ancestry"));
var NodePath_inference = _interopRequireWildcard(require("./inference"));
var NodePath_replacement = _interopRequireWildcard(require("./replacement"));
var NodePath_evaluation = _interopRequireWildcard(require("./evaluation"));
var NodePath_conversion = _interopRequireWildcard(require("./conversion"));
var NodePath_introspection = _interopRequireWildcard(require("./introspection"));
var NodePath_context = _interopRequireWildcard(require("./context"));
var NodePath_removal = _interopRequireWildcard(require("./removal"));
var NodePath_modification = _interopRequireWildcard(require("./modification"));
var NodePath_family = _interopRequireWildcard(require("./family"));
var NodePath_comments = _interopRequireWildcard(require("./comments"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
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; } }
var _debug = (0, _debug2().default)("babel");
var NodePath = function () {
function NodePath(hub, parent) {
this.parent = parent;
this.hub = hub;
this.contexts = [];
this.data = {};
this.shouldSkip = false;
this.shouldStop = false;
this.removed = false;
this.state = null;
this.opts = null;
this.skipKeys = null;
this.parentPath = null;
this.context = null;
this.container = null;
this.listKey = null;
this.inList = false;
this.parentKey = null;
this.key = null;
this.node = null;
this.scope = null;
this.type = null;
this.typeAnnotation = null;
}
NodePath.get = function get(_ref) {
var hub = _ref.hub,
parentPath = _ref.parentPath,
parent = _ref.parent,
container = _ref.container,
listKey = _ref.listKey,
key = _ref.key;
if (!hub && parentPath) {
hub = parentPath.hub;
}
(0, _invariant().default)(parent, "To get a node path the parent needs to exist");
var targetNode = container[key];
var paths = _cache.path.get(parent) || [];
if (!_cache.path.has(parent)) {
_cache.path.set(parent, paths);
}
var path;
for (var i = 0; i < paths.length; i++) {
var pathCheck = paths[i];
if (pathCheck.node === targetNode) {
path = pathCheck;
break;
}
}
if (!path) {
path = new NodePath(hub, parent);
paths.push(path);
}
path.setup(parentPath, container, listKey, key);
return path;
};
var _proto = NodePath.prototype;
_proto.getScope = function getScope(scope) {
return this.isScope() ? new _scope.default(this) : scope;
};
_proto.setData = function setData(key, val) {
return this.data[key] = val;
};
_proto.getData = function getData(key, def) {
var val = this.data[key];
if (!val && def) val = this.data[key] = def;
return val;
};
_proto.buildCodeFrameError = function buildCodeFrameError(msg, Error) {
if (Error === void 0) {
Error = SyntaxError;
}
return this.hub.file.buildCodeFrameError(this.node, msg, Error);
};
_proto.traverse = function traverse(visitor, state) {
(0, _index.default)(this.node, visitor, this.scope, state, this);
};
_proto.set = function set(key, node) {
t().validate(this.node, key, node);
this.node[key] = node;
};
_proto.getPathLocation = function getPathLocation() {
var parts = [];
var path = this;
do {
var key = path.key;
if (path.inList) key = path.listKey + "[" + key + "]";
parts.unshift(key);
} while (path = path.parentPath);
return parts.join(".");
};
_proto.debug = function debug(message) {
if (!_debug.enabled) return;
_debug(this.getPathLocation() + " " + this.type + ": " + message);
};
_proto.toString = function toString() {
return (0, _generator().default)(this.node).code;
};
return NodePath;
}();
exports.default = NodePath;
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);
var _arr = t().TYPES;
var _loop2 = function _loop2() {
var type = _arr[_i];
var typeKey = "is" + type;
NodePath.prototype[typeKey] = function (opts) {
return t()[typeKey](this.node, opts);
};
NodePath.prototype["assert" + type] = function (opts) {
if (!this[typeKey](opts)) {
throw new TypeError("Expected node path of type " + type);
}
};
};
for (var _i = 0; _i < _arr.length; _i++) {
_loop2();
}
var _loop = function _loop(type) {
if (type[0] === "_") return "continue";
if (t().TYPES.indexOf(type) < 0) t().TYPES.push(type);
var virtualType = virtualTypes[type];
NodePath.prototype["is" + type] = function (opts) {
return virtualType.checkPath(this, opts);
};
};
for (var type in virtualTypes) {
var _ret = _loop(type);
if (_ret === "continue") continue;
}