index.js 7.2 KB

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