BlockStatementDeadCodeInjectionNode.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import { inject, injectable, } from 'inversify';
  2. import { ServiceIdentifiers } from '../../container/ServiceIdentifiers';
  3. import type { BinaryOperator, BlockStatement } from 'estree';
  4. import { TIdentifierNamesGeneratorFactory } from '../../types/container/generators/TIdentifierNamesGeneratorFactory';
  5. import { TStatement } from '../../types/node/TStatement';
  6. import { ICustomCodeHelperFormatter } from '../../interfaces/custom-code-helpers/ICustomCodeHelperFormatter';
  7. import { IOptions } from '../../interfaces/options/IOptions';
  8. import { IRandomGenerator } from '../../interfaces/utils/IRandomGenerator';
  9. import { AbstractCustomNode } from '../AbstractCustomNode';
  10. import { NodeFactory } from '../../node/NodeFactory';
  11. import { NodeUtils } from '../../node/NodeUtils';
  12. @injectable()
  13. export class BlockStatementDeadCodeInjectionNode extends AbstractCustomNode {
  14. /**
  15. * @type {BlockStatement}
  16. */
  17. private blockStatementNode!: BlockStatement;
  18. /**
  19. * @type {BlockStatement}
  20. */
  21. private deadCodeInjectionRootAstHostNode!: BlockStatement;
  22. /**
  23. * @param {TIdentifierNamesGeneratorFactory} identifierNamesGeneratorFactory
  24. * @param {ICustomCodeHelperFormatter} customCodeHelperFormatter
  25. * @param {IRandomGenerator} randomGenerator
  26. * @param {IOptions} options
  27. */
  28. public constructor (
  29. @inject(ServiceIdentifiers.Factory__IIdentifierNamesGenerator)
  30. identifierNamesGeneratorFactory: TIdentifierNamesGeneratorFactory,
  31. @inject(ServiceIdentifiers.ICustomCodeHelperFormatter) customCodeHelperFormatter: ICustomCodeHelperFormatter,
  32. @inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
  33. @inject(ServiceIdentifiers.IOptions) options: IOptions
  34. ) {
  35. super(
  36. identifierNamesGeneratorFactory,
  37. customCodeHelperFormatter,
  38. randomGenerator,
  39. options
  40. );
  41. }
  42. /**
  43. * @param {BlockStatement} blockStatementNode
  44. * @param {BlockStatement} deadCodeInjectionRootAstHostNode
  45. */
  46. public initialize (
  47. blockStatementNode: BlockStatement,
  48. deadCodeInjectionRootAstHostNode: BlockStatement
  49. ): void {
  50. this.blockStatementNode = blockStatementNode;
  51. this.deadCodeInjectionRootAstHostNode = deadCodeInjectionRootAstHostNode;
  52. }
  53. /**
  54. * Have to override parent method to prevent a change of kinds of variables
  55. *
  56. * @returns {TStatement[]}
  57. */
  58. public getNode (): TStatement[] {
  59. return this.getNodeStructure();
  60. }
  61. /**
  62. * @returns {TStatement[]}
  63. */
  64. protected getNodeStructure (): TStatement[] {
  65. const random1: boolean = this.randomGenerator.getMathRandom() > 0.5;
  66. const random2: boolean = this.randomGenerator.getMathRandom() > 0.5;
  67. const operator: BinaryOperator = random1 ? '===' : '!==';
  68. const leftString: string = this.randomGenerator.getRandomString(5);
  69. const rightString: string = random2 ? leftString : this.randomGenerator.getRandomString(5);
  70. const [consequent, alternate]: [BlockStatement, BlockStatement] = random1 === random2
  71. ? [this.blockStatementNode, this.deadCodeInjectionRootAstHostNode]
  72. : [this.deadCodeInjectionRootAstHostNode, this.blockStatementNode];
  73. const structure: BlockStatement = NodeFactory.blockStatementNode([
  74. NodeFactory.ifStatementNode(
  75. NodeFactory.binaryExpressionNode(
  76. operator,
  77. NodeFactory.literalNode(leftString),
  78. NodeFactory.literalNode(rightString)
  79. ),
  80. consequent,
  81. alternate
  82. )
  83. ]);
  84. NodeUtils.parentizeAst(structure);
  85. return [structure];
  86. }
  87. }