# All files / src/node NodeAppender.ts

88.24% Statements 30/34
83.33% Branches 20/24
100% Functions 1/1
87.88% Lines 29/33

    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

    1x

    1x

    3x

    3x

    3x

    522x

    522x

    522x
    4x

    518x

    522x

    1549x

    1549x

    1549x

    1549x
    518x

    1031x

    5528x

    1043x

    1043x

    1043x

    1780x

    1780x

    1780x

    3343x
    3343x

    2826x

    2826x
    3343x

     import * as ESTree from 'estree';

    import { TNodeWithBlockStatement } from '../types/node/TNodeWithBlockStatement';
    import { TStatement } from '../types/node/TStatement';

    import { IStackTraceData } from '../interfaces/stack-trace-analyzer/IStackTraceData';

    import { RandomGeneratorUtils } from '../utils/RandomGeneratorUtils';

    /**
     * This class appends node into a first deepest BlockStatement in order of function calls
     *
     * For example:
     *
     * function Foo () {
     *     var baz = function () {
     *
     *     }
     *
     *     baz();
     * }
     *
     * foo();
     *
     * Appends node into block statement of `baz` function expression.
     */
    export class NodeAppender {
        /**
         * @param blockScopeNode
         * @param nodeBodyStatements
         */
        public static appendNode (
            blockScopeNode: TNodeWithBlockStatement,
            nodeBodyStatements: TStatement[]
        ): void {
            Iif (!NodeAppender.validateBodyStatements(nodeBodyStatements)) {
                nodeBodyStatements = [];
            }

            nodeBodyStatements = NodeAppender.parentizeBodyStatementsBeforeAppend(blockScopeNode, nodeBodyStatements);

            blockScopeNode.body = [
                ...blockScopeNode.body,
                ...nodeBodyStatements
            ];
        }

        /**
         * @param blockScopeStackTraceData
         * @param blockScopeNode
         * @param nodeBodyStatements
         * @param index
         */
        public static appendNodeToOptimalBlockScope (
            blockScopeStackTraceData: IStackTraceData[],
            blockScopeNode: TNodeWithBlockStatement,
            nodeBodyStatements: TStatement[],
            index: number = 0
        ): void {
            let targetBlockScope: TNodeWithBlockStatement;

            if (!blockScopeStackTraceData.length) {
                targetBlockScope = blockScopeNode;
            } else {
                targetBlockScope = NodeAppender.getOptimalBlockScope(
                    blockScopeStackTraceData,
                    index
                );
            }

            NodeAppender.prependNode(targetBlockScope, nodeBodyStatements);
        }

        /**
         * Returns deepest block scope node at given deep.
         *
         * @param blockScopeTraceData
         * @param index
         * @param deep
         * @returns {ESTree.BlockStatement}
         */
        public static getOptimalBlockScope (
            blockScopeTraceData: IStackTraceData[],
            index: number,
            deep: number = Infinity
        ): ESTree.BlockStatement {
            const firstCall: IStackTraceData = blockScopeTraceData[index];

            Iif (deep <= 0) {
                throw new Error(`Invalid \`deep\` argument value. Value should be bigger then 0.`);
            }

            if (deep > 1 && firstCall.stackTrace.length) {
                return NodeAppender.getOptimalBlockScope(firstCall.stackTrace, 0, --deep);
            } else {
                return firstCall.callee;
            }
        }

        /**
         * @param stackTraceRootLength
         */
        public static getRandomStackTraceIndex (stackTraceRootLength: number): number {
            return RandomGeneratorUtils.getRandomInteger(0, Math.max(0, Math.round(stackTraceRootLength - 1)));
        }

        /**
         * @param blockScopeNode
         * @param nodeBodyStatements
         * @param index
         */
        public static insertNodeAtIndex (
            blockScopeNode: TNodeWithBlockStatement,
            nodeBodyStatements: TStatement[],
            index: number
        ): void {
            Iif (!NodeAppender.validateBodyStatements(nodeBodyStatements)) {
                nodeBodyStatements = [];
            }

            nodeBodyStatements = NodeAppender.parentizeBodyStatementsBeforeAppend(blockScopeNode, nodeBodyStatements);

            blockScopeNode.body = [
                ...blockScopeNode.body.slice(0, index),
                ...nodeBodyStatements,
                ...blockScopeNode.body.slice(index)
            ];
        }

        /**
         * @param blockScopeNode
         * @param nodeBodyStatements
         */
        public static prependNode (
            blockScopeNode: TNodeWithBlockStatement,
            nodeBodyStatements: TStatement[]
        ): void {
            Iif (!NodeAppender.validateBodyStatements(nodeBodyStatements)) {
                nodeBodyStatements = [];
            }

            nodeBodyStatements = NodeAppender.parentizeBodyStatementsBeforeAppend(blockScopeNode, nodeBodyStatements);

            blockScopeNode.body = [
                ...nodeBodyStatements,
                ...blockScopeNode.body,
            ];
        }

        /**
         * @param blockScopeNode
         * @param nodeBodyStatements
         */
        private static parentizeBodyStatementsBeforeAppend (
            blockScopeNode: TNodeWithBlockStatement,
            nodeBodyStatements: TStatement[]
        ): TStatement[] {
            for (const statement of nodeBodyStatements) {
                statement.parentNode = blockScopeNode;
            }

            return nodeBodyStatements;
        }

        /**
         * @param nodeBodyStatements
         * @returns {boolean}
         */
        private static validateBodyStatements (nodeBodyStatements: TStatement[]): boolean {
            return nodeBodyStatements.every((statementNode: TStatement) => {
                return !!statementNode && statementNode.hasOwnProperty('type');
            });
        }
    }