index.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. call(superMember, args) {
  116. return (0, _helperOptimiseCallExpression().default)(this.get(superMember), t().thisExpression(), args);
  117. }
  118. };
  119. const looseHandlers = Object.assign({}, specHandlers, {
  120. prop(superMember) {
  121. const {
  122. property
  123. } = superMember.node;
  124. if (this.memoiser.has(property)) {
  125. return t().cloneNode(this.memoiser.get(property));
  126. }
  127. return t().cloneNode(property);
  128. },
  129. get(superMember) {
  130. const {
  131. isStatic,
  132. superRef
  133. } = this;
  134. const {
  135. computed
  136. } = superMember.node;
  137. const prop = this.prop(superMember);
  138. let object;
  139. if (isStatic) {
  140. object = superRef ? t().cloneNode(superRef) : t().memberExpression(t().identifier("Function"), t().identifier("prototype"));
  141. } else {
  142. object = superRef ? t().memberExpression(t().cloneNode(superRef), t().identifier("prototype")) : t().memberExpression(t().identifier("Object"), t().identifier("prototype"));
  143. }
  144. return t().memberExpression(object, prop, computed);
  145. },
  146. set(superMember, value) {
  147. const {
  148. computed
  149. } = superMember.node;
  150. const prop = this.prop(superMember);
  151. return t().assignmentExpression("=", t().memberExpression(t().thisExpression(), prop, computed), value);
  152. }
  153. });
  154. class ReplaceSupers {
  155. constructor(opts) {
  156. const path = opts.methodPath;
  157. this.methodPath = path;
  158. this.isStatic = path.isObjectMethod() || path.node.static;
  159. this.isPrivateMethod = path.isPrivate() && path.isMethod();
  160. this.file = opts.file;
  161. this.superRef = opts.superRef;
  162. this.isLoose = opts.isLoose;
  163. this.opts = opts;
  164. }
  165. getObjectRef() {
  166. return t().cloneNode(this.opts.objectRef || this.opts.getObjectRef());
  167. }
  168. replace() {
  169. const handler = this.isLoose ? looseHandlers : specHandlers;
  170. (0, _helperMemberExpressionToFunctions().default)(this.methodPath, visitor, Object.assign({
  171. file: this.file,
  172. isStatic: this.isStatic,
  173. isPrivateMethod: this.isPrivateMethod,
  174. getObjectRef: this.getObjectRef.bind(this),
  175. superRef: this.superRef
  176. }, handler));
  177. }
  178. }
  179. exports.default = ReplaceSupers;