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

100% Statements 22/22
80% Branches 8/10
100% Functions 2/2
100% Lines 22/22

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

    1x

    1x
    1x
    1x

    1x

    3530x

    5036x
    2016x

    5036x

    5036x
    2x

    2x

    5034x
    5034x

    2x
    2x

    5034x
    5034x

    5034x

    5034x

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

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

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

    /**
     * replaces:
     *     var object = { 'PSEUDO': 1 };
     *
     * or:
     *     var object = { PSEUDO: 1 };
     *
     * on:
     *     var object = { '\u0050\u0053\u0045\u0055\u0044\u004f': 1 };
     */
    export class ObjectExpressionObfuscator extends AbstractNodeObfuscator {
        /**
         * @param objectExpressionNode
         */
        public obfuscateNode (objectExpressionNode: ESTree.ObjectExpression): void {
            objectExpressionNode.properties
                .forEach((property: ESTree.Property) => {
                    if (property.shorthand) {
                        property.shorthand = false;
                    }

                    estraverse.replace(property.key, {
                        enter: (node: ESTree.Node, parentNode: ESTree.Node): any => {
                            if (Nodes.isLiteralNode(node)) {
                                this.obfuscateLiteralPropertyKey(node);

                                return;
                            }

                            Eif (Nodes.isIdentifierNode(node)) {
                                this.obfuscateIdentifierPropertyKey(node);
                            }
                        }
                    });
                });
        }

        /**
         * @param node
         */
        private obfuscateLiteralPropertyKey (node: ESTree.Literal): void {
            Eif (typeof node.value === 'string' && !node['x-verbatim-property']) {
                node['x-verbatim-property'] = {
                    content : Utils.stringToUnicode(node.value),
                    precedence: escodegen.Precedence.Primary
                };
            }
        }

        /**
         * @param node
         */
        private obfuscateIdentifierPropertyKey (node: ESTree.Identifier): void {
            let nodeValue: string = node.name,
                literalNode: ESTree.Literal = {
                    raw: `'${nodeValue}'`,
                    'x-verbatim-property': {
                        content : Utils.stringToUnicode(nodeValue),
                        precedence: escodegen.Precedence.Primary
                    },
                    type: NodeType.Literal,
                    value: nodeValue
                };

            delete node.name;

            Object.assign(node, literalNode);
        }
    }