All files / webpack:/src/node-obfuscators VariableDeclarationObfuscator.ts

94.44% Statements 17/18
54.55% Branches 6/11
100% Functions 3/3
93.33% Lines 14/15
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96            16x       32x                         16x       16x                 16x                   16x   16x       16x             28x     28x                               16x     16x           1830x   1830x            
import * as estraverse from 'estraverse';
 
import { INode } from "../interfaces/nodes/INode";
import { IVariableDeclarationNode } from "../interfaces/nodes/IVariableDeclarationNode";
import { IVariableDeclaratorNode } from "../interfaces/nodes/IVariableDeclaratorNode";
 
import { NodeType } from "../enums/NodeType";I
 
import { NodeObfuscator } from './NodeObfuscator';
import { Nodes } from "../Nodes";
import { NodeUtils } from "../NodeUtils";I
import { Utils } from '../Utils';
 
/**
 * replaces:
 *     var variable = 1;
 *     variable++;
 *
 * on:
 *     var _0x12d45f = 1;
 *     _0x12d45f++;
 *
 */
export class VariableDeclarationObfuscator extends NodeObfuscator {
    /**
     * @type {Map<string, string>}
     */
    private variableNames: Map <string, string> = new Map <string, string> ();
 
    /**
     * @param variableDeclarationNode
     * @param parentNode
     */
    public obfuscateNode (variableDeclarationNode: IVariableDeclarationNode, parentNode: INode): void {
        if (parentNode.type === NodeType.Program) {
            return;
        }
 
        this.storeVariableNames(variableDeclarationNode);
        this.replaceVariableNames(variableDeclarationNode, parentNode);
    }
 
    /**
     * @param variableDeclarationNode
     */
    private storeVariableNames (variableDeclarationNode: IVariableDeclarationNode): void {
        variableDeclarationNode.declarations
            .forEach((declarationNode: IVariableDeclaratorNode) => {
                estraverse.traverse(declarationNode.id, {
                    enter: (node: INode): any => this.storeIdentifiersNames(node, this.variableNames)
                });
            });
    }
 
    /**
     * @param variableDeclarationNode
     * @param variableParentNode
     */
    private replaceVariableNames (variableDeclarationNode: IVariableDeclarationNode, variableParentNode: INode): void {
        let scopeNode: INode = variableDeclarationNode.kind === 'var' ? NodeUtils.getBlockScopeOfNode(
                variableDeclarationNode
            ) : variableParentNode,
            isNodeAfterVariableDeclaratorFlag: boolean = false;
 
        estraverse.replace(scopeNode, {
            enter: (node: INode, parentNode: INode): any => {
                const functionNodes: string[] = [
                    NodeType.ArrowFunctionExpression,
                    NodeType.FunctionDeclaration,
                    NodeType.FunctionExpression
                ];
 
                if (Utils.arrayContains(functionNodes, node.type)) {
                    estraverse.replace(node, {
                        enter: (node: INode, parentNode: INode): any => {
                            this.replaceIdentifiersWithRandomNames(node, parentNode, this.variableNames);
                        }
                    });
                }
 
                if (node === variableDeclarationNode) {
                    isNodeAfterVariableDeclaratorFlag = true;
                }
 
                if (isNodeAfterVariableDeclaratorFlag) {
                    this.replaceIdentifiersWithRandomNames(node, parentNode, this.variableNames);
                }
            }
        });
    }
}
 
 
 
// WEBPACK FOOTER //
// ./src/node-obfuscators/VariableDeclarationObfuscator.ts