replaceShorthandObjectMethod.js 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. }
  46. // this function only replaces shorthand object methods (called ObjectMethod
  47. // in Babel-speak).
  48. if (!t.isObjectMethod(path.node)) {
  49. return path;
  50. }
  51. // this function only replaces generators.
  52. if (!path.node.generator) {
  53. return path;
  54. }
  55. var parameters = path.node.params.map(function (param) {
  56. return t.cloneDeep(param);
  57. });
  58. var functionExpression = t.functionExpression(null,
  59. // id
  60. parameters,
  61. // params
  62. t.cloneDeep(path.node.body),
  63. // body
  64. path.node.generator, path.node.async);
  65. util.replaceWithOrRemove(path, t.objectProperty(t.cloneDeep(path.node.key),
  66. // key
  67. functionExpression,
  68. //value
  69. path.node.computed,
  70. // computed
  71. false // shorthand
  72. ));
  73. // path now refers to the ObjectProperty AST node path, but we want to return a
  74. // Function AST node path for the function expression we created. we know that
  75. // the FunctionExpression we just created is the value of the ObjectProperty,
  76. // so return the "value" path off of this path.
  77. return path.get("value");
  78. }