From b3bd94c6194a624ff1cce8102f2aeff8742b3e02 Mon Sep 17 00:00:00 2001 From: j4k0xb <55899582+j4k0xb@users.noreply.github.com> Date: Tue, 16 Jan 2024 20:42:18 +0100 Subject: [PATCH] feat: transform sequence assignments with literal keys: `a[1] = ` --- .../src/unminify/test/sequence.test.ts | 56 +++++++++++++------ .../src/unminify/transforms/sequence.ts | 4 +- 2 files changed, 40 insertions(+), 20 deletions(-) diff --git a/packages/webcrack/src/unminify/test/sequence.test.ts b/packages/webcrack/src/unminify/test/sequence.test.ts index a00a0ee8..f2a3cdf1 100644 --- a/packages/webcrack/src/unminify/test/sequence.test.ts +++ b/packages/webcrack/src/unminify/test/sequence.test.ts @@ -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();;) {} @@ -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(); } `); }); diff --git a/packages/webcrack/src/unminify/transforms/sequence.ts b/packages/webcrack/src/unminify/transforms/sequence.ts index dd842cdc..cf0fb0b1 100644 --- a/packages/webcrack/src/unminify/transforms/sequence.ts +++ b/packages/webcrack/src/unminify/transforms/sequence.ts @@ -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', @@ -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(