CallExpressionFunctionNode.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import { inject, injectable, } from 'inversify';
  2. import { ServiceIdentifiers } from '../../container/ServiceIdentifiers';
  3. import * as ESTree 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 { initializable } from '../../decorators/Initializable';
  10. import { AbstractCustomNode } from '../AbstractCustomNode';
  11. import { NodeFactory } from '../../node/NodeFactory';
  12. import { NodeUtils } from '../../node/NodeUtils';
  13. import { NodeGuards } from '../../node/NodeGuards';
  14. @injectable()
  15. export class CallExpressionFunctionNode extends AbstractCustomNode {
  16. /**
  17. * @type {(ESTree.Expression | ESTree.SpreadElement)[]}
  18. */
  19. @initializable()
  20. private expressionArguments!: (ESTree.Expression | ESTree.SpreadElement)[];
  21. /**
  22. * @param {TIdentifierNamesGeneratorFactory} identifierNamesGeneratorFactory
  23. * @param {ICustomCodeHelperFormatter} customCodeHelperFormatter
  24. * @param {IRandomGenerator} randomGenerator
  25. * @param {IOptions} options
  26. */
  27. public constructor (
  28. @inject(ServiceIdentifiers.Factory__IIdentifierNamesGenerator)
  29. identifierNamesGeneratorFactory: TIdentifierNamesGeneratorFactory,
  30. @inject(ServiceIdentifiers.ICustomCodeHelperFormatter) customCodeHelperFormatter: ICustomCodeHelperFormatter,
  31. @inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
  32. @inject(ServiceIdentifiers.IOptions) options: IOptions
  33. ) {
  34. super(
  35. identifierNamesGeneratorFactory,
  36. customCodeHelperFormatter,
  37. randomGenerator,
  38. options
  39. );
  40. }
  41. /**
  42. * @param {(Expression | SpreadElement)[]} expressionArguments
  43. */
  44. public initialize (expressionArguments: (ESTree.Expression | ESTree.SpreadElement)[]): void {
  45. this.expressionArguments = expressionArguments;
  46. }
  47. /**
  48. * @returns {TStatement[]}
  49. */
  50. protected getNodeStructure (): TStatement[] {
  51. const calleeIdentifier: ESTree.Identifier = NodeFactory.identifierNode('callee');
  52. const params: (ESTree.Identifier | ESTree.RestElement)[] = [];
  53. const callArguments: (ESTree.Identifier | ESTree.SpreadElement)[] = [];
  54. const argumentsLength: number = this.expressionArguments.length;
  55. for (let i: number = 0; i < argumentsLength; i++) {
  56. const argument: ESTree.Expression | ESTree.SpreadElement = this.expressionArguments[i];
  57. const isSpreadCallArgument: boolean = NodeGuards.isSpreadElementNode(argument);
  58. const baseIdentifierNode: ESTree.Identifier = NodeFactory.identifierNode(`param${i + 1}`);
  59. params.push(
  60. isSpreadCallArgument
  61. ? NodeFactory.restElementNode(baseIdentifierNode)
  62. : baseIdentifierNode
  63. );
  64. callArguments.push(
  65. isSpreadCallArgument
  66. ? NodeFactory.spreadElementNode(baseIdentifierNode)
  67. : baseIdentifierNode
  68. );
  69. }
  70. const structure: TStatement = NodeFactory.expressionStatementNode(
  71. NodeFactory.functionExpressionNode(
  72. [
  73. calleeIdentifier,
  74. ...params
  75. ],
  76. NodeFactory.blockStatementNode([
  77. NodeFactory.returnStatementNode(
  78. NodeFactory.callExpressionNode(
  79. calleeIdentifier,
  80. callArguments
  81. )
  82. )
  83. ])
  84. )
  85. );
  86. NodeUtils.parentizeAst(structure);
  87. return [structure];
  88. }
  89. }