1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.default = void 0;
|
7 |
|
8 | var _binding = _interopRequireDefault(require("../binding"));
|
9 |
|
10 | var _helperSplitExportDeclaration = _interopRequireDefault(require("@babel/helper-split-export-declaration"));
|
11 |
|
12 | var t = _interopRequireWildcard(require("@babel/types"));
|
13 |
|
14 | function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
|
15 |
|
16 | function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
17 |
|
18 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
19 |
|
20 | const renameVisitor = {
|
21 | ReferencedIdentifier({
|
22 | node
|
23 | }, state) {
|
24 | if (node.name === state.oldName) {
|
25 | node.name = state.newName;
|
26 | }
|
27 | },
|
28 |
|
29 | Scope(path, state) {
|
30 | if (!path.scope.bindingIdentifierEquals(state.oldName, state.binding.identifier)) {
|
31 | path.skip();
|
32 | }
|
33 | },
|
34 |
|
35 | "AssignmentExpression|Declaration|VariableDeclarator"(path, state) {
|
36 | if (path.isVariableDeclaration()) return;
|
37 | const ids = path.getOuterBindingIdentifiers();
|
38 |
|
39 | for (const name in ids) {
|
40 | if (name === state.oldName) ids[name].name = state.newName;
|
41 | }
|
42 | }
|
43 |
|
44 | };
|
45 |
|
46 | class Renamer {
|
47 | constructor(binding, oldName, newName) {
|
48 | this.newName = newName;
|
49 | this.oldName = oldName;
|
50 | this.binding = binding;
|
51 | }
|
52 |
|
53 | maybeConvertFromExportDeclaration(parentDeclar) {
|
54 | const maybeExportDeclar = parentDeclar.parentPath;
|
55 |
|
56 | if (!maybeExportDeclar.isExportDeclaration()) {
|
57 | return;
|
58 | }
|
59 |
|
60 | if (maybeExportDeclar.isExportDefaultDeclaration() && !maybeExportDeclar.get("declaration").node.id) {
|
61 | return;
|
62 | }
|
63 |
|
64 | (0, _helperSplitExportDeclaration.default)(maybeExportDeclar);
|
65 | }
|
66 |
|
67 | maybeConvertFromClassFunctionDeclaration(path) {
|
68 | return;
|
69 | if (!path.isFunctionDeclaration() && !path.isClassDeclaration()) return;
|
70 | if (this.binding.kind !== "hoisted") return;
|
71 | path.node.id = t.identifier(this.oldName);
|
72 | path.node._blockHoist = 3;
|
73 | path.replaceWith(t.variableDeclaration("let", [t.variableDeclarator(t.identifier(this.newName), t.toExpression(path.node))]));
|
74 | }
|
75 |
|
76 | maybeConvertFromClassFunctionExpression(path) {
|
77 | return;
|
78 | if (!path.isFunctionExpression() && !path.isClassExpression()) return;
|
79 | if (this.binding.kind !== "local") return;
|
80 | path.node.id = t.identifier(this.oldName);
|
81 | this.binding.scope.parent.push({
|
82 | id: t.identifier(this.newName)
|
83 | });
|
84 | path.replaceWith(t.assignmentExpression("=", t.identifier(this.newName), path.node));
|
85 | }
|
86 |
|
87 | rename(block) {
|
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 |
|
99 | if (parentDeclar) {
|
100 | const bindingIds = parentDeclar.getOuterBindingIdentifiers();
|
101 |
|
102 | if (bindingIds[oldName] === binding.identifier) {
|
103 | this.maybeConvertFromExportDeclaration(parentDeclar);
|
104 | }
|
105 | }
|
106 |
|
107 | scope.traverse(block || scope.block, renameVisitor, this);
|
108 |
|
109 | if (!block) {
|
110 | scope.removeOwnBinding(oldName);
|
111 | scope.bindings[newName] = binding;
|
112 | this.binding.identifier.name = newName;
|
113 | }
|
114 |
|
115 | if (binding.type === "hoisted") {}
|
116 |
|
117 | if (parentDeclar) {
|
118 | this.maybeConvertFromClassFunctionDeclaration(parentDeclar);
|
119 | this.maybeConvertFromClassFunctionExpression(parentDeclar);
|
120 | }
|
121 | }
|
122 |
|
123 | }
|
124 |
|
125 | exports.default = Renamer; |
\ | No newline at end of file |