replaceShorthandObjectMethod.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. import * as util from "./util";
  8. // this function converts a shorthand object generator method into a normal
  9. // (non-shorthand) object property which is a generator function expression. for
  10. // example, this:
  11. //
  12. // var foo = {
  13. // *bar(baz) { return 5; }
  14. // }
  15. //
  16. // should be replaced with:
  17. //
  18. // var foo = {
  19. // bar: function*(baz) { return 5; }
  20. // }
  21. //
  22. // to do this, it clones the parameter array and the body of the object generator
  23. // method into a new FunctionExpression.
  24. //
  25. // this method can be passed any Function AST node path, and it will return
  26. // either:
  27. // a) the path that was passed in (iff the path did not need to be replaced) or
  28. // b) the path of the new FunctionExpression that was created as a replacement
  29. // (iff the path did need to be replaced)
  30. //
  31. // In either case, though, the caller can count on the fact that the return value
  32. // is a Function AST node path.
  33. //
  34. // If this function is called with an AST node path that is not a Function (or with an
  35. // argument that isn't an AST node path), it will throw an error.
  36. export default function replaceShorthandObjectMethod(path) {
  37. const t = util.getTypes();
  38. if (!path.node || !t.isFunction(path.node)) {
  39. throw new Error("replaceShorthandObjectMethod can only be called on Function AST node paths.");
  40. }
  41. // this function only replaces shorthand object methods (called ObjectMethod
  42. // in Babel-speak).
  43. if (!t.isObjectMethod(path.node)) {
  44. return path;
  45. }
  46. // this function only replaces generators.
  47. if (!path.node.generator) {
  48. return path;
  49. }
  50. const parameters = path.node.params.map(function (param) {
  51. return t.cloneDeep(param);
  52. })
  53. const functionExpression = t.functionExpression(
  54. null, // id
  55. parameters, // params
  56. t.cloneDeep(path.node.body), // body
  57. path.node.generator,
  58. path.node.async
  59. );
  60. util.replaceWithOrRemove(path,
  61. t.objectProperty(
  62. t.cloneDeep(path.node.key), // key
  63. functionExpression, //value
  64. path.node.computed, // computed
  65. false // shorthand
  66. )
  67. );
  68. // path now refers to the ObjectProperty AST node path, but we want to return a
  69. // Function AST node path for the function expression we created. we know that
  70. // the FunctionExpression we just created is the value of the ObjectProperty,
  71. // so return the "value" path off of this path.
  72. return path.get("value");
  73. }