From 5f6e29f7638fe549d30235c1039d14778f94e939 Mon Sep 17 00:00:00 2001 From: kachalov Date: Mon, 16 May 2016 13:22:12 +0300 Subject: [PATCH 1/2] Refactoring --- .../VariableDeclarationObfuscator.js | 19 ++++-------- .../VariableDeclarationObfuscator.ts | 30 ++++--------------- tests/dev-test.js | 2 +- tests/dev-test.ts | 2 +- 4 files changed, 13 insertions(+), 40 deletions(-) diff --git a/src/node-obfuscators/VariableDeclarationObfuscator.js b/src/node-obfuscators/VariableDeclarationObfuscator.js index 256b67ac..86dbf953 100644 --- a/src/node-obfuscators/VariableDeclarationObfuscator.js +++ b/src/node-obfuscators/VariableDeclarationObfuscator.js @@ -41,29 +41,20 @@ class VariableDeclarationObfuscator extends NodeObfuscator_1.NodeObfuscator { else { scopeNode = variableParentNode; } - let isNodeAfterVariableDeclaratorFlag = false, isNodeBeforeVariableDeclaratorFlag = true, functionParentScope, functionNextNode, functionIndex = -1; + let isNodeAfterVariableDeclaratorFlag = false; estraverse.replace(scopeNode, { enter: (node, parentNode) => { if (node.type === 'FunctionDeclaration' || node.type === 'FunctionExpression' || node.type === 'ArrowFunctionExpression') { - functionParentScope = NodeUtils_1.NodeUtils.getNodeScope(node); - if (NodeUtils_1.NodeUtils.isBlockStatementNode(functionParentScope)) { - functionIndex = functionParentScope.body.indexOf(node); - if (functionIndex >= 0) { - functionNextNode = functionParentScope.body[functionIndex + 1]; + estraverse.replace(node, { + enter: (node, parentNode) => { + this.replaceNodeIdentifierByNewValue(node, parentNode, this.variableNames); } - } - isNodeAfterVariableDeclaratorFlag = true; - } - if (functionNextNode && isNodeBeforeVariableDeclaratorFlag && node === functionNextNode) { - isNodeAfterVariableDeclaratorFlag = false; - functionNextNode = undefined; - functionIndex = -1; + }); } if (node === variableDeclarationNode) { isNodeAfterVariableDeclaratorFlag = true; - isNodeBeforeVariableDeclaratorFlag = false; } if (isNodeAfterVariableDeclaratorFlag) { this.replaceNodeIdentifierByNewValue(node, parentNode, this.variableNames); diff --git a/src/node-obfuscators/VariableDeclarationObfuscator.ts b/src/node-obfuscators/VariableDeclarationObfuscator.ts index 3b266e42..9b1101a3 100644 --- a/src/node-obfuscators/VariableDeclarationObfuscator.ts +++ b/src/node-obfuscators/VariableDeclarationObfuscator.ts @@ -76,13 +76,8 @@ export class VariableDeclarationObfuscator extends NodeObfuscator { scopeNode = variableParentNode; } - let isNodeAfterVariableDeclaratorFlag: boolean = false, - isNodeBeforeVariableDeclaratorFlag: boolean = true, - functionParentScope: ITreeNode, - functionNextNode: ITreeNode, - functionIndex: number = -1; + let isNodeAfterVariableDeclaratorFlag: boolean = false; - //TODO: REFACTOR THIS estraverse.replace(scopeNode, { enter: (node: ITreeNode, parentNode: ITreeNode) => { if ( @@ -90,28 +85,15 @@ export class VariableDeclarationObfuscator extends NodeObfuscator { node.type === 'FunctionExpression' || node.type === 'ArrowFunctionExpression' ) { - functionParentScope = NodeUtils.getNodeScope(node); - - if (NodeUtils.isBlockStatementNode(functionParentScope)) { - functionIndex = functionParentScope.body.indexOf(node); - - if (functionIndex >= 0) { - functionNextNode = functionParentScope.body[functionIndex + 1]; + estraverse.replace(node, { + enter: (node: ITreeNode, parentNode: ITreeNode) => { + this.replaceNodeIdentifierByNewValue(node, parentNode, this.variableNames); } - } - - isNodeAfterVariableDeclaratorFlag = true; - } - - if (functionNextNode && isNodeBeforeVariableDeclaratorFlag && node === functionNextNode) { - isNodeAfterVariableDeclaratorFlag = false; - functionNextNode = undefined; - functionIndex = -1; + }); } if (node === variableDeclarationNode) { isNodeAfterVariableDeclaratorFlag = true; - isNodeBeforeVariableDeclaratorFlag = false; } if (isNodeAfterVariableDeclaratorFlag) { @@ -120,4 +102,4 @@ export class VariableDeclarationObfuscator extends NodeObfuscator { } }); } -} \ No newline at end of file +} diff --git a/tests/dev-test.js b/tests/dev-test.js index 7ab801df..b5b1f08a 100644 --- a/tests/dev-test.js +++ b/tests/dev-test.js @@ -1,6 +1,6 @@ var JavaScriptObfuscator = require('../index.js'); var obfuscatedCode = JavaScriptObfuscator.obfuscate(` - (function(){ + (function(){ var result = 1, term1 = 0, term2 = 1, diff --git a/tests/dev-test.ts b/tests/dev-test.ts index adbd31c9..46bf68dc 100644 --- a/tests/dev-test.ts +++ b/tests/dev-test.ts @@ -2,7 +2,7 @@ var JavaScriptObfuscator = require('../index.js'); var obfuscatedCode = JavaScriptObfuscator.obfuscate( ` - (function(){ + (function(){ var result = 1, term1 = 0, term2 = 1, From b1bde77732b0efbb698323f70d507bd20e51be65 Mon Sep 17 00:00:00 2001 From: kachalov Date: Mon, 16 May 2016 13:47:46 +0300 Subject: [PATCH 2/2] Refactoring --- src/node-obfuscators/VariableDeclarationObfuscator.js | 7 +------ src/node-obfuscators/VariableDeclarationObfuscator.ts | 10 +++------- 2 files changed, 4 insertions(+), 13 deletions(-) diff --git a/src/node-obfuscators/VariableDeclarationObfuscator.js b/src/node-obfuscators/VariableDeclarationObfuscator.js index 86dbf953..5fba76a9 100644 --- a/src/node-obfuscators/VariableDeclarationObfuscator.js +++ b/src/node-obfuscators/VariableDeclarationObfuscator.js @@ -35,12 +35,7 @@ class VariableDeclarationObfuscator extends NodeObfuscator_1.NodeObfuscator { } replaceVariableCalls(variableDeclarationNode, variableParentNode) { let scopeNode; - if (variableDeclarationNode.kind === 'var') { - scopeNode = NodeUtils_1.NodeUtils.getNodeScope(variableDeclarationNode); - } - else { - scopeNode = variableParentNode; - } + scopeNode = variableDeclarationNode.kind === 'var' ? NodeUtils_1.NodeUtils.getNodeScope(variableDeclarationNode) : variableParentNode; let isNodeAfterVariableDeclaratorFlag = false; estraverse.replace(scopeNode, { enter: (node, parentNode) => { diff --git a/src/node-obfuscators/VariableDeclarationObfuscator.ts b/src/node-obfuscators/VariableDeclarationObfuscator.ts index 9b1101a3..bdaccc36 100644 --- a/src/node-obfuscators/VariableDeclarationObfuscator.ts +++ b/src/node-obfuscators/VariableDeclarationObfuscator.ts @@ -68,13 +68,9 @@ export class VariableDeclarationObfuscator extends NodeObfuscator { private replaceVariableCalls (variableDeclarationNode: IVariableDeclarationNode, variableParentNode: ITreeNode): void { let scopeNode: ITreeNode; - if (variableDeclarationNode.kind === 'var') { - scopeNode = NodeUtils.getNodeScope( - variableDeclarationNode - ); - } else { - scopeNode = variableParentNode; - } + scopeNode = variableDeclarationNode.kind === 'var' ? NodeUtils.getNodeScope( + variableDeclarationNode + ) : variableParentNode; let isNodeAfterVariableDeclaratorFlag: boolean = false;