util.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.getTypes = getTypes;
  4. exports.isReference = isReference;
  5. exports.replaceWithOrRemove = replaceWithOrRemove;
  6. exports.runtimeProperty = runtimeProperty;
  7. exports.wrapWithTypes = wrapWithTypes;
  8. /**
  9. * Copyright (c) 2014-present, Facebook, Inc.
  10. *
  11. * This source code is licensed under the MIT license found in the
  12. * LICENSE file in the root directory of this source tree.
  13. */
  14. var currentTypes = null;
  15. function wrapWithTypes(types, fn) {
  16. return function () {
  17. var oldTypes = currentTypes;
  18. currentTypes = types;
  19. try {
  20. for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
  21. args[_key] = arguments[_key];
  22. }
  23. return fn.apply(this, args);
  24. } finally {
  25. currentTypes = oldTypes;
  26. }
  27. };
  28. }
  29. function getTypes() {
  30. return currentTypes;
  31. }
  32. function runtimeProperty(name) {
  33. var t = getTypes();
  34. return t.memberExpression(t.identifier("regeneratorRuntime"), t.identifier(name), false);
  35. }
  36. function isReference(path) {
  37. return path.isReferenced() || path.parentPath.isAssignmentExpression({
  38. left: path.node
  39. });
  40. }
  41. function replaceWithOrRemove(path, replacement) {
  42. if (replacement) {
  43. path.replaceWith(replacement);
  44. } else {
  45. path.remove();
  46. }
  47. }