index.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 this._get(superMember, this._getThisRefs());
  89. },
  90. _get(superMember, thisRefs) {
  91. const proto = getPrototypeOfExpression(this.getObjectRef(), this.isStatic, this.file, this.isPrivateMethod);
  92. return t.callExpression(this.file.addHelper("get"), [thisRefs.memo ? t.sequenceExpression([thisRefs.memo, proto]) : proto, this.prop(superMember), thisRefs.this]);
  93. },
  94. _getThisRefs() {
  95. if (!this.isDerivedConstructor) {
  96. return {
  97. this: t.thisExpression()
  98. };
  99. }
  100. const thisRef = this.scope.generateDeclaredUidIdentifier("thisSuper");
  101. return {
  102. memo: t.assignmentExpression("=", thisRef, t.thisExpression()),
  103. this: t.cloneNode(thisRef)
  104. };
  105. },
  106. set(superMember, value) {
  107. const thisRefs = this._getThisRefs();
  108. const proto = getPrototypeOfExpression(this.getObjectRef(), this.isStatic, this.file, this.isPrivateMethod);
  109. return t.callExpression(this.file.addHelper("set"), [thisRefs.memo ? t.sequenceExpression([thisRefs.memo, proto]) : proto, this.prop(superMember), value, thisRefs.this, t.booleanLiteral(superMember.isInStrictMode())]);
  110. },
  111. destructureSet(superMember) {
  112. throw superMember.buildCodeFrameError(`Destructuring to a super field is not supported yet.`);
  113. },
  114. call(superMember, args) {
  115. const thisRefs = this._getThisRefs();
  116. return (0, _helperOptimiseCallExpression.default)(this._get(superMember, thisRefs), t.cloneNode(thisRefs.this), 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. destructureSet(superMember) {
  154. const {
  155. computed
  156. } = superMember.node;
  157. const prop = this.prop(superMember);
  158. return t.memberExpression(t.thisExpression(), prop, computed);
  159. },
  160. call(superMember, args) {
  161. return (0, _helperOptimiseCallExpression.default)(this.get(superMember), t.thisExpression(), args);
  162. }
  163. });
  164. class ReplaceSupers {
  165. constructor(opts) {
  166. const path = opts.methodPath;
  167. this.methodPath = path;
  168. this.isDerivedConstructor = path.isClassMethod({
  169. kind: "constructor"
  170. }) && !!opts.superRef;
  171. this.isStatic = path.isObjectMethod() || path.node.static;
  172. this.isPrivateMethod = path.isPrivate() && path.isMethod();
  173. this.file = opts.file;
  174. this.superRef = opts.superRef;
  175. this.isLoose = opts.isLoose;
  176. this.opts = opts;
  177. }
  178. getObjectRef() {
  179. return t.cloneNode(this.opts.objectRef || this.opts.getObjectRef());
  180. }
  181. replace() {
  182. const handler = this.isLoose ? looseHandlers : specHandlers;
  183. (0, _helperMemberExpressionToFunctions.default)(this.methodPath, visitor, Object.assign({
  184. file: this.file,
  185. scope: this.methodPath.scope,
  186. isDerivedConstructor: this.isDerivedConstructor,
  187. isStatic: this.isStatic,
  188. isPrivateMethod: this.isPrivateMethod,
  189. getObjectRef: this.getObjectRef.bind(this),
  190. superRef: this.superRef
  191. }, handler));
  192. }
  193. }
  194. exports.default = ReplaceSupers;