LogicalExpressionFunctionNode.ts 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import { inject, injectable, } 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. * @param {IRandomGenerator} randomGenerator
  20. * @param {IOptions} options
  21. */
  22. constructor (
  23. @inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
  24. @inject(ServiceIdentifiers.IOptions) options: IOptions
  25. ) {
  26. super(randomGenerator, options);
  27. }
  28. /**
  29. * @param {LogicalOperator} operator
  30. */
  31. public initialize (operator: LogicalOperator): void {
  32. this.operator = operator;
  33. }
  34. /**
  35. * @returns {TStatement[]}
  36. */
  37. protected getNodeStructure (): TStatement[] {
  38. const structure: TStatement = Nodes.getFunctionDeclarationNode(
  39. this.randomGenerator.getRandomString(3),
  40. [
  41. Nodes.getIdentifierNode('x'),
  42. Nodes.getIdentifierNode('y')
  43. ],
  44. Nodes.getBlockStatementNode([
  45. Nodes.getReturnStatementNode(
  46. Nodes.getLogicalExpressionNode(
  47. this.operator,
  48. Nodes.getIdentifierNode('x'),
  49. Nodes.getIdentifierNode('y')
  50. )
  51. )
  52. ])
  53. );
  54. NodeUtils.parentize(structure);
  55. return [structure];
  56. }
  57. }