index.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = _default;
  6. function _helperGetFunctionArity() {
  7. const data = _interopRequireDefault(require("@babel/helper-get-function-arity"));
  8. _helperGetFunctionArity = function () {
  9. return data;
  10. };
  11. return data;
  12. }
  13. function _template() {
  14. const data = _interopRequireDefault(require("@babel/template"));
  15. _template = function () {
  16. return data;
  17. };
  18. return data;
  19. }
  20. function t() {
  21. const data = _interopRequireWildcard(require("@babel/types"));
  22. t = function () {
  23. return data;
  24. };
  25. return data;
  26. }
  27. 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)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }
  28. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  29. const buildPropertyMethodAssignmentWrapper = (0, _template().default)(`
  30. (function (FUNCTION_KEY) {
  31. function FUNCTION_ID() {
  32. return FUNCTION_KEY.apply(this, arguments);
  33. }
  34. FUNCTION_ID.toString = function () {
  35. return FUNCTION_KEY.toString();
  36. }
  37. return FUNCTION_ID;
  38. })(FUNCTION)
  39. `);
  40. const buildGeneratorPropertyMethodAssignmentWrapper = (0, _template().default)(`
  41. (function (FUNCTION_KEY) {
  42. function* FUNCTION_ID() {
  43. return yield* FUNCTION_KEY.apply(this, arguments);
  44. }
  45. FUNCTION_ID.toString = function () {
  46. return FUNCTION_KEY.toString();
  47. };
  48. return FUNCTION_ID;
  49. })(FUNCTION)
  50. `);
  51. const visitor = {
  52. "ReferencedIdentifier|BindingIdentifier"(path, state) {
  53. if (path.node.name !== state.name) return;
  54. const localDeclar = path.scope.getBindingIdentifier(state.name);
  55. if (localDeclar !== state.outerDeclar) return;
  56. state.selfReference = true;
  57. path.stop();
  58. }
  59. };
  60. function getNameFromLiteralId(id) {
  61. if (t().isNullLiteral(id)) {
  62. return "null";
  63. }
  64. if (t().isRegExpLiteral(id)) {
  65. return `_${id.pattern}_${id.flags}`;
  66. }
  67. if (t().isTemplateLiteral(id)) {
  68. return id.quasis.map(quasi => quasi.value.raw).join("");
  69. }
  70. if (id.value !== undefined) {
  71. return id.value + "";
  72. }
  73. return "";
  74. }
  75. function wrap(state, method, id, scope) {
  76. if (state.selfReference) {
  77. if (scope.hasBinding(id.name) && !scope.hasGlobal(id.name)) {
  78. scope.rename(id.name);
  79. } else {
  80. if (!t().isFunction(method)) return;
  81. let build = buildPropertyMethodAssignmentWrapper;
  82. if (method.generator) {
  83. build = buildGeneratorPropertyMethodAssignmentWrapper;
  84. }
  85. const template = build({
  86. FUNCTION: method,
  87. FUNCTION_ID: id,
  88. FUNCTION_KEY: scope.generateUidIdentifier(id.name)
  89. }).expression;
  90. const params = template.callee.body.body[0].params;
  91. for (let i = 0, len = (0, _helperGetFunctionArity().default)(method); i < len; i++) {
  92. params.push(scope.generateUidIdentifier("x"));
  93. }
  94. return template;
  95. }
  96. }
  97. method.id = id;
  98. scope.getProgramParent().references[id.name] = true;
  99. }
  100. function visit(node, name, scope) {
  101. const state = {
  102. selfAssignment: false,
  103. selfReference: false,
  104. outerDeclar: scope.getBindingIdentifier(name),
  105. references: [],
  106. name: name
  107. };
  108. const binding = scope.getOwnBinding(name);
  109. if (binding) {
  110. if (binding.kind === "param") {
  111. state.selfReference = true;
  112. } else {}
  113. } else if (state.outerDeclar || scope.hasGlobal(name)) {
  114. scope.traverse(node, visitor, state);
  115. }
  116. return state;
  117. }
  118. function _default({
  119. node,
  120. parent,
  121. scope,
  122. id
  123. }, localBinding = false) {
  124. if (node.id) return;
  125. if ((t().isObjectProperty(parent) || t().isObjectMethod(parent, {
  126. kind: "method"
  127. })) && (!parent.computed || t().isLiteral(parent.key))) {
  128. id = parent.key;
  129. } else if (t().isVariableDeclarator(parent)) {
  130. id = parent.id;
  131. if (t().isIdentifier(id) && !localBinding) {
  132. const binding = scope.parent.getBinding(id.name);
  133. if (binding && binding.constant && scope.getBinding(id.name) === binding) {
  134. node.id = t().cloneNode(id);
  135. node.id[t().NOT_LOCAL_BINDING] = true;
  136. return;
  137. }
  138. }
  139. } else if (t().isAssignmentExpression(parent)) {
  140. id = parent.left;
  141. } else if (!id) {
  142. return;
  143. }
  144. let name;
  145. if (id && t().isLiteral(id)) {
  146. name = getNameFromLiteralId(id);
  147. } else if (id && t().isIdentifier(id)) {
  148. name = id.name;
  149. }
  150. if (name === undefined) {
  151. return;
  152. }
  153. name = t().toBindingIdentifierName(name);
  154. id = t().identifier(name);
  155. id[t().NOT_LOCAL_BINDING] = true;
  156. const state = visit(node, name, scope);
  157. return wrap(state, node, id, scope) || node;
  158. }