lib/condenser/processors/node_modules/@babel/traverse/lib/path/evaluation.js in condenser-0.0.4 vs lib/condenser/processors/node_modules/@babel/traverse/lib/path/evaluation.js in condenser-0.0.5
- old
+ new
@@ -3,44 +3,48 @@
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.evaluateTruthy = evaluateTruthy;
exports.evaluate = evaluate;
-var VALID_CALLEES = ["String", "Number", "Math"];
-var INVALID_METHODS = ["random"];
+const VALID_CALLEES = ["String", "Number", "Math"];
+const INVALID_METHODS = ["random"];
function evaluateTruthy() {
- var res = this.evaluate();
+ const res = this.evaluate();
if (res.confident) return !!res.value;
}
function deopt(path, state) {
if (!state.confident) return;
state.deoptPath = path;
state.confident = false;
}
function evaluateCached(path, state) {
- var node = path.node;
- var seen = state.seen;
+ const {
+ node
+ } = path;
+ const {
+ seen
+ } = state;
if (seen.has(node)) {
- var existing = seen.get(node);
+ const existing = seen.get(node);
if (existing.resolved) {
return existing.value;
} else {
deopt(path, state);
return;
}
} else {
- var item = {
+ const item = {
resolved: false
};
seen.set(node, item);
- var val = _evaluate(path, state);
+ const val = _evaluate(path, state);
if (state.confident) {
item.resolved = true;
item.value = val;
}
@@ -49,14 +53,16 @@
}
}
function _evaluate(path, state) {
if (!state.confident) return;
- var node = path.node;
+ const {
+ node
+ } = path;
if (path.isSequenceExpression()) {
- var exprs = path.get("expressions");
+ const exprs = path.get("expressions");
return evaluateCached(exprs[exprs.length - 1], state);
}
if (path.isStringLiteral() || path.isNumericLiteral() || path.isBooleanLiteral()) {
return node.value;
@@ -69,21 +75,25 @@
if (path.isTemplateLiteral()) {
return evaluateQuasis(path, node.quasis, state);
}
if (path.isTaggedTemplateExpression() && path.get("tag").isMemberExpression()) {
- var object = path.get("tag.object");
- var name = object.node.name;
- var property = path.get("tag.property");
+ const object = path.get("tag.object");
+ const {
+ node: {
+ name
+ }
+ } = object;
+ const property = path.get("tag.property");
if (object.isIdentifier() && name === "String" && !path.scope.getBinding(name, true) && property.isIdentifier && property.node.name === "raw") {
return evaluateQuasis(path, node.quasi.quasis, state, true);
}
}
if (path.isConditionalExpression()) {
- var testResult = evaluateCached(path.get("test"), state);
+ const testResult = evaluateCached(path.get("test"), state);
if (!state.confident) return;
if (testResult) {
return evaluateCached(path.get("consequent"), state);
} else {
@@ -96,26 +106,25 @@
}
if (path.isMemberExpression() && !path.parentPath.isCallExpression({
callee: node
})) {
- var _property = path.get("property");
+ const property = path.get("property");
+ const object = path.get("object");
- var _object = path.get("object");
+ if (object.isLiteral() && property.isIdentifier()) {
+ const value = object.node.value;
+ const type = typeof value;
- if (_object.isLiteral() && _property.isIdentifier()) {
- var value = _object.node.value;
- var type = typeof value;
-
if (type === "number" || type === "string") {
- return value[_property.node.name];
+ return value[property.node.name];
}
}
}
if (path.isReferencedIdentifier()) {
- var binding = path.scope.getBinding(node.name);
+ const binding = path.scope.getBinding(node.name);
if (binding && binding.constantViolations.length > 0) {
return deopt(binding.path, state);
}
@@ -132,11 +141,11 @@
return binding ? deopt(binding.path, state) : Infinity;
} else if (node.name === "NaN") {
return binding ? deopt(binding.path, state) : NaN;
}
- var resolved = path.resolve();
+ const resolved = path.resolve();
if (resolved === path) {
return deopt(path, state);
} else {
return evaluateCached(resolved, state);
@@ -149,17 +158,17 @@
})) {
if (node.operator === "void") {
return undefined;
}
- var argument = path.get("argument");
+ const argument = path.get("argument");
if (node.operator === "typeof" && (argument.isFunction() || argument.isClass())) {
return "function";
}
- var arg = evaluateCached(argument, state);
+ const arg = evaluateCached(argument, state);
if (!state.confident) return;
switch (node.operator) {
case "!":
return !arg;
@@ -177,28 +186,16 @@
return typeof arg;
}
}
if (path.isArrayExpression()) {
- var arr = [];
- var elems = path.get("elements");
+ const arr = [];
+ const elems = path.get("elements");
- for (var _iterator = elems, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
- var _ref;
+ for (const elem of elems) {
+ const elemValue = elem.evaluate();
- if (_isArray) {
- if (_i >= _iterator.length) break;
- _ref = _iterator[_i++];
- } else {
- _i = _iterator.next();
- if (_i.done) break;
- _ref = _i.value;
- }
-
- var elem = _ref;
- var elemValue = elem.evaluate();
-
if (elemValue.confident) {
arr.push(elemValue.value);
} else {
return deopt(elem, state);
}
@@ -206,33 +203,20 @@
return arr;
}
if (path.isObjectExpression()) {
- var obj = {};
- var props = path.get("properties");
+ const obj = {};
+ const props = path.get("properties");
- for (var _iterator2 = props, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : _iterator2[Symbol.iterator]();;) {
- var _ref2;
-
- if (_isArray2) {
- if (_i2 >= _iterator2.length) break;
- _ref2 = _iterator2[_i2++];
- } else {
- _i2 = _iterator2.next();
- if (_i2.done) break;
- _ref2 = _i2.value;
- }
-
- var prop = _ref2;
-
+ for (const prop of props) {
if (prop.isObjectMethod() || prop.isSpreadElement()) {
return deopt(prop, state);
}
- var keyPath = prop.get("key");
- var key = keyPath;
+ const keyPath = prop.get("key");
+ let key = keyPath;
if (prop.node.computed) {
key = key.evaluate();
if (!key.confident) {
@@ -244,205 +228,174 @@
key = key.node.name;
} else {
key = key.node.value;
}
- var valuePath = prop.get("value");
+ const valuePath = prop.get("value");
+ let value = valuePath.evaluate();
- var _value = valuePath.evaluate();
-
- if (!_value.confident) {
+ if (!value.confident) {
return deopt(valuePath, state);
}
- _value = _value.value;
- obj[key] = _value;
+ value = value.value;
+ obj[key] = value;
}
return obj;
}
if (path.isLogicalExpression()) {
- var wasConfident = state.confident;
- var left = evaluateCached(path.get("left"), state);
- var leftConfident = state.confident;
+ const wasConfident = state.confident;
+ const left = evaluateCached(path.get("left"), state);
+ const leftConfident = state.confident;
state.confident = wasConfident;
- var right = evaluateCached(path.get("right"), state);
- var rightConfident = state.confident;
- state.confident = leftConfident && rightConfident;
+ const right = evaluateCached(path.get("right"), state);
+ const rightConfident = state.confident;
switch (node.operator) {
case "||":
- if (left && leftConfident) {
- state.confident = true;
- return left;
- }
-
+ state.confident = leftConfident && (!!left || rightConfident);
if (!state.confident) return;
return left || right;
case "&&":
- if (!left && leftConfident || !right && rightConfident) {
- state.confident = true;
- }
-
+ state.confident = leftConfident && (!left || rightConfident);
if (!state.confident) return;
return left && right;
}
}
if (path.isBinaryExpression()) {
- var _left = evaluateCached(path.get("left"), state);
-
+ const left = evaluateCached(path.get("left"), state);
if (!state.confident) return;
-
- var _right = evaluateCached(path.get("right"), state);
-
+ const right = evaluateCached(path.get("right"), state);
if (!state.confident) return;
switch (node.operator) {
case "-":
- return _left - _right;
+ return left - right;
case "+":
- return _left + _right;
+ return left + right;
case "/":
- return _left / _right;
+ return left / right;
case "*":
- return _left * _right;
+ return left * right;
case "%":
- return _left % _right;
+ return left % right;
case "**":
- return Math.pow(_left, _right);
+ return Math.pow(left, right);
case "<":
- return _left < _right;
+ return left < right;
case ">":
- return _left > _right;
+ return left > right;
case "<=":
- return _left <= _right;
+ return left <= right;
case ">=":
- return _left >= _right;
+ return left >= right;
case "==":
- return _left == _right;
+ return left == right;
case "!=":
- return _left != _right;
+ return left != right;
case "===":
- return _left === _right;
+ return left === right;
case "!==":
- return _left !== _right;
+ return left !== right;
case "|":
- return _left | _right;
+ return left | right;
case "&":
- return _left & _right;
+ return left & right;
case "^":
- return _left ^ _right;
+ return left ^ right;
case "<<":
- return _left << _right;
+ return left << right;
case ">>":
- return _left >> _right;
+ return left >> right;
case ">>>":
- return _left >>> _right;
+ return left >>> right;
}
}
if (path.isCallExpression()) {
- var callee = path.get("callee");
- var context;
- var func;
+ const callee = path.get("callee");
+ let context;
+ let func;
if (callee.isIdentifier() && !path.scope.getBinding(callee.node.name, true) && VALID_CALLEES.indexOf(callee.node.name) >= 0) {
func = global[node.callee.name];
}
if (callee.isMemberExpression()) {
- var _object2 = callee.get("object");
+ const object = callee.get("object");
+ const property = callee.get("property");
- var _property2 = callee.get("property");
-
- if (_object2.isIdentifier() && _property2.isIdentifier() && VALID_CALLEES.indexOf(_object2.node.name) >= 0 && INVALID_METHODS.indexOf(_property2.node.name) < 0) {
- context = global[_object2.node.name];
- func = context[_property2.node.name];
+ if (object.isIdentifier() && property.isIdentifier() && VALID_CALLEES.indexOf(object.node.name) >= 0 && INVALID_METHODS.indexOf(property.node.name) < 0) {
+ context = global[object.node.name];
+ func = context[property.node.name];
}
- if (_object2.isLiteral() && _property2.isIdentifier()) {
- var _type = typeof _object2.node.value;
+ if (object.isLiteral() && property.isIdentifier()) {
+ const type = typeof object.node.value;
- if (_type === "string" || _type === "number") {
- context = _object2.node.value;
- func = context[_property2.node.name];
+ if (type === "string" || type === "number") {
+ context = object.node.value;
+ func = context[property.node.name];
}
}
}
if (func) {
- var args = path.get("arguments").map(function (arg) {
- return evaluateCached(arg, state);
- });
+ const args = path.get("arguments").map(arg => evaluateCached(arg, state));
if (!state.confident) return;
return func.apply(context, args);
}
}
deopt(path, state);
}
-function evaluateQuasis(path, quasis, state, raw) {
- if (raw === void 0) {
- raw = false;
- }
+function evaluateQuasis(path, quasis, state, raw = false) {
+ let str = "";
+ let i = 0;
+ const exprs = path.get("expressions");
- var str = "";
- var i = 0;
- var exprs = path.get("expressions");
-
- for (var _iterator3 = quasis, _isArray3 = Array.isArray(_iterator3), _i3 = 0, _iterator3 = _isArray3 ? _iterator3 : _iterator3[Symbol.iterator]();;) {
- var _ref3;
-
- if (_isArray3) {
- if (_i3 >= _iterator3.length) break;
- _ref3 = _iterator3[_i3++];
- } else {
- _i3 = _iterator3.next();
- if (_i3.done) break;
- _ref3 = _i3.value;
- }
-
- var elem = _ref3;
+ for (const elem of quasis) {
if (!state.confident) break;
str += raw ? elem.value.raw : elem.value.cooked;
- var expr = exprs[i++];
+ const expr = exprs[i++];
if (expr) str += String(evaluateCached(expr, state));
}
if (!state.confident) return;
return str;
}
function evaluate() {
- var state = {
+ const state = {
confident: true,
deoptPath: null,
seen: new Map()
};
- var value = evaluateCached(this, state);
+ let value = evaluateCached(this, state);
if (!state.confident) value = undefined;
return {
confident: state.confident,
deopt: state.deoptPath,
value: value
\ No newline at end of file