Code coverage report for src/container/modules/node-transformers/NodeTransformersModule.ts

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

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

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

    1x

    1x

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

    1x
    2628x

    2628x

    2628x

    2628x

    2628x

    2628x

    2628x

    2628x

    2628x

    2628x

    2628x

    2628x

    2622x

    2619x
    495400x
    495400x

    495400x
    163732x

    163732x
    151253x

    12479x

    12479x

    163732x

    495400x

     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) => {
        const nodeTransformersTag: string = 'nodeTransformers';

        // node control flow transformers
        bind<INodeTransformer>(ServiceIdentifiers.INodeTransformer)
            .to(FunctionControlFlowTransformer)
            .whenTargetTagged(nodeTransformersTag, NodeTransformers.FunctionControlFlowTransformer);

        // node obfuscators
        bind<INodeTransformer>(ServiceIdentifiers.INodeTransformer)
            .to(CatchClauseObfuscator)
            .whenTargetTagged(nodeTransformersTag, NodeTransformers.CatchClauseObfuscator);

        bind<INodeTransformer>(ServiceIdentifiers.INodeTransformer)
            .to(FunctionDeclarationObfuscator)
            .whenTargetTagged(nodeTransformersTag, NodeTransformers.FunctionDeclarationObfuscator);

        bind<INodeTransformer>(ServiceIdentifiers.INodeTransformer)
            .to(FunctionObfuscator)
            .whenTargetTagged(nodeTransformersTag, NodeTransformers.FunctionObfuscator);

        bind<INodeTransformer>(ServiceIdentifiers.INodeTransformer)
            .to(LabeledStatementObfuscator)
            .whenTargetTagged(nodeTransformersTag, NodeTransformers.LabeledStatementObfuscator);

        bind<INodeTransformer>(ServiceIdentifiers.INodeTransformer)
            .to(LiteralObfuscator)
            .whenTargetTagged(nodeTransformersTag, NodeTransformers.LiteralObfuscator);

        bind<INodeTransformer>(ServiceIdentifiers.INodeTransformer)
            .to(MemberExpressionObfuscator)
            .whenTargetTagged(nodeTransformersTag, NodeTransformers.MemberExpressionObfuscator);

        bind<INodeTransformer>(ServiceIdentifiers.INodeTransformer)
            .to(MethodDefinitionObfuscator)
            .whenTargetTagged(nodeTransformersTag, NodeTransformers.MethodDefinitionObfuscator);

        bind<INodeTransformer>(ServiceIdentifiers.INodeTransformer)
            .to(ObjectExpressionObfuscator)
            .whenTargetTagged(nodeTransformersTag, NodeTransformers.ObjectExpressionObfuscator);

        bind<INodeTransformer>(ServiceIdentifiers.INodeTransformer)
            .to(VariableDeclarationObfuscator)
            .whenTargetTagged(nodeTransformersTag, NodeTransformers.VariableDeclarationObfuscator);

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

                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.getTagged<INodeTransformer>(
                                ServiceIdentifiers.INodeTransformer,
                                nodeTransformersTag,
                                transformer
                            );
                            cache.set(transformer, nodeTransformer);
                        }

                        instancesArray.push(nodeTransformer);
                    });

                    return instancesArray;
                };
            });
    });