util.js 960 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. let currentTypes = null;
  8. export function wrapWithTypes(types, fn) {
  9. return function (...args) {
  10. const oldTypes = currentTypes;
  11. currentTypes = types;
  12. try {
  13. return fn.apply(this, args);
  14. } finally {
  15. currentTypes = oldTypes;
  16. }
  17. };
  18. }
  19. export function getTypes() {
  20. return currentTypes;
  21. }
  22. export function runtimeProperty(name) {
  23. const t = getTypes();
  24. return t.memberExpression(
  25. t.identifier("regeneratorRuntime"),
  26. t.identifier(name),
  27. false
  28. );
  29. }
  30. export function isReference(path) {
  31. return path.isReferenced() || path.parentPath.isAssignmentExpression({ left: path.node });
  32. }
  33. export function replaceWithOrRemove(path, replacement) {
  34. if (replacement) {
  35. path.replaceWith(replacement)
  36. } else {
  37. path.remove();
  38. }
  39. }