index.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = _default;
  6. function _helperGetFunctionArity() {
  7. var data = _interopRequireDefault(require("@babel/helper-get-function-arity"));
  8. _helperGetFunctionArity = function _helperGetFunctionArity() {
  9. return data;
  10. };
  11. return data;
  12. }
  13. function _template2() {
  14. var data = _interopRequireDefault(require("@babel/template"));
  15. _template2 = function _template2() {
  16. return data;
  17. };
  18. return data;
  19. }
  20. function t() {
  21. var data = _interopRequireWildcard(require("@babel/types"));
  22. t = function t() {
  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. var buildPropertyMethodAssignmentWrapper = (0, _template2().default)("\n (function (FUNCTION_KEY) {\n function FUNCTION_ID() {\n return FUNCTION_KEY.apply(this, arguments);\n }\n\n FUNCTION_ID.toString = function () {\n return FUNCTION_KEY.toString();\n }\n\n return FUNCTION_ID;\n })(FUNCTION)\n");
  30. var buildGeneratorPropertyMethodAssignmentWrapper = (0, _template2().default)("\n (function (FUNCTION_KEY) {\n function* FUNCTION_ID() {\n return yield* FUNCTION_KEY.apply(this, arguments);\n }\n\n FUNCTION_ID.toString = function () {\n return FUNCTION_KEY.toString();\n };\n\n return FUNCTION_ID;\n })(FUNCTION)\n");
  31. var visitor = {
  32. "ReferencedIdentifier|BindingIdentifier": function ReferencedIdentifierBindingIdentifier(path, state) {
  33. if (path.node.name !== state.name) return;
  34. var localDeclar = path.scope.getBindingIdentifier(state.name);
  35. if (localDeclar !== state.outerDeclar) return;
  36. state.selfReference = true;
  37. path.stop();
  38. }
  39. };
  40. function wrap(state, method, id, scope) {
  41. if (state.selfReference) {
  42. if (scope.hasBinding(id.name) && !scope.hasGlobal(id.name)) {
  43. scope.rename(id.name);
  44. } else {
  45. if (!t().isFunction(method)) return;
  46. var build = buildPropertyMethodAssignmentWrapper;
  47. if (method.generator) {
  48. build = buildGeneratorPropertyMethodAssignmentWrapper;
  49. }
  50. var _template = build({
  51. FUNCTION: method,
  52. FUNCTION_ID: id,
  53. FUNCTION_KEY: scope.generateUidIdentifier(id.name)
  54. }).expression;
  55. var params = _template.callee.body.body[0].params;
  56. for (var i = 0, len = (0, _helperGetFunctionArity().default)(method); i < len; i++) {
  57. params.push(scope.generateUidIdentifier("x"));
  58. }
  59. return _template;
  60. }
  61. }
  62. method.id = id;
  63. scope.getProgramParent().references[id.name] = true;
  64. }
  65. function visit(node, name, scope) {
  66. var state = {
  67. selfAssignment: false,
  68. selfReference: false,
  69. outerDeclar: scope.getBindingIdentifier(name),
  70. references: [],
  71. name: name
  72. };
  73. var binding = scope.getOwnBinding(name);
  74. if (binding) {
  75. if (binding.kind === "param") {
  76. state.selfReference = true;
  77. } else {}
  78. } else if (state.outerDeclar || scope.hasGlobal(name)) {
  79. scope.traverse(node, visitor, state);
  80. }
  81. return state;
  82. }
  83. function _default(_ref, localBinding) {
  84. var node = _ref.node,
  85. parent = _ref.parent,
  86. scope = _ref.scope,
  87. id = _ref.id;
  88. if (localBinding === void 0) {
  89. localBinding = false;
  90. }
  91. if (node.id) return;
  92. if ((t().isObjectProperty(parent) || t().isObjectMethod(parent, {
  93. kind: "method"
  94. })) && (!parent.computed || t().isLiteral(parent.key))) {
  95. id = parent.key;
  96. } else if (t().isVariableDeclarator(parent)) {
  97. id = parent.id;
  98. if (t().isIdentifier(id) && !localBinding) {
  99. var binding = scope.parent.getBinding(id.name);
  100. if (binding && binding.constant && scope.getBinding(id.name) === binding) {
  101. node.id = t().cloneNode(id);
  102. node.id[t().NOT_LOCAL_BINDING] = true;
  103. return;
  104. }
  105. }
  106. } else if (t().isAssignmentExpression(parent)) {
  107. id = parent.left;
  108. } else if (!id) {
  109. return;
  110. }
  111. var name;
  112. if (id && t().isLiteral(id)) {
  113. name = id.value;
  114. } else if (id && t().isIdentifier(id)) {
  115. name = id.name;
  116. } else {
  117. return;
  118. }
  119. name = t().toBindingIdentifierName(name);
  120. id = t().identifier(name);
  121. id[t().NOT_LOCAL_BINDING] = true;
  122. var state = visit(node, name, scope);
  123. return wrap(state, node, id, scope) || node;
  124. }