<commit_before>import * as estraverse from 'estraverse';

import { INode } from './interfaces/INode';
import { INodeObfuscator } from './interfaces/INodeObfuscator';
import { INodesGroup } from './interfaces/INodesGroup';
import { ITreeNode } from './interfaces/nodes/ITreeNode';

import { AppendState } from './enums/AppendState';
import { NodeType } from "./enums/NodeType";

import { CatchClauseObfuscator } from "./node-obfuscators/CatchClauseObfuscator";
import { ConsoleOutputDisableExpressionNode } from "./nodes/ConsoleOutputDisableExpressionNode";
import { DebugProtectionNodesGroup } from "./node-groups/DebugProtectionNodesGroup";
import { FunctionDeclarationObfuscator } from './node-obfuscators/FunctionDeclarationObfuscator';
import { FunctionObfuscator } from './node-obfuscators/FunctionObfuscator';
import { LiteralObfuscator } from './node-obfuscators/LiteralObfuscator';
import { MemberExpressionObfuscator } from './node-obfuscators/MemberExpressionObfuscator';
import { MethodDefinitionObfuscator } from './node-obfuscators/MethodDefinitionObfuscator';
import { ObjectExpressionObfuscator } from './node-obfuscators/ObjectExpressionObfuscator';
import { UnicodeArrayNode } from './nodes/UnicodeArrayNode';
import { UnicodeArrayNodesGroup } from './node-groups/UnicodeArrayNodesGroup';
import { Utils } from './Utils';
import { VariableDeclarationObfuscator } from './node-obfuscators/VariableDeclarationObfuscator';

export class Obfuscator {
    /**
     * @type {Map<string, Node>}
     */
    private nodes: Map <string, INode> = new Map <string, INode> ();

    /**
     * @type {Map<string, Function[]>}
     */
    private nodeObfuscators: Map <string, Function[]> = new Map <string, Function[]> ([
        [NodeType.ArrowFunctionExpression, [FunctionObfuscator]],
        [NodeType.ClassDeclaration, [FunctionDeclarationObfuscator]],
        [NodeType.CatchClause, [CatchClauseObfuscator]],
        [NodeType.FunctionDeclaration, [
            FunctionDeclarationObfuscator,
            FunctionObfuscator
        ]],
        [NodeType.FunctionExpression, [FunctionObfuscator]],
        [NodeType.MemberExpression, [MemberExpressionObfuscator]],
        [NodeType.MethodDefinition, [MethodDefinitionObfuscator]],
        [NodeType.ObjectExpression, [ObjectExpressionObfuscator]],
        [NodeType.VariableDeclaration, [VariableDeclarationObfuscator]],
        [NodeType.Literal, [LiteralObfuscator]]
    ]);

    /**
     * @type any
     */
    private options: any;

    /**
     * @param options
     */
    constructor (options: any = {}) {
        this.options = options;
    }

    /**
     * @param node
     */
    public obfuscateNode (node: ITreeNode): void {
        this.insertNewNodes(node);
        this.beforeObfuscation(node);

        estraverse.replace(node, {
            enter: (node: ITreeNode, parent: ITreeNode): any => this.nodeControllerFirstPass(node, parent)
        });

        estraverse.replace(node, {
            leave: (node: ITreeNode, parent: ITreeNode): any => this.nodeControllerSecondPass(node, parent)
        });

        this.afterObfuscation(node);
    }

    /**
     * @param nodeName
     * @param node
     */
    public setNode (nodeName: string, node: INode): void {
        this.nodes.set(nodeName, node);
    }

    /**
     * @param nodesGroup
     */
    public setNodesGroup (nodesGroup: INodesGroup): void {
        let nodes: Map <string, INode> = nodesGroup.getNodes();

        nodes.forEach((node: INode, key: string) => {
            this.nodes.set(key, node);
        });
    }

    /**
     * @param node
     */
    private afterObfuscation (node: ITreeNode): void {
        this.nodes.forEach((node: INode) => {
            if (node.getAppendState() === AppendState.AfterObfuscation) {
                node.appendNode();
            }
        });
    }

    /**
     * @param node
     */
    private beforeObfuscation (node: ITreeNode): void {
        this.nodes.forEach((node: INode) => {
            if (node.getAppendState() === AppendState.BeforeObfuscation) {
                node.appendNode();
            }
        });
    };

