ControlFlowStorageNode.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import { inject, injectable, } from 'inversify';
  2. import { ServiceIdentifiers } from '../../../container/ServiceIdentifiers';
  3. import * as ESTree from 'estree';
  4. import { TControlFlowStorage } from '../../../types/storages/TControlFlowStorage';
  5. import { TIdentifierNamesGeneratorFactory } from '../../../types/container/generators/TIdentifierNamesGeneratorFactory';
  6. import { TStatement } from '../../../types/node/TStatement';
  7. import { ICustomNode } from '../../../interfaces/custom-nodes/ICustomNode';
  8. import { IOptions } from '../../../interfaces/options/IOptions';
  9. import { IRandomGenerator } from '../../../interfaces/utils/IRandomGenerator';
  10. import { ICustomNodeFormatter } from '../../../interfaces/custom-nodes/ICustomNodeFormatter';
  11. import { initializable } from '../../../decorators/Initializable';
  12. import { AbstractCustomNode } from '../../AbstractCustomNode';
  13. import { NodeFactory } from '../../../node/NodeFactory';
  14. import { NodeGuards } from '../../../node/NodeGuards';
  15. import { NodeUtils } from '../../../node/NodeUtils';
  16. @injectable()
  17. export class ControlFlowStorageNode extends AbstractCustomNode {
  18. /**
  19. * @type {TControlFlowStorage}
  20. */
  21. @initializable()
  22. private controlFlowStorage!: TControlFlowStorage;
  23. /**
  24. * @param {TIdentifierNamesGeneratorFactory} identifierNamesGeneratorFactory
  25. * @param {ICustomNodeFormatter} customNodeFormatter
  26. * @param {IRandomGenerator} randomGenerator
  27. * @param {IOptions} options
  28. */
  29. public constructor (
  30. @inject(ServiceIdentifiers.Factory__IIdentifierNamesGenerator)
  31. identifierNamesGeneratorFactory: TIdentifierNamesGeneratorFactory,
  32. @inject(ServiceIdentifiers.ICustomNodeFormatter) customNodeFormatter: ICustomNodeFormatter,
  33. @inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
  34. @inject(ServiceIdentifiers.IOptions) options: IOptions
  35. ) {
  36. super(identifierNamesGeneratorFactory, customNodeFormatter, randomGenerator, options);
  37. }
  38. /**
  39. * @param {TControlFlowStorage} controlFlowStorage
  40. */
  41. public initialize (controlFlowStorage: TControlFlowStorage): void {
  42. this.controlFlowStorage = controlFlowStorage;
  43. }
  44. /**
  45. * @param {string} nodeTemplate
  46. * @returns {TStatement[]}
  47. */
  48. protected getNodeStructure (nodeTemplate: string): TStatement[] {
  49. const propertyNodes: ESTree.Property[] = Array
  50. .from<[string, ICustomNode]>(this.controlFlowStorage.getStorage())
  51. .map(([key, value]: [string, ICustomNode]) => {
  52. const node: ESTree.Node = value.getNode()[0];
  53. if (!NodeGuards.isExpressionStatementNode(node)) {
  54. throw new Error('Function node for control flow storage object should be passed inside the `ExpressionStatement` node!');
  55. }
  56. return NodeFactory.propertyNode(
  57. NodeFactory.identifierNode(key),
  58. node.expression
  59. );
  60. });
  61. let structure: ESTree.Node = NodeFactory.variableDeclarationNode(
  62. [
  63. NodeFactory.variableDeclaratorNode(
  64. NodeFactory.identifierNode(this.controlFlowStorage.getStorageId()),
  65. NodeFactory.objectExpressionNode(propertyNodes)
  66. )
  67. ],
  68. 'const'
  69. );
  70. structure = NodeUtils.parentizeAst(structure);
  71. return [structure];
  72. }
  73. }