source: trip-planner-front/node_modules/@babel/traverse/lib/path/introspection.js@ 6a80231

Last change on this file since 6a80231 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 11.2 KB
RevLine 
[6a3a178]1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.matchesPattern = matchesPattern;
7exports.has = has;
8exports.isStatic = isStatic;
9exports.isnt = isnt;
10exports.equals = equals;
11exports.isNodeType = isNodeType;
12exports.canHaveVariableDeclarationOrExpression = canHaveVariableDeclarationOrExpression;
13exports.canSwapBetweenExpressionAndStatement = canSwapBetweenExpressionAndStatement;
14exports.isCompletionRecord = isCompletionRecord;
15exports.isStatementOrBlock = isStatementOrBlock;
16exports.referencesImport = referencesImport;
17exports.getSource = getSource;
18exports.willIMaybeExecuteBefore = willIMaybeExecuteBefore;
19exports._guessExecutionStatusRelativeTo = _guessExecutionStatusRelativeTo;
20exports._guessExecutionStatusRelativeToDifferentFunctions = _guessExecutionStatusRelativeToDifferentFunctions;
21exports.resolve = resolve;
22exports._resolve = _resolve;
23exports.isConstantExpression = isConstantExpression;
24exports.isInStrictMode = isInStrictMode;
25exports.is = void 0;
26
27var _t = require("@babel/types");
28
29const {
30 STATEMENT_OR_BLOCK_KEYS,
31 VISITOR_KEYS,
32 isBlockStatement,
33 isExpression,
34 isIdentifier,
35 isLiteral,
36 isStringLiteral,
37 isType,
38 matchesPattern: _matchesPattern
39} = _t;
40
41function matchesPattern(pattern, allowPartial) {
42 return _matchesPattern(this.node, pattern, allowPartial);
43}
44
45function has(key) {
46 const val = this.node && this.node[key];
47
48 if (val && Array.isArray(val)) {
49 return !!val.length;
50 } else {
51 return !!val;
52 }
53}
54
55function isStatic() {
56 return this.scope.isStatic(this.node);
57}
58
59const is = has;
60exports.is = is;
61
62function isnt(key) {
63 return !this.has(key);
64}
65
66function equals(key, value) {
67 return this.node[key] === value;
68}
69
70function isNodeType(type) {
71 return isType(this.type, type);
72}
73
74function canHaveVariableDeclarationOrExpression() {
75 return (this.key === "init" || this.key === "left") && this.parentPath.isFor();
76}
77
78function canSwapBetweenExpressionAndStatement(replacement) {
79 if (this.key !== "body" || !this.parentPath.isArrowFunctionExpression()) {
80 return false;
81 }
82
83 if (this.isExpression()) {
84 return isBlockStatement(replacement);
85 } else if (this.isBlockStatement()) {
86 return isExpression(replacement);
87 }
88
89 return false;
90}
91
92function isCompletionRecord(allowInsideFunction) {
93 let path = this;
94 let first = true;
95
96 do {
97 const container = path.container;
98
99 if (path.isFunction() && !first) {
100 return !!allowInsideFunction;
101 }
102
103 first = false;
104
105 if (Array.isArray(container) && path.key !== container.length - 1) {
106 return false;
107 }
108 } while ((path = path.parentPath) && !path.isProgram());
109
110 return true;
111}
112
113function isStatementOrBlock() {
114 if (this.parentPath.isLabeledStatement() || isBlockStatement(this.container)) {
115 return false;
116 } else {
117 return STATEMENT_OR_BLOCK_KEYS.includes(this.key);
118 }
119}
120
121function referencesImport(moduleSource, importName) {
122 if (!this.isReferencedIdentifier()) {
123 if ((this.isMemberExpression() || this.isOptionalMemberExpression()) && (this.node.computed ? isStringLiteral(this.node.property, {
124 value: importName
125 }) : this.node.property.name === importName)) {
126 const object = this.get("object");
127 return object.isReferencedIdentifier() && object.referencesImport(moduleSource, "*");
128 }
129
130 return false;
131 }
132
133 const binding = this.scope.getBinding(this.node.name);
134 if (!binding || binding.kind !== "module") return false;
135 const path = binding.path;
136 const parent = path.parentPath;
137 if (!parent.isImportDeclaration()) return false;
138
139 if (parent.node.source.value === moduleSource) {
140 if (!importName) return true;
141 } else {
142 return false;
143 }
144
145 if (path.isImportDefaultSpecifier() && importName === "default") {
146 return true;
147 }
148
149 if (path.isImportNamespaceSpecifier() && importName === "*") {
150 return true;
151 }
152
153 if (path.isImportSpecifier() && isIdentifier(path.node.imported, {
154 name: importName
155 })) {
156 return true;
157 }
158
159 return false;
160}
161
162function getSource() {
163 const node = this.node;
164
165 if (node.end) {
166 const code = this.hub.getCode();
167 if (code) return code.slice(node.start, node.end);
168 }
169
170 return "";
171}
172
173function willIMaybeExecuteBefore(target) {
174 return this._guessExecutionStatusRelativeTo(target) !== "after";
175}
176
177function getOuterFunction(path) {
178 return (path.scope.getFunctionParent() || path.scope.getProgramParent()).path;
179}
180
181function isExecutionUncertain(type, key) {
182 switch (type) {
183 case "LogicalExpression":
184 return key === "right";
185
186 case "ConditionalExpression":
187 case "IfStatement":
188 return key === "consequent" || key === "alternate";
189
190 case "WhileStatement":
191 case "DoWhileStatement":
192 case "ForInStatement":
193 case "ForOfStatement":
194 return key === "body";
195
196 case "ForStatement":
197 return key === "body" || key === "update";
198
199 case "SwitchStatement":
200 return key === "cases";
201
202 case "TryStatement":
203 return key === "handler";
204
205 case "AssignmentPattern":
206 return key === "right";
207
208 case "OptionalMemberExpression":
209 return key === "property";
210
211 case "OptionalCallExpression":
212 return key === "arguments";
213
214 default:
215 return false;
216 }
217}
218
219function isExecutionUncertainInList(paths, maxIndex) {
220 for (let i = 0; i < maxIndex; i++) {
221 const path = paths[i];
222
223 if (isExecutionUncertain(path.parent.type, path.parentKey)) {
224 return true;
225 }
226 }
227
228 return false;
229}
230
231function _guessExecutionStatusRelativeTo(target) {
232 const funcParent = {
233 this: getOuterFunction(this),
234 target: getOuterFunction(target)
235 };
236
237 if (funcParent.target.node !== funcParent.this.node) {
238 return this._guessExecutionStatusRelativeToDifferentFunctions(funcParent.target);
239 }
240
241 const paths = {
242 target: target.getAncestry(),
243 this: this.getAncestry()
244 };
245 if (paths.target.indexOf(this) >= 0) return "after";
246 if (paths.this.indexOf(target) >= 0) return "before";
247 let commonPath;
248 const commonIndex = {
249 target: 0,
250 this: 0
251 };
252
253 while (!commonPath && commonIndex.this < paths.this.length) {
254 const path = paths.this[commonIndex.this];
255 commonIndex.target = paths.target.indexOf(path);
256
257 if (commonIndex.target >= 0) {
258 commonPath = path;
259 } else {
260 commonIndex.this++;
261 }
262 }
263
264 if (!commonPath) {
265 throw new Error("Internal Babel error - The two compared nodes" + " don't appear to belong to the same program.");
266 }
267
268 if (isExecutionUncertainInList(paths.this, commonIndex.this - 1) || isExecutionUncertainInList(paths.target, commonIndex.target - 1)) {
269 return "unknown";
270 }
271
272 const divergence = {
273 this: paths.this[commonIndex.this - 1],
274 target: paths.target[commonIndex.target - 1]
275 };
276
277 if (divergence.target.listKey && divergence.this.listKey && divergence.target.container === divergence.this.container) {
278 return divergence.target.key > divergence.this.key ? "before" : "after";
279 }
280
281 const keys = VISITOR_KEYS[commonPath.type];
282 const keyPosition = {
283 this: keys.indexOf(divergence.this.parentKey),
284 target: keys.indexOf(divergence.target.parentKey)
285 };
286 return keyPosition.target > keyPosition.this ? "before" : "after";
287}
288
289const executionOrderCheckedNodes = new WeakSet();
290
291function _guessExecutionStatusRelativeToDifferentFunctions(target) {
292 if (!target.isFunctionDeclaration() || target.parentPath.isExportDeclaration()) {
293 return "unknown";
294 }
295
296 const binding = target.scope.getBinding(target.node.id.name);
297 if (!binding.references) return "before";
298 const referencePaths = binding.referencePaths;
299 let allStatus;
300
301 for (const path of referencePaths) {
302 const childOfFunction = !!path.find(path => path.node === target.node);
303 if (childOfFunction) continue;
304
305 if (path.key !== "callee" || !path.parentPath.isCallExpression()) {
306 return "unknown";
307 }
308
309 if (executionOrderCheckedNodes.has(path.node)) continue;
310 executionOrderCheckedNodes.add(path.node);
311
312 const status = this._guessExecutionStatusRelativeTo(path);
313
314 executionOrderCheckedNodes.delete(path.node);
315
316 if (allStatus && allStatus !== status) {
317 return "unknown";
318 } else {
319 allStatus = status;
320 }
321 }
322
323 return allStatus;
324}
325
326function resolve(dangerous, resolved) {
327 return this._resolve(dangerous, resolved) || this;
328}
329
330function _resolve(dangerous, resolved) {
331 if (resolved && resolved.indexOf(this) >= 0) return;
332 resolved = resolved || [];
333 resolved.push(this);
334
335 if (this.isVariableDeclarator()) {
336 if (this.get("id").isIdentifier()) {
337 return this.get("init").resolve(dangerous, resolved);
338 } else {}
339 } else if (this.isReferencedIdentifier()) {
340 const binding = this.scope.getBinding(this.node.name);
341 if (!binding) return;
342 if (!binding.constant) return;
343 if (binding.kind === "module") return;
344
345 if (binding.path !== this) {
346 const ret = binding.path.resolve(dangerous, resolved);
347 if (this.find(parent => parent.node === ret.node)) return;
348 return ret;
349 }
350 } else if (this.isTypeCastExpression()) {
351 return this.get("expression").resolve(dangerous, resolved);
352 } else if (dangerous && this.isMemberExpression()) {
353 const targetKey = this.toComputedKey();
354 if (!isLiteral(targetKey)) return;
355 const targetName = targetKey.value;
356 const target = this.get("object").resolve(dangerous, resolved);
357
358 if (target.isObjectExpression()) {
359 const props = target.get("properties");
360
361 for (const prop of props) {
362 if (!prop.isProperty()) continue;
363 const key = prop.get("key");
364 let match = prop.isnt("computed") && key.isIdentifier({
365 name: targetName
366 });
367 match = match || key.isLiteral({
368 value: targetName
369 });
370 if (match) return prop.get("value").resolve(dangerous, resolved);
371 }
372 } else if (target.isArrayExpression() && !isNaN(+targetName)) {
373 const elems = target.get("elements");
374 const elem = elems[targetName];
375 if (elem) return elem.resolve(dangerous, resolved);
376 }
377 }
378}
379
380function isConstantExpression() {
381 if (this.isIdentifier()) {
382 const binding = this.scope.getBinding(this.node.name);
383 if (!binding) return false;
384 return binding.constant;
385 }
386
387 if (this.isLiteral()) {
388 if (this.isRegExpLiteral()) {
389 return false;
390 }
391
392 if (this.isTemplateLiteral()) {
393 return this.get("expressions").every(expression => expression.isConstantExpression());
394 }
395
396 return true;
397 }
398
399 if (this.isUnaryExpression()) {
400 if (this.node.operator !== "void") {
401 return false;
402 }
403
404 return this.get("argument").isConstantExpression();
405 }
406
407 if (this.isBinaryExpression()) {
408 return this.get("left").isConstantExpression() && this.get("right").isConstantExpression();
409 }
410
411 return false;
412}
413
414function isInStrictMode() {
415 const start = this.isProgram() ? this : this.parentPath;
416 const strictParent = start.find(path => {
417 if (path.isProgram({
418 sourceType: "module"
419 })) return true;
420 if (path.isClass()) return true;
421 if (!path.isProgram() && !path.isFunction()) return false;
422
423 if (path.isArrowFunctionExpression() && !path.get("body").isBlockStatement()) {
424 return false;
425 }
426
427 const body = path.isFunction() ? path.node.body : path.node;
428
429 for (const directive of body.directives) {
430 if (directive.value.value === "use strict") {
431 return true;
432 }
433 }
434 });
435 return !!strictParent;
436}
Note: See TracBrowser for help on using the repository browser.