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

100% Statements 18/18
100% Branches 8/8
100% Functions 0/0
100% Lines 18/18

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

    1x
    1x
    1x
    1x
    1x

    1x

    80269x
    5036x

    75233x

    6053x

    6053x

    18692x

    18692x

    47465x

    47465x

    3023x

    72210x

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

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

    export class LiteralObfuscator extends AbstractNodeObfuscator {
        /**
         * @param literalNode
         * @param parentNode
         */
        public obfuscateNode (literalNode: ESTree.Literal, parentNode: ESTree.Node): void {
            if (Nodes.isPropertyNode(parentNode) && parentNode.key === literalNode) {
                return;
            }

            let content: string;

            switch (typeof literalNode.value) {
                case 'boolean':
                    content = new BooleanLiteralReplacer(this.nodes, this.options)
                        .replace(<boolean>literalNode.value);

                    break;

                case 'number':
                    content = new NumberLiteralReplacer(this.nodes, this.options)
                        .replace(<number>literalNode.value);

                    break;

                case 'string':
                    content = new StringLiteralReplacer(this.nodes, this.options)
                            .replace(<string>literalNode.value);

                    break;

                default:
                    return;
            }

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