Code coverage report for src/node-obfuscators/LiteralTransformer.ts

# All files / src/node-obfuscators LiteralTransformer.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

    79485x
    5084x

    74401x

    6108x

    6108x

    18852x

    18852x

    46388x

    46388x

    3053x

    71348x

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

    import { AbstractNodeObfuscator } from './AbstractNodeObfuscator';
    import { BooleanLiteralReplacer } from './replacers/BooleanLiteralReplacer';
    import { Node } from '../node/Node';
    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 (Node.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
            };
        }
    }