AbstractCustomNode.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import { inject, injectable } from 'inversify';
  2. import { ServiceIdentifiers } from '../container/ServiceIdentifiers';
  3. import { TIdentifierNameGeneratorFactory } from '../types/container/generators/TIdentifierNameGeneratorFactory';
  4. import { TStatement } from '../types/node/TStatement';
  5. import { ICustomNode } from '../interfaces/custom-nodes/ICustomNode';
  6. import { IIdentifierNameGenerator } from '../interfaces/generators/identifier-name-generators/IIdentifierNameGenerator';
  7. import { IOptions } from '../interfaces/options/IOptions';
  8. import { IRandomGenerator } from '../interfaces/utils/IRandomGenerator';
  9. import { GlobalVariableTemplate1 } from '../templates/GlobalVariableTemplate1';
  10. import { GlobalVariableTemplate2 } from '../templates/GlobalVariableTemplate2';
  11. import { NodeUtils } from '../node/NodeUtils';
  12. @injectable()
  13. export abstract class AbstractCustomNode implements ICustomNode {
  14. /**
  15. * @type {string[]}
  16. */
  17. private static globalVariableTemplateFunctions: string[] = [
  18. GlobalVariableTemplate1(),
  19. GlobalVariableTemplate2()
  20. ];
  21. /**
  22. * @type {string}
  23. */
  24. protected cachedCode: string;
  25. /**
  26. * @type {TStatement[]}
  27. */
  28. protected cachedNode: TStatement[];
  29. /**
  30. * @type {IIdentifierNameGenerator}
  31. */
  32. protected readonly identifierNameGenerator: IIdentifierNameGenerator;
  33. /**
  34. * @type {IOptions}
  35. */
  36. protected readonly options: IOptions;
  37. /**
  38. * @type {IRandomGenerator}
  39. */
  40. protected readonly randomGenerator: IRandomGenerator;
  41. /**
  42. * @param {TIdentifierNameGeneratorFactory} identifierNameGeneratorFactory
  43. * @param {IRandomGenerator} randomGenerator
  44. * @param {IOptions} options
  45. */
  46. constructor (
  47. @inject(ServiceIdentifiers.Factory__IIdentifierNameGenerator)
  48. identifierNameGeneratorFactory: TIdentifierNameGeneratorFactory,
  49. @inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
  50. @inject(ServiceIdentifiers.IOptions) options: IOptions
  51. ) {
  52. this.identifierNameGenerator = identifierNameGeneratorFactory(options);
  53. this.randomGenerator = randomGenerator;
  54. this.options = options;
  55. }
  56. /**
  57. * @param {any[]} args
  58. */
  59. public abstract initialize (...args: any[]): void;
  60. /**
  61. * @returns {string}
  62. */
  63. public getCode (): string {
  64. if (!this.cachedCode) {
  65. this.cachedCode = NodeUtils.convertStructureToCode(this.getNode());
  66. }
  67. return this.cachedCode;
  68. }
  69. /**
  70. * @returns {TStatement[]}
  71. */
  72. public getNode (): TStatement[] {
  73. if (!this.cachedNode) {
  74. this.cachedNode = this.getNodeStructure();
  75. }
  76. return this.cachedNode;
  77. }
  78. /**
  79. * @returns {string}
  80. */
  81. protected getGlobalVariableTemplate (): string {
  82. return this.randomGenerator
  83. .getRandomGenerator()
  84. .pickone(AbstractCustomNode.globalVariableTemplateFunctions);
  85. }
  86. /**
  87. * @returns {TStatement[]}
  88. */
  89. protected abstract getNodeStructure (): TStatement[];
  90. }