|
1 | | -import type { Binding, NodePath } from '@babel/traverse'; |
2 | | -import traverse from '@babel/traverse'; |
3 | | -import * as t from '@babel/types'; |
4 | | -import * as m from '@codemod/matchers'; |
5 | | -import { codePreview } from './generator'; |
| 1 | +import type { Binding, NodePath } from '@babel/traverse' |
| 2 | +import traverse from '@babel/traverse' |
| 3 | +import * as t from '@babel/types' |
| 4 | +import * as m from '@codemod/matchers' |
| 5 | +import { codePreview } from './generator' |
6 | 6 |
|
7 | 7 | export function renameFast(binding: Binding, newName: string): void { |
8 | 8 | binding.referencePaths.forEach((ref) => { |
9 | | - if (ref.isExportDefaultDeclaration()) return; |
| 9 | + if (ref.isExportDefaultDeclaration()) return |
10 | 10 | if (!ref.isIdentifier()) { |
11 | 11 | throw new Error( |
12 | 12 | `Unexpected reference (${ref.type}): ${codePreview(ref.node)}`, |
13 | | - ); |
| 13 | + ) |
14 | 14 | } |
15 | 15 |
|
16 | 16 | // To avoid conflicts with other bindings of the same name |
17 | | - if (ref.scope.hasBinding(newName)) ref.scope.rename(newName); |
18 | | - ref.node.name = newName; |
19 | | - }); |
| 17 | + if (ref.scope.hasBinding(newName)) ref.scope.rename(newName) |
| 18 | + ref.node.name = newName |
| 19 | + }) |
20 | 20 |
|
21 | 21 | // Also update assignments |
22 | 22 | const patternMatcher = m.assignmentExpression( |
23 | 23 | '=', |
24 | 24 | m.or(m.arrayPattern(), m.objectPattern()), |
25 | | - ); |
| 25 | + ) |
26 | 26 | binding.constantViolations.forEach((ref) => { |
27 | 27 | // To avoid conflicts with other bindings of the same name |
28 | | - if (ref.scope.hasBinding(newName)) ref.scope.rename(newName); |
| 28 | + if (ref.scope.hasBinding(newName)) ref.scope.rename(newName) |
29 | 29 |
|
30 | 30 | if (ref.isAssignmentExpression() && t.isIdentifier(ref.node.left)) { |
31 | | - ref.node.left.name = newName; |
32 | | - } else if (ref.isUpdateExpression() && t.isIdentifier(ref.node.argument)) { |
33 | | - ref.node.argument.name = newName; |
34 | | - } else if ( |
35 | | - ref.isUnaryExpression({ operator: 'delete' }) && |
36 | | - t.isIdentifier(ref.node.argument) |
| 31 | + ref.node.left.name = newName |
| 32 | + } |
| 33 | + else if (ref.isUpdateExpression() && t.isIdentifier(ref.node.argument)) { |
| 34 | + ref.node.argument.name = newName |
| 35 | + } |
| 36 | + else if ( |
| 37 | + ref.isUnaryExpression({ operator: 'delete' }) |
| 38 | + && t.isIdentifier(ref.node.argument) |
37 | 39 | ) { |
38 | | - ref.node.argument.name = newName; |
39 | | - } else if (ref.isVariableDeclarator() && t.isIdentifier(ref.node.id)) { |
40 | | - ref.node.id.name = newName; |
41 | | - } else if (ref.isVariableDeclarator() && t.isArrayPattern(ref.node.id)) { |
42 | | - const ids = ref.getBindingIdentifiers(); |
| 40 | + ref.node.argument.name = newName |
| 41 | + } |
| 42 | + else if (ref.isVariableDeclarator() && t.isIdentifier(ref.node.id)) { |
| 43 | + ref.node.id.name = newName |
| 44 | + } |
| 45 | + else if (ref.isVariableDeclarator() && t.isArrayPattern(ref.node.id)) { |
| 46 | + const ids = ref.getBindingIdentifiers() |
43 | 47 | for (const id in ids) { |
44 | 48 | if (id === binding.identifier.name) { |
45 | | - ids[id].name = newName; |
| 49 | + ids[id].name = newName |
46 | 50 | } |
47 | 51 | } |
48 | | - } else if (ref.isFor() || patternMatcher.match(ref.node)) { |
| 52 | + } |
| 53 | + else if (ref.isFor() || patternMatcher.match(ref.node)) { |
49 | 54 | traverse(ref.node, { |
50 | 55 | Identifier(path) { |
51 | | - if (path.scope !== ref.scope) return path.skip(); |
| 56 | + if (path.scope !== ref.scope) return path.skip() |
52 | 57 | if (path.node.name === binding.identifier.name) { |
53 | | - path.node.name = newName; |
| 58 | + path.node.name = newName |
54 | 59 | } |
55 | 60 | }, |
56 | 61 | noScope: true, |
57 | | - }); |
58 | | - } else if (ref.isFunctionDeclaration() && t.isIdentifier(ref.node.id)) { |
59 | | - ref.node.id.name = newName; |
60 | | - } else { |
| 62 | + }) |
| 63 | + } |
| 64 | + else if (ref.isFunctionDeclaration() && t.isIdentifier(ref.node.id)) { |
| 65 | + ref.node.id.name = newName |
| 66 | + } |
| 67 | + else { |
61 | 68 | throw new Error( |
62 | 69 | `Unexpected constant violation (${ref.type}): ${codePreview(ref.node)}`, |
63 | | - ); |
| 70 | + ) |
64 | 71 | } |
65 | | - }); |
| 72 | + }) |
66 | 73 |
|
67 | | - binding.scope.removeOwnBinding(binding.identifier.name); |
68 | | - binding.scope.bindings[newName] = binding; |
69 | | - binding.identifier.name = newName; |
| 74 | + binding.scope.removeOwnBinding(binding.identifier.name) |
| 75 | + binding.scope.bindings[newName] = binding |
| 76 | + binding.identifier.name = newName |
70 | 77 | } |
71 | 78 |
|
72 | 79 | export function renameParameters( |
73 | 80 | path: NodePath<t.Function>, |
74 | 81 | newNames: string[], |
75 | 82 | ): void { |
76 | | - const params = path.node.params as t.Identifier[]; |
| 83 | + const params = path.node.params as t.Identifier[] |
77 | 84 | for (let i = 0; i < Math.min(params.length, newNames.length); i++) { |
78 | | - const binding = path.scope.getBinding(params[i].name)!; |
79 | | - renameFast(binding, newNames[i]); |
| 85 | + const binding = path.scope.getBinding(params[i].name)! |
| 86 | + renameFast(binding, newNames[i]) |
80 | 87 | } |
81 | 88 | } |
0 commit comments