ControlFlowStorageNode.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import { injectable, inject } from 'inversify';
  2. import { ServiceIdentifiers } from '../../container/ServiceIdentifiers';
  3. import * as ESTree from 'estree';
  4. import { TStatement } from '../../types/node/TStatement';
  5. import { ICustomNode } from '../../interfaces/custom-nodes/ICustomNode';
  6. import { IOptions } from '../../interfaces/options/IOptions';
  7. import { IStorage } from '../../interfaces/storages/IStorage';
  8. import { initializable } from '../../decorators/Initializable';
  9. import { AbstractCustomNode } from '../AbstractCustomNode';
  10. import { Nodes } from '../../node/Nodes';
  11. import { NodeUtils } from '../../node/NodeUtils';
  12. @injectable()
  13. export class ControlFlowStorageNode extends AbstractCustomNode {
  14. /**
  15. * @type {IStorage <ICustomNode>}
  16. */
  17. @initializable()
  18. private controlFlowStorage: IStorage <ICustomNode>;
  19. /**
  20. * @param options
  21. */
  22. constructor (
  23. @inject(ServiceIdentifiers.IOptions) options: IOptions
  24. ) {
  25. super(options);
  26. }
  27. /**
  28. * @param controlFlowStorage
  29. */
  30. public initialize (controlFlowStorage: IStorage <ICustomNode>): void {
  31. this.controlFlowStorage = controlFlowStorage;
  32. }
  33. /**
  34. * @returns {TStatement[]}
  35. */
  36. protected getNodeStructure (): TStatement[] {
  37. const structure: ESTree.Node = Nodes.getVariableDeclarationNode([
  38. Nodes.getVariableDeclaratorNode(
  39. Nodes.getIdentifierNode(this.controlFlowStorage.getStorageId()),
  40. Nodes.getObjectExpressionNode(
  41. Array
  42. .from(this.controlFlowStorage.getStorage())
  43. .map(([key, value]: [string, ICustomNode]) => {
  44. return Nodes.getPropertyNode(
  45. Nodes.getIdentifierNode(key),
  46. <any>value.getNode()[0]
  47. )
  48. })
  49. )
  50. )
  51. ]);
  52. NodeUtils.parentize(structure);
  53. return [structure];
  54. }
  55. }