UNPKG

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