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

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

64.29% Statements 9/14
100% Branches 0/0
0% Functions 0/1
61.54% Lines 8/13

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

    1x

    1x
    1x
    1x

    1x
    1x

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

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

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

    import { AbstractControlFlowReplacer } from './AbstractControlFlowReplacer';
    import { BinaryExpressionFunctionNode } from '../../../custom-nodes/control-flow-replacers-nodes/binary-expression-control-flow-replacer-nodes/BinaryExpressionFunctionNode';
    import { ControlFlowStorageCallNode } from '../../../custom-nodes/control-flow-replacers-nodes/binary-expression-control-flow-replacer-nodes/ControlFlowStorageCallNode';

    @injectable()
    export class BinaryExpressionControlFlowReplacer extends AbstractControlFlowReplacer {
        /**
         * @param customNodesStorage
         * @param options
         */
        constructor (
            @inject(ServiceIdentifiers['IStorage<ICustomNode>']) customNodesStorage: IStorage<ICustomNode>,
            @inject(ServiceIdentifiers.IOptions) options: IOptions
        ) {
            super(customNodesStorage, options);
        }

        /**
         * @param expressionNode
         * @returns {string}
         */
        private static getExpressionValue (expressionNode: ESTree.Expression): string {
            return escodegen.generate(expressionNode, {
                sourceMapWithCode: true
            }).code;
        }

        /**
         * @param binaryExpressionNode
         * @param parentNode
         * @param controlFlowStorage
         * @param controlFlowStorageCustomNodeName
         * @returns {ICustomNode | undefined}
         */
        public replace (
            binaryExpressionNode: ESTree.BinaryExpression,
            parentNode: ESTree.Node,
            controlFlowStorage: IStorage <ICustomNode>,
            controlFlowStorageCustomNodeName: string
        ): ICustomNode | undefined {
            const key: string = AbstractControlFlowReplacer.getStorageKey();

            controlFlowStorage.set(key, new BinaryExpressionFunctionNode(binaryExpressionNode.operator, this.options));

            return new ControlFlowStorageCallNode(
                controlFlowStorageCustomNodeName,
                key,
                BinaryExpressionControlFlowReplacer.getExpressionValue(binaryExpressionNode.left),
                BinaryExpressionControlFlowReplacer.getExpressionValue(binaryExpressionNode.right),
                this.options
            );
        }
    }