replaceShorthandObjectMethod.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.default = replaceShorthandObjectMethod;
  4. var _babelTypes = require("babel-types");
  5. var t = _interopRequireWildcard(_babelTypes);
  6. var _util = require("./util");
  7. var util = _interopRequireWildcard(_util);
  8. 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)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
  9. // this function converts a shorthand object generator method into a normal
  10. // (non-shorthand) object property which is a generator function expression. for
  11. // example, this:
  12. //
  13. // var foo = {
  14. // *bar(baz) { return 5; }
  15. // }
  16. //
  17. // should be replaced with:
  18. //
  19. // var foo = {
  20. // bar: function*(baz) { return 5; }
  21. // }
  22. //
  23. // to do this, it clones the parameter array and the body of the object generator
  24. // method into a new FunctionExpression.
  25. //
  26. // this method can be passed any Function AST node path, and it will return
  27. // either:
  28. // a) the path that was passed in (iff the path did not need to be replaced) or
  29. // b) the path of the new FunctionExpression that was created as a replacement
  30. // (iff the path did need to be replaced)
  31. //
  32. // In either case, though, the caller can count on the fact that the return value
  33. // is a Function AST node path.
  34. //
  35. // If this function is called with an AST node path that is not a Function (or with an
  36. // argument that isn't an AST node path), it will throw an error.
  37. function replaceShorthandObjectMethod(path) {
  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. var parameters = path.node.params.map(function (param) {
  51. return t.cloneDeep(param);
  52. });
  53. var functionExpression = t.functionExpression(null, // id
  54. parameters, // params
  55. t.cloneDeep(path.node.body), // body
  56. path.node.generator, path.node.async);
  57. util.replaceWithOrRemove(path, t.objectProperty(t.cloneDeep(path.node.key), // key
  58. functionExpression, //value
  59. path.node.computed, // computed
  60. false // shorthand
  61. ));
  62. // path now refers to the ObjectProperty AST node path, but we want to return a
  63. // Function AST node path for the function expression we created. we know that
  64. // the FunctionExpression we just created is the value of the ObjectProperty,
  65. // so return the "value" path off of this path.
  66. return path.get("value");
  67. }