evaluation.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. "use strict";
  2. exports.__esModule = true;
  3. var _typeof2 = require("babel-runtime/helpers/typeof");
  4. var _typeof3 = _interopRequireDefault(_typeof2);
  5. var _getIterator2 = require("babel-runtime/core-js/get-iterator");
  6. var _getIterator3 = _interopRequireDefault(_getIterator2);
  7. var _map = require("babel-runtime/core-js/map");
  8. var _map2 = _interopRequireDefault(_map);
  9. exports.evaluateTruthy = evaluateTruthy;
  10. exports.evaluate = evaluate;
  11. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  12. var VALID_CALLEES = ["String", "Number", "Math"];
  13. var INVALID_METHODS = ["random"];
  14. function evaluateTruthy() {
  15. var res = this.evaluate();
  16. if (res.confident) return !!res.value;
  17. }
  18. function evaluate() {
  19. var confident = true;
  20. var deoptPath = void 0;
  21. var seen = new _map2.default();
  22. function deopt(path) {
  23. if (!confident) return;
  24. deoptPath = path;
  25. confident = false;
  26. }
  27. var value = evaluate(this);
  28. if (!confident) value = undefined;
  29. return {
  30. confident: confident,
  31. deopt: deoptPath,
  32. value: value
  33. };
  34. function evaluate(path) {
  35. var node = path.node;
  36. if (seen.has(node)) {
  37. var existing = seen.get(node);
  38. if (existing.resolved) {
  39. return existing.value;
  40. } else {
  41. deopt(path);
  42. return;
  43. }
  44. } else {
  45. var item = { resolved: false };
  46. seen.set(node, item);
  47. var val = _evaluate(path);
  48. if (confident) {
  49. item.resolved = true;
  50. item.value = val;
  51. }
  52. return val;
  53. }
  54. }
  55. function _evaluate(path) {
  56. if (!confident) return;
  57. var node = path.node;
  58. if (path.isSequenceExpression()) {
  59. var exprs = path.get("expressions");
  60. return evaluate(exprs[exprs.length - 1]);
  61. }
  62. if (path.isStringLiteral() || path.isNumericLiteral() || path.isBooleanLiteral()) {
  63. return node.value;
  64. }
  65. if (path.isNullLiteral()) {
  66. return null;
  67. }
  68. if (path.isTemplateLiteral()) {
  69. var str = "";
  70. var i = 0;
  71. var _exprs = path.get("expressions");
  72. for (var _iterator = node.quasis, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : (0, _getIterator3.default)(_iterator);;) {
  73. var _ref;
  74. if (_isArray) {
  75. if (_i >= _iterator.length) break;
  76. _ref = _iterator[_i++];
  77. } else {
  78. _i = _iterator.next();
  79. if (_i.done) break;
  80. _ref = _i.value;
  81. }
  82. var elem = _ref;
  83. if (!confident) break;
  84. str += elem.value.cooked;
  85. var expr = _exprs[i++];
  86. if (expr) str += String(evaluate(expr));
  87. }
  88. if (!confident) return;
  89. return str;
  90. }
  91. if (path.isConditionalExpression()) {
  92. var testResult = evaluate(path.get("test"));
  93. if (!confident) return;
  94. if (testResult) {
  95. return evaluate(path.get("consequent"));
  96. } else {
  97. return evaluate(path.get("alternate"));
  98. }
  99. }
  100. if (path.isExpressionWrapper()) {
  101. return evaluate(path.get("expression"));
  102. }
  103. if (path.isMemberExpression() && !path.parentPath.isCallExpression({ callee: node })) {
  104. var property = path.get("property");
  105. var object = path.get("object");
  106. if (object.isLiteral() && property.isIdentifier()) {
  107. var _value = object.node.value;
  108. var type = typeof _value === "undefined" ? "undefined" : (0, _typeof3.default)(_value);
  109. if (type === "number" || type === "string") {
  110. return _value[property.node.name];
  111. }
  112. }
  113. }
  114. if (path.isReferencedIdentifier()) {
  115. var binding = path.scope.getBinding(node.name);
  116. if (binding && binding.constantViolations.length > 0) {
  117. return deopt(binding.path);
  118. }
  119. if (binding && path.node.start < binding.path.node.end) {
  120. return deopt(binding.path);
  121. }
  122. if (binding && binding.hasValue) {
  123. return binding.value;
  124. } else {
  125. if (node.name === "undefined") {
  126. return binding ? deopt(binding.path) : undefined;
  127. } else if (node.name === "Infinity") {
  128. return binding ? deopt(binding.path) : Infinity;
  129. } else if (node.name === "NaN") {
  130. return binding ? deopt(binding.path) : NaN;
  131. }
  132. var resolved = path.resolve();
  133. if (resolved === path) {
  134. return deopt(path);
  135. } else {
  136. return evaluate(resolved);
  137. }
  138. }
  139. }
  140. if (path.isUnaryExpression({ prefix: true })) {
  141. if (node.operator === "void") {
  142. return undefined;
  143. }
  144. var argument = path.get("argument");
  145. if (node.operator === "typeof" && (argument.isFunction() || argument.isClass())) {
  146. return "function";
  147. }
  148. var arg = evaluate(argument);
  149. if (!confident) return;
  150. switch (node.operator) {
  151. case "!":
  152. return !arg;
  153. case "+":
  154. return +arg;
  155. case "-":
  156. return -arg;
  157. case "~":
  158. return ~arg;
  159. case "typeof":
  160. return typeof arg === "undefined" ? "undefined" : (0, _typeof3.default)(arg);
  161. }
  162. }
  163. if (path.isArrayExpression()) {
  164. var arr = [];
  165. var elems = path.get("elements");
  166. for (var _iterator2 = elems, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : (0, _getIterator3.default)(_iterator2);;) {
  167. var _ref2;
  168. if (_isArray2) {
  169. if (_i2 >= _iterator2.length) break;
  170. _ref2 = _iterator2[_i2++];
  171. } else {
  172. _i2 = _iterator2.next();
  173. if (_i2.done) break;
  174. _ref2 = _i2.value;
  175. }
  176. var _elem = _ref2;
  177. _elem = _elem.evaluate();
  178. if (_elem.confident) {
  179. arr.push(_elem.value);
  180. } else {
  181. return deopt(_elem);
  182. }
  183. }
  184. return arr;
  185. }
  186. if (path.isObjectExpression()) {
  187. var obj = {};
  188. var props = path.get("properties");
  189. for (var _iterator3 = props, _isArray3 = Array.isArray(_iterator3), _i3 = 0, _iterator3 = _isArray3 ? _iterator3 : (0, _getIterator3.default)(_iterator3);;) {
  190. var _ref3;
  191. if (_isArray3) {
  192. if (_i3 >= _iterator3.length) break;
  193. _ref3 = _iterator3[_i3++];
  194. } else {
  195. _i3 = _iterator3.next();
  196. if (_i3.done) break;
  197. _ref3 = _i3.value;
  198. }
  199. var prop = _ref3;
  200. if (prop.isObjectMethod() || prop.isSpreadProperty()) {
  201. return deopt(prop);
  202. }
  203. var keyPath = prop.get("key");
  204. var key = keyPath;
  205. if (prop.node.computed) {
  206. key = key.evaluate();
  207. if (!key.confident) {
  208. return deopt(keyPath);
  209. }
  210. key = key.value;
  211. } else if (key.isIdentifier()) {
  212. key = key.node.name;
  213. } else {
  214. key = key.node.value;
  215. }
  216. var valuePath = prop.get("value");
  217. var _value2 = valuePath.evaluate();
  218. if (!_value2.confident) {
  219. return deopt(valuePath);
  220. }
  221. _value2 = _value2.value;
  222. obj[key] = _value2;
  223. }
  224. return obj;
  225. }
  226. if (path.isLogicalExpression()) {
  227. var wasConfident = confident;
  228. var left = evaluate(path.get("left"));
  229. var leftConfident = confident;
  230. confident = wasConfident;
  231. var right = evaluate(path.get("right"));
  232. var rightConfident = confident;
  233. confident = leftConfident && rightConfident;
  234. switch (node.operator) {
  235. case "||":
  236. if (left && leftConfident) {
  237. confident = true;
  238. return left;
  239. }
  240. if (!confident) return;
  241. return left || right;
  242. case "&&":
  243. if (!left && leftConfident || !right && rightConfident) {
  244. confident = true;
  245. }
  246. if (!confident) return;
  247. return left && right;
  248. }
  249. }
  250. if (path.isBinaryExpression()) {
  251. var _left = evaluate(path.get("left"));
  252. if (!confident) return;
  253. var _right = evaluate(path.get("right"));
  254. if (!confident) return;
  255. switch (node.operator) {
  256. case "-":
  257. return _left - _right;
  258. case "+":
  259. return _left + _right;
  260. case "/":
  261. return _left / _right;
  262. case "*":
  263. return _left * _right;
  264. case "%":
  265. return _left % _right;
  266. case "**":
  267. return Math.pow(_left, _right);
  268. case "<":
  269. return _left < _right;
  270. case ">":
  271. return _left > _right;
  272. case "<=":
  273. return _left <= _right;
  274. case ">=":
  275. return _left >= _right;
  276. case "==":
  277. return _left == _right;
  278. case "!=":
  279. return _left != _right;
  280. case "===":
  281. return _left === _right;
  282. case "!==":
  283. return _left !== _right;
  284. case "|":
  285. return _left | _right;
  286. case "&":
  287. return _left & _right;
  288. case "^":
  289. return _left ^ _right;
  290. case "<<":
  291. return _left << _right;
  292. case ">>":
  293. return _left >> _right;
  294. case ">>>":
  295. return _left >>> _right;
  296. }
  297. }
  298. if (path.isCallExpression()) {
  299. var callee = path.get("callee");
  300. var context = void 0;
  301. var func = void 0;
  302. if (callee.isIdentifier() && !path.scope.getBinding(callee.node.name, true) && VALID_CALLEES.indexOf(callee.node.name) >= 0) {
  303. func = global[node.callee.name];
  304. }
  305. if (callee.isMemberExpression()) {
  306. var _object = callee.get("object");
  307. var _property = callee.get("property");
  308. if (_object.isIdentifier() && _property.isIdentifier() && VALID_CALLEES.indexOf(_object.node.name) >= 0 && INVALID_METHODS.indexOf(_property.node.name) < 0) {
  309. context = global[_object.node.name];
  310. func = context[_property.node.name];
  311. }
  312. if (_object.isLiteral() && _property.isIdentifier()) {
  313. var _type = (0, _typeof3.default)(_object.node.value);
  314. if (_type === "string" || _type === "number") {
  315. context = _object.node.value;
  316. func = context[_property.node.name];
  317. }
  318. }
  319. }
  320. if (func) {
  321. var args = path.get("arguments").map(evaluate);
  322. if (!confident) return;
  323. return func.apply(context, args);
  324. }
  325. }
  326. deopt(path);
  327. }
  328. }