LogicalExpressionFunctionNode.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import { injectable, inject } from 'inversify';
  2. import { ServiceIdentifiers } from '../../container/ServiceIdentifiers';
  3. import { LogicalOperator } from 'estree';
  4. import { TStatement } from '../../types/node/TStatement';
  5. import { IOptions } from '../../interfaces/options/IOptions';
  6. import { IRandomGenerator } from '../../interfaces/utils/IRandomGenerator';
  7. import { initializable } from '../../decorators/Initializable';
  8. import { AbstractCustomNode } from '../AbstractCustomNode';
  9. import { Nodes } from '../../node/Nodes';
  10. import { NodeUtils } from '../../node/NodeUtils';
  11. @injectable()
  12. export class LogicalExpressionFunctionNode extends AbstractCustomNode {
  13. /**
  14. * @type {LogicalOperator}
  15. */
  16. @initializable()
  17. private operator: LogicalOperator;
  18. /**
  19. * @type {IRandomGenerator}
  20. */
  21. private readonly randomGenerator: IRandomGenerator;
  22. /**
  23. * @param randomGenerator
  24. * @param options
  25. */
  26. constructor (
  27. @inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
  28. @inject(ServiceIdentifiers.IOptions) options: IOptions
  29. ) {
  30. super(options);
  31. this.randomGenerator = randomGenerator;
  32. }
  33. /**
  34. * @param operator
  35. */
  36. public initialize (operator: LogicalOperator): void {
  37. this.operator = operator;
  38. }
  39. /**
  40. * @returns {TStatement[]}
  41. */
  42. protected getNodeStructure (): TStatement[] {
  43. const structure: TStatement = Nodes.getFunctionDeclarationNode(
  44. this.randomGenerator.getRandomString(3),
  45. [
  46. Nodes.getIdentifierNode('x'),
  47. Nodes.getIdentifierNode('y')
  48. ],
  49. Nodes.getBlockStatementNode([
  50. Nodes.getReturnStatementNode(
  51. Nodes.getLogicalExpressionNode(
  52. this.operator,
  53. Nodes.getIdentifierNode('x'),
  54. Nodes.getIdentifierNode('y')
  55. )
  56. )
  57. ])
  58. );
  59. NodeUtils.parentize(structure);
  60. return [structure];
  61. }
  62. }