BooleanLiteralReplacer.ts 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import { injectable, inject } from 'inversify';
  2. import { ServiceIdentifiers } from '../../../container/ServiceIdentifiers';
  3. import * as ESTree from 'estree';
  4. import { IOptions } from '../../../interfaces/options/IOptions';
  5. import { AbstractObfuscatingReplacer } from './AbstractObfuscatingReplacer';
  6. import { Nodes } from '../../../node/Nodes';
  7. @injectable()
  8. export class BooleanLiteralReplacer extends AbstractObfuscatingReplacer {
  9. /**
  10. * @param options
  11. */
  12. constructor (
  13. @inject(ServiceIdentifiers.IOptions) options: IOptions
  14. ) {
  15. super(options);
  16. }
  17. /**
  18. * @return {ESTree.UnaryExpression}
  19. */
  20. private static getTrueUnaryExpressionNode (): ESTree.UnaryExpression {
  21. return Nodes.getUnaryExpressionNode(
  22. '!',
  23. BooleanLiteralReplacer.getFalseUnaryExpressionNode()
  24. );
  25. }
  26. /**
  27. * @return {ESTree.UnaryExpression}
  28. */
  29. private static getFalseUnaryExpressionNode (): ESTree.UnaryExpression {
  30. return Nodes.getUnaryExpressionNode(
  31. '!',
  32. Nodes.getArrayExpressionNode()
  33. );
  34. }
  35. /**
  36. * @param nodeValue
  37. * @returns {ESTree.Node}
  38. */
  39. public replace (nodeValue: boolean): ESTree.Node {
  40. return nodeValue
  41. ? BooleanLiteralReplacer.getTrueUnaryExpressionNode()
  42. : BooleanLiteralReplacer.getFalseUnaryExpressionNode();
  43. }
  44. }