# All files / src/container/modules/node-transformers NodeTransformersModule.ts

100% Statements 37/37
100% Branches 4/4
100% Functions 4/4
100% Lines 37/37

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

    1x

    1x

    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x

    1x

    1819x

    1819x

    1819x

    1819x

    1819x

    1819x

    1819x

    1819x

    1819x

    1819x

    1819x

    1812x

    2011x
    285696x
    285696x

    285696x
    96616x

    96616x
    87477x

    9139x

    9139x

    96616x

    285696x

     import { ContainerModule, interfaces } from 'inversify';
    import { ServiceIdentifiers } from '../../ServiceIdentifiers';

    import { INodeTransformer } from '../../../interfaces/node-transformers/INodeTransformer';

    import { NodeTransformers } from '../../../enums/container/NodeTransformers';

    import { FunctionControlFlowTransformer } from '../../../node-transformers/node-control-flow-transformers/FunctionControlFlowTransformer';

    import { CatchClauseObfuscator } from '../../../node-transformers/node-obfuscators/CatchClauseObfuscator';
    import { FunctionDeclarationObfuscator } from '../../../node-transformers/node-obfuscators/FunctionDeclarationObfuscator';
    import { FunctionObfuscator } from '../../../node-transformers/node-obfuscators/FunctionObfuscator';
    import { LabeledStatementObfuscator } from '../../../node-transformers/node-obfuscators/LabeledStatementObfuscator';
    import { LiteralObfuscator } from '../../../node-transformers/node-obfuscators/LiteralObfuscator';
    import { MemberExpressionObfuscator } from '../../../node-transformers/node-obfuscators/MemberExpressionObfuscator';
    import { MethodDefinitionObfuscator } from '../../../node-transformers/node-obfuscators/MethodDefinitionObfuscator';
    import { ObjectExpressionObfuscator } from '../../../node-transformers/node-obfuscators/ObjectExpressionObfuscator';
    import { VariableDeclarationObfuscator } from '../../../node-transformers/node-obfuscators/VariableDeclarationObfuscator';

    export const nodeTransformersModule: interfaces.ContainerModule = new ContainerModule((bind: interfaces.Bind) => {
        // node control flow transformers
        bind<INodeTransformer>(ServiceIdentifiers.INodeTransformer)
            .to(FunctionControlFlowTransformer)
            .whenTargetNamed(NodeTransformers.FunctionControlFlowTransformer);

        // node obfuscators
        bind<INodeTransformer>(ServiceIdentifiers.INodeTransformer)
            .to(CatchClauseObfuscator)
            .whenTargetNamed(NodeTransformers.CatchClauseObfuscator);

        bind<INodeTransformer>(ServiceIdentifiers.INodeTransformer)
            .to(FunctionDeclarationObfuscator)
            .whenTargetNamed(NodeTransformers.FunctionDeclarationObfuscator);

        bind<INodeTransformer>(ServiceIdentifiers.INodeTransformer)
            .to(FunctionObfuscator)
            .whenTargetNamed(NodeTransformers.FunctionObfuscator);

        bind<INodeTransformer>(ServiceIdentifiers.INodeTransformer)
            .to(LabeledStatementObfuscator)
            .whenTargetNamed(NodeTransformers.LabeledStatementObfuscator);

        bind<INodeTransformer>(ServiceIdentifiers.INodeTransformer)
            .to(LiteralObfuscator)
            .whenTargetNamed(NodeTransformers.LiteralObfuscator);

        bind<INodeTransformer>(ServiceIdentifiers.INodeTransformer)
            .to(MemberExpressionObfuscator)
            .whenTargetNamed(NodeTransformers.MemberExpressionObfuscator);

        bind<INodeTransformer>(ServiceIdentifiers.INodeTransformer)
            .to(MethodDefinitionObfuscator)
            .whenTargetNamed(NodeTransformers.MethodDefinitionObfuscator);

        bind<INodeTransformer>(ServiceIdentifiers.INodeTransformer)
            .to(ObjectExpressionObfuscator)
            .whenTargetNamed(NodeTransformers.ObjectExpressionObfuscator);

        bind<INodeTransformer>(ServiceIdentifiers.INodeTransformer)
            .to(VariableDeclarationObfuscator)
            .whenTargetNamed(NodeTransformers.VariableDeclarationObfuscator);

        // node transformers factory
        bind<INodeTransformer[]>(ServiceIdentifiers['Factory<INodeTransformer[]>'])
            .toFactory<INodeTransformer[]>((context: interfaces.Context) => {
                const cache: Map <NodeTransformers, INodeTransformer> = new Map();

                return (nodeTransformersMap: Map<string, NodeTransformers[]>) => (nodeType: string) => {
                    const nodeTransformers: NodeTransformers[] = nodeTransformersMap.get(nodeType) || [];
                    const instancesArray: INodeTransformer[] = [];

                    nodeTransformers.forEach((transformer: NodeTransformers) => {
                        let nodeTransformer: INodeTransformer;

                        if (cache.has(transformer)) {
                            nodeTransformer = <INodeTransformer>cache.get(transformer);
                        } else {
                            nodeTransformer = context.container.getNamed<INodeTransformer>(
                                ServiceIdentifiers.INodeTransformer,
                                transformer
                            );
                            cache.set(transformer, nodeTransformer);
                        }

                        instancesArray.push(nodeTransformer);
                    });

                    return instancesArray;
                };
            });
    });