meta.js 2.8 KB

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