LogicalExpressionFunctionNode.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import { inject, injectable, } from 'inversify';
  2. import { ServiceIdentifiers } from '../../container/ServiceIdentifiers';
  3. import type { LogicalOperator } 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 LogicalExpressionFunctionNode extends AbstractCustomNode {
  14. /**
  15. * @type {LogicalOperator}
  16. */
  17. private operator!: LogicalOperator;
  18. /**
  19. * @param {TIdentifierNamesGeneratorFactory} identifierNamesGeneratorFactory
  20. * @param {ICustomCodeHelperFormatter} customCodeHelperFormatter
  21. * @param {IRandomGenerator} randomGenerator
  22. * @param {IOptions} options
  23. */
  24. public constructor (
  25. @inject(ServiceIdentifiers.Factory__IIdentifierNamesGenerator)
  26. identifierNamesGeneratorFactory: TIdentifierNamesGeneratorFactory,
  27. @inject(ServiceIdentifiers.ICustomCodeHelperFormatter) customCodeHelperFormatter: ICustomCodeHelperFormatter,
  28. @inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
  29. @inject(ServiceIdentifiers.IOptions) options: IOptions
  30. ) {
  31. super(
  32. identifierNamesGeneratorFactory,
  33. customCodeHelperFormatter,
  34. randomGenerator,
  35. options
  36. );
  37. }
  38. /**
  39. * @param {LogicalOperator} operator
  40. */
  41. public initialize (operator: LogicalOperator): void {
  42. this.operator = operator;
  43. }
  44. /**
  45. * @returns {TStatement[]}
  46. */
  47. protected getNodeStructure (): TStatement[] {
  48. const structure: TStatement = NodeFactory.expressionStatementNode(
  49. NodeFactory.functionExpressionNode(
  50. [
  51. NodeFactory.identifierNode('x'),
  52. NodeFactory.identifierNode('y')
  53. ],
  54. NodeFactory.blockStatementNode([
  55. NodeFactory.returnStatementNode(
  56. NodeFactory.logicalExpressionNode(
  57. this.operator,
  58. NodeFactory.identifierNode('x'),
  59. NodeFactory.identifierNode('y')
  60. )
  61. )
  62. ])
  63. )
  64. );
  65. NodeUtils.parentizeAst(structure);
  66. return [structure];
  67. }
  68. }