From 7113174e32f9567e2f2749db8cde74c508fa73df Mon Sep 17 00:00:00 2001 From: sanex3339 Date: Tue, 24 May 2016 16:13:06 +0300 Subject: [PATCH] refactoring --- src/NodeUtils.js | 21 +++++++++ src/NodeUtils.ts | 45 +++++++++++++++++++ .../ConsoleOutputDisableExpressionNode.js | 2 +- .../ConsoleOutputDisableExpressionNode.ts | 13 +----- src/nodes/DebugProtectionFunctionCallNode.js | 2 +- src/nodes/DebugProtectionFunctionCallNode.ts | 13 +----- .../DebugProtectionFunctionIntervalNode.js | 2 +- .../DebugProtectionFunctionIntervalNode.ts | 13 +----- src/nodes/DebugProtectionFunctionNode.js | 2 +- src/nodes/DebugProtectionFunctionNode.ts | 13 +----- src/nodes/Node.ts | 5 +++ src/nodes/UnicodeArrayNode.js | 4 +- src/nodes/UnicodeArrayNode.ts | 15 ++----- .../UnicodeArrayRotateFunctionCallNode.js | 2 +- .../UnicodeArrayRotateFunctionCallNode.ts | 14 ++---- src/nodes/UnicodeArrayRotateFunctionNode.js | 2 +- src/nodes/UnicodeArrayRotateFunctionNode.ts | 16 ++----- tests/dev-test.js | 1 - tests/dev-test.ts | 2 +- 19 files changed, 98 insertions(+), 89 deletions(-) diff --git a/src/NodeUtils.js b/src/NodeUtils.js index 25aa4ad8..507ab14f 100644 --- a/src/NodeUtils.js +++ b/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, diff --git a/src/NodeUtils.ts b/src/NodeUtils.ts index 56901875..7054458e 100644 --- a/src/NodeUtils.ts +++ b/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; + } } diff --git a/src/nodes/ConsoleOutputDisableExpressionNode.js b/src/nodes/ConsoleOutputDisableExpressionNode.js index 19060e2e..46a862ed 100644 --- a/src/nodes/ConsoleOutputDisableExpressionNode.js +++ b/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; diff --git a/src/nodes/ConsoleOutputDisableExpressionNode.ts b/src/nodes/ConsoleOutputDisableExpressionNode.ts index 0e6c456d..05602d67 100644 --- a/src/nodes/ConsoleOutputDisableExpressionNode.ts +++ b/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; } diff --git a/src/nodes/DebugProtectionFunctionCallNode.js b/src/nodes/DebugProtectionFunctionCallNode.js index b20abd63..2304c71e 100644 --- a/src/nodes/DebugProtectionFunctionCallNode.js +++ b/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; diff --git a/src/nodes/DebugProtectionFunctionCallNode.ts b/src/nodes/DebugProtectionFunctionCallNode.ts index 3025ac1c..cbe652e8 100644 --- a/src/nodes/DebugProtectionFunctionCallNode.ts +++ b/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; } diff --git a/src/nodes/DebugProtectionFunctionIntervalNode.js b/src/nodes/DebugProtectionFunctionIntervalNode.js index 50aa4e1a..22a758f5 100644 --- a/src/nodes/DebugProtectionFunctionIntervalNode.js +++ b/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; diff --git a/src/nodes/DebugProtectionFunctionIntervalNode.ts b/src/nodes/DebugProtectionFunctionIntervalNode.ts index 820ceda3..2f6c4a8f 100644 --- a/src/nodes/DebugProtectionFunctionIntervalNode.ts +++ b/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; } diff --git a/src/nodes/DebugProtectionFunctionNode.js b/src/nodes/DebugProtectionFunctionNode.js index 5b02f7e6..4fa082d9 100644 --- a/src/nodes/DebugProtectionFunctionNode.js +++ b/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; diff --git a/src/nodes/DebugProtectionFunctionNode.ts b/src/nodes/DebugProtectionFunctionNode.ts index 04ac85b6..8802f7b3 100644 --- a/src/nodes/DebugProtectionFunctionNode.ts +++ b/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; } diff --git a/src/nodes/Node.ts b/src/nodes/Node.ts index ed10e579..4409e499 100644 --- a/src/nodes/Node.ts +++ b/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} */ diff --git a/src/nodes/UnicodeArrayNode.js b/src/nodes/UnicodeArrayNode.js index 53da762b..54c3be4a 100644 --- a/src/nodes/UnicodeArrayNode.js +++ b/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 { diff --git a/src/nodes/UnicodeArrayNode.ts b/src/nodes/UnicodeArrayNode.ts index f4bb2e41..7074bd52 100644 --- a/src/nodes/UnicodeArrayNode.ts +++ b/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(); } /** diff --git a/src/nodes/UnicodeArrayRotateFunctionCallNode.js b/src/nodes/UnicodeArrayRotateFunctionCallNode.js index 21032bf8..15a90746 100644 --- a/src/nodes/UnicodeArrayRotateFunctionCallNode.js +++ b/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; diff --git a/src/nodes/UnicodeArrayRotateFunctionCallNode.ts b/src/nodes/UnicodeArrayRotateFunctionCallNode.ts index 31c89760..08c54bd2 100644 --- a/src/nodes/UnicodeArrayRotateFunctionCallNode.ts +++ b/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; } diff --git a/src/nodes/UnicodeArrayRotateFunctionNode.js b/src/nodes/UnicodeArrayRotateFunctionNode.js index b74fa7ec..6b76b4eb 100644 --- a/src/nodes/UnicodeArrayRotateFunctionNode.js +++ b/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; diff --git a/src/nodes/UnicodeArrayRotateFunctionNode.ts b/src/nodes/UnicodeArrayRotateFunctionNode.ts index ddaedd71..e0f17439 100644 --- a/src/nodes/UnicodeArrayRotateFunctionNode.ts +++ b/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; } diff --git a/tests/dev-test.js b/tests/dev-test.js index e867442e..5775b51b 100644 --- a/tests/dev-test.js +++ b/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)); diff --git a/tests/dev-test.ts b/tests/dev-test.ts index 186b1024..9ca7801a 100644 --- a/tests/dev-test.ts +++ b/tests/dev-test.ts @@ -50,7 +50,7 @@ let obfuscatedCode: string = JavaScriptObfuscator.obfuscate( `, { disableConsoleOutput: false, - rotateUnicodeArray: false + //rotateUnicodeArray: false } );