index.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. [`${t.staticBlock ? "StaticBlock|" : ""}ClassPrivateProperty|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"(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, false);
  117. },
  118. optionalCall(superMember, args) {
  119. const thisRefs = this._getThisRefs();
  120. return (0, _helperOptimiseCallExpression.default)(this._get(superMember, thisRefs), t.cloneNode(thisRefs.this), args, true);
  121. }
  122. };
  123. const looseHandlers = Object.assign({}, specHandlers, {
  124. prop(superMember) {
  125. const {
  126. property
  127. } = superMember.node;
  128. if (this.memoiser.has(property)) {
  129. return t.cloneNode(this.memoiser.get(property));
  130. }
  131. return t.cloneNode(property);
  132. },
  133. get(superMember) {
  134. const {
  135. isStatic,
  136. superRef
  137. } = this;
  138. const {
  139. computed
  140. } = superMember.node;
  141. const prop = this.prop(superMember);
  142. let object;
  143. if (isStatic) {
  144. object = superRef ? t.cloneNode(superRef) : t.memberExpression(t.identifier("Function"), t.identifier("prototype"));
  145. } else {
  146. object = superRef ? t.memberExpression(t.cloneNode(superRef), t.identifier("prototype")) : t.memberExpression(t.identifier("Object"), t.identifier("prototype"));
  147. }
  148. return t.memberExpression(object, prop, computed);
  149. },
  150. set(superMember, value) {
  151. const {
  152. computed
  153. } = superMember.node;
  154. const prop = this.prop(superMember);
  155. return t.assignmentExpression("=", t.memberExpression(t.thisExpression(), prop, computed), value);
  156. },
  157. destructureSet(superMember) {
  158. const {
  159. computed
  160. } = superMember.node;
  161. const prop = this.prop(superMember);
  162. return t.memberExpression(t.thisExpression(), prop, computed);
  163. },
  164. call(superMember, args) {
  165. return (0, _helperOptimiseCallExpression.default)(this.get(superMember), t.thisExpression(), args, false);
  166. },
  167. optionalCall(superMember, args) {
  168. return (0, _helperOptimiseCallExpression.default)(this.get(superMember), t.thisExpression(), args, true);
  169. }
  170. });
  171. class ReplaceSupers {
  172. constructor(opts) {
  173. const path = opts.methodPath;
  174. this.methodPath = path;
  175. this.isDerivedConstructor = path.isClassMethod({
  176. kind: "constructor"
  177. }) && !!opts.superRef;
  178. this.isStatic = path.isObjectMethod() || path.node.static;
  179. this.isPrivateMethod = path.isPrivate() && path.isMethod();
  180. this.file = opts.file;
  181. this.superRef = opts.superRef;
  182. this.isLoose = opts.isLoose;
  183. this.opts = opts;
  184. }
  185. getObjectRef() {
  186. return t.cloneNode(this.opts.objectRef || this.opts.getObjectRef());
  187. }
  188. replace() {
  189. const handler = this.isLoose ? looseHandlers : specHandlers;
  190. (0, _helperMemberExpressionToFunctions.default)(this.methodPath, visitor, Object.assign({
  191. file: this.file,
  192. scope: this.methodPath.scope,
  193. isDerivedConstructor: this.isDerivedConstructor,
  194. isStatic: this.isStatic,
  195. isPrivateMethod: this.isPrivateMethod,
  196. getObjectRef: this.getObjectRef.bind(this),
  197. superRef: this.superRef
  198. }, handler));
  199. }
  200. }
  201. exports.default = ReplaceSupers;