ObjectExpressionVariableDeclarationHostNode.ts 3.1 KB

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