# All files / src Nodes.ts

86.96% Statements 20/23
100% Branches 11/11
100% Functions 0/0
86.96% Lines 20/23

    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

    1x

    1x

    1x

    225888x

    813650x

    513027x

    401263x

    4548267x

    44957x

    1394996x

    55441x

    112913x

    1450820x

    4397778x
    3027268x

    1370510x

    1370510x

    1370510x

    867365x

    4x

     import * as ESTree from 'estree';

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

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

    export class Nodes {
        /**
         * @param bodyNode
         * @returns ESTree.Program
         */
        public static getProgramNode (bodyNode: TStatement[]): ESTree.Program {
            return {
                'type': NodeType.Program,
                'body': bodyNode,
                'sourceType': 'script',
                'obfuscated': false
            };
        }

        /**
         * @param node
         * @returns {boolean}
         */
        public static isArrowFunctionExpressionNode (node: ESTree.Node): node is ESTree.ArrowFunctionExpression {
            return node.type === NodeType.ArrowFunctionExpression;
        }

        /**
         * @param node
         * @returns {boolean}
         */
        public static isBlockStatementNode (node: ESTree.Node): node is ESTree.BlockStatement {
            return node.type === NodeType.BlockStatement;
        }

        /**
         * @param node
         * @returns {boolean}
         */
        public static isCallExpressionNode (node: ESTree.Node): node is ESTree.CallExpression {
            return node.type === NodeType.CallExpression;
        }

        /**
         * @param node
         * @returns {boolean}
         */
        public static isExpressionStatementNode (node: ESTree.Node): node is ESTree.ExpressionStatement {
            return node.type === NodeType.ExpressionStatement;
        }

        /**
         * @param node
         * @returns {boolean}
         */
        public static isFunctionDeclarationNode (node: ESTree.Node): node is ESTree.FunctionDeclaration {
            return node.type === NodeType.FunctionDeclaration;
        }

        /**
         * @param node
         * @returns {boolean}
         */
        public static isFunctionExpressionNode (node: ESTree.Node): node is ESTree.FunctionExpression {
            return node.type === NodeType.FunctionExpression;
        }

        /**
         * @param node
         * @returns {boolean}
         */
        public static isIdentifierNode (node: ESTree.Node): node is ESTree.Identifier {
            return node.type === NodeType.Identifier;
        }

        /**
         * @param node
         * @returns {boolean}
         */
        public static isLiteralNode (node: ESTree.Node): node is ESTree.Literal {
            return node.type === NodeType.Literal;
        }

        /**
         * @param node
         * @returns {boolean}
         */
        public static isMemberExpressionNode (node: ESTree.Node): node is ESTree.MemberExpression {
            return node.type === NodeType.MemberExpression;
        }

        /**
         * @param node
         * @returns {boolean}
         */
        public static isObjectExpressionNode (node: ESTree.Node): node is ESTree.ObjectExpression {
            return node.type === NodeType.ObjectExpression;
        }

        /**
         *
         * @param node
         * @returns {boolean}
         */
        public static isProgramNode (node: ESTree.Node): node is ESTree.Program {
            return node.type === NodeType.Program;
        }

        /**
         *
         * @param node
         * @returns {boolean}
         */
        public static isPropertyNode (node: ESTree.Node): node is ESTree.Property {
            return node.type === NodeType.Property;
        }

        /**
         * @param node
         * @param parentNode
         * @returns {boolean}
         */
        public static isReplaceableIdentifierNode (node: ESTree.Node, parentNode: ESTree.Node): node is ESTree.Identifier {
            if (!Nodes.isIdentifierNode(node)) {
                return false;
            }

            const parentNodeIsPropertyNode: boolean = Nodes.isPropertyNode(parentNode) && parentNode.key === node;
            const parentNodeIsMemberExpressionNode: boolean = (
                Nodes.isMemberExpressionNode(parentNode) &&
                parentNode.computed === false &&
                parentNode.property === node
            );

            return !parentNodeIsPropertyNode && !parentNodeIsMemberExpressionNode;
        }

        /**
         *
         * @param node
         * @returns {boolean}
         */
        public static isVariableDeclarationNode (node: ESTree.Node): node is ESTree.VariableDeclaration {
            return node.type === NodeType.VariableDeclaration;
        }

        /**
         *
         * @param node
         * @returns {boolean}
         */
        public static isVariableDeclaratorNode (node: ESTree.Node): node is ESTree.VariableDeclarator {
            return node.type === NodeType.VariableDeclarator;
        }

        /**
         * @param node
         * @returns {boolean}
         */
        public static isNodeHasBlockStatement (node: ESTree.Node): node is TNodeWithBlockStatement {
            return node.hasOwnProperty('body') && Array.isArray((<TNodeWithBlockStatement>node).body);
        }
    }