All files / src/node-transformers/obfuscating-transformers ObjectExpressionTransformer.ts

100% Statements 27/27
83.33% Branches 10/12
100% Functions 3/3
100% Lines 24/24
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 1031x 1x   1x           1x   1x 1x 1x                         1x             6262x               2x 2x           2x               9543x                             6262x   665151x 7714x                       7714x   9545x 2422x     9545x 2x 9543x 9543x       7714x      
import { injectable, inject } from 'inversify';
import { ServiceIdentifiers } from '../../container/ServiceIdentifiers';
 
import * as escodegen from 'escodegen';
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';
import { Utils } from '../../utils/Utils';
 
/**
 * replaces:
 *     var object = { 'PSEUDO': 1 };
 *
 * or:
 *     var object = { PSEUDO: 1 };
 *
 * on:
 *     var object = { '\u0050\u0053\u0045\u0055\u0044\u004f': 1 };
 */
@injectable()
export class ObjectExpressionTransformer extends AbstractNodeTransformer {
    /**
     * @param options
     */
    constructor (
        @inject(ServiceIdentifiers.IOptions) options: IOptions
    ) {
        super(options);
    }
 
    /**
     * @param node
     * @returns {ESTree.Literal}
     */
    private static transformLiteralPropertyKey (node: ESTree.Literal): ESTree.Literal {
        Eif (typeof node.value === 'string' && !node['x-verbatim-property']) {
            node['x-verbatim-property'] = {
                content : `'${Utils.stringToUnicodeEscapeSequence(node.value)}'`,
                precedence: escodegen.Precedence.Primary
            };
        }
 
        return node;
    }
 
    /**
     * @param node
     * @returns {ESTree.Literal}
     */
    private static transformIdentifierPropertyKey (node: ESTree.Identifier): ESTree.Literal {
        return {
            type: NodeType.Literal,
            value: node.name,
            raw: `'${node.name}'`,
            'x-verbatim-property': {
                content : `'${Utils.stringToUnicodeEscapeSequence(node.name)}'`,
                precedence: escodegen.Precedence.Primary
            }
        };
    }
 
    /**
     * @return {IVisitor}
     */
    public getVisitor (): IVisitor {
        return {
            enter: (node: ESTree.Node, parentNode: ESTree.Node) => {
                if (Node.isObjectExpressionNode(node)) {
                    return this.transformNode(node, parentNode);
                }
            }
        };
    }
 
    /**
     * @param objectExpressionNode
     * @param parentNode
     * @returns {ESTree.Node}
     */
    public transformNode (objectExpressionNode: ESTree.ObjectExpression, parentNode: ESTree.Node): ESTree.Node {
        objectExpressionNode.properties
            .forEach((property: ESTree.Property) => {
                if (property.shorthand) {
                    property.shorthand = false;
                }
 
                if (Node.isLiteralNode(property.key)) {
                    property.key = ObjectExpressionTransformer.transformLiteralPropertyKey(property.key);
                } else Eif (Node.isIdentifierNode(property.key)) {
                    property.key = ObjectExpressionTransformer.transformIdentifierPropertyKey(property.key);
                }
            });
 
        return objectExpressionNode;
    }
}