index.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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 = require("@babel/traverse");
  8. var _helperMemberExpressionToFunctions = require("@babel/helper-member-expression-to-functions");
  9. var _helperOptimiseCallExpression = require("@babel/helper-optimise-call-expression");
  10. var _t = require("@babel/types");
  11. const {
  12. VISITOR_KEYS,
  13. assignmentExpression,
  14. booleanLiteral,
  15. callExpression,
  16. cloneNode,
  17. identifier,
  18. memberExpression,
  19. sequenceExpression,
  20. staticBlock,
  21. stringLiteral,
  22. thisExpression
  23. } = _t;
  24. function getPrototypeOfExpression(objectRef, isStatic, file, isPrivateMethod) {
  25. objectRef = cloneNode(objectRef);
  26. const targetRef = isStatic || isPrivateMethod ? objectRef : memberExpression(objectRef, identifier("prototype"));
  27. return callExpression(file.addHelper("getPrototypeOf"), [targetRef]);
  28. }
  29. function skipAllButComputedKey(path) {
  30. if (!path.node.computed) {
  31. path.skip();
  32. return;
  33. }
  34. const keys = VISITOR_KEYS[path.type];
  35. for (const key of keys) {
  36. if (key !== "key") path.skipKey(key);
  37. }
  38. }
  39. const environmentVisitor = {
  40. [`${staticBlock ? "StaticBlock|" : ""}ClassPrivateProperty|TypeAnnotation`](path) {
  41. path.skip();
  42. },
  43. Function(path) {
  44. if (path.isMethod()) return;
  45. if (path.isArrowFunctionExpression()) return;
  46. path.skip();
  47. },
  48. "Method|ClassProperty"(path) {
  49. skipAllButComputedKey(path);
  50. }
  51. };
  52. exports.environmentVisitor = environmentVisitor;
  53. const visitor = _traverse.default.visitors.merge([environmentVisitor, {
  54. Super(path, state) {
  55. const {
  56. node,
  57. parentPath
  58. } = path;
  59. if (!parentPath.isMemberExpression({
  60. object: node
  61. })) return;
  62. state.handle(parentPath);
  63. }
  64. }]);
  65. const unshadowSuperBindingVisitor = _traverse.default.visitors.merge([environmentVisitor, {
  66. Scopable(path, {
  67. refName
  68. }) {
  69. const binding = path.scope.getOwnBinding(refName);
  70. if (binding && binding.identifier.name === refName) {
  71. path.scope.rename(refName);
  72. }
  73. }
  74. }]);
  75. const specHandlers = {
  76. memoise(superMember, count) {
  77. const {
  78. scope,
  79. node
  80. } = superMember;
  81. const {
  82. computed,
  83. property
  84. } = node;
  85. if (!computed) {
  86. return;
  87. }
  88. const memo = scope.maybeGenerateMemoised(property);
  89. if (!memo) {
  90. return;
  91. }
  92. this.memoiser.set(property, memo, count);
  93. },
  94. prop(superMember) {
  95. const {
  96. computed,
  97. property
  98. } = superMember.node;
  99. if (this.memoiser.has(property)) {
  100. return cloneNode(this.memoiser.get(property));
  101. }
  102. if (computed) {
  103. return cloneNode(property);
  104. }
  105. return stringLiteral(property.name);
  106. },
  107. get(superMember) {
  108. return this._get(superMember, this._getThisRefs());
  109. },
  110. _get(superMember, thisRefs) {
  111. const proto = getPrototypeOfExpression(this.getObjectRef(), this.isStatic, this.file, this.isPrivateMethod);
  112. return callExpression(this.file.addHelper("get"), [thisRefs.memo ? sequenceExpression([thisRefs.memo, proto]) : proto, this.prop(superMember), thisRefs.this]);
  113. },
  114. _getThisRefs() {
  115. if (!this.isDerivedConstructor) {
  116. return {
  117. this: thisExpression()
  118. };
  119. }
  120. const thisRef = this.scope.generateDeclaredUidIdentifier("thisSuper");
  121. return {
  122. memo: assignmentExpression("=", thisRef, thisExpression()),
  123. this: cloneNode(thisRef)
  124. };
  125. },
  126. set(superMember, value) {
  127. const thisRefs = this._getThisRefs();
  128. const proto = getPrototypeOfExpression(this.getObjectRef(), this.isStatic, this.file, this.isPrivateMethod);
  129. return callExpression(this.file.addHelper("set"), [thisRefs.memo ? sequenceExpression([thisRefs.memo, proto]) : proto, this.prop(superMember), value, thisRefs.this, booleanLiteral(superMember.isInStrictMode())]);
  130. },
  131. destructureSet(superMember) {
  132. throw superMember.buildCodeFrameError(`Destructuring to a super field is not supported yet.`);
  133. },
  134. call(superMember, args) {
  135. const thisRefs = this._getThisRefs();
  136. return (0, _helperOptimiseCallExpression.default)(this._get(superMember, thisRefs), cloneNode(thisRefs.this), args, false);
  137. },
  138. optionalCall(superMember, args) {
  139. const thisRefs = this._getThisRefs();
  140. return (0, _helperOptimiseCallExpression.default)(this._get(superMember, thisRefs), cloneNode(thisRefs.this), args, true);
  141. }
  142. };
  143. const looseHandlers = Object.assign({}, specHandlers, {
  144. prop(superMember) {
  145. const {
  146. property
  147. } = superMember.node;
  148. if (this.memoiser.has(property)) {
  149. return cloneNode(this.memoiser.get(property));
  150. }
  151. return cloneNode(property);
  152. },
  153. get(superMember) {
  154. const {
  155. isStatic,
  156. getSuperRef
  157. } = this;
  158. const {
  159. computed
  160. } = superMember.node;
  161. const prop = this.prop(superMember);
  162. let object;
  163. if (isStatic) {
  164. var _getSuperRef;
  165. object = (_getSuperRef = getSuperRef()) != null ? _getSuperRef : memberExpression(identifier("Function"), identifier("prototype"));
  166. } else {
  167. var _getSuperRef2;
  168. object = memberExpression((_getSuperRef2 = getSuperRef()) != null ? _getSuperRef2 : identifier("Object"), identifier("prototype"));
  169. }
  170. return memberExpression(object, prop, computed);
  171. },
  172. set(superMember, value) {
  173. const {
  174. computed
  175. } = superMember.node;
  176. const prop = this.prop(superMember);
  177. return assignmentExpression("=", memberExpression(thisExpression(), prop, computed), value);
  178. },
  179. destructureSet(superMember) {
  180. const {
  181. computed
  182. } = superMember.node;
  183. const prop = this.prop(superMember);
  184. return memberExpression(thisExpression(), prop, computed);
  185. },
  186. call(superMember, args) {
  187. return (0, _helperOptimiseCallExpression.default)(this.get(superMember), thisExpression(), args, false);
  188. },
  189. optionalCall(superMember, args) {
  190. return (0, _helperOptimiseCallExpression.default)(this.get(superMember), thisExpression(), args, true);
  191. }
  192. });
  193. class ReplaceSupers {
  194. constructor(opts) {
  195. var _opts$constantSuper;
  196. const path = opts.methodPath;
  197. this.methodPath = path;
  198. this.isDerivedConstructor = path.isClassMethod({
  199. kind: "constructor"
  200. }) && !!opts.superRef;
  201. this.isStatic = path.isObjectMethod() || path.node.static || (path.isStaticBlock == null ? void 0 : path.isStaticBlock());
  202. this.isPrivateMethod = path.isPrivate() && path.isMethod();
  203. this.file = opts.file;
  204. this.constantSuper = (_opts$constantSuper = opts.constantSuper) != null ? _opts$constantSuper : opts.isLoose;
  205. this.opts = opts;
  206. }
  207. getObjectRef() {
  208. return cloneNode(this.opts.objectRef || this.opts.getObjectRef());
  209. }
  210. getSuperRef() {
  211. if (this.opts.superRef) return cloneNode(this.opts.superRef);
  212. if (this.opts.getSuperRef) return cloneNode(this.opts.getSuperRef());
  213. }
  214. replace() {
  215. if (this.opts.refToPreserve) {
  216. this.methodPath.traverse(unshadowSuperBindingVisitor, {
  217. refName: this.opts.refToPreserve.name
  218. });
  219. }
  220. const handler = this.constantSuper ? looseHandlers : specHandlers;
  221. (0, _helperMemberExpressionToFunctions.default)(this.methodPath, visitor, Object.assign({
  222. file: this.file,
  223. scope: this.methodPath.scope,
  224. isDerivedConstructor: this.isDerivedConstructor,
  225. isStatic: this.isStatic,
  226. isPrivateMethod: this.isPrivateMethod,
  227. getObjectRef: this.getObjectRef.bind(this),
  228. getSuperRef: this.getSuperRef.bind(this),
  229. boundGet: handler.get
  230. }, handler));
  231. }
  232. }
  233. exports.default = ReplaceSupers;