Skip to content

Commit 4449944

Browse files
committed
Fix #106
1 parent 3d6e345 commit 4449944

File tree

2 files changed

+39
-15
lines changed

2 files changed

+39
-15
lines changed

src/transforms/extraction/objectExtraction.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,10 @@ export default class ObjectExtraction extends Transform {
316316
...variableDeclarators
317317
);
318318

319+
if (declaration.kind === "const") {
320+
declaration.kind = "var";
321+
}
322+
319323
// update all identifiers that pointed to the old object
320324
objectDefChanges[name] &&
321325
objectDefChanges[name].forEach((change) => {

test/transforms/extraction/objectExtraction.test.ts

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import JsConfuser from "../../../src/index";
22

3-
it("should extract properties", async () => {
3+
test("Variant #1: Extract properties", async () => {
44
var code = `
55
var TEST_OBJECT = {
66
TEST_1: "Hello World",
@@ -34,7 +34,7 @@ it("should extract properties", async () => {
3434
eval(output);
3535
});
3636

37-
it("should extract function properties correctly", async () => {
37+
test("Variant #2: Extract function properties correctly", async () => {
3838
var code = `
3939
var TEST_OBJECT = {
4040
isBoolean: x=>typeof x === "boolean",
@@ -68,7 +68,7 @@ it("should extract function properties correctly", async () => {
6868
eval(output);
6969
});
7070

71-
it("should not extract properties on with dynamically added keys", async () => {
71+
test("Variant #3: Not extract properties on with dynamically added keys", async () => {
7272
var code = `
7373
var TEST_OBJECT = {
7474
first_key: 1
@@ -100,7 +100,7 @@ it("should not extract properties on with dynamically added keys", async () => {
100100
eval(output);
101101
});
102102

103-
it("should not extract properties on with dynamically added keys even when in nested contexts", async () => {
103+
test("Variant #4: Not extract properties on with dynamically added keys even when in nested contexts", async () => {
104104
var code = `
105105
var TEST_OBJECT = {
106106
first_key: 1
@@ -135,7 +135,7 @@ it("should not extract properties on with dynamically added keys even when in ne
135135
eval(output);
136136
});
137137

138-
it("should not extract properties on objects with computed properties", async () => {
138+
test("Variant #5: Not extract properties on objects with computed properties", async () => {
139139
var code = `
140140
141141
var key = "111"
@@ -166,7 +166,7 @@ it("should not extract properties on objects with computed properties", async ()
166166
eval(output);
167167
});
168168

169-
it("should not extract properties on objects with computed properties (string)", async () => {
169+
test("Variant #6: Not extract properties on objects with computed properties (string)", async () => {
170170
var code = `
171171
172172
var v = "key";
@@ -197,7 +197,7 @@ it("should not extract properties on objects with computed properties (string)",
197197
eval(output);
198198
});
199199

200-
it("should not extract properties on objects when the object is referenced independently", async () => {
200+
test("Variant #7: Not extract properties on objects when the object is referenced independently", async () => {
201201
var code = `
202202
203203
var TEST_OBJECT = {
@@ -229,7 +229,7 @@ it("should not extract properties on objects when the object is referenced indep
229229
eval(output);
230230
});
231231

232-
it("should not extract properties on objects when the variable gets redefined", async () => {
232+
test("Variant #8: Not extract properties on objects when the variable gets redefined", async () => {
233233
var code = `
234234
235235
var TEST_OBJECT = {
@@ -260,7 +260,7 @@ it("should not extract properties on objects when the variable gets redefined",
260260
eval(output);
261261
});
262262

263-
it("should not extract properties on objects when the variable gets reassigned", async () => {
263+
test("Variant #9: Not extract properties on objects when the variable gets reassigned", async () => {
264264
var code = `
265265
266266
var TEST_OBJECT = {
@@ -292,7 +292,7 @@ it("should not extract properties on objects when the variable gets reassigned",
292292
eval(output);
293293
});
294294

295-
it("should not extract properties on objects with methods referencing 'this'", async () => {
295+
test("Variant #10: Not extract properties on objects with methods referencing 'this'", async () => {
296296
var code = `
297297
298298
var TEST_OBJECT = {
@@ -325,7 +325,7 @@ it("should not extract properties on objects with methods referencing 'this'", a
325325
eval(output);
326326
});
327327

328-
it("should not extract properties on objects when properties are dynamically deleted", async () => {
328+
test("Variant #11: Not extract properties on objects when properties are dynamically deleted", async () => {
329329
var code = `
330330
331331
var TEST_OBJECT = {
@@ -356,7 +356,7 @@ it("should not extract properties on objects when properties are dynamically del
356356
eval(output);
357357
});
358358

359-
it("should not extract properties on objects with computed accessors", async () => {
359+
test("Variant #12: Not extract properties on objects with computed accessors", async () => {
360360
var code = `
361361
362362
var TEST_OBJECT = {
@@ -388,7 +388,7 @@ it("should not extract properties on objects with computed accessors", async ()
388388
eval(output);
389389
});
390390

391-
it("should properly use custom callback to exclude certain names from being changed", async () => {
391+
test("Variant #13: Properly use custom callback to exclude certain names from being changed", async () => {
392392
var code = `
393393
394394
var TEST_OBJECT = {
@@ -424,7 +424,7 @@ it("should properly use custom callback to exclude certain names from being chan
424424
eval(output);
425425
});
426426

427-
it("should not apply to objects with non-init properties (method, set, get)", async () => {
427+
test("Variant #14: Not apply to objects with non-init properties (method, set, get)", async () => {
428428
var code = `
429429
430430
var realValue = 0;
@@ -448,7 +448,7 @@ it("should not apply to objects with non-init properties (method, set, get)", as
448448
});
449449

450450
// https://github.com/MichaelXF/js-confuser/issues/78
451-
it("should handle objects with spread elements", async () => {
451+
test("Variant #15: Handle objects with spread elements", async () => {
452452
var output = await JsConfuser(
453453
`
454454
var x = { firstName: "John", lastName: "Doe" }
@@ -467,3 +467,23 @@ it("should handle objects with spread elements", async () => {
467467

468468
expect(TEST_OUTPUT).toStrictEqual({ firstName: "John", lastName: "Doe" });
469469
});
470+
471+
// https://github.com/MichaelXF/js-confuser/issues/106
472+
test("Variant #16: Handle const declarations", async () => {
473+
var output = await JsConfuser(
474+
`
475+
const obj = {prop: 0};
476+
obj.prop = 1;
477+
TEST_OUTPUT = obj.prop;
478+
`,
479+
{
480+
target: "node",
481+
objectExtraction: true,
482+
}
483+
);
484+
485+
var TEST_OUTPUT;
486+
eval(output);
487+
488+
expect(TEST_OUTPUT).toStrictEqual(1);
489+
});

0 commit comments

Comments
 (0)