All files NodeUtils.js

90.48% Statements 38/42
82.35% Branches 28/34
100% Functions 2/2
90.48% Lines 38/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     1x   1x 1x           1x     1x   4x 4x 3x 1x   2x   1x   10x 10x 10x 1x   9x 3x     3x 1x   2x   6x 4x   2x     1x     1x     7x 7x   37x           37x         3x     3x     5x     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) => {
                Eif (Nodes_1.Nodes.isLiteralNode(node)) {
                    node['x-verbatim-property'] = node.raw;
                }
            }
        });
    }
    static appendNode(blockScopeBody, node) {
        Iif (!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)) {
            Iif (!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) {
        Iif (!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) {
        Iif (!NodeUtils.validateNode(node)) {
            return;
        }
        blockScopeBody.unshift(node);
    }
    static validateNode(node) {
        return !!node;
    }
}
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