# All files / src/node-obfuscators MethodDefinitionObfuscator.ts

100% Statements 15/15
100% Branches 5/5
100% Functions 2/2
100% Lines 14/14

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

    1x
    1x
    1x
    1x

    6x

    6x

    6x

    6x

    6x

    3x
    3x

    3x

    3x

     import * as estraverse from 'estraverse';
    import * as ESTree from 'estree';

    import { AbstractNodeObfuscator } from './AbstractNodeObfuscator';
    import { Nodes } from '../Nodes';
    import { Utils } from '../Utils';
    import { StringLiteralReplacer } from './replacers/StringLiteralReplacer';

    /**
     * replaces:
     *     foo () { //... };
     *
     * on:
     *     [_0x9a4e('0x0')] { //... };
     */
    export class MethodDefinitionObfuscator extends AbstractNodeObfuscator {
        /**
         * @type {string[]}
         */
        private ignoredNames: string[] = ['constructor'];

        /**
         * @param methodDefinitionNode
         * @param parentNode
         */
        public obfuscateNode (methodDefinitionNode: ESTree.MethodDefinition, parentNode: ESTree.Node): void {
            this.replaceMethodName(methodDefinitionNode);
        }

        /**
         * @param methodDefinitionNode
         */
        private replaceMethodName (methodDefinitionNode: ESTree.MethodDefinition): void {
            estraverse.replace(methodDefinitionNode.key, {
                enter: (node: ESTree.Node): any => {
                    if (
                        Nodes.isIdentifierNode(node) &&
                        !Utils.arrayContains(this.ignoredNames, node.name) &&
                        methodDefinitionNode.computed === false
                    ) {
                        methodDefinitionNode.computed = true;
                        node.name = new StringLiteralReplacer(this.nodes, this.options)
                            .replace(node.name);

                        return;
                    }

                    return estraverse.VisitorOption.Skip;
                }
            });
        }
    }