LogicalExpressionFunctionNode.ts 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. 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 options
  20. */
  21. constructor (
  22. @inject(ServiceIdentifiers.IOptions) options: IOptions
  23. ) {
  24. super(options);
  25. }
  26. /**
  27. * @param operator
  28. */
  29. public initialize (operator: LogicalOperator): void {
  30. this.operator = operator;
  31. }
  32. /**
  33. * @returns {TStatement[]}
  34. */
  35. protected getNodeStructure (): TStatement[] {
  36. const structure: TStatement = Nodes.getFunctionDeclarationNode(
  37. RandomGeneratorUtils.getRandomString(3),
  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. NodeUtils.parentize(structure);
  53. return [structure];
  54. }
  55. }