All files JavaScriptObfuscator.ts

100% Statements 12/12
100% Branches 0/0
100% Functions 2/2
100% Lines 12/12
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    1x 1x             1x 1x   1x       1x                 4x       4x   4x               4x   4x       4x      
"use strict";
 
import * as esprima from 'esprima';
import * as escodegen from 'escodegen';
 
import { INode } from './interfaces/nodes/INode';
import { IObfuscator } from "./interfaces/IObfuscator";
import { IOptions } from './interfaces/IOptions';
import { IObfuscatorOptions } from "./interfaces/IObfuscatorOptions";
 
import { Obfuscator } from "./Obfuscator";
import { Options } from "./Options";
 
export class JavaScriptObfuscator {
    /**
     * @type {GenerateOptions}
     */
    private static escodegenParams: escodegen.GenerateOptions = {
        verbatim: 'x-verbatim-property'
    };
 
    /**
     * @param sourceCode
     * @param customOptions
     */
    public static obfuscate (sourceCode: string, customOptions?: IObfuscatorOptions): string {
        let astTree: INode = esprima.parse(sourceCode),
            options: IOptions = new Options(customOptions),
            obfuscator: IObfuscator = new Obfuscator(options);
 
        astTree = obfuscator.obfuscateNode(astTree);
 
        return JavaScriptObfuscator.generateCode(astTree, options);
    }
 
    /**
     * @param astTree
     * @param options
     */
    private static generateCode (astTree: INode, options: IOptions): string {
        let escodegenParams: escodegen.GenerateOptions = Object.assign({}, JavaScriptObfuscator.escodegenParams);
 
        escodegenParams.format = {
            compact: options.get<boolean>('compact')
        };
 
        return escodegen.generate(astTree, escodegenParams);
    }
}