index.js 4.3 KB

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