Skip to content

Commit

Permalink
fix: rename errors for delete, array patterns and exports (#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
j4k0xb committed Aug 16, 2024
1 parent 70064cf commit e426513
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
13 changes: 13 additions & 0 deletions packages/webcrack/src/ast-utils/rename.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { codePreview } from './generator';

export function renameFast(binding: Binding, newName: string): void {
binding.referencePaths.forEach((ref) => {
if (ref.isExportDefaultDeclaration()) return;
if (!ref.isIdentifier()) {
throw new Error(
`Unexpected reference (${ref.type}): ${codePreview(ref.node)}`,
Expand All @@ -30,8 +31,20 @@ export function renameFast(binding: Binding, newName: string): void {
ref.node.left.name = newName;
} else if (ref.isUpdateExpression() && t.isIdentifier(ref.node.argument)) {
ref.node.argument.name = newName;
} else if (
ref.isUnaryExpression({ operator: 'delete' }) &&
t.isIdentifier(ref.node.argument)
) {
ref.node.argument.name = newName;
} else if (ref.isVariableDeclarator() && t.isIdentifier(ref.node.id)) {
ref.node.id.name = newName;
} else if (ref.isVariableDeclarator() && t.isArrayPattern(ref.node.id)) {
const ids = ref.getBindingIdentifiers();
for (const id in ids) {
if (id === binding.identifier.name) {
ids[id].name = newName;
}
}
} else if (ref.isFor() || patternMatcher.match(ref.node)) {
traverse(ref.node, {
Identifier(path) {
Expand Down
4 changes: 4 additions & 0 deletions packages/webcrack/src/ast-utils/test/rename.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,11 @@ describe('rename variable', () => {
a = 3;
a++;
[a] = [2];
var [a] = [1];
({...a} = {});
for (a of []);
for (a in []);
delete a;
`);
traverse(ast, {
Program(path) {
Expand All @@ -57,11 +59,13 @@ describe('rename variable', () => {
b = 3;
b++;
[b] = [2];
var [b] = [1];
({
...b
} = {});
for (b of []);
for (b in []);
delete b;
`);
});
});
Expand Down
1 change: 1 addition & 0 deletions packages/webcrack/test/mangle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ test('class', () => {

test('function', () => {
expectJS('function abc() {}').toMatchInlineSnapshot('function f() {}');
expectJS('export default function x() {}').toMatchInlineSnapshot(`export default function f() {}`);
});

test('parameters', () => {
Expand Down

0 comments on commit e426513

Please sign in to comment.