All files / src/stack-trace-analyzer StackTraceAnalyzer.ts

97.56% Statements 40/41
91.67% Branches 11/12
100% Functions 3/3
97.44% Lines 38/39
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 1731x 1x   1x                   1x   1x 1x                                                                   1x       1x                 1x         1x                   6267x               16799x 16799x   16799x   16799x 5x       5x         16799x               6277x               16795x 16795x 16795x   36530x 36530x 3x     36527x   36527x   434915x 396974x     37941x 17715x     20226x         16795x                           20226x 60678x     60678x 50160x     10518x              
import { injectable, inject } from 'inversify';
import { ServiceIdentifiers } from '../container/ServiceIdentifiers';
 
import * as estraverse from 'estraverse';
import * as ESTree from 'estree';
 
import { TCalleeDataExtractorFactory } from '../types/container/TCalleeDataExtractorFactory';
 
import { ICalleeData } from '../interfaces/stack-trace-analyzer/ICalleeData';
import { ICalleeDataExtractor } from '../interfaces/stack-trace-analyzer/ICalleeDataExtractor';
import { IStackTraceAnalyzer } from '../interfaces/stack-trace-analyzer/IStackTraceAnalyzer';
import { IStackTraceData } from '../interfaces/stack-trace-analyzer/IStackTraceData';
 
import { CalleeDataExtractors } from '../enums/container/CalleeDataExtractors';
 
import { Node } from '../node/Node';
import { NodeUtils } from '../node/NodeUtils';
 
/**
 * This class generates a data with code stack trace functions calls
 *
 * For example:
 *
 * function Foo () {
 *     var baz = function () {
 *
 *     }
 *
 *     baz();
 * }
 *
 * foo();
 *
 * Will generate a structure like:
 *
 * [
 *      {
 *          callee: FOO_FUNCTION_NODE
 *          name: 'Foo',
 *          trace: [
 *              {
 *                  callee: BAZ_FUNCTION_NODE,
 *                  name: 'baz,
 *                  trace: []
 *              }
 *          ]
 *      }
 * ]
 */
@injectable()
export class StackTraceAnalyzer implements IStackTraceAnalyzer {
    /**
     * @type {CalleeDataExtractors[]}
     */
    private static readonly calleeDataExtractorsList: CalleeDataExtractors[] = [
        CalleeDataExtractors.FunctionDeclarationCalleeDataExtractor,
        CalleeDataExtractors.FunctionExpressionCalleeDataExtractor,
        CalleeDataExtractors.ObjectExpressionCalleeDataExtractor
    ];
 
    /**
     * @type {number}
     */
    private static readonly limitThresholdActivationLength: number = 25;
 
    /**
     * @type {number}
     */
    private static readonly limitThreshold: number = 0.002;
 
    /**
     * @type {(calleeDataExtractorName: CalleeDataExtractors) => ICalleeDataExtractor}
     */
    private calleeDataExtractorFactory: (calleeDataExtractorName: CalleeDataExtractors) => ICalleeDataExtractor;
 
    constructor (
        @inject(ServiceIdentifiers.Factory__ICalleeDataExtractor) calleeDataExtractorFactory: TCalleeDataExtractorFactory
    ) {
        this.calleeDataExtractorFactory = calleeDataExtractorFactory;
    }
 
    /**
     * @param blockScopeBodyLength
     * @returns {number}
     */
    public static getLimitIndex (blockScopeBodyLength: number): number {
        const lastIndex: number = blockScopeBodyLength - 1;
        const limitThresholdActivationIndex: number = StackTraceAnalyzer.limitThresholdActivationLength - 1;
 
        let limitIndex: number = lastIndex;
 
        if (lastIndex > limitThresholdActivationIndex) {
            limitIndex = Math.round(
                limitThresholdActivationIndex + (lastIndex * StackTraceAnalyzer.limitThreshold)
            );
 
            Iif (limitIndex > lastIndex) {
                limitIndex = lastIndex;
            }
        }
 
        return limitIndex;
    }
 
    /**
     * @param blockScopeBody
     * @returns {IStackTraceData[]}
     */
    public analyze (blockScopeBody: ESTree.Node[]): IStackTraceData[] {
        return this.analyzeRecursive(blockScopeBody);
    }
 
    /**
     * @param blockScopeBody
     * @returns {IStackTraceData[]}
     */
    private analyzeRecursive (blockScopeBody: ESTree.Node[]): IStackTraceData[] {
        const limitIndex: number = StackTraceAnalyzer.getLimitIndex(blockScopeBody.length);
        const stackTraceData: IStackTraceData[] = [];
        const blockScopeBodyLength: number = blockScopeBody.length;
 
        for (let index: number = 0; index < blockScopeBodyLength; index++) {
            if (index > limitIndex) {
                break;
            }
 
            const blockScopeBodyNode: ESTree.Node = blockScopeBody[index];
 
            estraverse.traverse(blockScopeBodyNode, {
                enter: (node: ESTree.Node): any => {
                    if (!Node.isCallExpressionNode(node)) {
                        return;
                    }
 
                    if (blockScopeBodyNode.parentNode !== NodeUtils.getBlockScopesOfNode(node)[0]) {
                        return estraverse.VisitorOption.Skip;
                    }
 
                    this.analyzeCallExpressionNode(stackTraceData, blockScopeBody, node);
                }
            });
        }
 
        return stackTraceData;
    }
 
    /**
     * @param stackTraceData
     * @param blockScopeBody
     * @param callExpressionNode
     * @returns {IStackTraceData[]}
     */
    private analyzeCallExpressionNode (
        stackTraceData: IStackTraceData[],
        blockScopeBody: ESTree.Node[],
        callExpressionNode: ESTree.CallExpression
    ): void {
        StackTraceAnalyzer.calleeDataExtractorsList.forEach((calleeDataExtractorName: CalleeDataExtractors) => {
            const calleeData: ICalleeData | null = this.calleeDataExtractorFactory(calleeDataExtractorName)
                .extract(blockScopeBody, callExpressionNode.callee);
 
            if (!calleeData) {
                return;
            }
 
            stackTraceData.push({
                ...calleeData,
                stackTrace: this.analyzeRecursive(calleeData.callee.body)
            });
        });
    }
}