ObjectExpressionVariableDeclarationHostNode.ts 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 { TNodeWithLexicalScope } from '../../types/node/TNodeWithLexicalScope';
  6. import { TStatement } from '../../types/node/TStatement';
  7. import { IOptions } from '../../interfaces/options/IOptions';
  8. import { IRandomGenerator } from '../../interfaces/utils/IRandomGenerator';
  9. import { ICustomNodeFormatter } from '../../interfaces/custom-nodes/ICustomNodeFormatter';
  10. import { AbstractCustomNode } from '../AbstractCustomNode';
  11. import { NodeFactory } from '../../node/NodeFactory';
  12. @injectable()
  13. export class ObjectExpressionVariableDeclarationHostNode extends AbstractCustomNode {
  14. /**
  15. * @type {TNodeWithLexicalScope}
  16. */
  17. private lexicalScopeNode!: TNodeWithLexicalScope;
  18. /**
  19. * @ type {Property}
  20. */
  21. private properties!: ESTree.Property[];
  22. /**
  23. * @param {TIdentifierNamesGeneratorFactory} identifierNamesGeneratorFactory
  24. * @param {ICustomNodeFormatter} customNodeFormatter
  25. * @param {IRandomGenerator} randomGenerator
  26. * @param {IOptions} options
  27. */
  28. public constructor (
  29. @inject(ServiceIdentifiers.Factory__IIdentifierNamesGenerator)
  30. identifierNamesGeneratorFactory: TIdentifierNamesGeneratorFactory,
  31. @inject(ServiceIdentifiers.ICustomNodeFormatter) customNodeFormatter: ICustomNodeFormatter,
  32. @inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
  33. @inject(ServiceIdentifiers.IOptions) options: IOptions
  34. ) {
  35. super(identifierNamesGeneratorFactory, customNodeFormatter, randomGenerator, options);
  36. }
  37. public initialize (lexicalScopeNode: TNodeWithLexicalScope, properties: ESTree.Property[]): void {
  38. this.lexicalScopeNode = lexicalScopeNode;
  39. this.properties = properties;
  40. }
  41. /**
  42. * @param {string} nodeTemplate
  43. * @returns {TStatement[]}
  44. */
  45. protected getNodeStructure (nodeTemplate: string): TStatement[] {
  46. const structure: TStatement = NodeFactory.variableDeclarationNode(
  47. [
  48. NodeFactory.variableDeclaratorNode(
  49. NodeFactory.identifierNode(
  50. this.identifierNamesGenerator.generateForLexicalScope(this.lexicalScopeNode)
  51. ),
  52. NodeFactory.objectExpressionNode(this.properties)
  53. )
  54. ],
  55. 'const'
  56. );
  57. return [structure];
  58. }
  59. }