# All files / src/node-transformers/node-obfuscators LiteralObfuscator.ts

100% Statements 23/23
100% Branches 8/8
100% Functions 1/1
100% Lines 20/20

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

    1x

    1x

    1x
    1x

    1x

    1806x

    1806x

    46516x
    2400x

    44116x

    4086x

    4086x

    13274x

    13274x

    25222x

    25222x

    1534x

    42582x

     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 { IObfuscatorReplacer } from '../../interfaces/node-transformers/IObfuscatorReplacer';

    import { NodeObfuscatorsReplacers } from '../../enums/container/NodeObfuscatorsReplacers';

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

    @injectable()
    export class LiteralObfuscator extends AbstractNodeTransformer {
        /**
         * @type {(replacer: NodeObfuscatorsReplacers) => IObfuscatorReplacer}
         */
        private readonly replacersFactory: (replacer: NodeObfuscatorsReplacers) => IObfuscatorReplacer;

        /**
         * @param replacersFactory
         * @param options
         */
        constructor(
            @inject(ServiceIdentifiers['Factory<IObfuscatorReplacer>']) replacersFactory: (replacer: NodeObfuscatorsReplacers) => IObfuscatorReplacer,
            @inject(ServiceIdentifiers.IOptions) options: IOptions
        ) {
            super(options);

            this.replacersFactory = replacersFactory;
        }

        /**
         * @param literalNode
         * @param parentNode
         */
        public transformNode (literalNode: ESTree.Literal, parentNode: ESTree.Node): void {
            if (Node.isPropertyNode(parentNode) && parentNode.key === literalNode) {
                return;
            }

            let content: string;

            switch (typeof literalNode.value) {
                case 'boolean':
                    content = this.replacersFactory(NodeObfuscatorsReplacers.BooleanReplacer)
                        .replace(<boolean>literalNode.value);

                    break;

                case 'number':
                    content = this.replacersFactory(NodeObfuscatorsReplacers.NumberLiteralReplacer)
                        .replace(<number>literalNode.value);

                    break;

                case 'string':
                    content = this.replacersFactory(NodeObfuscatorsReplacers.StringLiteralReplacer)
                        .replace(<string>literalNode.value);

                    break;

                default:
                    return;
            }

            literalNode['x-verbatim-property'] = {
                content : content,
                precedence: escodegen.Precedence.Primary
            };
        }
    }