sanex3339 9 éve
szülő
commit
7113174e32

+ 21 - 0
src/NodeUtils.js

@@ -2,6 +2,12 @@
 const NodeType_1 = require("./enums/NodeType");
 const Utils_1 = require("./Utils");
 class NodeUtils {
+    static appendNode(blockScopeBody, node) {
+        if (!NodeUtils.validateNode(node)) {
+            return;
+        }
+        blockScopeBody.push(node);
+    }
     static getBlockScopeNodeByIndex(node, index = 0) {
         if (NodeUtils.isNodeHasBlockScope(node) && node.body[index]) {
             return node.body[index];
@@ -35,6 +41,12 @@ class NodeUtils {
         }
         return node.parentNode;
     }
+    static insertNodeAtIndex(blockScopeBody, node, index) {
+        if (!NodeUtils.validateNode(node)) {
+            return;
+        }
+        blockScopeBody.splice(index, 0, node);
+    }
     static isBlockStatementNode(node) {
         return node.type === NodeType_1.NodeType.BlockStatement;
     }
@@ -59,6 +71,15 @@ class NodeUtils {
     static isVariableDeclaratorNode(node) {
         return node.type === NodeType_1.NodeType.VariableDeclarator;
     }
+    static prependNode(blockScopeBody, node) {
+        if (!NodeUtils.validateNode(node)) {
+            return;
+        }
+        blockScopeBody.unshift(node);
+    }
+    static validateNode(node) {
+        return !!node;
+    }
 }
 NodeUtils.scopeNodes = [
     NodeType_1.NodeType.ArrowFunctionExpression,

+ 45 - 0
src/NodeUtils.ts

@@ -24,6 +24,18 @@ export class NodeUtils {
         NodeType.MethodDefinition
     ];
 
+    /**
+     * @param blockScopeBody
+     * @param node
+     */
+    public static appendNode (blockScopeBody: ITreeNode[], node: ITreeNode): void {
+        if (!NodeUtils.validateNode(node)) {
+            return;
+        }
+
+        blockScopeBody.push(node);
+    }
+
     /**
      * @param node
      * @param index
@@ -90,6 +102,19 @@ export class NodeUtils {
         return node.parentNode;
     }
 
+    /**
+     * @param blockScopeBody
+     * @param node
+     * @param index
+     */
+    public static insertNodeAtIndex (blockScopeBody: ITreeNode[], node: ITreeNode, index: number): void {
+        if (!NodeUtils.validateNode(node)) {
+            return;
+        }
+
+        blockScopeBody.splice(index, 0, node);
+    }
+
     /**
      * @param node
      * @returns {boolean}
@@ -158,4 +183,24 @@ export class NodeUtils {
     public static isVariableDeclaratorNode (node: ITreeNode): node is IVariableDeclaratorNode {
         return node.type === NodeType.VariableDeclarator;
     }
+
+    /**
+     * @param blockScopeBody
+     * @param node
+     */
+    public static prependNode (blockScopeBody: ITreeNode[], node: ITreeNode): void {
+        if (!NodeUtils.validateNode(node)) {
+            return;
+        }
+
+        blockScopeBody.unshift(node);
+    }
+
+    /**
+     * @param node
+     * @returns {boolean}
+     */
+    private static validateNode (node: ITreeNode): boolean {
+        return !!node;
+    }
 }

+ 1 - 1
src/nodes/ConsoleOutputDisableExpressionNode.js

@@ -13,7 +13,7 @@ class ConsoleOutputDisableExpressionNode extends Node_1.Node {
         estraverse.replace(this.astTree, {
             leave: (node, parent) => {
                 if (NodeUtils_1.NodeUtils.isProgramNode(node)) {
-                    node.body.unshift(this.getNode());
+                    NodeUtils_1.NodeUtils.prependNode(node.body, this.getNode());
                     return estraverse.VisitorOption.Break;
                 }
                 return estraverse.VisitorOption.Skip;

+ 2 - 11
src/nodes/ConsoleOutputDisableExpressionNode.ts

@@ -7,16 +7,6 @@ import { Node } from './Node';
 import { NodeUtils } from "../NodeUtils";
 
 export class ConsoleOutputDisableExpressionNode extends Node {
-    /**
-     * @type {ITreeNode}
-     */
-    protected node: ITreeNode;
-
-    /**
-     * @type {ITreeNode}
-     */
-    private astTree: ITreeNode;
-
     /**
      * @param astTree
      */
@@ -24,6 +14,7 @@ export class ConsoleOutputDisableExpressionNode extends Node {
         super();
 
         this.astTree = astTree;
+        
         this.node = this.getNodeStructure();
     }
 
@@ -31,7 +22,7 @@ export class ConsoleOutputDisableExpressionNode extends Node {
         estraverse.replace(this.astTree, {
             leave: (node: ITreeNode, parent: ITreeNode): any => {
                 if (NodeUtils.isProgramNode(node)) {
-                    node.body.unshift(this.getNode());
+                    NodeUtils.prependNode(node.body, this.getNode());
 
                     return estraverse.VisitorOption.Break;
                 }

+ 1 - 1
src/nodes/DebugProtectionFunctionCallNode.js

@@ -14,7 +14,7 @@ class DebugProtectionFunctionCallNode extends Node_1.Node {
         estraverse.replace(this.astTree, {
             leave: (node, parent) => {
                 if (NodeUtils_1.NodeUtils.isProgramNode(node)) {
-                    node.body.push(this.getNode());
+                    NodeUtils_1.NodeUtils.appendNode(node.body, this.getNode());
                     return estraverse.VisitorOption.Break;
                 }
                 return estraverse.VisitorOption.Skip;

+ 2 - 11
src/nodes/DebugProtectionFunctionCallNode.ts

@@ -8,16 +8,6 @@ import { Node } from './Node';
 import { NodeUtils } from "../NodeUtils";
 
 export class DebugProtectionFunctionCallNode extends Node {
-    /**
-     * @type {ITreeNode}
-     */
-    protected node: ITreeNode;
-
-    /**
-     * @type {ITreeNode}
-     */
-    private astTree: ITreeNode;
-
     /**
      * @type {string}
      */
@@ -35,6 +25,7 @@ export class DebugProtectionFunctionCallNode extends Node {
 
         this.astTree = astTree;
         this.debugProtectionFunctionName = debugProtectionFunctionName;
+        
         this.node = this.getNodeStructure();
     }
 
@@ -42,7 +33,7 @@ export class DebugProtectionFunctionCallNode extends Node {
         estraverse.replace(this.astTree, {
             leave: (node: ITreeNode, parent: ITreeNode): any => {
                 if (NodeUtils.isProgramNode(node)) {
-                    node.body.push(this.getNode());
+                    NodeUtils.appendNode(node.body, this.getNode());
 
                     return estraverse.VisitorOption.Break;
                 }

+ 1 - 1
src/nodes/DebugProtectionFunctionIntervalNode.js

@@ -14,7 +14,7 @@ class DebugProtectionFunctionIntervalNode extends Node_1.Node {
         estraverse.replace(this.astTree, {
             leave: (node, parent) => {
                 if (NodeUtils_1.NodeUtils.isProgramNode(node)) {
-                    node.body.push(this.getNode());
+                    NodeUtils_1.NodeUtils.appendNode(node.body, this.getNode());
                     return estraverse.VisitorOption.Break;
                 }
                 return estraverse.VisitorOption.Skip;

+ 2 - 11
src/nodes/DebugProtectionFunctionIntervalNode.ts

@@ -8,16 +8,6 @@ import { Node } from './Node';
 import { NodeUtils } from '../NodeUtils';
 
 export class DebugProtectionFunctionIntervalNode extends Node {
-    /**
-     * @type {ITreeNode}
-     */
-    protected node: ITreeNode;
-
-    /**
-     * @type {ITreeNode}
-     */
-    private astTree: ITreeNode;
-
     /**
      * @type {string}
      */
@@ -35,6 +25,7 @@ export class DebugProtectionFunctionIntervalNode extends Node {
 
         this.astTree = astTree;
         this.debugProtectionFunctionName = debugProtectionFunctionName;
+
         this.node = this.getNodeStructure();
     }
 
@@ -42,7 +33,7 @@ export class DebugProtectionFunctionIntervalNode extends Node {
         estraverse.replace(this.astTree, {
             leave: (node: ITreeNode, parent: ITreeNode): any => {
                 if (NodeUtils.isProgramNode(node)) {
-                    node.body.push(this.getNode());
+                    NodeUtils.appendNode(node.body, this.getNode());
 
                     return estraverse.VisitorOption.Break;
                 }

+ 1 - 1
src/nodes/DebugProtectionFunctionNode.js

@@ -15,7 +15,7 @@ class DebugProtectionFunctionNode extends Node_1.Node {
         estraverse.replace(this.astTree, {
             leave: (node, parent) => {
                 if (NodeUtils_1.NodeUtils.isProgramNode(node)) {
-                    node.body.splice(this.debugProtectionFunctionIndex, 0, this.getNode());
+                    NodeUtils_1.NodeUtils.insertNodeAtIndex(node.body, this.getNode(), this.debugProtectionFunctionIndex);
                     return estraverse.VisitorOption.Break;
                 }
                 return estraverse.VisitorOption.Skip;

+ 2 - 11
src/nodes/DebugProtectionFunctionNode.ts

@@ -7,16 +7,6 @@ import { Node } from './Node';
 import { NodeUtils } from '../NodeUtils';
 
 export class DebugProtectionFunctionNode extends Node {
-    /**
-     * @type {ITreeNode}
-     */
-    protected node: ITreeNode;
-
-    /**
-     * @type {ITreeNode}
-     */
-    private astTree: ITreeNode;
-
     /**
      * @type {string}
      */
@@ -42,6 +32,7 @@ export class DebugProtectionFunctionNode extends Node {
         this.astTree = astTree;
         this.debugProtectionFunctionName = debugProtectionFunctionName;
         this.debugProtectionFunctionIndex = debugProtectionFunctionIndex;
+
         this.node = this.getNodeStructure();
     }
 
@@ -49,7 +40,7 @@ export class DebugProtectionFunctionNode extends Node {
         estraverse.replace(this.astTree, {
             leave: (node: ITreeNode, parent: ITreeNode): any => {
                 if (NodeUtils.isProgramNode(node)) {
-                    node.body.splice(this.debugProtectionFunctionIndex, 0, this.getNode());
+                    NodeUtils.insertNodeAtIndex(node.body, this.getNode(), this.debugProtectionFunctionIndex);
 
                     return estraverse.VisitorOption.Break;
                 }

+ 5 - 0
src/nodes/Node.ts

@@ -9,6 +9,11 @@ export abstract class Node implements INode {
      */
     protected appendState: AppendState = AppendState.BeforeObfuscation;
 
+    /**
+     * @type {ITreeNode}
+     */
+    protected astTree: ITreeNode;
+
     /**
      * @type {ITreeNode}
      */

+ 2 - 2
src/nodes/UnicodeArrayNode.js

@@ -20,7 +20,7 @@ class UnicodeArrayNode extends Node_1.Node {
         estraverse.replace(this.astTree, {
             leave: (node, parent) => {
                 if (NodeUtils_1.NodeUtils.isProgramNode(node)) {
-                    node.body.unshift(this.getNode());
+                    NodeUtils_1.NodeUtils.prependNode(node.body, this.getNode());
                     return estraverse.VisitorOption.Break;
                 }
                 return estraverse.VisitorOption.Skip;
@@ -36,7 +36,7 @@ class UnicodeArrayNode extends Node_1.Node {
     getNode() {
         Utils_1.Utils.arrayRotate(this.unicodeArray, this.unicodeArrayRotateValue);
         this.updateNode();
-        return this.node;
+        return super.getNode();
     }
     getNodeStructure() {
         return {

+ 3 - 12
src/nodes/UnicodeArrayNode.ts

@@ -21,16 +21,6 @@ export class UnicodeArrayNode extends Node {
      */
     protected appendState: AppendState = AppendState.AfterObfuscation;
 
-    /**
-     * @type {ITreeNode}
-     */
-    protected node: ITreeNode;
-
-    /**
-     * @type {ITreeNode}
-     */
-    private astTree: ITreeNode;
-
     /**
      * @type {string[]}
      */
@@ -57,6 +47,7 @@ export class UnicodeArrayNode extends Node {
         this.astTree = astTree;
         this.unicodeArrayName = unicodeArrayName;
         this.unicodeArrayRotateValue = unicodeArrayRotateValue;
+
         this.node = this.getNodeStructure();
     }
 
@@ -64,7 +55,7 @@ export class UnicodeArrayNode extends Node {
         estraverse.replace(this.astTree, {
             leave: (node: ITreeNode, parent: ITreeNode): any => {
                 if (NodeUtils.isProgramNode(node)) {
-                    node.body.unshift(this.getNode());
+                    NodeUtils.prependNode(node.body, this.getNode());
 
                     return estraverse.VisitorOption.Break;
                 }
@@ -96,7 +87,7 @@ export class UnicodeArrayNode extends Node {
 
         this.updateNode();
 
-        return this.node;
+        return super.getNode();
     }
 
     /**

+ 1 - 1
src/nodes/UnicodeArrayRotateFunctionCallNode.js

@@ -16,7 +16,7 @@ class UnicodeArrayRotateFunctionCallNode extends Node_1.Node {
         estraverse.replace(this.astTree, {
             leave: (node, parent) => {
                 if (NodeUtils_1.NodeUtils.isProgramNode(node)) {
-                    node.body.unshift(this.getNode());
+                    NodeUtils_1.NodeUtils.prependNode(node.body, this.getNode());
                     return estraverse.VisitorOption.Break;
                 }
                 return estraverse.VisitorOption.Skip;

+ 3 - 11
src/nodes/UnicodeArrayRotateFunctionCallNode.ts

@@ -9,16 +9,6 @@ import { Node } from './Node';
 import { NodeUtils } from "../NodeUtils";
 
 export class UnicodeArrayRotateFunctionCallNode extends Node {
-    /**
-     * @type {ITreeNode}
-     */
-    protected node: ITreeNode;
-
-    /**
-     * @type {ITreeNode}
-     */
-    private astTree: ITreeNode;
-
     /**
      * @type {string}
      */
@@ -49,9 +39,11 @@ export class UnicodeArrayRotateFunctionCallNode extends Node {
         super();
 
         this.astTree = astTree;
+
         this.unicodeArrayRotateFunctionName = unicodeArrayRotateFunctionName;
         this.unicodeArrayName = unicodeArrayName;
         this.unicodeArrayRotateValue = unicodeArrayRotateValue;
+
         this.node = this.getNodeStructure();
     }
 
@@ -59,7 +51,7 @@ export class UnicodeArrayRotateFunctionCallNode extends Node {
         estraverse.replace(this.astTree, {
             leave: (node: ITreeNode, parent: ITreeNode): any => {
                 if (NodeUtils.isProgramNode(node)) {
-                    node.body.unshift(this.getNode());
+                    NodeUtils.prependNode(node.body, this.getNode());
 
                     return estraverse.VisitorOption.Break;
                 }

+ 1 - 1
src/nodes/UnicodeArrayRotateFunctionNode.js

@@ -16,7 +16,7 @@ class UnicodeArrayRotateFunctionNode extends Node_1.Node {
         estraverse.replace(this.astTree, {
             leave: (node, parent) => {
                 if (NodeUtils_1.NodeUtils.isProgramNode(node)) {
-                    node.body.push(this.getNode());
+                    NodeUtils_1.NodeUtils.appendNode(node.body, this.getNode());
                     return estraverse.VisitorOption.Break;
                 }
                 return estraverse.VisitorOption.Skip;

+ 4 - 12
src/nodes/UnicodeArrayRotateFunctionNode.ts

@@ -1,6 +1,7 @@
+import * as escodegen from 'escodegen';
+import * as esprima from 'esprima';
 import * as estraverse from 'estraverse';
 
-import { IProgramNode } from '../interfaces/nodes/IProgramNode';
 import { ITreeNode } from '../interfaces/nodes/ITreeNode';
 
 import { NodeType } from "../enums/NodeType";
@@ -10,16 +11,6 @@ import { NodeUtils } from "../NodeUtils";
 import { Utils } from '../Utils';
 
 export class UnicodeArrayRotateFunctionNode extends Node {
-    /**
-     * @type {ITreeNode}
-     */
-    protected node: ITreeNode;
-
-    /**
-     * @type {ITreeNode}
-     */
-    private astTree: ITreeNode;
-
     /**
      * @type {string}
      */
@@ -45,6 +36,7 @@ export class UnicodeArrayRotateFunctionNode extends Node {
         this.astTree = astTree;
         this.unicodeArrayRotateFunctionName = unicodeArrayRotateFunctionName;
         this.unicodeArrayName = unicodeArrayName;
+
         this.node = this.getNodeStructure();
     }
 
@@ -52,7 +44,7 @@ export class UnicodeArrayRotateFunctionNode extends Node {
         estraverse.replace(this.astTree, {
             leave: (node: ITreeNode, parent: ITreeNode): any => {
                 if (NodeUtils.isProgramNode(node)) {
-                    node.body.push(this.getNode());
+                    NodeUtils.appendNode(node.body, this.getNode());
 
                     return estraverse.VisitorOption.Break;
                 }

+ 0 - 1
tests/dev-test.js

@@ -48,7 +48,6 @@ let obfuscatedCode = index_1.JavaScriptObfuscator.obfuscate(`
     })();
     `, {
     disableConsoleOutput: false,
-    rotateUnicodeArray: false
 });
 console.log(obfuscatedCode);
 console.log(eval(obfuscatedCode));

+ 1 - 1
tests/dev-test.ts

@@ -50,7 +50,7 @@ let obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
     `,
     {
         disableConsoleOutput: false,
-        rotateUnicodeArray: false
+        //rotateUnicodeArray: false
     }
 );