index.js 7.5 KB

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