Skip to content

Commit

Permalink
fix: rename function declaration
Browse files Browse the repository at this point in the history
  • Loading branch information
j4k0xb committed Jan 3, 2024
1 parent ed75f2b commit aa26838
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/webcrack/src/ast-utils/rename.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ export function renameFast(binding: Binding, newName: string): void {
},
noScope: true,
});
} else if (ref.isFunctionDeclaration() && t.isIdentifier(ref.node.id)) {
ref.node.id.name = newName;
} else {
throw new Error(
`Unexpected constant violation (${ref.type}): ${codePreview(ref.node)}`,
Expand Down
14 changes: 14 additions & 0 deletions packages/webcrack/src/ast-utils/test/rename.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,20 @@ describe('rename variable', () => {
`);
});

test('duplicate function binding', () => {
const ast = parse('var a; function a() {}');
traverse(ast, {
Program(path) {
const binding = path.scope.getBinding('a')!;
renameFast(binding, 'b');
},
});
expect(ast).toMatchInlineSnapshot(`
var b;
function b() {}
`);
});

test('different types of assignments', () => {
const ast = parse(`
var a = 1;
Expand Down

0 comments on commit aa26838

Please sign in to comment.