Skip to content

Commit

Permalink
feat: transform sequence assignments with literal keys: `a[1] = <sequ…
Browse files Browse the repository at this point in the history
…ence>`
  • Loading branch information
j4k0xb committed Jan 16, 2024
1 parent 8e8714a commit b3bd94c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 20 deletions.
56 changes: 38 additions & 18 deletions packages/webcrack/src/unminify/test/sequence.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ test('rearrange variable declarator', () => {
`);

expectJS(`
for(let a = (b(), c());;) {}
for (let a = (b(), c());;) {}
`).toMatchInlineSnapshot(`
b();
for (let a = c();;) {}
Expand All @@ -104,39 +104,59 @@ test('rearrange variable declarator', () => {

test('rearrange assignment', () => {
expectJS(`
a = (b = null, c);
a = (b(), c());
`).toMatchInlineSnapshot(`
b = null;
a = c;
b();
a = c();
`);

expectJS(`
console.log(a = (b = null, c));
`).toMatchInlineSnapshot(`console.log((b = null, a = c));`);
a.x = (b(), c());
`).toMatchInlineSnapshot(`
b();
a.x = c();
`);

expectJS(`
while (a = (b = null, c));
a[1] = (b(), c());
`).toMatchInlineSnapshot(`
b = null;
while (a = c);
b();
a[1] = c();
`);

expectJS(`
a ||= (b, c);
a &&= (b, c);
a ??= (b, c);
a[x()] = (b(), c());
`).toMatchInlineSnapshot(`a[x()] = (b(), c());`);

expectJS(`
console.log(a = (b(), c()));
`).toMatchInlineSnapshot(`
a ||= (b, c);
a &&= (b, c);
a ??= (b, c);
console.log((b(), a = c()));
`);

expectJS(`
for (;;) a = (b, c);
while (a = (b(), c()));
`).toMatchInlineSnapshot(`
b();
while (a = c());
`);

expectJS(`
a ||= (b(), c());
a &&= (b(), c());
a ??= (b(), c());
`).toMatchInlineSnapshot(`
a ||= (b(), c());
a &&= (b(), c());
a ??= (b(), c());
`);

expectJS(`
for (;;) a = (b(), c());
`).toMatchInlineSnapshot(`
for (;;) {
b;
a = c;
b();
a = c();
}
`);
});
4 changes: 2 additions & 2 deletions packages/webcrack/src/unminify/transforms/sequence.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as t from '@babel/types';
import * as m from '@codemod/matchers';
import type { Transform } from '../../ast-utils';
import { safeLiteral, type Transform } from '../../ast-utils';

export default {
name: 'sequence',
Expand All @@ -10,7 +10,7 @@ export default {
// `obj.foo.bar = (x(), y());` would trigger the getter for `obj.foo` before `x()` is evaluated.
const assignmentVariable = m.or(
m.identifier(),
m.memberExpression(m.identifier(), m.identifier()),
m.memberExpression(m.identifier(), m.or(m.identifier(), safeLiteral)),
);
const assignedSequence = m.capture(m.sequenceExpression());
const assignmentMatcher = m.assignmentExpression(
Expand Down

0 comments on commit b3bd94c

Please sign in to comment.