99 lines
2.7 KiB
JavaScript
99 lines
2.7 KiB
JavaScript
/**
|
|
* Copyright (c) 2014, Facebook, Inc.
|
|
* All rights reserved.
|
|
*
|
|
* This source code is licensed under the BSD-style license found in the
|
|
* https://raw.github.com/facebook/regenerator/master/LICENSE file. An
|
|
* additional grant of patent rights can be found in the PATENTS file in
|
|
* the same directory.
|
|
*/
|
|
|
|
var assert = require("assert");
|
|
var types = require("ast-types");
|
|
var m = require("private").makeAccessor();
|
|
var _ = require("lodash");
|
|
|
|
var isArray = types.builtInTypes.array;
|
|
var n = types.namedTypes;
|
|
|
|
function makePredicate(propertyName, knownTypes) {
|
|
function onlyChildren(node) {
|
|
n.Node.check(node);
|
|
|
|
// Assume no side effects until we find out otherwise.
|
|
var result = false;
|
|
|
|
function check(child) {
|
|
if (result) {
|
|
// Do nothing.
|
|
} else if (isArray.check(child)) {
|
|
child.some(check);
|
|
} else if (n.Node.check(child)) {
|
|
assert.strictEqual(result, false);
|
|
result = predicate(child);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
types.eachField(node, function (name, child) {
|
|
check(child);
|
|
});
|
|
|
|
return result;
|
|
}
|
|
|
|
function predicate(node) {
|
|
n.Node.check(node);
|
|
|
|
var meta = m(node);
|
|
if (_.has(meta, propertyName)) return meta[propertyName];
|
|
|
|
// Certain types are "opaque," which means they have no side
|
|
// effects or leaps and we don't care about their subexpressions.
|
|
if (_.has(opaqueTypes, node.type)) return meta[propertyName] = false;
|
|
|
|
if (_.has(knownTypes, node.type)) return meta[propertyName] = true;
|
|
|
|
return meta[propertyName] = onlyChildren(node);
|
|
}
|
|
|
|
predicate.onlyChildren = onlyChildren;
|
|
|
|
return predicate;
|
|
}
|
|
|
|
var opaqueTypes = {
|
|
FunctionExpression: true
|
|
};
|
|
|
|
// These types potentially have side effects regardless of what side
|
|
// effects their subexpressions have.
|
|
var sideEffectTypes = {
|
|
CallExpression: true, // Anything could happen!
|
|
ForInStatement: true, // Modifies the key variable.
|
|
UnaryExpression: true, // Think delete.
|
|
BinaryExpression: true, // Might invoke .toString() or .valueOf().
|
|
AssignmentExpression: true, // Side-effecting by definition.
|
|
UpdateExpression: true, // Updates are essentially assignments.
|
|
NewExpression: true // Similar to CallExpression.
|
|
};
|
|
|
|
// These types are the direct cause of all leaps in control flow.
|
|
var leapTypes = {
|
|
YieldExpression: true,
|
|
BreakStatement: true,
|
|
ContinueStatement: true,
|
|
ReturnStatement: true,
|
|
ThrowStatement: true
|
|
};
|
|
|
|
// All leap types are also side effect types.
|
|
for (var type in leapTypes) {
|
|
if (_.has(leapTypes, type)) {
|
|
sideEffectTypes[type] = leapTypes[type];
|
|
}
|
|
}
|
|
|
|
exports.hasSideEffects = makePredicate("hasSideEffects", sideEffectTypes);
|
|
exports.containsLeap = makePredicate("containsLeap", leapTypes);
|