replaceShorthandObjectMethod.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. "use strict";
  2. exports.__esModule = true;
  3. exports["default"] = replaceShorthandObjectMethod;
  4. var util = _interopRequireWildcard(require("./util"));
  5. function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
  6. function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
  7. /**
  8. * Copyright (c) 2014-present, Facebook, Inc.
  9. *
  10. * This source code is licensed under the MIT license found in the
  11. * LICENSE file in the root directory of this source tree.
  12. */
  13. // this function converts a shorthand object generator method into a normal
  14. // (non-shorthand) object property which is a generator function expression. for
  15. // example, this:
  16. //
  17. // var foo = {
  18. // *bar(baz) { return 5; }
  19. // }
  20. //
  21. // should be replaced with:
  22. //
  23. // var foo = {
  24. // bar: function*(baz) { return 5; }
  25. // }
  26. //
  27. // to do this, it clones the parameter array and the body of the object generator
  28. // method into a new FunctionExpression.
  29. //
  30. // this method can be passed any Function AST node path, and it will return
  31. // either:
  32. // a) the path that was passed in (iff the path did not need to be replaced) or
  33. // b) the path of the new FunctionExpression that was created as a replacement
  34. // (iff the path did need to be replaced)
  35. //
  36. // In either case, though, the caller can count on the fact that the return value
  37. // is a Function AST node path.
  38. //
  39. // If this function is called with an AST node path that is not a Function (or with an
  40. // argument that isn't an AST node path), it will throw an error.
  41. function replaceShorthandObjectMethod(path) {
  42. var t = util.getTypes();
  43. if (!path.node || !t.isFunction(path.node)) {
  44. throw new Error("replaceShorthandObjectMethod can only be called on Function AST node paths.");
  45. } // this function only replaces shorthand object methods (called ObjectMethod
  46. // in Babel-speak).
  47. if (!t.isObjectMethod(path.node)) {
  48. return path;
  49. } // this function only replaces generators.
  50. if (!path.node.generator) {
  51. return path;
  52. }
  53. var parameters = path.node.params.map(function (param) {
  54. return t.cloneDeep(param);
  55. });
  56. var functionExpression = t.functionExpression(null, // id
  57. parameters, // params
  58. t.cloneDeep(path.node.body), // body
  59. path.node.generator, path.node.async);
  60. util.replaceWithOrRemove(path, t.objectProperty(t.cloneDeep(path.node.key), // key
  61. functionExpression, //value
  62. path.node.computed, // computed
  63. false // shorthand
  64. )); // path now refers to the ObjectProperty AST node path, but we want to return a
  65. // Function AST node path for the function expression we created. we know that
  66. // the FunctionExpression we just created is the value of the ObjectProperty,
  67. // so return the "value" path off of this path.
  68. return path.get("value");
  69. }