Skip to content

Commit

Permalink
fix: duplicated node when extracting sequence from for loop (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
j4k0xb committed Jan 28, 2024
1 parent ac65b12 commit 2d889c6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
15 changes: 13 additions & 2 deletions packages/webcrack/src/unminify/test/sequence.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,25 @@ test('rearrange from for-in', () =>
for (let key in object) {}
`));

test('rearrange from for loop init', () =>
test('rearrange from for loop init', () => {
expectJS(`
for((a(), b());;);
`).toMatchInlineSnapshot(`
a();
b();
for (;;);
`));
`);

expectJS(`
if (1) for ((a(), b());;) {}
`).toMatchInlineSnapshot(`
if (1) {
a();
b();
for (;;) {}
}
`);
});

test('rearrange from for loop update', () =>
expectJS(`
Expand Down
10 changes: 5 additions & 5 deletions packages/webcrack/src/unminify/transforms/sequence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ export default {
exit(path) {
if (!assignmentMatcher.match(path.node)) return;

const value = assignedSequence.current!.expressions.pop()!;
path.get('right').replaceWith(value);
const { expressions } = assignedSequence.current!;
path.node.right = expressions.pop()!;
const newNodes = path.parentPath.isExpressionStatement()
? assignedSequence.current!.expressions.map(t.expressionStatement)
: assignedSequence.current!.expressions;
? expressions.map(t.expressionStatement)
: expressions;
path.insertBefore(newNodes);
this.changes++;
},
Expand Down Expand Up @@ -122,8 +122,8 @@ export default {
const statements = path.node.init.expressions.map(
t.expressionStatement,
);
path.insertBefore(statements);
path.node.init = null;
path.insertBefore(statements);
this.changes++;
}
if (
Expand Down

0 comments on commit 2d889c6

Please sign in to comment.