meta.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /**
  2. * Copyright (c) 2014, Facebook, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * https://raw.github.com/facebook/regenerator/master/LICENSE file. An
  7. * additional grant of patent rights can be found in the PATENTS file in
  8. * the same directory.
  9. */
  10. var assert = require("assert");
  11. var m = require("private").makeAccessor();
  12. var types = require("ast-types");
  13. var isArray = types.builtInTypes.array;
  14. var n = types.namedTypes;
  15. var hasOwn = Object.prototype.hasOwnProperty;
  16. function makePredicate(propertyName, knownTypes) {
  17. function onlyChildren(node) {
  18. n.Node.assert(node);
  19. // Assume no side effects until we find out otherwise.
  20. var result = false;
  21. function check(child) {
  22. if (result) {
  23. // Do nothing.
  24. } else if (isArray.check(child)) {
  25. child.some(check);
  26. } else if (n.Node.check(child)) {
  27. assert.strictEqual(result, false);
  28. result = predicate(child);
  29. }
  30. return result;
  31. }
  32. types.eachField(node, function(name, child) {
  33. check(child);
  34. });
  35. return result;
  36. }
  37. function predicate(node) {
  38. n.Node.assert(node);
  39. var meta = m(node);
  40. if (hasOwn.call(meta, propertyName))
  41. return meta[propertyName];
  42. // 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))
  45. return meta[propertyName] = false;
  46. if (hasOwn.call(knownTypes, node.type))
  47. return meta[propertyName] = true;
  48. return meta[propertyName] = onlyChildren(node);
  49. }
  50. predicate.onlyChildren = onlyChildren;
  51. return predicate;
  52. }
  53. var opaqueTypes = {
  54. FunctionExpression: true
  55. };
  56. // These types potentially have side effects regardless of what side
  57. // effects their subexpressions have.
  58. var sideEffectTypes = {
  59. CallExpression: true, // Anything could happen!
  60. ForInStatement: true, // Modifies the key variable.
  61. UnaryExpression: true, // Think delete.
  62. BinaryExpression: true, // Might invoke .toString() or .valueOf().
  63. AssignmentExpression: true, // Side-effecting by definition.
  64. UpdateExpression: true, // Updates are essentially assignments.
  65. NewExpression: true // Similar to CallExpression.
  66. };
  67. // These types are the direct cause of all leaps in control flow.
  68. var leapTypes = {
  69. YieldExpression: true,
  70. BreakStatement: true,
  71. ContinueStatement: true,
  72. ReturnStatement: true,
  73. ThrowStatement: true
  74. };
  75. // All leap types are also side effect types.
  76. for (var type in leapTypes) {
  77. if (hasOwn.call(leapTypes, type)) {
  78. sideEffectTypes[type] = leapTypes[type];
  79. }
  80. }
  81. exports.hasSideEffects = makePredicate("hasSideEffects", sideEffectTypes);
  82. exports.containsLeap = makePredicate("containsLeap", leapTypes);