ConsoleOutputDisableExpressionNode.ts 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import { inject, injectable, } from 'inversify';
  2. import { ServiceIdentifiers } from '../../container/ServiceIdentifiers';
  3. import { TIdentifierNamesGeneratorFactory } from '../../types/container/generators/TIdentifierNamesGeneratorFactory';
  4. import { TStatement } from '../../types/node/TStatement';
  5. import { IOptions } from '../../interfaces/options/IOptions';
  6. import { IRandomGenerator } from '../../interfaces/utils/IRandomGenerator';
  7. import { ITemplateFormatter } from '../../interfaces/utils/ITemplateFormatter';
  8. import { ObfuscationTarget } from '../../enums/ObfuscationTarget';
  9. import { ConsoleOutputDisableExpressionTemplate } from '../../templates/console-output-nodes/console-output-disable-expression-node/ConsoleOutputDisableExpressionTemplate';
  10. import { GlobalVariableNoEvalTemplate } from '../../templates/GlobalVariableNoEvalTemplate';
  11. import { initializable } from '../../decorators/Initializable';
  12. import { AbstractCustomNode } from '../AbstractCustomNode';
  13. import { NodeUtils } from '../../node/NodeUtils';
  14. @injectable()
  15. export class ConsoleOutputDisableExpressionNode extends AbstractCustomNode {
  16. /**
  17. * @type {string}
  18. */
  19. @initializable()
  20. private callsControllerFunctionName!: string;
  21. /**
  22. * @param {TIdentifierNamesGeneratorFactory} identifierNamesGeneratorFactory
  23. * @param {ITemplateFormatter} templateFormatter
  24. * @param {IRandomGenerator} randomGenerator
  25. * @param {IOptions} options
  26. */
  27. constructor (
  28. @inject(ServiceIdentifiers.Factory__IIdentifierNamesGenerator)
  29. identifierNamesGeneratorFactory: TIdentifierNamesGeneratorFactory,
  30. @inject(ServiceIdentifiers.ITemplateFormatter) templateFormatter: ITemplateFormatter,
  31. @inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
  32. @inject(ServiceIdentifiers.IOptions) options: IOptions
  33. ) {
  34. super(identifierNamesGeneratorFactory, templateFormatter, randomGenerator, options);
  35. }
  36. /**
  37. * @param {string} callsControllerFunctionName
  38. */
  39. public initialize (callsControllerFunctionName: string): void {
  40. this.callsControllerFunctionName = callsControllerFunctionName;
  41. }
  42. /**
  43. * @returns {TStatement[]}
  44. */
  45. protected getNodeStructure (): TStatement[] {
  46. return NodeUtils.convertCodeToStructure(this.getTemplate());
  47. }
  48. /**
  49. * @returns {string}
  50. */
  51. protected getTemplate (): string {
  52. const globalVariableTemplate: string = this.options.target !== ObfuscationTarget.BrowserNoEval
  53. ? this.getGlobalVariableTemplate()
  54. : GlobalVariableNoEvalTemplate();
  55. return this.templateFormatter.format(ConsoleOutputDisableExpressionTemplate(), {
  56. consoleLogDisableFunctionName: this.identifierNamesGenerator.generate(),
  57. globalVariableTemplate,
  58. singleNodeCallControllerFunctionName: this.callsControllerFunctionName
  59. });
  60. }
  61. }