Code coverage report for src/node/Node.ts

# All files / src/node Node.ts

90% Statements 27/30
100% Branches 21/21
100% Functions 0/0
90% Lines 27/30

    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
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207

    1x

    1x

    1x

    230140x

    1150805x

    823391x

    1150805x

    519061x

    405989x

    4552652x

    1150805x
    1150805x
    1150805x

    1150805x

    1150805x

    45415x

    1400390x

    56085x

    115902x

    1455152x

    4400462x
    3024856x

    1375606x

    1375606x

    1375606x

    877328x

    4x

     import * as ESTree from 'estree';

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

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

    export class Node {
        /**
         * @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 isBreakStatementNode (node: ESTree.Node): node is ESTree.BreakStatement {
            return node.type === NodeType.BreakStatement;
        }

        /**
         * @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 isContinueStatementNode (node: ESTree.Node): node is ESTree.ContinueStatement {
            return node.type === NodeType.ContinueStatement;
        }

        /**
         * @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
         * @param parentNode
         * @returns {boolean}
         */
        public static isLabelIdentifierNode (node: ESTree.Node, parentNode: ESTree.Node): node is ESTree.Identifier {
            const parentNodeIsLabeledStatementNode: boolean = Node.isLabeledStatementNode(parentNode) && parentNode.label === node;
            const parentNodeIsContinueStatementNode: boolean = Node.isContinueStatementNode(parentNode) && parentNode.label === node;
            const parentNodeIsBreakStatementNode: boolean = Node.isBreakStatementNode(parentNode) && parentNode.label === node;

            return parentNodeIsLabeledStatementNode ||
                parentNodeIsContinueStatementNode ||
                parentNodeIsBreakStatementNode;
        }

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

        /**
         * @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 (!Node.isIdentifierNode(node)) {
                return false;
            }

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

            return !parentNodeIsPropertyNode &&
                !parentNodeIsMemberExpressionNode &&
                !Node.isLabelIdentifierNode(node, parentNode);
        }

        /**
         *
         * @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);
        }
    }