renamer.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var t = require("@babel/types");
  7. var _t = t;
  8. var _traverseNode = require("../../traverse-node.js");
  9. var _visitors = require("../../visitors.js");
  10. var _context = require("../../path/context.js");
  11. const {
  12. getAssignmentIdentifiers
  13. } = _t;
  14. const renameVisitor = {
  15. ReferencedIdentifier({
  16. node
  17. }, state) {
  18. if (node.name === state.oldName) {
  19. node.name = state.newName;
  20. }
  21. },
  22. Scope(path, state) {
  23. if (!path.scope.bindingIdentifierEquals(state.oldName, state.binding.identifier)) {
  24. path.skip();
  25. if (path.isMethod()) {
  26. if (!path.requeueComputedKeyAndDecorators) {
  27. _context.requeueComputedKeyAndDecorators.call(path);
  28. } else {
  29. path.requeueComputedKeyAndDecorators();
  30. }
  31. }
  32. }
  33. },
  34. ObjectProperty({
  35. node,
  36. scope
  37. }, state) {
  38. const {
  39. name
  40. } = node.key;
  41. if (node.shorthand && (name === state.oldName || name === state.newName) && scope.getBindingIdentifier(name) === state.binding.identifier) {
  42. node.shorthand = false;
  43. {
  44. var _node$extra;
  45. if ((_node$extra = node.extra) != null && _node$extra.shorthand) node.extra.shorthand = false;
  46. }
  47. }
  48. },
  49. "AssignmentExpression|Declaration|VariableDeclarator"(path, state) {
  50. if (path.isVariableDeclaration()) return;
  51. const ids = path.isAssignmentExpression() ? getAssignmentIdentifiers(path.node) : path.getOuterBindingIdentifiers();
  52. for (const name in ids) {
  53. if (name === state.oldName) ids[name].name = state.newName;
  54. }
  55. }
  56. };
  57. class Renamer {
  58. constructor(binding, oldName, newName) {
  59. this.newName = newName;
  60. this.oldName = oldName;
  61. this.binding = binding;
  62. }
  63. maybeConvertFromExportDeclaration(parentDeclar) {
  64. const maybeExportDeclar = parentDeclar.parentPath;
  65. if (!maybeExportDeclar.isExportDeclaration()) {
  66. return;
  67. }
  68. if (maybeExportDeclar.isExportDefaultDeclaration()) {
  69. const {
  70. declaration
  71. } = maybeExportDeclar.node;
  72. if (t.isDeclaration(declaration) && !declaration.id) {
  73. return;
  74. }
  75. }
  76. if (maybeExportDeclar.isExportAllDeclaration()) {
  77. return;
  78. }
  79. maybeExportDeclar.splitExportDeclaration();
  80. }
  81. maybeConvertFromClassFunctionDeclaration(path) {
  82. return path;
  83. }
  84. maybeConvertFromClassFunctionExpression(path) {
  85. return path;
  86. }
  87. rename() {
  88. const {
  89. binding,
  90. oldName,
  91. newName
  92. } = this;
  93. const {
  94. scope,
  95. path
  96. } = binding;
  97. const parentDeclar = path.find(path => path.isDeclaration() || path.isFunctionExpression() || path.isClassExpression());
  98. if (parentDeclar) {
  99. const bindingIds = parentDeclar.getOuterBindingIdentifiers();
  100. if (bindingIds[oldName] === binding.identifier) {
  101. this.maybeConvertFromExportDeclaration(parentDeclar);
  102. }
  103. }
  104. const blockToTraverse = arguments[0] || scope.block;
  105. const skipKeys = {
  106. discriminant: true
  107. };
  108. if (t.isMethod(blockToTraverse)) {
  109. if (blockToTraverse.computed) {
  110. skipKeys.key = true;
  111. }
  112. if (!t.isObjectMethod(blockToTraverse)) {
  113. skipKeys.decorators = true;
  114. }
  115. }
  116. (0, _traverseNode.traverseNode)(blockToTraverse, (0, _visitors.explode)(renameVisitor), scope, this, scope.path, skipKeys);
  117. if (!arguments[0]) {
  118. scope.removeOwnBinding(oldName);
  119. scope.bindings[newName] = binding;
  120. this.binding.identifier.name = newName;
  121. }
  122. if (parentDeclar) {
  123. this.maybeConvertFromClassFunctionDeclaration(path);
  124. this.maybeConvertFromClassFunctionExpression(path);
  125. }
  126. }
  127. }
  128. exports.default = Renamer;
  129. //# sourceMappingURL=renamer.js.map