Debian Codegraph @

packages/babel-traverse/src/scope/lib/renamer.ts

raw symbols SCIP
1 import type Binding from "../binding";
2 import splitExportDeclaration from "@babel/helper-split-export-declaration";
3 import * as t from "@babel/types";
4 import type { NodePath, Visitor } from "../..";
5 import { requeueComputedKeyAndDecorators } from "@babel/helper-environment-visitor";
6 import { traverseNode } from "../../traverse-node";
7 import { explode } from "../../visitors";
8
9 const renameVisitor: Visitor<Renamer> = {
10 ReferencedIdentifier({ node }, state) {
11 if (node.name === state.oldName) {
12 node.name = state.newName;
13 }
14 },
15
16 Scope(path, state) {
17 if (
18 !path.scope.bindingIdentifierEquals(
19 state.oldName,
20 state.binding.identifier,
21 )
22 ) {
23 path.skip();
24 if (path.isMethod()) {
25 requeueComputedKeyAndDecorators(path);
26 }
27 }
28 },
29
30 "AssignmentExpression|Declaration|VariableDeclarator"(
31 path: NodePath<t.AssignmentPattern | t.Declaration | t.VariableDeclarator>,
32 state,
33 ) {
34 if (path.isVariableDeclaration()) return;
35 const ids = path.getOuterBindingIdentifiers();
36
37 for (const name in ids) {
38 if (name === state.oldName) ids[name].name = state.newName;
39 }
40 },
41 };
42
43 export default class Renamer {
44 constructor(binding: Binding, oldName: string, newName: string) {
45 this.newName = newName;
46 this.oldName = oldName;
47 this.binding = binding;
48 }
49
50 declare oldName: string;
51 declare newName: string;
52 declare binding: Binding;
53
54 maybeConvertFromExportDeclaration(parentDeclar: NodePath) {
55 const maybeExportDeclar = parentDeclar.parentPath;
56
57 if (!maybeExportDeclar.isExportDeclaration()) {
58 return;
59 }
60
61 if (maybeExportDeclar.isExportDefaultDeclaration()) {
62 const { declaration } = maybeExportDeclar.node;
63 if (t.isDeclaration(declaration) && !declaration.id) {
64 return;
65 }
66 }
67
68 if (maybeExportDeclar.isExportAllDeclaration()) {
69 return;
70 }
71
72 splitExportDeclaration(
73 maybeExportDeclar as NodePath<
74 Exclude<t.ExportDeclaration, t.ExportAllDeclaration>
75 >,
76 );
77 }
78
79 maybeConvertFromClassFunctionDeclaration(path: NodePath) {
80 return path; // TODO
81
82 // // retain the `name` of a class/function declaration
83
84 // if (!path.isFunctionDeclaration() && !path.isClassDeclaration()) return;
85 // if (this.binding.kind !== "hoisted") return;
86
87 // path.node.id = identifier(this.oldName);
88 // path.node._blockHoist = 3;
89
90 // path.replaceWith(
91 // variableDeclaration("let", [
92 // variableDeclarator(identifier(this.newName), toExpression(path.node)),
93 // ]),
94 // );
95 }
96
97 maybeConvertFromClassFunctionExpression(path: NodePath) {
98 return path; // TODO
99
100 // // retain the `name` of a class/function expression
101
102 // if (!path.isFunctionExpression() && !path.isClassExpression()) return;
103 // if (this.binding.kind !== "local") return;
104
105 // path.node.id = identifier(this.oldName);
106
107 // this.binding.scope.parent.push({
108 // id: identifier(this.newName),
109 // });
110
111 // path.replaceWith(
112 // assignmentExpression("=", identifier(this.newName), path.node),
113 // );
114 }
115
116 rename(/* Babel 7 - block?: t.Pattern | t.Scopable */) {
117 const { binding, oldName, newName } = this;
118 const { scope, path } = binding;
119
120 const parentDeclar = path.find(
121 path =>
122 path.isDeclaration() ||
123 path.isFunctionExpression() ||
124 path.isClassExpression(),
125 );
126 if (parentDeclar) {
127 const bindingIds = parentDeclar.getOuterBindingIdentifiers();
128 if (bindingIds[oldName] === binding.identifier) {
129 // When we are renaming an exported identifier, we need to ensure that
130 // the exported binding keeps the old name.
131 this.maybeConvertFromExportDeclaration(parentDeclar);
132 }
133 }
134
135 const blockToTraverse = process.env.BABEL_8_BREAKING
136 ? scope.block
137 : (arguments[0] as t.Pattern | t.Scopable) || scope.block;
138 traverseNode(
139 blockToTraverse,
140 explode(renameVisitor),
141 scope,
142 this,
143 scope.path,
144 // When blockToTraverse is a SwitchStatement, the discriminant
145 // is not part of the current scope and thus should be skipped.
146 { discriminant: true },
147 );
148
149 if (process.env.BABEL_8_BREAKING) {
150 scope.removeOwnBinding(oldName);
151 scope.bindings[newName] = binding;
152 this.binding.identifier.name = newName;
153 } else if (!arguments[0]) {
154 scope.removeOwnBinding(oldName);
155 scope.bindings[newName] = binding;
156 this.binding.identifier.name = newName;
157 }
158
159 if (parentDeclar) {
160 this.maybeConvertFromClassFunctionDeclaration(path);
161 this.maybeConvertFromClassFunctionExpression(path);
162 }
163 }
164 }