# All files / src/node-obfuscators VariableDeclarationObfuscator.js

100% Statements 29/29
90.91% Branches 10/11
100% Functions 5/5
100% Lines 27/27

    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
    1x
    1x
    1x
    1x
    1x

    88x

    22x

    22x
    3x

    19x
    19x

    19x
    19x

    30x
    30x

    19x
    19x
    19x

    2028x

    2028x
    29x

    564x

    2028x
    19x

    2028x
    1493x

    1x
     "use strict";
    const estraverse = require('estraverse');
    const NodeType_1 = require("../enums/NodeType");
    const NodeObfuscator_1 = require('./NodeObfuscator');
    const NodeUtils_1 = require("../NodeUtils");
    const Utils_1 = require('../Utils');
    class VariableDeclarationObfuscator extends NodeObfuscator_1.NodeObfuscator {
        constructor(...args) {
            super(...args);
            this.variableNames = new Map();
        }
        obfuscateNode(variableDeclarationNode, parentNode) {
            if (parentNode.type === NodeType_1.NodeType.Program) {
                return;
            }
            this.storeVariableNames(variableDeclarationNode);
            this.replaceVariableNames(variableDeclarationNode, parentNode);
        }
        storeVariableNames(variableDeclarationNode) {
            variableDeclarationNode.declarations
                .forEach((declarationNode) => {
                estraverse.traverse(declarationNode.id, {
                    enter: (node) => this.storeIdentifiersNames(node, this.variableNames)
                });
            });
        }
        replaceVariableNames(variableDeclarationNode, variableParentNode) {
            let scopeNode = variableDeclarationNode.kind === 'var' ? NodeUtils_1.NodeUtils.getBlockScopeOfNode(variableDeclarationNode) : variableParentNode, isNodeAfterVariableDeclaratorFlag = false;
            estraverse.replace(scopeNode, {
                enter: (node, parentNode) => {
                    const functionNodes = [
                        NodeType_1.NodeType.ArrowFunctionExpression,
                        NodeType_1.NodeType.FunctionDeclaration,
                        NodeType_1.NodeType.FunctionExpression
                    ];
                    if (Utils_1.Utils.arrayContains(functionNodes, node.type)) {
                        estraverse.replace(node, {
                            enter: (node, parentNode) => {
                                this.replaceIdentifiersWithRandomNames(node, parentNode, this.variableNames);
                            }
                        });
                    }
                    if (node === variableDeclarationNode) {
                        isNodeAfterVariableDeclaratorFlag = true;
                    }
                    if (isNodeAfterVariableDeclaratorFlag) {
                        this.replaceIdentifiersWithRandomNames(node, parentNode, this.variableNames);
                    }
                }
            });
        }
    }
    exports.VariableDeclarationObfuscator = VariableDeclarationObfuscator;
    //# sourceMappingURL=VariableDeclarationObfuscator.js.map