Code coverage report for src/node-transformers/node-obfuscators/replacers/IdentifierReplacer.ts

# All files / src/node-transformers/node-obfuscators/replacers IdentifierReplacer.ts

90% Statements 18/20
66.67% Branches 4/6
50% Functions 1/2
89.47% Lines 17/19

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

    1x
    1x

    1x
    1x

    6241x

    6241x

    1150784x

    1150784x
    1017216x

    133568x

    49032x

    45901x

    45901x
    45901x

    45901x

     import { injectable, inject } from 'inversify';
    import { ServiceIdentifiers } from '../../../container/ServiceIdentifiers';

    import { ICustomNode } from '../../../interfaces/custom-nodes/ICustomNode';
    import { IOptions } from '../../../interfaces/options/IOptions';
    import { IStorage } from '../../../interfaces/storages/IStorage';

    import { AbstractReplacer } from './AbstractReplacer';
    import { Utils } from '../../../Utils';

    @injectable()
    export class IdentifierReplacer extends AbstractReplacer {
        /**
         * @type {Map<string, string>}
         */
        private readonly namesMap: Map<string, string> = new Map<string, string>();

        /**
         * @type {string}
         */
        private uniquePrefix: string;

        /**
         * @param customNodesStorage
         * @param options
         */
        constructor (
            @inject(ServiceIdentifiers['IStorage<ICustomNode>']) customNodesStorage: IStorage<ICustomNode>,
            @inject(ServiceIdentifiers.IOptions) options: IOptions
        ) {
            super(customNodesStorage, options);
        }

        /**
         * @param nodeValue
         * @returns {string}
         */
        public replace (nodeValue: string): string {
            const obfuscatedIdentifierName: string|undefined = this.namesMap.get(`${nodeValue}-${this.uniquePrefix}`);

            if (!obfuscatedIdentifierName) {
                return nodeValue;
            }

            return obfuscatedIdentifierName;
        }

        /**
         * @param uniquePrefix
         */
        public setPrefix (uniquePrefix: string): void {
            this.uniquePrefix = uniquePrefix
        }

        /**
         * Store all identifiers names as keys in given `namesMap` with random names as value.
         * Reserved names will be ignored.
         *
         * @param nodeName
         */
        public storeNames (nodeName: string): void {
            Iif (!this.uniquePrefix) {
                throw new Error('`uniquePrefix` is `undefined`. Set it before `storeNames`');
            }

            Eif (!this.isReservedName(nodeName)) {
                this.namesMap.set(`${nodeName}-${this.uniquePrefix}`, Utils.getRandomVariableName());
            }
        }

        /**
         * @param name
         * @returns {boolean}
         */
        private isReservedName (name: string): boolean {
            return this.options.reservedNames
                .some((reservedName: string) => {
                    return new RegExp(reservedName, 'g').test(name);
                });
        }
    }