index.js 6.2 KB

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