Code coverage report for src/node-transformers/converting-transformers/MethodDefinitionTransformer.ts

# All files / src/node-transformers/converting-transformers MethodDefinitionTransformer.ts

100% Statements 18/18
100% Branches 7/7
100% Functions 2/2
100% Lines 15/15

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

    1x

    1x
    1x

    1x

    1x

    6262x

    6262x

    665151x
    6x

    6x

    3x
    3x

    6x

     import { injectable, inject } from 'inversify';
    import { ServiceIdentifiers } from '../../container/ServiceIdentifiers';

    import * as ESTree from 'estree';

    import { IOptions } from '../../interfaces/options/IOptions';
    import { IVisitor } from '../../interfaces/IVisitor';

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

    import { AbstractNodeTransformer } from '../AbstractNodeTransformer';
    import { Node } from '../../node/Node';

    /**
     * replaces:
     *     foo () { //... };
     *
     * on:
     *     ['foo'] { //... };
     *
     * Literal node will be obfuscated by LiteralTransformer
     */
    @injectable()
    export class MethodDefinitionTransformer extends AbstractNodeTransformer {
        /**
         * @type {string[]}
         */
        private static readonly ignoredNames: string[] = ['constructor'];

        /**
         * @param options
         */
        constructor (
            @inject(ServiceIdentifiers.IOptions) options: IOptions
        ) {
            super(options);
        }

        /**
         * @return {IVisitor}
         */
        public getVisitor (): IVisitor {
            return {
                enter: (node: ESTree.Node, parentNode: ESTree.Node) => {
                    if (Node.isMethodDefinitionNode(node)) {
                        return this.transformNode(node, parentNode);
                    }
                }
            };
        }

        /**
         * replaces:
         *     object.identifier = 1;
         *
         * on:
         *     object['identifier'] = 1;
         *
         * and skip:
         *     object[identifier] = 1;
         * Literal node will be obfuscated by LiteralTransformer
         *
         * @param methodDefinitionNode
         * @param parentNode
         * @returns {ESTree.Node}
         */
        public transformNode (methodDefinitionNode: ESTree.MethodDefinition, parentNode: ESTree.Node): ESTree.Node {
            if (
                Node.isIdentifierNode(methodDefinitionNode.key) &&
                !MethodDefinitionTransformer.ignoredNames.includes(methodDefinitionNode.key.name) &&
                methodDefinitionNode.computed === false
            ) {
                methodDefinitionNode.computed = true;
                methodDefinitionNode.key = {
                    type: NodeType.Literal,
                    value: methodDefinitionNode.key.name,
                    raw: `'${methodDefinitionNode.key.name}'`
                };
            }

            return methodDefinitionNode;
        }
    }