index.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. Object.defineProperty(exports, "environmentVisitor", {
  7. enumerable: true,
  8. get: function () {
  9. return _helperEnvironmentVisitor.default;
  10. }
  11. });
  12. Object.defineProperty(exports, "skipAllButComputedKey", {
  13. enumerable: true,
  14. get: function () {
  15. return _helperEnvironmentVisitor.skipAllButComputedKey;
  16. }
  17. });
  18. var _helperEnvironmentVisitor = require("@babel/helper-environment-visitor");
  19. var _helperMemberExpressionToFunctions = require("@babel/helper-member-expression-to-functions");
  20. var _helperOptimiseCallExpression = require("@babel/helper-optimise-call-expression");
  21. var _template = require("@babel/template");
  22. var _traverse = require("@babel/traverse");
  23. var _t = require("@babel/types");
  24. const {
  25. assignmentExpression,
  26. booleanLiteral,
  27. callExpression,
  28. cloneNode,
  29. identifier,
  30. memberExpression,
  31. sequenceExpression,
  32. stringLiteral,
  33. thisExpression
  34. } = _t;
  35. function getPrototypeOfExpression(objectRef, isStatic, file, isPrivateMethod) {
  36. objectRef = cloneNode(objectRef);
  37. const targetRef = isStatic || isPrivateMethod ? objectRef : memberExpression(objectRef, identifier("prototype"));
  38. return callExpression(file.addHelper("getPrototypeOf"), [targetRef]);
  39. }
  40. const visitor = _traverse.default.visitors.merge([_helperEnvironmentVisitor.default, {
  41. Super(path, state) {
  42. const {
  43. node,
  44. parentPath
  45. } = path;
  46. if (!parentPath.isMemberExpression({
  47. object: node
  48. })) return;
  49. state.handle(parentPath);
  50. }
  51. }]);
  52. const unshadowSuperBindingVisitor = _traverse.default.visitors.merge([_helperEnvironmentVisitor.default, {
  53. Scopable(path, {
  54. refName
  55. }) {
  56. const binding = path.scope.getOwnBinding(refName);
  57. if (binding && binding.identifier.name === refName) {
  58. path.scope.rename(refName);
  59. }
  60. }
  61. }]);
  62. const specHandlers = {
  63. memoise(superMember, count) {
  64. const {
  65. scope,
  66. node
  67. } = superMember;
  68. const {
  69. computed,
  70. property
  71. } = node;
  72. if (!computed) {
  73. return;
  74. }
  75. const memo = scope.maybeGenerateMemoised(property);
  76. if (!memo) {
  77. return;
  78. }
  79. this.memoiser.set(property, memo, count);
  80. },
  81. prop(superMember) {
  82. const {
  83. computed,
  84. property
  85. } = superMember.node;
  86. if (this.memoiser.has(property)) {
  87. return cloneNode(this.memoiser.get(property));
  88. }
  89. if (computed) {
  90. return cloneNode(property);
  91. }
  92. return stringLiteral(property.name);
  93. },
  94. get(superMember) {
  95. return this._get(superMember, this._getThisRefs());
  96. },
  97. _get(superMember, thisRefs) {
  98. const proto = getPrototypeOfExpression(this.getObjectRef(), this.isStatic, this.file, this.isPrivateMethod);
  99. return callExpression(this.file.addHelper("get"), [
  100. thisRefs.memo ? sequenceExpression([thisRefs.memo, proto]) : proto, this.prop(superMember), thisRefs.this]);
  101. },
  102. _getThisRefs() {
  103. if (!this.isDerivedConstructor) {
  104. return {
  105. this: thisExpression()
  106. };
  107. }
  108. const thisRef = this.scope.generateDeclaredUidIdentifier("thisSuper");
  109. return {
  110. memo: assignmentExpression("=", thisRef, thisExpression()),
  111. this: cloneNode(thisRef)
  112. };
  113. },
  114. set(superMember, value) {
  115. const thisRefs = this._getThisRefs();
  116. const proto = getPrototypeOfExpression(this.getObjectRef(), this.isStatic, this.file, this.isPrivateMethod);
  117. return callExpression(this.file.addHelper("set"), [
  118. thisRefs.memo ? sequenceExpression([thisRefs.memo, proto]) : proto, this.prop(superMember), value, thisRefs.this, booleanLiteral(superMember.isInStrictMode())]);
  119. },
  120. destructureSet(superMember) {
  121. throw superMember.buildCodeFrameError(`Destructuring to a super field is not supported yet.`);
  122. },
  123. call(superMember, args) {
  124. const thisRefs = this._getThisRefs();
  125. return (0, _helperOptimiseCallExpression.default)(this._get(superMember, thisRefs), cloneNode(thisRefs.this), args, false);
  126. },
  127. optionalCall(superMember, args) {
  128. const thisRefs = this._getThisRefs();
  129. return (0, _helperOptimiseCallExpression.default)(this._get(superMember, thisRefs), cloneNode(thisRefs.this), args, true);
  130. },
  131. delete(superMember) {
  132. if (superMember.node.computed) {
  133. return sequenceExpression([callExpression(this.file.addHelper("toPropertyKey"), [cloneNode(superMember.node.property)]), _template.default.expression.ast`
  134. function () { throw new ReferenceError("'delete super[expr]' is invalid"); }()
  135. `]);
  136. } else {
  137. return _template.default.expression.ast`
  138. function () { throw new ReferenceError("'delete super.prop' is invalid"); }()
  139. `;
  140. }
  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() ||
  202. path.node.static || (path.isStaticBlock == null ? void 0 : path.isStaticBlock());
  203. this.isPrivateMethod = path.isPrivate() && path.isMethod();
  204. this.file = opts.file;
  205. this.constantSuper = (_opts$constantSuper = opts.constantSuper) != null ? _opts$constantSuper : opts.isLoose;
  206. this.opts = opts;
  207. }
  208. getObjectRef() {
  209. return cloneNode(this.opts.objectRef || this.opts.getObjectRef());
  210. }
  211. getSuperRef() {
  212. if (this.opts.superRef) return cloneNode(this.opts.superRef);
  213. if (this.opts.getSuperRef) {
  214. return cloneNode(this.opts.getSuperRef());
  215. }
  216. }
  217. replace() {
  218. if (this.opts.refToPreserve) {
  219. this.methodPath.traverse(unshadowSuperBindingVisitor, {
  220. refName: this.opts.refToPreserve.name
  221. });
  222. }
  223. const handler = this.constantSuper ? looseHandlers : specHandlers;
  224. (0, _helperMemberExpressionToFunctions.default)(this.methodPath, visitor, Object.assign({
  225. file: this.file,
  226. scope: this.methodPath.scope,
  227. isDerivedConstructor: this.isDerivedConstructor,
  228. isStatic: this.isStatic,
  229. isPrivateMethod: this.isPrivateMethod,
  230. getObjectRef: this.getObjectRef.bind(this),
  231. getSuperRef: this.getSuperRef.bind(this),
  232. boundGet: handler.get
  233. }, handler));
  234. }
  235. }
  236. exports.default = ReplaceSupers;
  237. //# sourceMappingURL=index.js.map