All files / webpack:/src/node-obfuscators MethodDefinitionObfuscator.ts

20% Statements 2/10
0% Branches 0/12
0% Functions 0/2
12.5% Lines 1/8
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                                    1x                                                                                
import * as estraverse from 'estraverse';
 
import { IMethodDefinitionNode } from "../interfaces/nodes/IMethodDefinitionNode";
import { INode } from "../interfaces/nodes/INode";
 
import { NodeObfuscator } from './NodeObfuscator';
import { Nodes } from "../Nodes";
import { Utils } from "../Utils";
 
/**
 * replaces:
 *     function foo () { //... };
 *     foo();
 *
 * on:
 *     function _0x12d45f () { //... };
 *     _0x12d45f();
 */
export class MethodDefinitionObfuscator extends NodeObfuscator {
    /**
     * @type {string[]}
     */
    private ignoredNames: string[] = ['constructor'];
 
    /**
     * @param methodDefinitionNode
     * @param parentNode
     */
    public obfuscateNode (methodDefinitionNode: IMethodDefinitionNode, parentNode: INode): void {
        this.replaceMethodName(methodDefinitionNode);
    }
 
    /**
     * @param methodDefinitionNode
     */
    private replaceMethodName (methodDefinitionNode: IMethodDefinitionNode): void {
        estraverse.replace(methodDefinitionNode.key, {
            leave: (node: INode): any => {
                if (
                    Nodes.isIdentifierNode(node) &&
                    !Utils.arrayContains(this.ignoredNames, node.name) &&
                    methodDefinitionNode.computed === false
                ) {
                    methodDefinitionNode.computed = true;
                    node.name = this.replaceLiteralValueWithUnicodeValue(node.name);
 
                    return;
                }
 
                return estraverse.VisitorOption.Skip;
            }
        });
    }
}
 
 
 
// WEBPACK FOOTER //
// ./src/node-obfuscators/MethodDefinitionObfuscator.ts