Skip to content

Commit

Permalink
feat: mangle with additional sources for init names (#155)
Browse files Browse the repository at this point in the history
* incorporate number/string literals

* add arrays and object expressions

* limit strings to 100 chars

* test: mangle string with non-ASCII characters

---------

Co-authored-by: j4k0xb <[email protected]>
  • Loading branch information
Le0Developer and j4k0xb authored Mar 8, 2025
1 parent 60dbb7f commit 5dd3d93
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
12 changes: 11 additions & 1 deletion packages/webcrack/src/transforms/mangle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,21 @@ function generateExpressionName(
);
} else if (expression.isThisExpression()) {
return 'this';
} else if (expression.isNumericLiteral()) {
return 'LN' + expression.node.value.toString();
} else if (expression.isStringLiteral()) {
return 'LS' + titleCase(expression.node.value).slice(0, 100);
} else if (expression.isObjectExpression()) {
return 'O';
} else if (expression.isArrayExpression()) {
return 'A';
} else {
return undefined;
}
}

function titleCase(str: string) {
return str.length > 0 ? str[0].toUpperCase() + str.slice(1) : str;
return str
.replace(/(?:^|\s)([a-z])/g, (_, m) => (m as string).toUpperCase())
.replace(/[^a-zA-Z0-9$_]/g, '');
}
37 changes: 34 additions & 3 deletions packages/webcrack/test/mangle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import mangle from '../src/transforms/mangle';
const expectJS = testTransform(mangle);

test('variable', () => {
expectJS('let x = 1;').toMatchInlineSnapshot('let v = 1;');
expectJS('let x = 1;').toMatchInlineSnapshot('let vLN1 = 1;');
expectJS('let x = exports;').toMatchInlineSnapshot(`let vExports = exports;`);
expectJS('let x = () => {};').toMatchInlineSnapshot(`let vF = () => {};`);
expectJS('let x = class {};').toMatchInlineSnapshot(`let vC = class {};`);
expectJS('let x = Array(100);').toMatchInlineSnapshot(
`let vArray = Array(100);`,
);
expectJS('let x = []').toMatchInlineSnapshot(`let vA = [];`);
expectJS('let x = {}').toMatchInlineSnapshot(`let vO = {};`);
expectJS('let [x] = 1;').toMatchInlineSnapshot(`let [v] = 1;`);
expectJS('const x = require("fs");').toMatchInlineSnapshot(
`const fs = require("fs");`,
Expand All @@ -23,6 +25,33 @@ test('variable', () => {
const nodeFs = require("node:fs");
const nodeFs2 = require("node:fs");
`);

expectJS(
`
let a = 100;
let b = 200;
let c = 300;
`,
).toMatchInlineSnapshot(`
let vLN100 = 100;
let vLN200 = 200;
let vLN300 = 300;
`);

expectJS(
`
let a = "hello world";
let b = "foo-bar-🗿-ä";
`,
).toMatchInlineSnapshot(`
let vLSHelloWorld = "hello world";
let vLSFoobar = "foo-bar-🗿-ä";
`);

const veryLongString = 'a'.repeat(1000);
expectJS(`let x = "${veryLongString}";`).toMatchInlineSnapshot(
`let vLSA${veryLongString.slice(0, 99)} = "${veryLongString}";`,
);
});

test('ignore exports', () => {
Expand All @@ -38,7 +67,7 @@ test('only rename _0x variable', () => {
`,
(id) => id.startsWith('_0x'),
).toMatchInlineSnapshot(`
let v = 1;
let vLN1 = 1;
let foo = 2;
`);
});
Expand All @@ -49,7 +78,9 @@ test('class', () => {

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

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

0 comments on commit 5dd3d93

Please sign in to comment.