replaceShorthandObjectMethod.js 3.0 KB

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