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