index.js 4.4 KB

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