Code coverage report for src/node/NodeUtils.ts

# All files / src/node NodeUtils.ts

98.15% Statements 53/54
92.86% Branches 39/42
100% Functions 4/4
98.15% Lines 53/54

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

    1x

    1x
    1x

    1x

    1x

    3649x

    44353x

    3648x

    3648x
    3648x

    3648x

    4x
    4x
    3x
    1x

    2x

    1x

    230091x
    230091x

    230091x
    1x

    230090x
    116865x

    116865x
    19999x
    96866x
    1x

    96865x

    113225x
    6305x

    106920x

    6274x

    6274x

    765398x

    765398x
    6274x
    6273x

    1x
    1x

    6274x

    759124x

    765398x
    765398x

    49552x

    49552x

    49552x

    373215x
    45903x

    373215x
    44353x

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

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

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

    import { Node } from './Node';
    import { Utils } from '../Utils';

    export class NodeUtils {
        /**
         * @type {string[]}
         */
        private static nodesWithBlockScope: string[] = [
            NodeType.ArrowFunctionExpression,
            NodeType.FunctionDeclaration,
            NodeType.FunctionExpression,
            NodeType.MethodDefinition,
            NodeType.Program
        ];

        /**
         * @param node
         */
        public static addXVerbatimPropertyToLiterals (node: ESTree.Node): void {
            NodeUtils.typedReplace(node, NodeType.Literal, {
                leave: (node: ESTree.Literal) => {
                    node['x-verbatim-property'] = {
                        content : node.raw,
                        precedence: escodegen.Precedence.Primary
                    };
                }
            });
        }

        /**
         * @param code
         * @returns {TStatement[]}
         */
        public static convertCodeToStructure (code: string): TStatement[] {
            let structure: ESTree.Program = esprima.parse(code);

            NodeUtils.addXVerbatimPropertyToLiterals(structure);
            NodeUtils.parentize(structure);

            return <TStatement[]>structure.body;
        }

        /**
         * @param node
         * @param index
         * @returns {ESTree.Node}
         */
        public static getBlockStatementNodeByIndex (node: ESTree.Node, index: number = 0): ESTree.Node {
            if (Node.isNodeHasBlockStatement(node)) {
                if (node.body[index] === undefined) {
                    throw new ReferenceError(`Wrong index \`${index}\`. Block-statement body length is \`${node.body.length}\``);
                }

                return node.body[index];
            }

            throw new TypeError('The specified node have no a block-statement');
        }

        /**
         * @param node
         * @param depth
         * @returns {ESTree.Node}
         */
        public static getBlockScopeOfNode (node: ESTree.Node, depth: number = 0): TNodeWithBlockStatement {
            let parentNode: ESTree.Node | undefined = node.parentNode;

            if (!parentNode) {
                throw new ReferenceError('`parentNode` property of given node is `undefined`');
            }

            if (Node.isBlockStatementNode(parentNode)) {
                Iif (!parentNode.parentNode) {
                    throw new ReferenceError('`parentNode` property of `parentNode` of given node is `undefined`');
                }

                if (!Utils.arrayContains(NodeUtils.nodesWithBlockScope, parentNode.parentNode.type)) {
                    return NodeUtils.getBlockScopeOfNode(parentNode, depth);
                } else if (depth > 0) {
                    return NodeUtils.getBlockScopeOfNode(parentNode, --depth);
                }

                return parentNode;
            }

            if (Node.isProgramNode(parentNode)) {
                return parentNode;
            }

            return NodeUtils.getBlockScopeOfNode(parentNode);
        }

        /**
         * @param node
         */
        public static parentize (node: ESTree.Node): void {
            let isRootNode: boolean = true;

            estraverse.replace(node, {
                enter: (node: ESTree.Node, parentNode: ESTree.Node): any => {
                    let value: ESTree.Node;

                    if (isRootNode) {
                        if (node.type === NodeType.Program) {
                            value = node;
                        } else {
                            value = Node.getProgramNode(<TStatement[]>[node]);
                            value.parentNode = value;
                        }

                        isRootNode = false;
                    } else {
                        value = parentNode || node;
                    }

                    node.parentNode = value;
                    node.obfuscated = false;
                }
            });
        }

        /**
         * @param node
         * @param nodeType
         * @param visitor
         */
        public static typedReplace (
            node: ESTree.Node,
            nodeType: string,
            visitor: {enter?: (node: ESTree.Node) => void, leave?: (node: ESTree.Node) => void},
        ): void {
            NodeUtils.typedTraverse(node, nodeType, visitor, 'replace');
        }

        /**
         * @param node
         * @param nodeType
         * @param visitor
         * @param traverseType
         */
        public static typedTraverse (
            node: ESTree.Node,
            nodeType: string,
            visitor: estraverse.Visitor,
            traverseType: string = 'traverse'
        ): void {
            (<any>estraverse)[traverseType](node, {
                enter: (node: ESTree.Node, parentNode: ESTree.Node): any => {
                    if (node.type === nodeType && visitor.enter) {
                        visitor.enter(node, parentNode);
                    }
                },
                leave: (node: ESTree.Node, parentNode: ESTree.Node): any => {
                    if (node.type === nodeType && visitor.leave) {
                        visitor.leave(node, parentNode);
                    }
                }
            });
        }
    }