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