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. 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.needAccessFirst ? sequenceExpression([thisRefs.this, proto]) : proto, this.prop(superMember), thisRefs.this]);
  91. },
  92. _getThisRefs() {
  93. return {
  94. needAccessFirst: this.isDerivedConstructor,
  95. this: thisExpression()
  96. };
  97. },
  98. set(superMember, value) {
  99. const thisRefs = this._getThisRefs();
  100. const proto = getPrototypeOfExpression(this.getObjectRef(), this.isStatic, this.file, this.isPrivateMethod);
  101. return callExpression(this.file.addHelper("set"), [thisRefs.needAccessFirst ? sequenceExpression([thisRefs.this, proto]) : proto, this.prop(superMember), value, thisRefs.this, booleanLiteral(superMember.isInStrictMode())]);
  102. },
  103. destructureSet(superMember) {
  104. throw superMember.buildCodeFrameError(`Destructuring to a super field is not supported yet.`);
  105. },
  106. call(superMember, args) {
  107. const thisRefs = this._getThisRefs();
  108. return (0, _helperOptimiseCallExpression.default)(this._get(superMember, thisRefs), cloneNode(thisRefs.this), args, false);
  109. },
  110. optionalCall(superMember, args) {
  111. const thisRefs = this._getThisRefs();
  112. return (0, _helperOptimiseCallExpression.default)(this._get(superMember, thisRefs), cloneNode(thisRefs.this), args, true);
  113. },
  114. delete(superMember) {
  115. if (superMember.node.computed) {
  116. return sequenceExpression([callExpression(this.file.addHelper("toPropertyKey"), [cloneNode(superMember.node.property)]), _core.template.expression.ast`
  117. function () { throw new ReferenceError("'delete super[expr]' is invalid"); }()
  118. `]);
  119. } else {
  120. return _core.template.expression.ast`
  121. function () { throw new ReferenceError("'delete super.prop' is invalid"); }()
  122. `;
  123. }
  124. }
  125. };
  126. const looseHandlers = Object.assign({}, specHandlers, {
  127. prop(superMember) {
  128. const {
  129. property
  130. } = superMember.node;
  131. if (this.memoiser.has(property)) {
  132. return cloneNode(this.memoiser.get(property));
  133. }
  134. return cloneNode(property);
  135. },
  136. get(superMember) {
  137. const {
  138. isStatic,
  139. getSuperRef
  140. } = this;
  141. const {
  142. computed
  143. } = superMember.node;
  144. const prop = this.prop(superMember);
  145. let object;
  146. if (isStatic) {
  147. var _getSuperRef;
  148. object = (_getSuperRef = getSuperRef()) != null ? _getSuperRef : memberExpression(identifier("Function"), identifier("prototype"));
  149. } else {
  150. var _getSuperRef2;
  151. object = memberExpression((_getSuperRef2 = getSuperRef()) != null ? _getSuperRef2 : identifier("Object"), identifier("prototype"));
  152. }
  153. return memberExpression(object, prop, computed);
  154. },
  155. set(superMember, value) {
  156. const {
  157. computed
  158. } = superMember.node;
  159. const prop = this.prop(superMember);
  160. return assignmentExpression("=", memberExpression(thisExpression(), prop, computed), value);
  161. },
  162. destructureSet(superMember) {
  163. const {
  164. computed
  165. } = superMember.node;
  166. const prop = this.prop(superMember);
  167. return memberExpression(thisExpression(), prop, computed);
  168. },
  169. call(superMember, args) {
  170. return (0, _helperOptimiseCallExpression.default)(this.get(superMember), thisExpression(), args, false);
  171. },
  172. optionalCall(superMember, args) {
  173. return (0, _helperOptimiseCallExpression.default)(this.get(superMember), thisExpression(), args, true);
  174. }
  175. });
  176. class ReplaceSupers {
  177. constructor(opts) {
  178. var _opts$constantSuper;
  179. const path = opts.methodPath;
  180. this.methodPath = path;
  181. this.isDerivedConstructor = path.isClassMethod({
  182. kind: "constructor"
  183. }) && !!opts.superRef;
  184. this.isStatic = path.isObjectMethod() || path.node.static || (path.isStaticBlock == null ? void 0 : path.isStaticBlock());
  185. this.isPrivateMethod = path.isPrivate() && path.isMethod();
  186. this.file = opts.file;
  187. this.constantSuper = (_opts$constantSuper = opts.constantSuper) != null ? _opts$constantSuper : opts.isLoose;
  188. this.opts = opts;
  189. }
  190. getObjectRef() {
  191. return cloneNode(this.opts.objectRef || this.opts.getObjectRef());
  192. }
  193. getSuperRef() {
  194. if (this.opts.superRef) return cloneNode(this.opts.superRef);
  195. if (this.opts.getSuperRef) {
  196. return cloneNode(this.opts.getSuperRef());
  197. }
  198. }
  199. replace() {
  200. const {
  201. methodPath
  202. } = this;
  203. if (this.opts.refToPreserve) {
  204. methodPath.traverse(unshadowSuperBindingVisitor, {
  205. refName: this.opts.refToPreserve.name
  206. });
  207. }
  208. const handler = this.constantSuper ? looseHandlers : specHandlers;
  209. visitor.shouldSkip = path => {
  210. if (path.parentPath === methodPath) {
  211. if (path.parentKey === "decorators" || path.parentKey === "key") {
  212. return true;
  213. }
  214. }
  215. };
  216. (0, _helperMemberExpressionToFunctions.default)(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