index.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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 unshadowSuperBindingVisitor = _traverse.default.visitors.merge([environmentVisitor, {
  56. Scopable(path, {
  57. refName
  58. }) {
  59. const binding = path.scope.getOwnBinding(refName);
  60. if (binding && binding.identifier.name === refName) {
  61. path.scope.rename(refName);
  62. }
  63. }
  64. }]);
  65. const specHandlers = {
  66. memoise(superMember, count) {
  67. const {
  68. scope,
  69. node
  70. } = superMember;
  71. const {
  72. computed,
  73. property
  74. } = node;
  75. if (!computed) {
  76. return;
  77. }
  78. const memo = scope.maybeGenerateMemoised(property);
  79. if (!memo) {
  80. return;
  81. }
  82. this.memoiser.set(property, memo, count);
  83. },
  84. prop(superMember) {
  85. const {
  86. computed,
  87. property
  88. } = superMember.node;
  89. if (this.memoiser.has(property)) {
  90. return t.cloneNode(this.memoiser.get(property));
  91. }
  92. if (computed) {
  93. return t.cloneNode(property);
  94. }
  95. return t.stringLiteral(property.name);
  96. },
  97. get(superMember) {
  98. return this._get(superMember, this._getThisRefs());
  99. },
  100. _get(superMember, thisRefs) {
  101. const proto = getPrototypeOfExpression(this.getObjectRef(), this.isStatic, this.file, this.isPrivateMethod);
  102. return t.callExpression(this.file.addHelper("get"), [thisRefs.memo ? t.sequenceExpression([thisRefs.memo, proto]) : proto, this.prop(superMember), thisRefs.this]);
  103. },
  104. _getThisRefs() {
  105. if (!this.isDerivedConstructor) {
  106. return {
  107. this: t.thisExpression()
  108. };
  109. }
  110. const thisRef = this.scope.generateDeclaredUidIdentifier("thisSuper");
  111. return {
  112. memo: t.assignmentExpression("=", thisRef, t.thisExpression()),
  113. this: t.cloneNode(thisRef)
  114. };
  115. },
  116. set(superMember, value) {
  117. const thisRefs = this._getThisRefs();
  118. const proto = getPrototypeOfExpression(this.getObjectRef(), this.isStatic, this.file, this.isPrivateMethod);
  119. 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())]);
  120. },
  121. destructureSet(superMember) {
  122. throw superMember.buildCodeFrameError(`Destructuring to a super field is not supported yet.`);
  123. },
  124. call(superMember, args) {
  125. const thisRefs = this._getThisRefs();
  126. return (0, _helperOptimiseCallExpression.default)(this._get(superMember, thisRefs), t.cloneNode(thisRefs.this), args, false);
  127. },
  128. optionalCall(superMember, args) {
  129. const thisRefs = this._getThisRefs();
  130. return (0, _helperOptimiseCallExpression.default)(this._get(superMember, thisRefs), t.cloneNode(thisRefs.this), args, true);
  131. }
  132. };
  133. const looseHandlers = Object.assign({}, specHandlers, {
  134. prop(superMember) {
  135. const {
  136. property
  137. } = superMember.node;
  138. if (this.memoiser.has(property)) {
  139. return t.cloneNode(this.memoiser.get(property));
  140. }
  141. return t.cloneNode(property);
  142. },
  143. get(superMember) {
  144. const {
  145. isStatic,
  146. superRef
  147. } = this;
  148. const {
  149. computed
  150. } = superMember.node;
  151. const prop = this.prop(superMember);
  152. let object;
  153. if (isStatic) {
  154. object = superRef ? t.cloneNode(superRef) : t.memberExpression(t.identifier("Function"), t.identifier("prototype"));
  155. } else {
  156. object = superRef ? t.memberExpression(t.cloneNode(superRef), t.identifier("prototype")) : t.memberExpression(t.identifier("Object"), t.identifier("prototype"));
  157. }
  158. return t.memberExpression(object, prop, computed);
  159. },
  160. set(superMember, value) {
  161. const {
  162. computed
  163. } = superMember.node;
  164. const prop = this.prop(superMember);
  165. return t.assignmentExpression("=", t.memberExpression(t.thisExpression(), prop, computed), value);
  166. },
  167. destructureSet(superMember) {
  168. const {
  169. computed
  170. } = superMember.node;
  171. const prop = this.prop(superMember);
  172. return t.memberExpression(t.thisExpression(), prop, computed);
  173. },
  174. call(superMember, args) {
  175. return (0, _helperOptimiseCallExpression.default)(this.get(superMember), t.thisExpression(), args, false);
  176. },
  177. optionalCall(superMember, args) {
  178. return (0, _helperOptimiseCallExpression.default)(this.get(superMember), t.thisExpression(), args, true);
  179. }
  180. });
  181. class ReplaceSupers {
  182. constructor(opts) {
  183. var _opts$constantSuper;
  184. const path = opts.methodPath;
  185. this.methodPath = path;
  186. this.isDerivedConstructor = path.isClassMethod({
  187. kind: "constructor"
  188. }) && !!opts.superRef;
  189. this.isStatic = path.isObjectMethod() || path.node.static;
  190. this.isPrivateMethod = path.isPrivate() && path.isMethod();
  191. this.file = opts.file;
  192. this.superRef = opts.superRef;
  193. this.constantSuper = (_opts$constantSuper = opts.constantSuper) != null ? _opts$constantSuper : opts.isLoose;
  194. this.opts = opts;
  195. }
  196. getObjectRef() {
  197. return t.cloneNode(this.opts.objectRef || this.opts.getObjectRef());
  198. }
  199. replace() {
  200. if (this.opts.refToPreserve) {
  201. this.methodPath.traverse(unshadowSuperBindingVisitor, {
  202. refName: this.opts.refToPreserve.name
  203. });
  204. }
  205. const handler = this.constantSuper ? looseHandlers : specHandlers;
  206. (0, _helperMemberExpressionToFunctions.default)(this.methodPath, visitor, Object.assign({
  207. file: this.file,
  208. scope: this.methodPath.scope,
  209. isDerivedConstructor: this.isDerivedConstructor,
  210. isStatic: this.isStatic,
  211. isPrivateMethod: this.isPrivateMethod,
  212. getObjectRef: this.getObjectRef.bind(this),
  213. superRef: this.superRef
  214. }, handler));
  215. }
  216. }
  217. exports.default = ReplaceSupers;