meta.js 2.9 KB

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