1 | "use strict";
|
---|
2 |
|
---|
3 | var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
---|
4 | var _assert = _interopRequireDefault(require("assert"));
|
---|
5 | var _util = require("./util.js");
|
---|
6 | /**
|
---|
7 | * Copyright (c) 2014-present, Facebook, Inc.
|
---|
8 | *
|
---|
9 | * This source code is licensed under the MIT license found in the
|
---|
10 | * LICENSE file in the root directory of this source tree.
|
---|
11 | */
|
---|
12 |
|
---|
13 | var mMap = new WeakMap();
|
---|
14 | function m(node) {
|
---|
15 | if (!mMap.has(node)) {
|
---|
16 | mMap.set(node, {});
|
---|
17 | }
|
---|
18 | return mMap.get(node);
|
---|
19 | }
|
---|
20 | var hasOwn = Object.prototype.hasOwnProperty;
|
---|
21 | function makePredicate(propertyName, knownTypes) {
|
---|
22 | function onlyChildren(node) {
|
---|
23 | var t = (0, _util.getTypes)();
|
---|
24 | t.assertNode(node);
|
---|
25 |
|
---|
26 | // Assume no side effects until we find out otherwise.
|
---|
27 | var result = false;
|
---|
28 | function check(child) {
|
---|
29 | if (result) {
|
---|
30 | // Do nothing.
|
---|
31 | } else if (Array.isArray(child)) {
|
---|
32 | child.some(check);
|
---|
33 | } else if (t.isNode(child)) {
|
---|
34 | _assert["default"].strictEqual(result, false);
|
---|
35 | result = predicate(child);
|
---|
36 | }
|
---|
37 | return result;
|
---|
38 | }
|
---|
39 | var keys = t.VISITOR_KEYS[node.type];
|
---|
40 | if (keys) {
|
---|
41 | for (var i = 0; i < keys.length; i++) {
|
---|
42 | var key = keys[i];
|
---|
43 | var child = node[key];
|
---|
44 | check(child);
|
---|
45 | }
|
---|
46 | }
|
---|
47 | return result;
|
---|
48 | }
|
---|
49 | function predicate(node) {
|
---|
50 | (0, _util.getTypes)().assertNode(node);
|
---|
51 | var meta = m(node);
|
---|
52 | if (hasOwn.call(meta, propertyName)) return meta[propertyName];
|
---|
53 |
|
---|
54 | // Certain types are "opaque," which means they have no side
|
---|
55 | // effects or leaps and we don't care about their subexpressions.
|
---|
56 | if (hasOwn.call(opaqueTypes, node.type)) return meta[propertyName] = false;
|
---|
57 | if (hasOwn.call(knownTypes, node.type)) return meta[propertyName] = true;
|
---|
58 | return meta[propertyName] = onlyChildren(node);
|
---|
59 | }
|
---|
60 | predicate.onlyChildren = onlyChildren;
|
---|
61 | return predicate;
|
---|
62 | }
|
---|
63 | var opaqueTypes = {
|
---|
64 | FunctionExpression: true,
|
---|
65 | ArrowFunctionExpression: true
|
---|
66 | };
|
---|
67 |
|
---|
68 | // These types potentially have side effects regardless of what side
|
---|
69 | // effects their subexpressions have.
|
---|
70 | var sideEffectTypes = {
|
---|
71 | CallExpression: true,
|
---|
72 | // Anything could happen!
|
---|
73 | ForInStatement: true,
|
---|
74 | // Modifies the key variable.
|
---|
75 | UnaryExpression: true,
|
---|
76 | // Think delete.
|
---|
77 | BinaryExpression: true,
|
---|
78 | // Might invoke .toString() or .valueOf().
|
---|
79 | AssignmentExpression: true,
|
---|
80 | // Side-effecting by definition.
|
---|
81 | UpdateExpression: true,
|
---|
82 | // Updates are essentially assignments.
|
---|
83 | NewExpression: true // Similar to CallExpression.
|
---|
84 | };
|
---|
85 |
|
---|
86 | // These types are the direct cause of all leaps in control flow.
|
---|
87 | var leapTypes = {
|
---|
88 | YieldExpression: true,
|
---|
89 | BreakStatement: true,
|
---|
90 | ContinueStatement: true,
|
---|
91 | ReturnStatement: true,
|
---|
92 | ThrowStatement: true
|
---|
93 | };
|
---|
94 |
|
---|
95 | // All leap types are also side effect types.
|
---|
96 | for (var type in leapTypes) {
|
---|
97 | if (hasOwn.call(leapTypes, type)) {
|
---|
98 | sideEffectTypes[type] = leapTypes[type];
|
---|
99 | }
|
---|
100 | }
|
---|
101 | exports.hasSideEffects = makePredicate("hasSideEffects", sideEffectTypes);
|
---|
102 | exports.containsLeap = makePredicate("containsLeap", leapTypes); |
---|