# All files / src NodeUtils.js

100% Statements 42/42
97.3% Branches 36/37
100% Functions 2/2
100% Lines 42/42

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

    8x

    526x
    128x

    4x
    2x

    2x

    13x
    13x
    12x
    1x

    11x

    1x

    49x
    49x
    49x
    1x

    48x
    34x
    8x

    26x
    1x

    25x

    14x
    4x

    10x

    9x
    2x

    7x

    20x
    20x

    1441x

    1441x

    10x
    2x

    8x

    23x

    1x

    1x
     "use strict";
    const estraverse = require('estraverse');
    const NodeType_1 = require("./enums/NodeType");
    const Nodes_1 = require("./Nodes");
    const Utils_1 = require("./Utils");
    class NodeUtils {
        static addXVerbatimPropertyToLiterals(node) {
            estraverse.replace(node, {
                enter: (node, parentNode) => {
                    if (Nodes_1.Nodes.isLiteralNode(node)) {
                        node['x-verbatim-property'] = node.raw;
                    }
                }
            });
        }
        static appendNode(blockScopeBody, node) {
            if (!NodeUtils.validateNode(node)) {
                return;
            }
            blockScopeBody.push(node);
        }
        static getBlockStatementNodeByIndex(node, index = 0) {
            if (Nodes_1.Nodes.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');
        }
        static getBlockScopeOfNode(node, depth = 0) {
            let parentNode = node.parentNode;
            if (!parentNode) {
                throw new ReferenceError('`parentNode` property of given node is `undefined`');
            }
            if (Nodes_1.Nodes.isBlockStatementNode(parentNode)) {
                if (!Utils_1.Utils.arrayContains(NodeUtils.nodesWithBlockScope, parentNode['parentNode'].type)) {
                    return NodeUtils.getBlockScopeOfNode(parentNode, depth);
                }
                else if (depth > 0) {
                    return NodeUtils.getBlockScopeOfNode(parentNode, --depth);
                }
                return parentNode;
            }
            if (Nodes_1.Nodes.isProgramNode(parentNode)) {
                return parentNode;
            }
            return NodeUtils.getBlockScopeOfNode(parentNode);
        }
        static insertNodeAtIndex(blockScopeBody, node, index) {
            if (!NodeUtils.validateNode(node)) {
                return;
            }
            blockScopeBody.splice(index, 0, node);
        }
        static parentize(node) {
            let isRootNode = true;
            estraverse.replace(node, {
                enter: (node, parentNode) => {
                    Object.defineProperty(node, 'parentNode', {
                        configurable: true,
                        enumerable: true,
                        value: isRootNode ? Nodes_1.Nodes.getProgramNode([node]) : parentNode || node,
                        writable: true
                    });
                    isRootNode = false;
                }
            });
        }
        static prependNode(blockScopeBody, node) {
            if (!NodeUtils.validateNode(node)) {
                return;
            }
            blockScopeBody.unshift(node);
        }
        static validateNode(node) {
            return !!node && node.hasOwnProperty('type');
        }
    }
    NodeUtils.nodesWithBlockScope = [
        NodeType_1.NodeType.ArrowFunctionExpression,
        NodeType_1.NodeType.FunctionDeclaration,
        NodeType_1.NodeType.FunctionExpression,
        NodeType_1.NodeType.MethodDefinition,
        NodeType_1.NodeType.Program
    ];
    exports.NodeUtils = NodeUtils;
    //# sourceMappingURL=NodeUtils.js.map