index.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 = _template.default.statement(`
  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 = _template.default.statement(`
  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. 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, supportUnicodeId = false) {
  124. if (node.id) return;
  125. if ((isObjectProperty(parent) || isObjectMethod(parent, {
  126. kind: "method"
  127. })) && (!parent.computed || isLiteral(parent.key))) {
  128. id = parent.key;
  129. } else if (isVariableDeclarator(parent)) {
  130. id = parent.id;
  131. if (isIdentifier(id) && !localBinding) {
  132. const binding = scope.parent.getBinding(id.name);
  133. if (binding && binding.constant && scope.getBinding(id.name) === binding) {
  134. node.id = cloneNode(id);
  135. node.id[NOT_LOCAL_BINDING] = true;
  136. return;
  137. }
  138. }
  139. } else if (isAssignmentExpression(parent, {
  140. operator: "="
  141. })) {
  142. id = parent.left;
  143. } else if (!id) {
  144. return;
  145. }
  146. let name;
  147. if (id && isLiteral(id)) {
  148. name = getNameFromLiteralId(id);
  149. } else if (id && isIdentifier(id)) {
  150. name = id.name;
  151. }
  152. if (name === undefined) {
  153. return;
  154. }
  155. if (!supportUnicodeId && isFunction(node) && /[\uD800-\uDFFF]/.test(name)) {
  156. return;
  157. }
  158. name = toBindingIdentifierName(name);
  159. const newId = identifier(name);
  160. newId[NOT_LOCAL_BINDING] = true;
  161. const state = visit(node, name, scope);
  162. return wrap(state, node, newId, scope) || node;
  163. }
  164. //# sourceMappingURL=index.js.map