AbstractStringArrayCallNode.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 { TStringArrayIndexesType } from '../../types/options/TStringArrayIndexesType';
  6. import { TStringArrayIndexNodeFactory } from '../../types/container/custom-nodes/string-array-index-nodes/TStringArrayIndexNodeFactory';
  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 { IStringArrayStorage } from '../../interfaces/storages/string-array-transformers/IStringArrayStorage';
  11. import { StringArrayIndexesType } from '../../enums/node-transformers/string-array-transformers/StringArrayIndexesType';
  12. import { StringArrayIndexNode } from '../../enums/custom-nodes/string-array-index-nodes/StringArrayIndexNode';
  13. import { AbstractCustomNode } from '../AbstractCustomNode';
  14. import { NodeFactory } from '../../node/NodeFactory';
  15. import { NodeMetadata } from '../../node/NodeMetadata';
  16. import { NodeUtils } from '../../node/NodeUtils';
  17. import { IArrayUtils } from '../../interfaces/utils/IArrayUtils';
  18. @injectable()
  19. export abstract class AbstractStringArrayCallNode extends AbstractCustomNode {
  20. /**
  21. * Max count of root string array calls wrapper parameters
  22. *
  23. * @type {number}
  24. */
  25. protected static readonly stringArrayRootCallsWrapperParametersCount: number = 2;
  26. /**
  27. * @type {Map<TStringArrayIndexesType, StringArrayIndexNode>}
  28. */
  29. private static readonly stringArrayIndexNodesMap: Map<TStringArrayIndexesType, StringArrayIndexNode> = new Map([
  30. [StringArrayIndexesType.HexadecimalNumber, StringArrayIndexNode.StringArrayHexadecimalNumberIndexNode],
  31. [StringArrayIndexesType.HexadecimalNumericString, StringArrayIndexNode.StringArrayHexadecimalNumericStringIndexNode]
  32. ]);
  33. /**
  34. * @type {IArrayUtils}
  35. */
  36. protected readonly arrayUtils: IArrayUtils;
  37. /**
  38. * @type {IStringArrayStorage}
  39. */
  40. protected readonly stringArrayStorage: IStringArrayStorage;
  41. /**
  42. * @type {TStringArrayIndexNodeFactory}
  43. */
  44. private readonly stringArrayIndexNodeFactory: TStringArrayIndexNodeFactory;
  45. /**
  46. * @param {TIdentifierNamesGeneratorFactory} identifierNamesGeneratorFactory
  47. * @param {TStringArrayIndexNodeFactory} stringArrayIndexNodeFactory
  48. * @param {ICustomCodeHelperFormatter} customCodeHelperFormatter
  49. * @param {IStringArrayStorage} stringArrayStorage
  50. * @param {IArrayUtils} arrayUtils
  51. * @param {IRandomGenerator} randomGenerator
  52. * @param {IOptions} options
  53. */
  54. public constructor (
  55. @inject(ServiceIdentifiers.Factory__IIdentifierNamesGenerator)
  56. identifierNamesGeneratorFactory: TIdentifierNamesGeneratorFactory,
  57. @inject(ServiceIdentifiers.Factory__IStringArrayIndexNode)
  58. stringArrayIndexNodeFactory: TStringArrayIndexNodeFactory,
  59. @inject(ServiceIdentifiers.ICustomCodeHelperFormatter) customCodeHelperFormatter: ICustomCodeHelperFormatter,
  60. @inject(ServiceIdentifiers.IStringArrayStorage) stringArrayStorage: IStringArrayStorage,
  61. @inject(ServiceIdentifiers.IArrayUtils) arrayUtils: IArrayUtils,
  62. @inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
  63. @inject(ServiceIdentifiers.IOptions) options: IOptions
  64. ) {
  65. super(
  66. identifierNamesGeneratorFactory,
  67. customCodeHelperFormatter,
  68. randomGenerator,
  69. options
  70. );
  71. this.stringArrayIndexNodeFactory = stringArrayIndexNodeFactory;
  72. this.stringArrayStorage = stringArrayStorage;
  73. this.arrayUtils = arrayUtils;
  74. }
  75. /**
  76. * @param {number} index
  77. * @returns {Expression}
  78. */
  79. protected getStringArrayIndexNode (index: number): ESTree.Expression {
  80. const isPositive: boolean = index >= 0;
  81. const normalizedIndex: number = Math.abs(index);
  82. const stringArrayCallsIndexType: TStringArrayIndexesType = this.randomGenerator
  83. .getRandomGenerator()
  84. .pickone(this.options.stringArrayIndexesType);
  85. const stringArrayIndexNodeName: StringArrayIndexNode | null = AbstractStringArrayCallNode.stringArrayIndexNodesMap.get(stringArrayCallsIndexType) ?? null;
  86. if (!stringArrayIndexNodeName) {
  87. throw new Error('Invalid string array index node name');
  88. }
  89. const stringArrayCallIndexNode: ESTree.Expression = this.stringArrayIndexNodeFactory(stringArrayIndexNodeName)
  90. .getNode(normalizedIndex);
  91. NodeMetadata.set(stringArrayCallIndexNode, { stringArrayCallLiteralNode: true });
  92. const hexadecimalNode: ESTree.Expression = isPositive
  93. ? stringArrayCallIndexNode
  94. : NodeFactory.unaryExpressionNode(
  95. '-',
  96. stringArrayCallIndexNode
  97. );
  98. NodeUtils.parentizeAst(hexadecimalNode);
  99. return hexadecimalNode;
  100. }
  101. /**
  102. * @param {string} decodeKey
  103. * @returns {Literal}
  104. */
  105. protected getRc4KeyLiteralNode (decodeKey: string): ESTree.Literal {
  106. const rc4KeyLiteralNode: ESTree.Literal = NodeFactory.literalNode(decodeKey);
  107. NodeMetadata.set(rc4KeyLiteralNode, { stringArrayCallLiteralNode: true });
  108. return rc4KeyLiteralNode;
  109. }
  110. }