index.js 7.5 KB

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