meta.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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); // Assume no side effects until we find out otherwise.
  23. var result = false;
  24. function check(child) {
  25. if (result) {// Do nothing.
  26. } else if (Array.isArray(child)) {
  27. child.some(check);
  28. } else if (t.isNode(child)) {
  29. _assert["default"].strictEqual(result, false);
  30. result = predicate(child);
  31. }
  32. return result;
  33. }
  34. var keys = t.VISITOR_KEYS[node.type];
  35. if (keys) {
  36. for (var i = 0; i < keys.length; i++) {
  37. var key = keys[i];
  38. var child = node[key];
  39. check(child);
  40. }
  41. }
  42. return result;
  43. }
  44. function predicate(node) {
  45. (0, _util.getTypes)().assertNode(node);
  46. var meta = m(node);
  47. if (hasOwn.call(meta, propertyName)) return meta[propertyName]; // Certain types are "opaque," which means they have no side
  48. // effects or leaps and we don't care about their subexpressions.
  49. if (hasOwn.call(opaqueTypes, node.type)) return meta[propertyName] = false;
  50. if (hasOwn.call(knownTypes, node.type)) return meta[propertyName] = true;
  51. return meta[propertyName] = onlyChildren(node);
  52. }
  53. predicate.onlyChildren = onlyChildren;
  54. return predicate;
  55. }
  56. var opaqueTypes = {
  57. FunctionExpression: true,
  58. ArrowFunctionExpression: true
  59. }; // These types potentially have side effects regardless of what side
  60. // effects their subexpressions have.
  61. var sideEffectTypes = {
  62. CallExpression: true,
  63. // Anything could happen!
  64. ForInStatement: true,
  65. // Modifies the key variable.
  66. UnaryExpression: true,
  67. // Think delete.
  68. BinaryExpression: true,
  69. // Might invoke .toString() or .valueOf().
  70. AssignmentExpression: true,
  71. // Side-effecting by definition.
  72. UpdateExpression: true,
  73. // Updates are essentially assignments.
  74. NewExpression: true // Similar to CallExpression.
  75. }; // These types are the direct cause of all leaps in control flow.
  76. var leapTypes = {
  77. YieldExpression: true,
  78. BreakStatement: true,
  79. ContinueStatement: true,
  80. ReturnStatement: true,
  81. ThrowStatement: true
  82. }; // All leap types are also side effect types.
  83. for (var type in leapTypes) {
  84. if (hasOwn.call(leapTypes, type)) {
  85. sideEffectTypes[type] = leapTypes[type];
  86. }
  87. }
  88. exports.hasSideEffects = makePredicate("hasSideEffects", sideEffectTypes);
  89. exports.containsLeap = makePredicate("containsLeap", leapTypes);