Browse Source

refactoring

sanex3339 9 years ago
parent
commit
e78256b382

+ 3 - 0
src/custom-nodes/unicode-array-nodes/UnicodeArrayNode.js

@@ -24,6 +24,9 @@ class UnicodeArrayNode extends Node_1.Node {
         return this.unicodeArray;
     }
     getNode() {
+        if (!this.unicodeArray.length) {
+            return;
+        }
         Utils_1.Utils.arrayRotate(this.unicodeArray, this.unicodeArrayRotateValue);
         this.updateNode();
         return super.getNode();

+ 4 - 0
src/custom-nodes/unicode-array-nodes/UnicodeArrayNode.ts

@@ -75,6 +75,10 @@ export class UnicodeArrayNode extends Node {
      * @returns {INode}
      */
     public getNode (): INode {
+        if (!this.unicodeArray.length) {
+            return;
+        }
+
         Utils.arrayRotate(this.unicodeArray, this.unicodeArrayRotateValue);
 
         this.updateNode();

+ 16 - 7
src/custom-nodes/unicode-array-nodes/UnicodeArrayRotateFunctionNode.js

@@ -1,23 +1,32 @@
 "use strict";
+const AppendState_1 = require("../../enums/AppendState");
 const NodeType_1 = require("../../enums/NodeType");
 const Node_1 = require('../Node');
 const NodeUtils_1 = require("../../NodeUtils");
 const Utils_1 = require('../../Utils');
 class UnicodeArrayRotateFunctionNode extends Node_1.Node {
-    constructor(unicodeArrayName, unicodeArrayRotateValue) {
+    constructor(unicodeArrayName, unicodeArray, unicodeArrayRotateValue) {
         super();
+        this.appendState = AppendState_1.AppendState.AfterObfuscation;
         this.unicodeArrayName = unicodeArrayName;
+        this.unicodeArray = unicodeArray;
         this.unicodeArrayRotateValue = unicodeArrayRotateValue;
         this.node = this.getNodeStructure();
     }
     appendNode(blockScopeNode) {
-        NodeUtils_1.NodeUtils.prependNode(blockScopeNode.body, this.getNode());
+        NodeUtils_1.NodeUtils.insertNodeAtIndex(blockScopeNode.body, this.getNode(), 1);
+    }
+    getNode() {
+        if (!this.unicodeArray.length) {
+            return;
+        }
+        return super.getNode();
     }
     getNodeStructure() {
         return {
-            "type": "ExpressionStatement",
+            "type": NodeType_1.NodeType.ExpressionStatement,
             "expression": {
-                "type": "CallExpression",
+                "type": NodeType_1.NodeType.CallExpression,
                 "callee": {
                     'type': NodeType_1.NodeType.FunctionExpression,
                     'id': null,
@@ -255,16 +264,16 @@ class UnicodeArrayRotateFunctionNode extends Node_1.Node {
                 },
                 "arguments": [
                     {
-                        'type': 'Identifier',
+                        'type': NodeType_1.NodeType.Identifier,
                         'name': this.unicodeArrayName
                     },
                     {
-                        'type': 'Literal',
+                        'type': NodeType_1.NodeType.Literal,
                         'value': this.unicodeArrayRotateValue,
                         'raw': `'${this.unicodeArrayRotateValue}'`
                     },
                     {
-                        'type': 'Literal',
+                        'type': NodeType_1.NodeType.Literal,
                         'value': true,
                         'raw': 'true'
                     }

+ 33 - 6
src/custom-nodes/unicode-array-nodes/UnicodeArrayRotateFunctionNode.ts

@@ -1,5 +1,8 @@
+import { INode } from "../../interfaces/nodes/INode";
+
 import { BlockScopeNode } from "../../types/BlockScopeNode";
 
+import { AppendState } from "../../enums/AppendState";
 import { NodeType } from "../../enums/NodeType";
 
 import { Node } from '../Node';
@@ -7,6 +10,16 @@ import { NodeUtils } from "../../NodeUtils";
 import { Utils } from '../../Utils';
 
 export class UnicodeArrayRotateFunctionNode extends Node {
+    /**
+     * @type {AppendState}
+     */
+    protected appendState: AppendState = AppendState.AfterObfuscation;
+
+    /**
+     * @type {string[]}
+     */
+    private unicodeArray: string[];
+
     /**
      * @type {string}
      */
@@ -19,15 +32,18 @@ export class UnicodeArrayRotateFunctionNode extends Node {
 
     /**
      * @param unicodeArrayName
+     * @param unicodeArray
      * @param unicodeArrayRotateValue
      */
     constructor (
         unicodeArrayName: string,
+        unicodeArray: string[],
         unicodeArrayRotateValue
     ) {
         super();
 
         this.unicodeArrayName = unicodeArrayName;
+        this.unicodeArray = unicodeArray;
         this.unicodeArrayRotateValue = unicodeArrayRotateValue;
 
         this.node = this.getNodeStructure();
@@ -37,7 +53,18 @@ export class UnicodeArrayRotateFunctionNode extends Node {
      * @param blockScopeNode
      */
     public appendNode (blockScopeNode: BlockScopeNode): void {
-        NodeUtils.prependNode(blockScopeNode.body, this.getNode());
+        NodeUtils.insertNodeAtIndex(blockScopeNode.body, this.getNode(), 1);
+    }
+
+    /**
+     * @returns {INode}
+     */
+    public getNode (): INode {
+        if (!this.unicodeArray.length) {
+            return;
+        }
+
+        return super.getNode();
     }
 
     /**
@@ -45,9 +72,9 @@ export class UnicodeArrayRotateFunctionNode extends Node {
      */
     protected getNodeStructure (): any {
         return {
-            "type": "ExpressionStatement",
+            "type": NodeType.ExpressionStatement,
             "expression": {
-                "type": "CallExpression",
+                "type": NodeType.CallExpression,
                 "callee": {
                     'type': NodeType.FunctionExpression,
                     'id': null,
@@ -285,16 +312,16 @@ export class UnicodeArrayRotateFunctionNode extends Node {
                 },
                 "arguments": [
                     {
-                        'type': 'Identifier',
+                        'type': NodeType.Identifier,
                         'name': this.unicodeArrayName
                     },
                     {
-                        'type': 'Literal',
+                        'type': NodeType.Literal,
                         'value': this.unicodeArrayRotateValue,
                         'raw': `'${this.unicodeArrayRotateValue}'`
                     },
                     {
-                        'type': 'Literal',
+                        'type': NodeType.Literal,
                         'value': true,
                         'raw': 'true'
                     }

+ 3 - 2
src/node-groups/UnicodeArrayNodesGroup.js

@@ -8,14 +8,15 @@ class UnicodeArrayNodesGroup extends NodesGroup_1.NodesGroup {
         super();
         this.unicodeArrayName = Utils_1.Utils.getRandomVariableName(UnicodeArrayNode_1.UnicodeArrayNode.UNICODE_ARRAY_RANDOM_LENGTH);
         this.unicodeArrayRotateValue = Utils_1.Utils.getRandomInteger(100, 500);
+        let unicodeArrayNode = new UnicodeArrayNode_1.UnicodeArrayNode(this.unicodeArrayName, this.unicodeArrayRotateValue), unicodeArray = unicodeArrayNode.getNodeData();
         this.nodes = new Map([
             [
                 'unicodeArrayNode',
-                new UnicodeArrayNode_1.UnicodeArrayNode(this.unicodeArrayName, this.unicodeArrayRotateValue)
+                unicodeArrayNode
             ],
             [
                 'unicodeArrayRotateFunctionNode',
-                new UnicodeArrayRotateFunctionNode_1.UnicodeArrayRotateFunctionNode(this.unicodeArrayName, this.unicodeArrayRotateValue)
+                new UnicodeArrayRotateFunctionNode_1.UnicodeArrayRotateFunctionNode(this.unicodeArrayName, unicodeArray, this.unicodeArrayRotateValue)
             ]
         ]);
     }

+ 8 - 4
src/node-groups/UnicodeArrayNodesGroup.ts

@@ -19,18 +19,22 @@ export class UnicodeArrayNodesGroup extends NodesGroup {
     constructor () {
         super();
 
+        let unicodeArrayNode: UnicodeArrayNode = new UnicodeArrayNode(
+                this.unicodeArrayName,
+                this.unicodeArrayRotateValue
+            ),
+            unicodeArray: string [] = unicodeArrayNode.getNodeData();
+
         this.nodes = new Map <string, ICustomNode> ([
             [
                 'unicodeArrayNode',
-                new UnicodeArrayNode(
-                    this.unicodeArrayName,
-                    this.unicodeArrayRotateValue
-                )
+                unicodeArrayNode
             ],
             [
                 'unicodeArrayRotateFunctionNode',
                 new UnicodeArrayRotateFunctionNode(
                     this.unicodeArrayName,
+                    unicodeArray,
                     this.unicodeArrayRotateValue
                 )
             ]

+ 1 - 0
tests/dev-test.js

@@ -48,6 +48,7 @@ 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
     }
 );