Browse Source

Improved nested variable declarations merging

sanex3339 5 years ago
parent
commit
f10d1d479d

+ 1 - 1
CHANGELOG.md

@@ -2,7 +2,7 @@ Change Log
 
 v1.7.0
 ---
-* `simplify` option now affects all block statements
+* `simplify` option now affects all block statements. Improved variable declarations merging.
 
 v1.6.0
 ---

File diff suppressed because it is too large
+ 0 - 0
dist/index.browser.js


File diff suppressed because it is too large
+ 0 - 0
dist/index.cli.js


File diff suppressed because it is too large
+ 0 - 0
dist/index.js


+ 1 - 1
src/node-transformers/simplifying-transformers/VariableDeclarationsMergeTransformer.ts

@@ -46,7 +46,7 @@ export class VariableDeclarationsMergeTransformer extends AbstractNodeTransforme
         switch (nodeTransformationStage) {
             case NodeTransformationStage.Simplifying:
                 return {
-                    enter: (
+                    leave: (
                         node: ESTree.Node,
                         parentNode: ESTree.Node | null
                     ): ESTree.Node | estraverse.VisitorOption | undefined => {

+ 8 - 9
test/dev/dev.ts

@@ -7,15 +7,14 @@ import { NO_ADDITIONAL_NODES_PRESET } from '../../src/options/presets/NoCustomNo
 
     let obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
         `
-            function foo () {
-                console.log(1);
-                
-                const bar = 123;
-                console.log(2);
-                console.log(3);
-                
-                return bar;
-            }
+            var foo = function () {};
+            var bar = function () {
+                var baz = function () {};
+                var bark = function () {
+                    var hawk = 1;
+                    var dog = 2;
+                };
+            };
         `,
         {
             ...NO_ADDITIONAL_NODES_PRESET,

+ 32 - 0
test/functional-tests/node-transformers/simplifying-transformers/variable-declarations-merge-transformer/VariableDeclarationsMergeTransformer.spec.ts

@@ -141,6 +141,38 @@ describe('VariableDeclarationsMergeTransformer', () => {
                 assert.match(obfuscatedCode, regExp);
             });
         });
+
+        describe('Variant #6: declarations inside nested function expressions', () => {
+            const regExp: RegExp = new RegExp(
+                'var foo *= *function *\\(\\) *{ *}, *' +
+                    'bar *= *function *\\(\\) *{ *' +
+                        'var _0x([a-f0-9]){4,6} *= *function *\\(\\) *{ *}, *' +
+                            '_0x([a-f0-9]){4,6} *= *function *\\(\\) *{ *' +
+                                'var _0x([a-f0-9]){4,6} *= *0x1, *' +
+                                    '_0x([a-f0-9]){4,6} *= *0x2; *' +
+                            '}; *' +
+                    '};'
+            );
+
+
+            let obfuscatedCode: string;
+
+            before(() => {
+                const code: string = readFileAsString(__dirname + '/fixtures/declarations-inside-nested-function-expressions.js');
+
+                obfuscatedCode = JavaScriptObfuscator.obfuscate(
+                    code,
+                    {
+                        ...NO_ADDITIONAL_NODES_PRESET,
+                        simplify: true
+                    }
+                ).getObfuscatedCode();
+            });
+
+            it('should merge variable declarations', () => {
+                assert.match(obfuscatedCode, regExp);
+            });
+        });
     });
 
     describe('object pattern as initializer', () => {

+ 8 - 0
test/functional-tests/node-transformers/simplifying-transformers/variable-declarations-merge-transformer/fixtures/declarations-inside-nested-function-expressions.js

@@ -0,0 +1,8 @@
+var foo = function () {};
+var bar = function () {
+    var baz = function () {};
+    var bark = function () {
+        var hawk = 1;
+        var dog = 2;
+    };
+};

Some files were not shown because too many files changed in this diff