Bladeren bron

Added additional test for properties extractors

sanex3339 7 jaren geleden
bovenliggende
commit
1d9d784f91

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


+ 3 - 2
src/node-transformers/converting-transformers/properties-extractors/VariableDeclaratorPropertiesExtractor.ts

@@ -82,13 +82,14 @@ export class VariableDeclaratorPropertiesExtractor extends AbstractPropertiesExt
         const hostVariableDeclarator: ESTree.VariableDeclarator = this.getHostVariableDeclaratorNode(objectExpressionNode);
         const hostVariableDeclarator: ESTree.VariableDeclarator = this.getHostVariableDeclaratorNode(objectExpressionNode);
         const hostVariableDeclaration: ESTree.VariableDeclaration = this.getHostVariableDeclarationNode(hostVariableDeclarator);
         const hostVariableDeclaration: ESTree.VariableDeclaration = this.getHostVariableDeclarationNode(hostVariableDeclarator);
         const { declarations } = hostVariableDeclaration;
         const { declarations } = hostVariableDeclaration;
+        const indexOfDeclarator: number = declarations.indexOf(hostVariableDeclarator);
+        const isLastDeclarator: boolean = indexOfDeclarator === (declarations.length - 1);
 
 
         // avoid unnecessary checks
         // avoid unnecessary checks
-        if (declarations.length === 1) {
+        if (isLastDeclarator) {
             return false;
             return false;
         }
         }
 
 
-        const indexOfDeclarator: number = declarations.indexOf(hostVariableDeclarator);
         const declaratorsAfterCurrentDeclarator: ESTree.VariableDeclarator[] = declarations.slice(indexOfDeclarator);
         const declaratorsAfterCurrentDeclarator: ESTree.VariableDeclarator[] = declarations.slice(indexOfDeclarator);
 
 
         let isProhibitedObjectExpressionNode: boolean = false;
         let isProhibitedObjectExpressionNode: boolean = false;

+ 29 - 0
test/functional-tests/node-transformers/converting-transformers/object-expression-keys-transformer/ObjectExpressionKeysTransformer.spec.ts

@@ -400,6 +400,35 @@ describe('ObjectExpressionKeysTransformer', () => {
                     assert.match(obfuscatedCode,  regExp);
                     assert.match(obfuscatedCode,  regExp);
                 });
                 });
             });
             });
+
+            describe('Variant #3: two objects', () => {
+                const match: string = `` +
+                    `const *${variableMatch} *= *{}, *` +
+                        `${variableMatch} *= *{'bar': *'bar'}, *` +
+                        `${variableMatch} *= *${variableMatch}\\['bar']; *` +
+                    `${variableMatch}\\['foo'] *= *'foo';` +
+                ``;
+                const regExp: RegExp = new RegExp(match);
+
+                let obfuscatedCode: string;
+
+                before(() => {
+                    const code: string = readFileAsString(__dirname + '/fixtures/variable-declarator-with-object-call-3.js');
+                    const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
+                        code,
+                        {
+                            ...NO_ADDITIONAL_NODES_PRESET,
+                            transformObjectKeys: true
+                        }
+                    );
+
+                    obfuscatedCode = obfuscationResult.getObfuscatedCode();
+                });
+
+                it('should correctly transform first object keys and ignore second object keys', () => {
+                    assert.match(obfuscatedCode,  regExp);
+                });
+            });
         });
         });
     });
     });
 
 

+ 5 - 0
test/functional-tests/node-transformers/converting-transformers/object-expression-keys-transformer/fixtures/variable-declarator-with-object-call-3.js

@@ -0,0 +1,5 @@
+(function () {
+    const object1 = {foo: 'foo'},
+        object2 = {bar: 'bar'},
+        variable = object2.bar;
+})();

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