/
renamer.js
38 lines (32 loc) 路 948 Bytes
/
renamer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
const t = require('@babel/types');
function rename(scope, oldName, newName) {
if (oldName === newName) {
return;
}
let binding = scope.getBinding(oldName);
// Rename all constant violations
for (let violation of binding.constantViolations) {
let bindingIds = violation.getBindingIdentifierPaths(true, false);
for (let name in bindingIds) {
if (name === oldName) {
for (let idPath of bindingIds[name]) {
idPath.node.name = newName;
}
}
}
}
// Rename all references
for (let path of binding.referencePaths) {
if (t.isExportSpecifier(path.parent) && path.parentPath.parent.source) {
continue;
}
if (path.node.name === oldName) {
path.node.name = newName;
}
}
// Rename binding identifier, and update scope.
scope.removeOwnBinding(oldName);
scope.bindings[newName] = binding;
binding.identifier.name = newName;
}
module.exports = rename;