1b3d4a1296ad5d4259c39373df54dccdd8462f3038f19c11808dcbc9e6dc00c885925344b7b37144bbb2fbddf25f2097504a5114edb33fd4273bbe3b8fc3ad 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. "use strict";
  2. exports.__esModule = true;
  3. var _symbol = require("babel-runtime/core-js/symbol");
  4. var _symbol2 = _interopRequireDefault(_symbol);
  5. var _plugin = require("../plugin");
  6. var _plugin2 = _interopRequireDefault(_plugin);
  7. var _babelTypes = require("babel-types");
  8. var t = _interopRequireWildcard(_babelTypes);
  9. 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; } }
  10. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  11. var SUPER_THIS_BOUND = (0, _symbol2.default)("super this bound");
  12. var superVisitor = {
  13. CallExpression: function CallExpression(path) {
  14. if (!path.get("callee").isSuper()) return;
  15. var node = path.node;
  16. if (node[SUPER_THIS_BOUND]) return;
  17. node[SUPER_THIS_BOUND] = true;
  18. path.replaceWith(t.assignmentExpression("=", this.id, node));
  19. }
  20. };
  21. exports.default = new _plugin2.default({
  22. name: "internal.shadowFunctions",
  23. visitor: {
  24. ThisExpression: function ThisExpression(path) {
  25. remap(path, "this");
  26. },
  27. ReferencedIdentifier: function ReferencedIdentifier(path) {
  28. if (path.node.name === "arguments") {
  29. remap(path, "arguments");
  30. }
  31. }
  32. }
  33. });
  34. function shouldShadow(path, shadowPath) {
  35. if (path.is("_forceShadow")) {
  36. return true;
  37. } else {
  38. return shadowPath;
  39. }
  40. }
  41. function remap(path, key) {
  42. var shadowPath = path.inShadow(key);
  43. if (!shouldShadow(path, shadowPath)) return;
  44. var shadowFunction = path.node._shadowedFunctionLiteral;
  45. var currentFunction = void 0;
  46. var passedShadowFunction = false;
  47. var fnPath = path.find(function (innerPath) {
  48. if (innerPath.parentPath && innerPath.parentPath.isClassProperty() && innerPath.key === "value") {
  49. return true;
  50. }
  51. if (path === innerPath) return false;
  52. if (innerPath.isProgram() || innerPath.isFunction()) {
  53. currentFunction = currentFunction || innerPath;
  54. }
  55. if (innerPath.isProgram()) {
  56. passedShadowFunction = true;
  57. return true;
  58. } else if (innerPath.isFunction() && !innerPath.isArrowFunctionExpression()) {
  59. if (shadowFunction) {
  60. if (innerPath === shadowFunction || innerPath.node === shadowFunction.node) return true;
  61. } else {
  62. if (!innerPath.is("shadow")) return true;
  63. }
  64. passedShadowFunction = true;
  65. return false;
  66. }
  67. return false;
  68. });
  69. if (shadowFunction && fnPath.isProgram() && !shadowFunction.isProgram()) {
  70. fnPath = path.findParent(function (p) {
  71. return p.isProgram() || p.isFunction();
  72. });
  73. }
  74. if (fnPath === currentFunction) return;
  75. if (!passedShadowFunction) return;
  76. var cached = fnPath.getData(key);
  77. if (!cached) {
  78. var id = path.scope.generateUidIdentifier(key);
  79. fnPath.setData(key, id);
  80. cached = id;
  81. var classPath = fnPath.findParent(function (p) {
  82. return p.isClass();
  83. });
  84. var hasSuperClass = !!(classPath && classPath.node && classPath.node.superClass);
  85. if (key === "this" && fnPath.isMethod({ kind: "constructor" }) && hasSuperClass) {
  86. fnPath.scope.push({ id: id });
  87. fnPath.traverse(superVisitor, { id: id });
  88. } else {
  89. var init = key === "this" ? t.thisExpression() : t.identifier(key);
  90. if (shadowFunction) init._shadowedFunctionLiteral = shadowFunction;
  91. fnPath.scope.push({ id: id, init: init });
  92. }
  93. }
  94. var node = t.cloneDeep(cached);
  95. node.loc = path.node.loc;
  96. return path.replaceWith(node);
  97. }
  98. module.exports = exports["default"];