You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
2.9 KiB

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