LogicalExpressionFunctionNode.ts 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 { initializable } from '../../../decorators/Initializable';
  7. import { AbstractCustomNode } from '../../AbstractCustomNode';
  8. import { Nodes } from '../../../node/Nodes';
  9. import { RandomGeneratorUtils } from '../../../utils/RandomGeneratorUtils';
  10. @injectable()
  11. export class LogicalExpressionFunctionNode extends AbstractCustomNode {
  12. /**
  13. * @type {LogicalOperator}
  14. */
  15. @initializable()
  16. private operator: LogicalOperator;
  17. /**
  18. * @param options
  19. */
  20. constructor (
  21. @inject(ServiceIdentifiers.IOptions) options: IOptions
  22. ) {
  23. super(options);
  24. }
  25. /**
  26. * @param operator
  27. */
  28. public initialize (operator: LogicalOperator): void {
  29. this.operator = operator;
  30. }
  31. /**
  32. * @returns {TStatement[]}
  33. */
  34. protected getNodeStructure (): TStatement[] {
  35. return [
  36. Nodes.getFunctionDeclarationNode(
  37. RandomGeneratorUtils.getRandomVariableName(1, true, false),
  38. [
  39. Nodes.getIdentifierNode('x'),
  40. Nodes.getIdentifierNode('y')
  41. ],
  42. Nodes.getBlockStatementNode([
  43. Nodes.getReturnStatementNode(
  44. Nodes.getLogicalExpressionNode(
  45. this.operator,
  46. Nodes.getIdentifierNode('x'),
  47. Nodes.getIdentifierNode('y')
  48. )
  49. )
  50. ])
  51. )
  52. ];
  53. }
  54. }