index.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. "use strict";
  2. exports.__esModule = true;
  3. var _classCallCheck2 = require("babel-runtime/helpers/classCallCheck");
  4. var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
  5. var _symbol = require("babel-runtime/core-js/symbol");
  6. var _symbol2 = _interopRequireDefault(_symbol);
  7. var _babelHelperOptimiseCallExpression = require("babel-helper-optimise-call-expression");
  8. var _babelHelperOptimiseCallExpression2 = _interopRequireDefault(_babelHelperOptimiseCallExpression);
  9. var _babelMessages = require("babel-messages");
  10. var messages = _interopRequireWildcard(_babelMessages);
  11. var _babelTypes = require("babel-types");
  12. var t = _interopRequireWildcard(_babelTypes);
  13. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
  14. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  15. var HARDCORE_THIS_REF = (0, _symbol2.default)();
  16. function isIllegalBareSuper(node, parent) {
  17. if (!t.isSuper(node)) return false;
  18. if (t.isMemberExpression(parent, { computed: false })) return false;
  19. if (t.isCallExpression(parent, { callee: node })) return false;
  20. return true;
  21. }
  22. function isMemberExpressionSuper(node) {
  23. return t.isMemberExpression(node) && t.isSuper(node.object);
  24. }
  25. function getPrototypeOfExpression(objectRef, isStatic) {
  26. var targetRef = isStatic ? objectRef : t.memberExpression(objectRef, t.identifier("prototype"));
  27. return t.logicalExpression("||", t.memberExpression(targetRef, t.identifier("__proto__")), t.callExpression(t.memberExpression(t.identifier("Object"), t.identifier("getPrototypeOf")), [targetRef]));
  28. }
  29. var visitor = {
  30. Function: function Function(path) {
  31. if (!path.inShadow("this")) {
  32. path.skip();
  33. }
  34. },
  35. ReturnStatement: function ReturnStatement(path, state) {
  36. if (!path.inShadow("this")) {
  37. state.returns.push(path);
  38. }
  39. },
  40. ThisExpression: function ThisExpression(path, state) {
  41. if (!path.node[HARDCORE_THIS_REF]) {
  42. state.thises.push(path);
  43. }
  44. },
  45. enter: function enter(path, state) {
  46. var callback = state.specHandle;
  47. if (state.isLoose) callback = state.looseHandle;
  48. var isBareSuper = path.isCallExpression() && path.get("callee").isSuper();
  49. var result = callback.call(state, path);
  50. if (result) {
  51. state.hasSuper = true;
  52. }
  53. if (isBareSuper) {
  54. state.bareSupers.push(path);
  55. }
  56. if (result === true) {
  57. path.requeue();
  58. }
  59. if (result !== true && result) {
  60. if (Array.isArray(result)) {
  61. path.replaceWithMultiple(result);
  62. } else {
  63. path.replaceWith(result);
  64. }
  65. }
  66. }
  67. };
  68. var ReplaceSupers = function () {
  69. function ReplaceSupers(opts) {
  70. var inClass = arguments.length <= 1 || arguments[1] === undefined ? false : arguments[1];
  71. (0, _classCallCheck3.default)(this, ReplaceSupers);
  72. this.forceSuperMemoisation = opts.forceSuperMemoisation;
  73. this.methodPath = opts.methodPath;
  74. this.methodNode = opts.methodNode;
  75. this.superRef = opts.superRef;
  76. this.isStatic = opts.isStatic;
  77. this.hasSuper = false;
  78. this.inClass = inClass;
  79. this.isLoose = opts.isLoose;
  80. this.scope = this.methodPath.scope;
  81. this.file = opts.file;
  82. this.opts = opts;
  83. this.bareSupers = [];
  84. this.returns = [];
  85. this.thises = [];
  86. }
  87. ReplaceSupers.prototype.getObjectRef = function getObjectRef() {
  88. return this.opts.objectRef || this.opts.getObjectRef();
  89. };
  90. ReplaceSupers.prototype.setSuperProperty = function setSuperProperty(property, value, isComputed) {
  91. return t.callExpression(this.file.addHelper("set"), [getPrototypeOfExpression(this.getObjectRef(), this.isStatic), isComputed ? property : t.stringLiteral(property.name), value, t.thisExpression()]);
  92. };
  93. ReplaceSupers.prototype.getSuperProperty = function getSuperProperty(property, isComputed) {
  94. return t.callExpression(this.file.addHelper("get"), [getPrototypeOfExpression(this.getObjectRef(), this.isStatic), isComputed ? property : t.stringLiteral(property.name), t.thisExpression()]);
  95. };
  96. ReplaceSupers.prototype.replace = function replace() {
  97. this.methodPath.traverse(visitor, this);
  98. };
  99. ReplaceSupers.prototype.getLooseSuperProperty = function getLooseSuperProperty(id, parent) {
  100. var methodNode = this.methodNode;
  101. var superRef = this.superRef || t.identifier("Function");
  102. if (parent.property === id) {
  103. return;
  104. } else if (t.isCallExpression(parent, { callee: id })) {
  105. return;
  106. } else if (t.isMemberExpression(parent) && !methodNode.static) {
  107. return t.memberExpression(superRef, t.identifier("prototype"));
  108. } else {
  109. return superRef;
  110. }
  111. };
  112. ReplaceSupers.prototype.looseHandle = function looseHandle(path) {
  113. var node = path.node;
  114. if (path.isSuper()) {
  115. return this.getLooseSuperProperty(node, path.parent);
  116. } else if (path.isCallExpression()) {
  117. var callee = node.callee;
  118. if (!t.isMemberExpression(callee)) return;
  119. if (!t.isSuper(callee.object)) return;
  120. t.appendToMemberExpression(callee, t.identifier("call"));
  121. node.arguments.unshift(t.thisExpression());
  122. return true;
  123. }
  124. };
  125. ReplaceSupers.prototype.specHandleAssignmentExpression = function specHandleAssignmentExpression(ref, path, node) {
  126. if (node.operator === "=") {
  127. return this.setSuperProperty(node.left.property, node.right, node.left.computed);
  128. } else {
  129. ref = ref || path.scope.generateUidIdentifier("ref");
  130. return [t.variableDeclaration("var", [t.variableDeclarator(ref, node.left)]), t.expressionStatement(t.assignmentExpression("=", node.left, t.binaryExpression(node.operator[0], ref, node.right)))];
  131. }
  132. };
  133. ReplaceSupers.prototype.specHandle = function specHandle(path) {
  134. var property = void 0;
  135. var computed = void 0;
  136. var args = void 0;
  137. var thisReference = void 0;
  138. var parent = path.parent;
  139. var node = path.node;
  140. if (isIllegalBareSuper(node, parent)) {
  141. throw path.buildCodeFrameError(messages.get("classesIllegalBareSuper"));
  142. }
  143. if (t.isCallExpression(node)) {
  144. var callee = node.callee;
  145. if (t.isSuper(callee)) {
  146. return;
  147. } else if (isMemberExpressionSuper(callee)) {
  148. property = callee.property;
  149. computed = callee.computed;
  150. args = node.arguments;
  151. }
  152. } else if (t.isMemberExpression(node) && t.isSuper(node.object)) {
  153. property = node.property;
  154. computed = node.computed;
  155. } else if (t.isUpdateExpression(node) && isMemberExpressionSuper(node.argument)) {
  156. var binary = t.binaryExpression(node.operator[0], node.argument, t.numericLiteral(1));
  157. if (node.prefix) {
  158. return this.specHandleAssignmentExpression(null, path, binary);
  159. } else {
  160. var ref = path.scope.generateUidIdentifier("ref");
  161. return this.specHandleAssignmentExpression(ref, path, binary).concat(t.expressionStatement(ref));
  162. }
  163. } else if (t.isAssignmentExpression(node) && isMemberExpressionSuper(node.left)) {
  164. return this.specHandleAssignmentExpression(null, path, node);
  165. }
  166. if (!property) return;
  167. var superProperty = this.getSuperProperty(property, computed, thisReference);
  168. if (args) {
  169. return this.optimiseCall(superProperty, args);
  170. } else {
  171. return superProperty;
  172. }
  173. };
  174. ReplaceSupers.prototype.optimiseCall = function optimiseCall(callee, args) {
  175. var thisNode = t.thisExpression();
  176. thisNode[HARDCORE_THIS_REF] = true;
  177. return (0, _babelHelperOptimiseCallExpression2.default)(callee, thisNode, args);
  178. };
  179. return ReplaceSupers;
  180. }();
  181. exports.default = ReplaceSupers;
  182. module.exports = exports["default"];