Code coverage report for src/node-transformers/node-control-flow-transformers/FunctionControlFlowTransformer.ts

# All files / src/node-transformers/node-control-flow-transformers FunctionControlFlowTransformer.ts

98.36% Statements 60/61
88.89% Branches 16/18
100% Functions 2/2
98.28% Lines 57/58

    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
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    1901x
    1x

    1x

    1x
    1x

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

    1x

    1x

    1x

    1x

    838x

    838x

    838x

    838x
    838x
    838x

    2540x

    2540x
    1850x

    690x

    690x
    690x

    690x
    40x

    690x

    2540x

    2540x
    2540x

    2540x
    2540x
    2540x

    2540x
    1024x

    1516x

    1516x
    1516x
    1516x

    1516x

    2540x

    2540x
    554x
    548x

    554x

    554x

    2540x

    2540x

    81187x
    72867x

    8320x
    2789x

    5531x

    5531x

     import { injectable, inject } from 'inversify';
    import { ServiceIdentifiers } from '../../container/ServiceIdentifiers';

    import * as estraverse from 'estraverse';
    import * as ESTree from 'estree';

    import { TControlFlowReplacerFactory } from '../../types/container/TControlFlowReplacerFactory';
    import { TControlFlowStorageFactory } from '../../types/container/TControlFlowStorageFactory';
    import { TCustomNodeFactory } from '../../types/container/TCustomNodeFactory';
    import { TNodeWithBlockStatement } from '../../types/node/TNodeWithBlockStatement';

    import { ICustomNode } from '../../interfaces/custom-nodes/ICustomNode';
    import { IOptions } from '../../interfaces/options/IOptions';
    import { IStorage } from '../../interfaces/storages/IStorage';

    import { CustomNodes } from '../../enums/container/CustomNodes';
    import { NodeType } from '../../enums/NodeType';

    import { AbstractNodeTransformer } from '../AbstractNodeTransformer';
    import { Node } from '../../node/Node';
    import { NodeAppender } from '../../node/NodeAppender';
    import { NodeControlFlowReplacers } from '../../enums/container/NodeControlFlowReplacers';
    import { NodeUtils } from '../../node/NodeUtils';
    import { RandomGeneratorUtils } from '../../utils/RandomGeneratorUtils';

    @injectable()
    export class FunctionControlFlowTransformer extends AbstractNodeTransformer {
        /**
         * @type {Map <string, NodeControlFlowReplacers>}
         */
        private static readonly controlFlowReplacersMap: Map <string, NodeControlFlowReplacers> = new Map([
            [NodeType.BinaryExpression, NodeControlFlowReplacers.BinaryExpressionControlFlowReplacer],
            [NodeType.CallExpression, NodeControlFlowReplacers.CallExpressionControlFlowReplacer],
            [NodeType.LogicalExpression, NodeControlFlowReplacers.LogicalExpressionControlFlowReplacer]
        ]);

        /**
         * @type {number}
         */
        private static readonly hostNodeSearchMinDepth: number = 0;

        /**
         * @type {number}
         */
        private static readonly hostNodeSearchMaxDepth: number = 2;

        /**
         * @type {Map<ESTree.Node, IStorage<ICustomNode>>}
         */
        private controlFlowData: Map <ESTree.Node, IStorage<ICustomNode>> = new Map();

        /**
         * @type {TNodeWithBlockStatement[]}
         */
        private readonly hostNodesWithControlFlowNode: TNodeWithBlockStatement[] = [];

        /**
         * @type {TControlFlowReplacerFactory}
         */
        private readonly controlFlowReplacerFactory: TControlFlowReplacerFactory;

        /**
         * @type {TControlFlowStorageFactory}
         */
        private readonly controlFlowStorageFactory: TControlFlowStorageFactory;

        /**
         * @type {TCustomNodeFactory}
         */
        private readonly customNodeFactory: TCustomNodeFactory;

        /**
         * @param controlFlowStorageFactory
         * @param controlFlowReplacerFactory
         * @param customNodeFactory
         * @param options
         */
        constructor (
            @inject(ServiceIdentifiers.Factory__TControlFlowStorage) controlFlowStorageFactory: TControlFlowStorageFactory,
            @inject(ServiceIdentifiers.Factory__IControlFlowReplacer) controlFlowReplacerFactory: TControlFlowReplacerFactory,
            @inject(ServiceIdentifiers.Factory__ICustomNode) customNodeFactory: TCustomNodeFactory,
            @inject(ServiceIdentifiers.IOptions) options: IOptions
        ) {
            super(options);

            this.controlFlowStorageFactory = controlFlowStorageFactory;
            this.controlFlowReplacerFactory = controlFlowReplacerFactory;
            this.customNodeFactory = customNodeFactory;
        }

        /**
         * @param functionNode
         * @returns {TNodeWithBlockStatement}
         */
        private static getHostNode (functionNode: ESTree.FunctionDeclaration | ESTree.FunctionExpression): TNodeWithBlockStatement {
            const blockScopesOfNode: TNodeWithBlockStatement[] = NodeUtils.getBlockScopesOfNode(functionNode);

            if (blockScopesOfNode.length === 1) {
                return functionNode.body;
            } else {
                blockScopesOfNode.pop();
            }

            Eif (blockScopesOfNode.length > FunctionControlFlowTransformer.hostNodeSearchMinDepth) {
                blockScopesOfNode.splice(0, FunctionControlFlowTransformer.hostNodeSearchMinDepth);
            }

            if (blockScopesOfNode.length > FunctionControlFlowTransformer.hostNodeSearchMaxDepth) {
                blockScopesOfNode.length = FunctionControlFlowTransformer.hostNodeSearchMaxDepth;
            }

            return RandomGeneratorUtils.getRandomGenerator().pickone(blockScopesOfNode);
        }

        /**
         * @param functionNode
         * @returns {ESTree.Node}
         */
        public transformNode (functionNode: ESTree.Function): ESTree.Node {
            Iif (Node.isArrowFunctionExpressionNode(functionNode)) {
                return functionNode;
            }

            const hostNode: TNodeWithBlockStatement = FunctionControlFlowTransformer.getHostNode(functionNode);
            const controlFlowStorage: IStorage<ICustomNode> = this.getControlFlowStorage(hostNode);

            this.controlFlowData.set(hostNode, controlFlowStorage);
            this.transformFunctionBody(functionNode.body, controlFlowStorage);
            NodeUtils.parentize(functionNode);

            if (!controlFlowStorage.getLength()) {
                return functionNode;
            }

            const controlFlowStorageCustomNode: ICustomNode = this.customNodeFactory(CustomNodes.ControlFlowStorageNode);

            controlFlowStorageCustomNode.initialize(controlFlowStorage);
            NodeAppender.prependNode(hostNode, controlFlowStorageCustomNode.getNode());
            this.hostNodesWithControlFlowNode.push(hostNode);

            return functionNode;
        }

        /**
         * @param hostNode
         * @return {IStorage<ICustomNode>}
         */
        private getControlFlowStorage (hostNode: TNodeWithBlockStatement): IStorage<ICustomNode> {
            const controlFlowStorage: IStorage <ICustomNode> = this.controlFlowStorageFactory();

            if (this.controlFlowData.has(hostNode)) {
                if (this.hostNodesWithControlFlowNode.indexOf(hostNode) !== -1) {
                    hostNode.body.shift();
                }

                const hostControlFlowStorage: IStorage<ICustomNode> = <IStorage<ICustomNode>>this.controlFlowData.get(hostNode);

                controlFlowStorage.mergeWith(hostControlFlowStorage, true);
            }

            return controlFlowStorage;
        }

        /**
         * @param functionNodeBody
         * @param controlFlowStorage
         */
        private transformFunctionBody (functionNodeBody: ESTree.BlockStatement, controlFlowStorage: IStorage<ICustomNode>): void {
            estraverse.replace(functionNodeBody, {
                enter: (node: ESTree.Node, parentNode: ESTree.Node): any => {
                    if (!FunctionControlFlowTransformer.controlFlowReplacersMap.has(node.type)) {
                        return node;
                    }

                    if (RandomGeneratorUtils.getRandomFloat(0, 1) > this.options.controlFlowFlatteningThreshold) {
                        return node;
                    }

                    const controlFlowReplacerName: NodeControlFlowReplacers = <NodeControlFlowReplacers>FunctionControlFlowTransformer
                        .controlFlowReplacersMap.get(node.type);

                    return {
                        ...this.controlFlowReplacerFactory(controlFlowReplacerName).replace(node, parentNode, controlFlowStorage),
                        parentNode
                    };
                }
            });
        }
    }