Browse Source

DeadCodeInjection: more compact way to generate matrix of properties for IfStatement

sanex3339 8 years ago
parent
commit
7e02a91f7d

+ 12 - 27
src/node-transformers/dead-code-injection-transformers/DeadCodeInjectionTransformer.ts

@@ -94,37 +94,22 @@ export class DeadCodeInjectionTransformer extends AbstractNodeTransformer {
         blockStatementNode: ESTree.BlockStatement,
         randomBlockStatementNode: ESTree.BlockStatement
     ): ESTree.BlockStatement {
+        const random1: boolean = RandomGeneratorUtils.getMathRandom() > 0.5;
+        const random2: boolean = RandomGeneratorUtils.getMathRandom() > 0.5;
+
+        const operator: ESTree.BinaryOperator = random1 ? '===' : '!==';
         const leftString: string = RandomGeneratorUtils.getRandomString(3);
+        const rightString: string = random2 ? leftString : RandomGeneratorUtils.getRandomString(3);
 
-        let operator: ESTree.BinaryOperator,
-            rightString: string,
-            consequent: ESTree.BlockStatement,
+        let consequent: ESTree.BlockStatement,
             alternate: ESTree.BlockStatement;
 
-        if (RandomGeneratorUtils.getMathRandom() > 0.5) {
-            operator = '===';
-
-            if (RandomGeneratorUtils.getMathRandom() > 0.5) {
-                rightString = leftString;
-                consequent = blockStatementNode;
-                alternate = randomBlockStatementNode;
-            } else {
-                rightString = RandomGeneratorUtils.getRandomString(3);
-                consequent = randomBlockStatementNode;
-                alternate = blockStatementNode;
-            }
+        if ((random1 && random2) || (!random1 && !random2)) {
+            consequent = blockStatementNode;
+            alternate = randomBlockStatementNode;
         } else {
-            operator = '!==';
-
-            if (RandomGeneratorUtils.getMathRandom() > 0.5) {
-                rightString = leftString;
-                consequent = randomBlockStatementNode;
-                alternate = blockStatementNode;
-            } else {
-                rightString = RandomGeneratorUtils.getRandomString(3);
-                consequent = blockStatementNode;
-                alternate = randomBlockStatementNode;
-            }
+            consequent = randomBlockStatementNode;
+            alternate = blockStatementNode;
         }
 
         let newBlockStatementNode: ESTree.BlockStatement = Nodes.getBlockStatementNode([
@@ -178,7 +163,7 @@ export class DeadCodeInjectionTransformer extends AbstractNodeTransformer {
         });
 
         if (this.collectedBlockStatements.length < 10) {
-            return;
+            //return;
         }
 
         estraverse.replace(programNode, {

+ 12 - 0
test/dev/dev.ts

@@ -14,8 +14,20 @@ import { NO_CUSTOM_NODES_PRESET } from '../../src/options/presets/NoCustomNodes'
                         console.log('efg');
                         console.log('hij');
                     };
+                    var bar = function () {
+                        console.log('klm');
+                        console.log('nop');
+                        console.log('qrs');
+                    };
+                    var baz = function () {
+                        console.log('tuv');
+                        console.log('wxy');
+                        console.log('z');
+                    };
 
                     foo();
+                    bar();
+                    baz();
                 }
             })();
         `,