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