    private insertNewNodes (astTree: ITreeNode): void {
        if (this.options['disableConsoleOutput']) {<commit_msg>added option for disabling console output<commit_after>import * as estraverse from 'estraverse';

import { INode } from './interfaces/INode';
import { INodeObfuscator } from './interfaces/INodeObfuscator';
import { INodesGroup } from './interfaces/INodesGroup';
import { ITreeNode } from './interfaces/nodes/ITreeNode';

import { AppendState } from './enums/AppendState';
import { NodeType } from "./enums/NodeType";

import { CatchClauseObfuscator } from "./node-obfuscators/CatchClauseObfuscator";
import { ConsoleOutputDisableExpressionNode } from "./nodes/ConsoleOutputDisableExpressionNode";
import { DebugProtectionNodesGroup } from "./node-groups/DebugProtectionNodesGroup";
import { FunctionDeclarationObfuscator } from './node-obfuscators/FunctionDeclarationObfuscator';
import { FunctionObfuscator } from './node-obfuscators/FunctionObfuscator';
import { LiteralObfuscator } from './node-obfuscators/LiteralObfuscator';
import { MemberExpressionObfuscator } from './node-obfuscators/MemberExpressionObfuscator';
import { MethodDefinitionObfuscator } from './node-obfuscators/MethodDefinitionObfuscator';
import { ObjectExpressionObfuscator } from './node-obfuscators/ObjectExpressionObfuscator';
import { UnicodeArrayNode } from './nodes/UnicodeArrayNode';
import { UnicodeArrayNodesGroup } from './node-groups/UnicodeArrayNodesGroup';
import { Utils } from './Utils';
import { VariableDeclarationObfuscator } from './node-obfuscators/VariableDeclarationObfuscator';

export class Obfuscator {
    /**
     * @type {Map<string, Node>}
     */
    private nodes: Map <string, INode> = new Map <string, INode> ();

    /**
     * @type {Map<string, Function[]>}
     */
    private nodeObfuscators: Map <string, Function[]> = new Map <string, Function[]> ([
        [NodeType.ArrowFunctionExpression, [FunctionObfuscator]],
        [NodeType.ClassDeclaration, [FunctionDeclarationObfuscator]],
        [NodeType.CatchClause, [CatchClauseObfuscator]],
        [NodeType.FunctionDeclaration, [
            FunctionDeclarationObfuscator,
            FunctionObfuscator
        ]],
        [NodeType.FunctionExpression, [FunctionObfuscator]],
        [NodeType.MemberExpression, [MemberExpressionObfuscator]],
        [NodeType.MethodDefinition, [MethodDefinitionObfuscator]],
        [NodeType.ObjectExpression, [ObjectExpressionObfuscator]],
        [NodeType.VariableDeclaration, [VariableDeclarationObfuscator]],
        [NodeType.Literal, [LiteralObfuscator]]
    ]);

    /**
     * @type any
     */
    private options: any;

    /**
     * @param options
     */
    constructor (options: any = {}) {
        this.options = options;
    }

    /**
     * @param node
     */
    public obfuscateNode (node: ITreeNode): void {
        this.setNewNodes(node);
        this.beforeObfuscation(node);

        estraverse.replace(node, {
            enter: (node: ITreeNode, parent: ITreeNode): any => this.nodeControllerFirstPass(node, parent)
        });

        estraverse.replace(node, {
            leave: (node: ITreeNode, parent: ITreeNode): any => this.nodeControllerSecondPass(node, parent)
        });

        this.afterObfuscation(node);
    }

    /**
     * @param nodeName
     * @param node
     */
    public setNode (nodeName: string, node: INode): void {
        this.nodes.set(nodeName, node);
    }

    /**
     * @param nodesGroup
     */
    public setNodesGroup (nodesGroup: INodesGroup): void {
        let nodes: Map <string, INode> = nodesGroup.getNodes();

        nodes.forEach((node: INode, key: string) => {
            this.nodes.set(key, node);
        });
    }

    /**
     * @param node
     */
    private afterObfuscation (node: ITreeNode): void {
        this.nodes.forEach((node: INode) => {
            if (node.getAppendState() === AppendState.AfterObfuscation) {
                node.appendNode();
            }
        });
    }

    /**
     * @param node
     */
    private beforeObfuscation (node: ITreeNode): void {
        this.nodes.forEach((node: INode) => {
            if (node.getAppendState() === AppendState.BeforeObfuscation) {
                node.appendNode();
            }
        });
    };

    private setNewNodes (astTree: ITreeNode): void {
        if (this.options['disableConsoleOutput']) {
{'fraction_non_alphanumeric': 0.1277595115077501, 'fraction_numerical': 0.0, 'mean_word_length': 3.0557142857142856, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2049707', 'n_tokens_mistral': 2495, 'n_tokens_neox': 2477, 'n_words': 473}