ObjectExpressionVariableDeclarationHostNode.ts 3.1 KB

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