meta.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. "use strict";
  2. var _assert = require("assert");
  3. var _assert2 = _interopRequireDefault(_assert);
  4. var _babelTypes = require("babel-types");
  5. var t = _interopRequireWildcard(_babelTypes);
  6. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
  7. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  8. var m = require("private").makeAccessor(); /**
  9. * Copyright (c) 2014, Facebook, Inc.
  10. * All rights reserved.
  11. *
  12. * This source code is licensed under the BSD-style license found in the
  13. * https://raw.github.com/facebook/regenerator/master/LICENSE file. An
  14. * additional grant of patent rights can be found in the PATENTS file in
  15. * the same directory.
  16. */
  17. var hasOwn = Object.prototype.hasOwnProperty;
  18. function makePredicate(propertyName, knownTypes) {
  19. function onlyChildren(node) {
  20. t.assertNode(node);
  21. // Assume no side effects until we find out otherwise.
  22. var 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. _assert2.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. t.assertNode(node);
  46. var meta = m(node);
  47. if (hasOwn.call(meta, propertyName)) return meta[propertyName];
  48. // Certain types are "opaque," which means they have no side
  49. // effects or leaps and we don't care about their subexpressions.
  50. if (hasOwn.call(opaqueTypes, node.type)) return meta[propertyName] = false;
  51. if (hasOwn.call(knownTypes, node.type)) return meta[propertyName] = true;
  52. return meta[propertyName] = onlyChildren(node);
  53. }
  54. predicate.onlyChildren = onlyChildren;
  55. return predicate;
  56. }
  57. var opaqueTypes = {
  58. FunctionExpression: true,
  59. ArrowFunctionExpression: true
  60. };
  61. // These types potentially have side effects regardless of what side
  62. // effects their subexpressions have.
  63. var sideEffectTypes = {
  64. CallExpression: true, // Anything could happen!
  65. ForInStatement: true, // Modifies the key variable.
  66. UnaryExpression: true, // Think delete.
  67. BinaryExpression: true, // Might invoke .toString() or .valueOf().
  68. AssignmentExpression: true, // Side-effecting by definition.
  69. UpdateExpression: true, // Updates are essentially assignments.
  70. NewExpression: true // Similar to CallExpression.
  71. };
  72. // These types are the direct cause of all leaps in control flow.
  73. var leapTypes = {
  74. YieldExpression: true,
  75. BreakStatement: true,
  76. ContinueStatement: true,
  77. ReturnStatement: true,
  78. ThrowStatement: true
  79. };
  80. // All leap types are also side effect types.
  81. for (var type in leapTypes) {
  82. if (hasOwn.call(leapTypes, type)) {
  83. sideEffectTypes[type] = leapTypes[type];
  84. }
  85. }
  86. exports.hasSideEffects = makePredicate("hasSideEffects", sideEffectTypes);
  87. exports.containsLeap = makePredicate("containsLeap", leapTypes);