index.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = exports.environmentVisitor = void 0;
  6. function _traverse() {
  7. const data = _interopRequireDefault(require("@babel/traverse"));
  8. _traverse = function () {
  9. return data;
  10. };
  11. return data;
  12. }
  13. function _helperMemberExpressionToFunctions() {
  14. const data = _interopRequireDefault(require("@babel/helper-member-expression-to-functions"));
  15. _helperMemberExpressionToFunctions = function () {
  16. return data;
  17. };
  18. return data;
  19. }
  20. function _helperOptimiseCallExpression() {
  21. const data = _interopRequireDefault(require("@babel/helper-optimise-call-expression"));
  22. _helperOptimiseCallExpression = function () {
  23. return data;
  24. };
  25. return data;
  26. }
  27. function t() {
  28. const data = _interopRequireWildcard(require("@babel/types"));
  29. t = function () {
  30. return data;
  31. };
  32. return data;
  33. }
  34. 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; } }
  35. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  36. function getPrototypeOfExpression(objectRef, isStatic, file, isPrivateMethod) {
  37. objectRef = t().cloneNode(objectRef);
  38. const targetRef = isStatic || isPrivateMethod ? objectRef : t().memberExpression(objectRef, t().identifier("prototype"));
  39. return t().callExpression(file.addHelper("getPrototypeOf"), [targetRef]);
  40. }
  41. function skipAllButComputedKey(path) {
  42. if (!path.node.computed) {
  43. path.skip();
  44. return;
  45. }
  46. const keys = t().VISITOR_KEYS[path.type];
  47. for (const key of keys) {
  48. if (key !== "key") path.skipKey(key);
  49. }
  50. }
  51. const environmentVisitor = {
  52. TypeAnnotation(path) {
  53. path.skip();
  54. },
  55. Function(path) {
  56. if (path.isMethod()) return;
  57. if (path.isArrowFunctionExpression()) return;
  58. path.skip();
  59. },
  60. "Method|ClassProperty|ClassPrivateProperty"(path) {
  61. skipAllButComputedKey(path);
  62. }
  63. };
  64. exports.environmentVisitor = environmentVisitor;
  65. const visitor = _traverse().default.visitors.merge([environmentVisitor, {
  66. Super(path, state) {
  67. const {
  68. node,
  69. parentPath
  70. } = path;
  71. if (!parentPath.isMemberExpression({
  72. object: node
  73. })) return;
  74. state.handle(parentPath);
  75. }
  76. }]);
  77. const specHandlers = {
  78. memoise(superMember, count) {
  79. const {
  80. scope,
  81. node
  82. } = superMember;
  83. const {
  84. computed,
  85. property
  86. } = node;
  87. if (!computed) {
  88. return;
  89. }
  90. const memo = scope.maybeGenerateMemoised(property);
  91. if (!memo) {
  92. return;
  93. }
  94. this.memoiser.set(property, memo, count);
  95. },
  96. prop(superMember) {
  97. const {
  98. computed,
  99. property
  100. } = superMember.node;
  101. if (this.memoiser.has(property)) {
  102. return t().cloneNode(this.memoiser.get(property));
  103. }
  104. if (computed) {
  105. return t().cloneNode(property);
  106. }
  107. return t().stringLiteral(property.name);
  108. },
  109. get(superMember) {
  110. return t().callExpression(this.file.addHelper("get"), [getPrototypeOfExpression(this.getObjectRef(), this.isStatic, this.file, this.isPrivateMethod), this.prop(superMember), t().thisExpression()]);
  111. },
  112. set(superMember, value) {
  113. return t().callExpression(this.file.addHelper("set"), [getPrototypeOfExpression(this.getObjectRef(), this.isStatic, this.file, this.isPrivateMethod), this.prop(superMember), value, t().thisExpression(), t().booleanLiteral(superMember.isInStrictMode())]);
  114. },
  115. destructureSet(superMember) {
  116. throw superMember.buildCodeFrameError(`Destructuring to a super field is not supported yet.`);
  117. },
  118. call(superMember, args) {
  119. return (0, _helperOptimiseCallExpression().default)(this.get(superMember), t().thisExpression(), args);
  120. }
  121. };
  122. const looseHandlers = Object.assign({}, specHandlers, {
  123. prop(superMember) {
  124. const {
  125. property
  126. } = superMember.node;
  127. if (this.memoiser.has(property)) {
  128. return t().cloneNode(this.memoiser.get(property));
  129. }
  130. return t().cloneNode(property);
  131. },
  132. get(superMember) {
  133. const {
  134. isStatic,
  135. superRef
  136. } = this;
  137. const {
  138. computed
  139. } = superMember.node;
  140. const prop = this.prop(superMember);
  141. let object;
  142. if (isStatic) {
  143. object = superRef ? t().cloneNode(superRef) : t().memberExpression(t().identifier("Function"), t().identifier("prototype"));
  144. } else {
  145. object = superRef ? t().memberExpression(t().cloneNode(superRef), t().identifier("prototype")) : t().memberExpression(t().identifier("Object"), t().identifier("prototype"));
  146. }
  147. return t().memberExpression(object, prop, computed);
  148. },
  149. set(superMember, value) {
  150. const {
  151. computed
  152. } = superMember.node;
  153. const prop = this.prop(superMember);
  154. return t().assignmentExpression("=", t().memberExpression(t().thisExpression(), prop, computed), value);
  155. },
  156. destructureSet(superMember) {
  157. const {
  158. computed
  159. } = superMember.node;
  160. const prop = this.prop(superMember);
  161. return t().memberExpression(t().thisExpression(), prop, computed);
  162. }
  163. });
  164. class ReplaceSupers {
  165. constructor(opts) {
  166. const path = opts.methodPath;
  167. this.methodPath = path;
  168. this.isStatic = path.isObjectMethod() || path.node.static;
  169. this.isPrivateMethod = path.isPrivate() && path.isMethod();
  170. this.file = opts.file;
  171. this.superRef = opts.superRef;
  172. this.isLoose = opts.isLoose;
  173. this.opts = opts;
  174. }
  175. getObjectRef() {
  176. return t().cloneNode(this.opts.objectRef || this.opts.getObjectRef());
  177. }
  178. replace() {
  179. const handler = this.isLoose ? looseHandlers : specHandlers;
  180. (0, _helperMemberExpressionToFunctions().default)(this.methodPath, visitor, Object.assign({
  181. file: this.file,
  182. isStatic: this.isStatic,
  183. isPrivateMethod: this.isPrivateMethod,
  184. getObjectRef: this.getObjectRef.bind(this),
  185. superRef: this.superRef
  186. }, handler));
  187. }
  188. }
  189. exports.default = ReplaceSupers;