index.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.default = function (_ref) {
  4. var node = _ref.node,
  5. parent = _ref.parent,
  6. scope = _ref.scope,
  7. id = _ref.id;
  8. if (node.id) return;
  9. if ((t.isObjectProperty(parent) || t.isObjectMethod(parent, { kind: "method" })) && (!parent.computed || t.isLiteral(parent.key))) {
  10. id = parent.key;
  11. } else if (t.isVariableDeclarator(parent)) {
  12. id = parent.id;
  13. if (t.isIdentifier(id)) {
  14. var binding = scope.parent.getBinding(id.name);
  15. if (binding && binding.constant && scope.getBinding(id.name) === binding) {
  16. node.id = id;
  17. node.id[t.NOT_LOCAL_BINDING] = true;
  18. return;
  19. }
  20. }
  21. } else if (t.isAssignmentExpression(parent)) {
  22. id = parent.left;
  23. } else if (!id) {
  24. return;
  25. }
  26. var name = void 0;
  27. if (id && t.isLiteral(id)) {
  28. name = id.value;
  29. } else if (id && t.isIdentifier(id)) {
  30. name = id.name;
  31. } else {
  32. return;
  33. }
  34. name = t.toBindingIdentifierName(name);
  35. id = t.identifier(name);
  36. id[t.NOT_LOCAL_BINDING] = true;
  37. var state = visit(node, name, scope);
  38. return wrap(state, node, id, scope) || node;
  39. };
  40. var _babelHelperGetFunctionArity = require("babel-helper-get-function-arity");
  41. var _babelHelperGetFunctionArity2 = _interopRequireDefault(_babelHelperGetFunctionArity);
  42. var _babelTemplate = require("babel-template");
  43. var _babelTemplate2 = _interopRequireDefault(_babelTemplate);
  44. var _babelTypes = require("babel-types");
  45. var t = _interopRequireWildcard(_babelTypes);
  46. 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)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
  47. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  48. var buildPropertyMethodAssignmentWrapper = (0, _babelTemplate2.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");
  49. var buildGeneratorPropertyMethodAssignmentWrapper = (0, _babelTemplate2.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");
  50. var visitor = {
  51. "ReferencedIdentifier|BindingIdentifier": function ReferencedIdentifierBindingIdentifier(path, state) {
  52. if (path.node.name !== state.name) return;
  53. var localDeclar = path.scope.getBindingIdentifier(state.name);
  54. if (localDeclar !== state.outerDeclar) return;
  55. state.selfReference = true;
  56. path.stop();
  57. }
  58. };
  59. function wrap(state, method, id, scope) {
  60. if (state.selfReference) {
  61. if (scope.hasBinding(id.name) && !scope.hasGlobal(id.name)) {
  62. scope.rename(id.name);
  63. } else {
  64. if (!t.isFunction(method)) return;
  65. var build = buildPropertyMethodAssignmentWrapper;
  66. if (method.generator) build = buildGeneratorPropertyMethodAssignmentWrapper;
  67. var _template = build({
  68. FUNCTION: method,
  69. FUNCTION_ID: id,
  70. FUNCTION_KEY: scope.generateUidIdentifier(id.name)
  71. }).expression;
  72. _template.callee._skipModulesRemap = true;
  73. var params = _template.callee.body.body[0].params;
  74. for (var i = 0, len = (0, _babelHelperGetFunctionArity2.default)(method); i < len; i++) {
  75. params.push(scope.generateUidIdentifier("x"));
  76. }
  77. return _template;
  78. }
  79. }
  80. method.id = id;
  81. scope.getProgramParent().references[id.name] = true;
  82. }
  83. function visit(node, name, scope) {
  84. var state = {
  85. selfAssignment: false,
  86. selfReference: false,
  87. outerDeclar: scope.getBindingIdentifier(name),
  88. references: [],
  89. name: name
  90. };
  91. var binding = scope.getOwnBinding(name);
  92. if (binding) {
  93. if (binding.kind === "param") {
  94. state.selfReference = true;
  95. } else {}
  96. } else if (state.outerDeclar || scope.hasGlobal(name)) {
  97. scope.traverse(node, visitor, state);
  98. }
  99. return state;
  100. }
  101. module.exports = exports["default"];