UNPKG

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